如何解决转移/减少冲突? [英] How to solve a shift/reduce conflict?

查看:132
本文介绍了如何解决转移/减少冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CUP创建论文所需的解析器.我的语法有移位/减少冲突.我有这个生产规则:

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;

我有这个警告:

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

现在,我实际上希望它转移,所以我对此很满意,但是我的教授告诉我找到一种解决冲突的方法.我瞎了.我一直在阅读有关if/else冲突的信息,但对我而言似乎并非如此. 你能帮我吗?

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("和RPAREN)"是终端,parlist和command不是终端.

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

推荐答案

您的问题根本不在这些规则之内.尽管迈克尔·莫罗泽克(Michael Mrozek)的回答是解决悬而未决的问题"的正确方法,但它并不能解决当前的问题.

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.

如果查看错误消息,则会发现在对LPAREN进行词法化时出现了shift/reduce冲突.我非常确定,仅规则不会造成冲突.

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.

我看不到你的语法,所以我帮不了你.但是您的冲突可能是在command之后跟随以LPAREN开头的其他规则.

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.

查看可能在command之后并以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.

这篇关于如何解决转移/减少冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆