Jison:二元操作语法冲突

时间:2015-08-20 18:53:58

标签: 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 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 个答案:

答案 0 :(得分:1)

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