I am using JavaCUP tool in order to produce a parser for my language. I am trying to write a grammar that matches nested and multiple if_else statements.
Input file
// matches
if ()
if ()
else
if ()
else
else
// no match -> modifying grammar leads to shift/reduce conflict
if ()
else
Grammar
expr ::= if_then_else_statement;
if_then_else_statement ::= IF LPAREN RPAREN if_then_else_statement ELSE if_then_else_statement
| ;
This grammar matches nested if_else statements. However it only recognizes the first nested if_else statement of my input file.
I modified my grammar in order to match multiple statements like this:
expr ::= expr if_then_else_statement;
| ;
if_then_else_statement ::= IF LPAREN RPAREN if_then_else_statement ELSE if_then_else_statement
| ;
The result was a shift/reduce conflict caused by the empty rule (I guess). How can I modify it to support both nested and multiple if_else statements without the use of precedence?