野牛文件结尾 [英] bison end of file
问题描述
如果我忘记在任何文件的末尾添加一个空行,则程序会出现语法错误.问题是我的语法期望换行符可以结束当前行.由于不存在换行符,因此bison会因为未完成规则而生成语法错误.
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.
我该如何解决?当我这样做时,我尝试使<<EOF>>
返回MY_EOF
但该lex导致可怕的死亡.我猜它的默认EOF中有我没有调用的代码.我不知道它们可能有什么功能.使用EOF创建错误symbol EOF is used, but is not defined as a token and has no rules
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
推荐答案
您可以使用flex EOF规则在输入中添加换行符:
You could use a flex EOF rule to append a newline to the input:
<<EOF>> { static int once = 0; return once++ ? 0 : '\n' }
这篇关于野牛文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!