The following is my grammar:
arithmetic_expression : expression + expression
| expression - expression
expression : constant
| ID
| arithmetic_expression
| ternary
ternary : expression ? expression : expression
I receive a shift-reduce error in this state:
state 126
(19) ternary -> expression QUESTION_MARK expression COLON expression .
(27) arithmetic_exp -> expression . PLUS expression
(28) arithmetic_exp -> expression . MINUS expression
(19) ternary -> expression . QUESTION_MARK expression COLON expression
! shift/reduce conflict for PLUS resolved as shift
! shift/reduce conflict for MINUS resolved as shift
! shift/reduce conflict for QUESTION_MARK resolved as shift
PLUS shift and go to state 86
MINUS shift and go to state 88
QUESTION_MARK shift and go to state 85
! PLUS [ reduce using rule 19 (ternary -> expression QUESTION_MARK expression COLON expression .) ]
! MINUS [ reduce using rule 19 (ternary -> expression QUESTION_MARK expression COLON expression .) ]
! QUESTION_MARK [ reduce using rule 19 (ternary -> expression QUESTION_MARK expression COLON expression .) ]
I believe the conflict is that
true ? 1 : false ? 3 : 2
can be interpreted as true ? 1 : (false ? 3 : 2)
or (true ? 1 : false) ? 3 : 2
.
I have set the precedence for +
, and -
as left associative and higher level than ?
(which I set to right associativity).
What am I doing wrong?