bison end of file

大兔子大兔子 提交于 2019-11-29 04:03:38

You could use a flex EOF rule to append 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"
Aymanadou

In fact to catch end of file in lex|flex you can use the yywrap() function which is called by lexical analyzer if end of the input file is reached.

this solution is available by both lex and flex.the callback of yywrap() indicates the EOFso you can reimplement this function and inject the work you need to do at the end of your input stream.

Previous works fine for me.

If you use C Bizon (not C++), just use END for token::END and in yacc file %token END

Had another issue after that, if the macros return not YY_NULL, it never terminates (infinite loop)

It can be solved like this:

bool term = false;
#define yyterminate() return (term = !term)?END : YY_NULL
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!