问题描述
我有一个从yacc/lex生成的解析器.对于我设定的所有规则(一种情况除外),它都可以正常工作.
I have a parser with me generated from yacc/lex. It is working fine for all the rules I have set except one case.
如果此解析器正在解析的文件为空,则会产生错误.我想添加规则,以便在文件为空时不给出错误.我没有在我的.l/.y文件中添加任何检查.
If file is empty which this parser is parsing it gives error. I want to add rule so that it does not give error when file is empty. I have not added any checks for that in either of my .l/.y file.
如何使用YACC/LEX做到这一点?
How can this be done with YACC/LEX?
预先感谢!
推荐答案
词法分析器应识别输入的结尾并相应地返回令牌(即EOF
).
The lexer should recognize the end of input and return a token accordingly (i.e. EOF
).
您的语法的开始规则如下:
Your grammar's start rule could look like this:
%start program
...
program : EOF
| instructions EOF
;
正如艾拉·巴克斯特(Ira Baxter)所指出的那样,简单的空"规则也足够了. GNU野牛手册为此提供了一个示例:
As Ira Baxter pointed out a simple "empty" rule would also suffice. The GNU bison manual provides an example for this:
input : /* empty */
| input line
;
这篇关于解析空文件yacc/lex时没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!