views:

230

answers:

1

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

I know that this can be done using the yywrap() function in the lex file, but the requirement is that we want if the EOF is explicitly defined as in the hex value 0x1a, then how do we reference that.

Example:

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

I want the above not to be a syntax error, but to be reported as an error Unbalanced parentheses, Or is wrongly defined.

Can the above be done? My requirement is just to have the EOF token in the parser, which unfortunately, I have not been able to do so till date :-((, the rest of the work will be easily done as then, I just have to give a rule such as:

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

Make your lexer return a token for the EOF.

I suggest that you return the token for two cases:

  1. getc() returned -1 (the usual EOF sign). In this case, make the token text empty

  2. getc() returned '\x1a'. In this case, put this character in the token text.

This allows your grammar to differentiate between these two cases.

Aaron Digulla