gpt4 book ai didi

compiler-construction - Jison:二元运算语法冲突

转载 作者:行者123 更新时间:2023-12-04 08:37:55 25 4
gpt4 key购买 nike

在尝试设置我的 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)}
;

我没有收到这样的错误,为什么第一个语法有冲突,而第二个没有?它们似乎与我的理解相同。

提前致谢!

最佳答案

前瞻太多了,反正第一种形式是错的。第二种形式是正确的。您需要为 AND 和 OR 以及所有其他运算符编写单独的产生式。否则你无法获得运算符优先级。

关于compiler-construction - Jison:二元运算语法冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32126147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com