tags:

views:

366

answers:

2

If i forget to put an empty line at the end of any of my files my program gets a syntax error. The problem is my grammar expects a newline to end the current line. Since a newline doesnt exist bison generates a syntax error bc it does not finish the rule.

How do i solve this? I tried making <> return MY_EOF BUT when i do that lex crashes a horrible death. I guess theres code in its default EOF that i am not calling. and i have no idea what functions they may be. Using EOF create the error symbol EOF is used, but is not defined as a token and has no rules

A: 

In your lex file

#define yyterminate() return token::END

In your yacc file

%token END 0 "end of file"
DevDevDev
I havent seen token:: before, are you using the C++ version? I had to change it to return MY_END But %token MY_END 0 "end of file" get me the warning `./test.y:90.11-75: warning: rule never reduced because of conflicts: $accept: program "end of file"`. It seems to work but i am not 100% sure.
acidzombie24
Yes that is the C++ version.
DevDevDev
+1  A: 

You could use a flex EOF rule to append a newline to the input:

<<EOF>> { static int once = 0; return once++ ? 0 : '\n' }
Chris Dodd