gpt4 book ai didi

冲突 Bison 解析器

转载 作者:行者123 更新时间:2023-11-30 16:53:00 37 4
gpt4 key购买 nike

我是 Bison 新手,在处理 Shift/Reduce 冲突时遇到了麻烦...

我正在为C语言编写语法规则:ID是标识变量的标记,我编写此规则是为了确保即使将标识符写在括号中,也可以考虑它。

id              : '(' ID ')'    {printf("(ID) %s\n", $2);}
| ID {printf("ID %s\n", $1);}
;

Bison 冲突的输出是:

State 82

12 id: '(' ID . ')'
13 | ID .

')' shift, and go to state 22

')' [reduce using rule 13 (id)]
$default reduce using rule 13 (id)

如何解决此冲突?

希望我说得清楚,并感谢您的帮助。

最佳答案

您的 id 规则本身不会导致移位/归约错误。您的语法中必须有其他一些使用 ID 的规则。例如,您有一个表达式规则,例如:

expr: '(' expr ')'
| ID
;

在上面的示例中,ID 可以简化为 idexpr,并且解析器不知道要进行哪种简化。检查状态 22 中的内容。

<小时/>编辑:您问“ 我可以做什么来解决冲突?

I'm writing the rules for grammar for the C language: ID is a token that identifies a variable, and I wrote this rule to ensure that the identifier can be considered even if it is written in parentheses

括号内的变量作为左侧是无效的,因此它只能出现在右侧。然后您可以将其视为一个表达式,因此只需删除您的规则并将使用 id 的位置替换为 expr

关于冲突 Bison 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41059035/

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