سؤال

When I'm trying to compile this simple parser using Lemon, I get a conflict but I can't see which rule is wrong. The conflict disappear if I remove the binaryexpression or the callexpression.

%left Add.

program ::= expression.

expression ::= binaryexpression.
expression ::= callexpression.

binaryexpression ::= expression Add expression.

callexpression ::= expression arguments.

arguments ::= LParenthesis argumentlist RParenthesis.
arguments ::= LParenthesis RParenthesis.

argumentlist ::= expression argumentlist.
argumentlist ::= expression.

[edit] Adding a left-side associativity to LParenthesis has solved the conflict. However, I'm willing to know if it's the correct thing to do : I've seen that some grammars (f.e. C++) have a different precedence for the construction-operator '()' and the call-operator '()'. So I'm not sure about the right thing to do.

هل كانت مفيدة؟

المحلول

The problem is that the grammar is ambiguous. It is not possible to decide between reducing to binaryexpression or callexpression without looking at all the input sequence. The ambiguity is because of the left recursion over expression, which cannot be ended because expression cannot derive a terminal.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top