I'm trying to learn basics of the Lemon parser generator, but I stuck quickly.
Here's a tiny grammar:
%right PLUS_PLUS.
%left DOT.
program ::= expr.
member_expr ::= expr DOT IDENTIFIER.
lhs_expr ::= member_expr.
expr ::= lhs_expr.
expr ::= PLUS_PLUS lhs_expr.
It causes 1 parsing conflict:
State 3:
(3) expr ::= lhs_expr *
(4) expr ::= PLUS_PLUS lhs_expr *
DOT reduce 3 expr ::= lhs_expr
DOT reduce 4 ** Parsing conflict **
{default} reduce 4 expr ::= PLUS_PLUS lhs_expr
Whereas, if I rewrite the last rule as follows:
expr ::= PLUS_PLUS expr DOT IDENTIFIER.
Then it causes no conflicts. But I don't think that this is the right way to go.
I'd appreciate if someone could explain what's the right way, and why.
lhs_expr
you could simply writeexpr DOT IDENTIFIER
to see clearly what is really being asked for. If everything is in terms ofexpr
you can see the conflict more clearly. – Alan Mimmslhs_expr
can be something other thanexpr DOT IDENTIFIER
. This particular grammar does not contain other rules for it, because I wanted it to be very minimal, but it can also be e.g.IDENTIFIER
, orexpr LBRACKET expr RBRACKET
, etc. And I want to write the rulePLUS_PLUS lhs_expr
just once, and cover all possible left-hand side expressions being pre-incremented. – Dmitry Frank