End of file descriptor in grammatical implementation

I am working on a compiler implementation and want to check if end of file has been reached?

I know this can be done using the yywrap () function in the lex file, but the requirement is that we want the EOF to be explicitly defined as in the hex value 0x1a, whereas we are referencing that.

Example:

Main () {printf ("Check EOF marker \ n"); '0x1a' <is the actual EOF marker.

I want the above not to be a syntax error, but reported as an error Unbalanced parentheses, or incorrectly defined.

Is it possible to do this? My requirement is to just have an EOF token in the parser, which unfortunately I was not able to do this until the date: - ((, the rest of the work will be easily done as it was then, I just need to give a rule like:

print    :    print_stmt '(' stmt_valid ')' colon '\n' 
{
    OK do the rest
}
         |    print_stmt '(' stmt_valid ')' colon end_indicator
{
    print error message and close application.
}

      

0


source to share


1 answer


Make your lexer return a token for EOF.

I suggest you return the token for two cases:



  • getc()

    returned -1

    (normal EOF). In this case, make an empty token

  • getc()

    returned '\x1a'

    . In this case, put this symbol in the token text.

This allows your grammar to distinguish between the two.

+3


source







All Articles