如何解决转移/减少冲突?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题根本不在于这些规则。尽管 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 aLPAREN
.Look at any other rules that can potentially be after
command
and start withLPAREN
. You will then have to consolidate the rules. There is a very good chance that your grammar is erroneous for a specific input.您有两个产生式:
当输入标记为 IDENTIFIER LPAREN 时,会发生移位/归约冲突,因为:
IDENTIFIER
减少为command
,并保留command LPAREN
。LPAREN
转移到IDENTIFIER
旁边的堆栈上,并继续读取,尝试找到parlist
。您可以通过执行以下操作来修复它:
You have two productions:
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 theIDENTIFIER
already on the stack intocommand
, and havecommand LPAREN
remainingLPAREN
onto the stack next toIDENTIFIER
and keep reading, trying to find aparlist
.You can fix it by doing something like this:
尝试设置优先级:
它强制 CUP 决定冲突,采取左侧匹配。
Try to set a precedence:
It forces CUP to decide the conflict, taking the left match.