bison end of file

2019-02-08 23:11发布

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 doesn't exist bison generates a syntax error because it does not finish the rule.

How do I solve this? I tried making <<EOF>> return MY_EOF BUT when I do that lex crashes a horrible death. I guess there's code in its default EOF that I am not calling. 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

标签: yacc bison lex eof
4条回答
The star\"
2楼-- · 2019-02-08 23:44

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.

查看更多
Evening l夕情丶
3楼-- · 2019-02-08 23:45

In your lex file

#define yyterminate() return token::END

In your yacc file

%token END 0 "end of file"
查看更多
相关推荐>>
4楼-- · 2019-02-09 00:05

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

<<EOF>> { static int once = 0; return once++ ? 0 : '\n' }
查看更多
老娘就宠你
5楼-- · 2019-02-09 00:05

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
查看更多
登录 后发表回答