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)} 
; 

我没有这样的错误或者,为什么第一个语法有冲突,但不是第二个?他们似乎相当于我的理解。

提前致谢!

回答

1

预测太多,但第一种形式无论如何都是错误的。第二种形式是正确的。您需要为AND和OR以及所有其他操作员编写单独的作品。否则,您无法获得运算符优先级。

+0

是的,但为什么不能将具有相同优先级的运算符组合在一起? –

+1

@GabrielRatener:因为优先级是静态的;每个生产都有一个优先。换句话说,优先级不会“查看”前一个约简,所以“操作:表达式运算符表达式”具有相同的优先级,而不管“运算符”发生什么减少。 – rici