no error while parsing empty file yacc/lex

核能气质少年 提交于 2020-01-06 02:58:06

问题


I have a parser with me generated from yacc/lex. It is working fine for all the rules I have set except one case.

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.

How can this be done with YACC/LEX?

Thanks in advance !!


回答1:


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
        ;

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
        ;


来源:https://stackoverflow.com/questions/11047227/no-error-while-parsing-empty-file-yacc-lex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!