[ACCEPTED]-bison end of file-eof
You could use a flex EOF rule to append 1 a newline to the input:
<<EOF>> { static int once = 0; return once++ ? 0 : '\n' }
In your lex file
#define yyterminate() return token::END
In your yacc file
%token END 0 "end of file"
0
In fact to catch end of file in lex|flex 7 you can use the yywrap()
function which is called 6 by lexical analyzer if end of the input 5 file is reached.
this solution is available 4 by both lex and flex.the callback of yywrap()
indicates 3 the EOF
so you can reimplement this function 2 and inject the work you need to do at the 1 end of your input stream.
Previous works fine for me.
If you use C 5 Bizon (not C++), just use END for token::END 4 and in yacc file %token END
Had another issue after 3 that, if the macros return not YY_NULL, it 2 never terminates (infinite loop)
It can be 1 solved like this:
bool term = false;
#define yyterminate() return (term = !term)?END : YY_NULL
An alternative approach would be to restructure 8 your grammar to not need a newline at the 7 end. As long as your language allows blank 6 lines (usually the case), you can write 5 your grammar using newline as a line separator rather 4 than a line terminator
input: line | input '\n' line ;
line: /* empty */
| ... various other rules ...
Now if you do have a newline at 3 the end of the input, this gets treated 2 as a blank line after that newline, which 1 is probably fine.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.