以下是我的 Bison 语法规则的相关部分:
statement:
expression ';' |
IF expression THEN statement ELSE statement END_IF ';'
;
expression:
IDENTIFIER |
IDENTIFIER '('expressions')' |
LIT_INT |
LIT_REAL |
BOOL_OP |
LOG_NOT expression |
expression operator expression |
'('expression')'
;
expressions:
expression |
expressions ',' expression
;
operator:
REL_OP |
ADD_OP |
MULT_OP |
LOG_OR |
LOG_AND
;
编译时,我得到 10 个 shift/reduce 冲突:
5个冲突是由LOG_NOT表达式规则引起的:
State 45
25 expression: LOG_NOT expression .
26 | expression . operator expression
REL_OP shift, and go to state 48
ADD_OP shift, and go to state 49
MULT_OP shift, and go to state 50
LOG_OR shift, and go to state 51
LOG_AND shift, and go to state 52
REL_OP [reduce using rule 25 (expression)]
ADD_OP [reduce using rule 25 (expression)]
MULT_OP [reduce using rule 25 (expression)]
LOG_OR [reduce using rule 25 (expression)]
LOG_AND [reduce using rule 25 (expression)]
$default reduce using rule 25 (expression)
operator go to state 54
5个冲突是由表达式运算符表达式规则引起的:
State 62
26 expression: expression . operator expression
26 | expression operator expression .
REL_OP shift, and go to state 48
ADD_OP shift, and go to state 49
MULT_OP shift, and go to state 50
LOG_OR shift, and go to state 51
LOG_AND shift, and go to state 52
REL_OP [reduce using rule 26 (expression)]
ADD_OP [reduce using rule 26 (expression)]
MULT_OP [reduce using rule 26 (expression)]
LOG_OR [reduce using rule 26 (expression)]
LOG_AND [reduce using rule 26 (expression)]
$default reduce using rule 26 (expression)
operator go to state 54
我知道问题与优先级有关。例如,如果表达式是:
a + b * c
Bison 是在 a + 之后移动并希望找到一个表达式,还是将 a 简化为一个表达式?我有一种感觉,这是由于 Bison 1-token 前瞻限制,但我不知道如何重写规则来解决冲突。
我的教授会因班次/减少冲突而扣分,所以我不能使用 %expect。我的教授还说我们不能使用 %left 或 %right 优先级值。
这是我在 Stack 上的第一篇文章,所以如果我发布的都是错误的,请告诉我。我已经搜索了现有的帖子,但这似乎是一个个案。如果我使用 Stack 中的任何代码,我会在我提交的项目中注明源代码。
谢谢!