I've got a shift/reduce conflict that I can't figure out why it's occurring, and how to resolve it.
Given this grammar:
%token IDENTIFIER
%start Expression
%%
CallExpression
: Expression "(" ")"
;
Lambda
: "(" ")" "=>" Expression
;
Expression
: IDENTIFIER
| CallExpression
| Lambda
;
I want to be able to parse expressions like this (not exhaustive):
foo
foo()
() => foo
() => () => foo
() => foo()
But I receive a shift/reduce conflict here:
State 11
1 CallExpression: Expression . "(" ")"
2 Lambda: "(" ")" "=>" Expression . [$end, "("]
"(" shift, and go to state 8
"(" [reduce using rule 2 (Lambda)]
$default reduce using rule 2 (Lambda)
I thought I understood when shift/reduces occur, but this one is escaping me so I need to get schooled.
I tried learning more about the precedence directives available, left, right, precedence, nonassoc
but any use of them I try does not resolve the ambiguity and they also give me warning: useless precedence and associativity
so I'm doing something wrong. Hoping the answer is obvious to others.
I initially thought this was related to the fact that Lambdas start with ()
and CallExpressions end with it, but changing these tokens to not conflict makes no difference.
/facepalm