I have a small grammar written in lemon that is causing a parsing conflict.
This is the part of the grammar that is causing the conflict:
selection_statement ::= KWD_IF LPAREN expression RPAREN statement.
selection_statement ::= KWD_IF LPAREN expression RPAREN statement KWD_ELSE statement.
I have seen this answer but it only works for bison/yacc I can't figure out how to replicate it in lemon.
What is the best way to resolve this parsing conflict?
Thanks in advance.
Lemon implements precedence rules in a way that is similar but not quite identical to Bison, and that feature can be used to resolve the "dangling else" shift/reduce conflict which you've encountered, as it is usually applied in bison.
There are two main differences between Lemon and Bison precedence declarations:
Bison provides
%precedenceas an alternative to%left,%rightand%nonassoc. However,%nonassoccan generally be used anywhere where%precedencewould be more appropriate.In Bison, you can explicitly declare the precedence of a production using
%prec TERMINAL. In Lemon, you do the same thing by placing[TERMINAL]after the production. (This is explained in the precedence rules section of the manual, linked above.)Also, Bison allows you to use double-quoted strings for terminals, a feature not available in Lemon.
Putting that together, you can adapt the Bison solution to Lemon as follows:
It is also possible to make the grammar unambiguous, but it's somewhat more work. There's an example of how to do that in the linked Wikipedia entry on dangling else.