本文介绍了YACC规则未减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习YACC,在弄清楚它给我的警告消息时遇到了一些麻烦.这是我文件的一部分:

I'm trying to learn YACC and having a bit of trouble figuring out the warning messages it is giving me. Here is part of my file:

define_character: WORD IS STRING COLOR
{
    printf("%s's full name is %s and thier color is %s", $1, $3, $4);
};

dialog: WORD COLON STRING
{
    printf("%s says %s", $1, $3);
};

change_scene: SCENE SETSCENE WORD
{
    printf("%s is the new scene", $3);
};

它给我的警告是:

2 rules never reduced
2 useless nonterminals and 2 useless rules
warning: useless nonterminal: dialog
warning: useless nonterminal: change_scene
warning: useless rule: dialog: WORD SEMICOLON STRING
warning: useless rule: change_scene: SCENE SETSCENE WORD

如何解决这些问题?我尝试搜索,但发现由于移位/减少冲突而出现错误的人.似乎YACC通常会将其添加到警告输出中(如果有的话),但只是为了确保我尝试从change_scene中删除WORD,这样它就不会查找与其他令牌相同的任何令牌,并且它仍然不会减少.我可以测试所有规则,因为哪个规则最有效.我是否在第一条规则的末尾缺少某些语法,导致其他语法出现问题?

How do I fix these? I've tried searching and I've found people who have the error due to shift/reduce conflicts. It seems that YACC usually adds that to the warning output if there are any, but just to be sure I tried removing WORD from change_scene so it would not be looking for any of the same tokens as the other ones and it still doesn't reduce. I can test all of the rules because whichever one is at top is the one that works. Am I missing some syntax at the end of the first rule that is causing problems with the rest of them?

推荐答案

我认为问题在于yacc假定第一个产品的左侧是开始符号.在这种情况下,这意味着既然您拥有

I believe that the problem is that yacc assumes that the very first production's left-hand side is the start symbol. In this case, this means that since you have

define_character: WORD IS STRING COLOR

yacc认为define_character是开始符号.您还有另外两个作品,即

yacc thinks that define_character is the start symbol. You have two other productions, namely

dialog: WORD COLON STRING

change_scene: SCENE SETSCENE WORD

但是,请注意,无法从define_character派生dialogchange_scene.因此,yacc告诉您您有两个无用的非终结符,也就是这两个,因为它们永远无法被导出.出现两个无用生产的错误是因为解析器实际上无法触发上述两个生产.

Notice, however, that there's no way to derive either dialog or change_scene from define_character. Consequently, yacc is telling you that you have two useless nonterminals, namely these two, since they can never be derived. The errors about two useless productions arise because both of the above productions can't ever actually be triggered by the parser.

我不确定我是否理解您要使用此代码执行的操作,但是要解决此问题,您需要以某种方式使它生效,以便可以访问这些非终结符.您能否详细说明您要使用yacc完成的工作?

I'm not sure I understand what you're trying to do with this code, but to fix this you'll need to somehow make it so that these nonterminals can be reached. Can you elaborate on precisely what you're trying to accomplish with yacc?

这篇关于YACC规则未减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:14