1
votes

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

1 Answers

2
votes

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
        ;