3
在試圖建立我的Jison語法我有:Jison:二進制操作語法衝突
%left 'OR' 'AND'
%%
Expression:
Operation
;
Operation:
Expression Operator Expression {$$ = new yy.LogicalExpression($2, $1, $3)}
;
Operator:
'AND'
| 'OR'
;
但是,這導致以下衝突的消息:
Conflict in grammar: multiple actions possible when lookahead token is OR in state 6
- reduce by rule: Operation -> Expression Operator Expression
- shift token (then go to state 5)
Conflict in grammar: multiple actions possible when lookahead token is AND in state 6
- reduce by rule: Operation -> Expression Operator Expression
- shift token (then go to state 4)
States with conflicts:
State 6
Operation -> Expression Operator Expression . #lookaheads= $end OR AND
Operation -> Expression .Operator Expression
Operator -> .AND
Operator -> .OR
當我更換消除Operator
非而不是直接寫出表達模式:
%left 'OR' 'AND'
%%
Expression:
Operation
;
Operation:
Expression 'AND' Expression {$$ = new yy.LogicalExpression($2, $1, $3)}
| Expression 'OR' Expression {$$ = new yy.LogicalExpression($2, $1, $3)}
;
我沒有這樣的錯誤或者,爲什麼第一個語法有衝突,但不是第二個?他們似乎相當於我的理解。
提前致謝!
是的,但爲什麼不能將具有相同優先級的運算符組合在一起? –
@GabrielRatener:因爲優先級是靜態的;每個生產都有一個優先。換句話說,優先級不會「查看」前一個約簡,所以「操作:表達式運算符表達式」具有相同的優先級,而不管「運算符」發生什麼減少。 – rici