There is no error parsing an empty yacc / lex file

I have a parser with me created from yacc / lex. It works great for all the rules that I set, except in one case.

If the file is empty, which the parser parses, it gives an error. I want to add a rule so that it does not throw an error when the file is empty. I have not added any checks for this in any of the .l / .y files.

How can this be done with YACC / LEX?

Thanks in advance!

+1
source share
1 answer

The lexer should recognize the end of the input and return the token (i.e. EOF) accordingly .

A grammar start rule might look like this:

%start program

...

program : EOF 
        | instructions EOF
        ;

, "" . GNU bison:

input   : /* empty */
        | input line
        ;
+2

All Articles