Home > OS >  Using Yacc and Lex to generate parse tree
Using Yacc and Lex to generate parse tree

Time:05-09

I've spent 6 hours writing a compiler for the productions below with Lex and Yacc. The terminal report warning: empty rule for typed nonterminal, and no action but I still can't find out what's wrong with my code.

P -> L | LP
L -> S
S -> ID = E | if C the S | if C then S else S
C -> E>E | E<E | E=E 
E -> E T | E-T | T
T -> F | T*F | T/F
F -> (E)| ID

yacc part code

lex part code

CodePudding user response:

The warning you get seems completely accurate:

F:      | …

F is a non-terminal; it has a type, and the first alternative is empty with no action. If a non-terminal has a production with no action, bison will automatically add the action $$ = $1, but it can't do that for an empty production, so it complains.

I assume the | in that line is a typo.

  • Related