以下是我的语法:
arithmetic_expression : expression + expression
| expression - expression
expression : constant
| ID
| arithmetic_expression
| ternary
ternary : expression ? expression : expression
在这种状态下,我收到一个 shift-reduce 错误:
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 .) ]
我相信冲突在于
true ? 1 : false ? 3 : 2
可以解释为true ? 1 : (false ? 3 : 2)
或(true ? 1 : false) ? 3 : 2
。
我已将 和 的优先级设置+
为-
左关联和比?
(我设置为右关联)更高的级别。
我究竟做错了什么?