Compiler construction 二进制操作语法冲突

Compiler construction 二进制操作语法冲突,compiler-construction,grammar,context-free-grammar,parser-generator,jison,Compiler Construction,Grammar,Context Free Grammar,Parser Generator,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 possi

在尝试设置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
当我替换时,消除
运算符
非终结符,而是直接写出表达式模式:

%left 'OR' 'AND'

%%

Expression:
    Operation
;


Operation:
    Expression 'AND' Expression {$$ = new yy.LogicalExpression($2, $1, $3)}
|   Expression 'OR' Expression {$$ = new yy.LogicalExpression($2, $1, $3)}
;
我没有发现这样的错误,为什么第一个语法有冲突,而第二个却没有?它们似乎与我的理解相当


提前谢谢

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

是的,但为什么不能将具有相同优先级的运算符分组在一起?@GabrielRatener:因为优先级是静态的;每个产品都有一个优先级。换句话说,优先级不会“查看”先前的缩减,因此无论发生了什么
操作符
的缩减,
操作:表达式操作符Expression
都具有相同的优先级。