如何解决转移/减少冲突?

发布于 2024-09-08 00:31:30 字数 516 浏览 10 评论 0原文

我正在使用 CUP 创建论文所需的解析器。我的语法中存在移位/归约冲突。我有这个产生式规则:

command ::= IDENTIFIER | IDENTIFIER LPAREN parlist RPAREN;

并且我有这个警告:

Warning : *** Shift/Reduce conflict found in state #3
between command ::= IDENTIFIER (*) 
and     command ::= IDENTIFIER (*) LPAREN parlist RPAREN 
under symbol LPAREN

现在,我实际上希望它改变,所以我对此很满意,但我的教授告诉我找到一种解决冲突的方法。我是瞎子。我总是读到有关 if/else 冲突的文章,但对我来说,情况似乎并非如此。 你能帮助我吗?

PS:IDENTIFIER、LPAREN“(”和RPAREN“)”是终端,parlist和command不是。

I'm using CUP to create a parser that I need for my thesis. I have a shift/reduce conflict in my grammar. I have this production rule:

command ::= IDENTIFIER | IDENTIFIER LPAREN parlist RPAREN;

and I have this warning:

Warning : *** Shift/Reduce conflict found in state #3
between command ::= IDENTIFIER (*) 
and     command ::= IDENTIFIER (*) LPAREN parlist RPAREN 
under symbol LPAREN

Now, I actually wanted it to shift so I'm pretty ok with it, but my professor told me to find a way to solve the conflict. I'm blind. I've always read about the if/else conflict but to me this doesn't seem the case.
Can you help me?

P.S.: IDENTIFIER, LPAREN "(" and RPAREN ")" are terminal, parlist and command are not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱,才寂寞 2024-09-15 00:31:30

你的问题根本不在于这些规则。尽管 Michael Mrozek 的答案是解决“悬挂的 else 问题”的正确方法,但它并没有抓住手头的问题。

如果您查看错误消息,您会发现词法分析 LPAREN 时存在移位/归约冲突。我很确定规则本身不会造成冲突。

我看不到你的语法,所以我帮不了你。但是您的冲突可能是当命令后面跟着以LPAREN开头的不同规则时。

查看可能位于 command 之后并以 LPAREN 开头的任何其他规则。然后你必须巩固规则。对于特定输入,您的语法很可能是错误的。

Your problem is not in those rules at all. Although Michael Mrozek answer is correct approach to resolving the "dangling else problem", it does not grasp the problem at hand.

If you look at the error message, you see that the shift / reduce conflict is present when lexing LPAREN. I am pretty sure that the rules alone will not create a conflict.

I can't see your grammar, so I can't help you. But your conflict is probably when a command is followed by a different rule that start with a LPAREN.

Look at any other rules that can potentially be after command and start with LPAREN. You will then have to consolidate the rules. There is a very good chance that your grammar is erroneous for a specific input.

水水月牙 2024-09-15 00:31:30

您有两个产生式:

command ::= IDENTIFIER
command ::= IDENTIFIER LPAREN parlist RPAREN;

当输入标记为 IDENTIFIER LPAREN 时,会发生移位/归约冲突,因为:

  • LPAREN 可能是您未列出的新产生式的开始,在这种情况下,解析器应该将堆栈上已有的IDENTIFIER减少为command,并保留command LPAREN
  • 它们都可以是第二个产生式,因此它应该将 LPAREN 转移到 IDENTIFIER 旁边的堆栈上,并继续读取,尝试找到 parlist

您可以通过执行以下操作来修复它:

command ::= IDENTIFIER command2
command2 ::= LPAREN parlist RPAREN |;

You have two productions:

command ::= IDENTIFIER
command ::= IDENTIFIER LPAREN parlist RPAREN;

It's a shift/reduce conflict when the input tokens are IDENTIFIER LPAREN, because:

  • LPAREN could be the start of a new production you haven't listed, in which case the parser should reduce the IDENTIFIER already on the stack into command, and have command LPAREN remaining
  • They could both be the start of the second production, so it should shift the LPAREN onto the stack next to IDENTIFIER and keep reading, trying to find a parlist.

You can fix it by doing something like this:

command ::= IDENTIFIER command2
command2 ::= LPAREN parlist RPAREN |;
眼泪也成诗 2024-09-15 00:31:30

尝试设置优先级:

precedence left     LPAREN, RPARENT;

它强制 CUP 决定冲突,采取左侧匹配。

Try to set a precedence:

precedence left     LPAREN, RPARENT;

It forces CUP to decide the conflict, taking the left match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文