简体   繁体   English

警告:规则在语法中无用 (Bison/Yacc)

[英]Warning: rule useless in grammar (Bison/Yacc)

I have been trying to solve this issue related to a current school assignment and would highly appreciate if anyone could explain to my why I am getting warnings from my compiler such as decafast.y:201.13-16: warning: rule useless in grammar [-Wother] | Type.我一直在尝试解决与当前学校作业相关的这个问题,如果有人能向我解释为什么我会收到来自编译器的警告,例如decafast.y:201.13-16: warning: rule useless in grammar [-Wother] | Type. decafast.y:201.13-16: warning: rule useless in grammar [-Wother] | Type.

I have provided my code in the two following pastebin files:我在以下两个 pastebin 文件中提供了我的代码:

decafast.lex: https://pastebin.com/2qzG2cwW
decafast.y: https://pastebin.com/Akg5ehW1

I have also been provided with a file 'decafast.cc' that contains classes and methods that enable me to create lists (I believe that is the purpose), found at:我还收到了一个文件“decafast.cc”,其中包含使我能够创建列表的类和方法(我相信这就是目的),可在以下位置找到:

https://pastebin.com/M7XRJunL

and the specification that I am supposed to follow (grammar found at bottom of page):以及我应该遵循的规范(在页面底部找到语法):

http://anoopsarkar.github.io/compilers-class/decafspec.html

My main concern is why I seem to be getting these warnings that are (what I assume to be) causing my code to fail.我主要担心的是为什么我似乎收到这些警告(我认为是)导致我的代码失败。 Nearly every (if not all) of my grammar is deemed to be useless, and despite my online searching (or lack thereof of understanding what has been said already), I am still unsuccessful.几乎所有(如果不是全部)我的语法都被认为是无用的,尽管我在网上搜索(或缺乏理解已经说过的内容),但我仍然没有成功。

I also had a secondary question if anyone is able to enlighten me.如果有人能够启发我,我还有一个次要问题。 Regarding the .cc file above, I have been given a few classes that implement the decafAST class.关于上面的 .cc 文件,我得到了一些实现 decafAST 类的类。 In my parser generator file (decafast.y), I attempt to create lists by doing something along the lines of在我的解析器生成器文件 (decafast.y) 中,我尝试通过执行以下操作来创建列表

decafStmtList *s = new decafStmtList();

I assumed that this would allow me to use the push_back() and push_front() methods, which is why I try things such as (in the case of ident_list, line 94) if I see one T_ID, then I create the list for T_ID (identifiers) and push the current T_ID into the list.我假设这将允许我使用 push_back() 和 push_front() 方法,这就是为什么我尝试诸如(在 ident_list 的情况下,第 94 行)如果我看到一个 T_ID,然后我为 T_ID 创建列表(标识符)并将当前 T_ID 推送到列表中。 If I see a situation of ident_list T_COMMA T_ID (which is what I assumed to be the case of a recurring list of comma-separated identifiers), then I would recognize that as an ident_list pattern, and thus push that T_ID into the list as well.如果我看到 ident_list T_COMMA T_ID 的情况(这是我假设的逗号分隔标识符的重复列表的情况),那么我会将其识别为 ident_list 模式,从而将该 T_ID 也推送到列表中. Is this the correct way to use the lists that I have been provided?这是使用我提供的列表的正确方法吗?

I would like to stress that as this is an assignment question, I am pleading for any help that you can provide that will allow me to learn on my own terms.我想强调的是,由于这是一个作业问题,我恳请您提供任何帮助,使我能够按照自己的方式学习。 I am certain that the users on this website would easily be able to solve this assignment, therefore I would very highly appreciate any insight that you may be able to offer without giving me explicit answers.我确信本网站上的用户可以轻松解决此任务,因此我非常感谢您在不给我明确答案的情况下提供的任何见解。 Thank you all for your time!谢谢大家的时间!

The grammar you were provided starts with:为您提供的语法开头为:

Program = Externs package identifier "{" FieldDecls MethodDecls "}" .

That is, a Program consists of:也就是说,一个程序包括:

  • A possibly empty list of external declarations (library functions used)一个可能为空的外部声明列表(使用的库函数)
  • The keyword "package"关键词“包”
  • An identifier标识符
  • An open brace开放式大括号
  • A possibly empty list of field declarations一个可能为空的字段声明列表
  • A possibly empty list of method declarations一个可能为空的方法声明列表
  • A close brace.一个紧密的支撑。

Most of the rest of the grammar defines what field and method declarations look like, although a couple of productions define external declarations.语法的其余部分定义了字段和方法声明的样子,尽管有几个产生式定义了外部声明。

But your grammar is quite different: (I removed the actions because they aren't relevant to the grammar)但是你的语法完全不同:(我删除了动作,因为它们与语法无关)

start: program
program: extern_list decafpackage
extern_list: 
    | ExternDefn
decafpackage: T_PACKAGE T_ID T_LCB T_RCB

Your decafpackage consists only of package ID { } , with nothing between the braces.您的decafpackage仅包含package ID { } ,大括号之间没有任何内容。

So most of the rest of the grammar productions, which detail field amd method declarations, can never be used, making them useless.因此,其余的大部分语法产生式(详细说明字段和方法声明)永远无法使用,从而使它们无用。

(Also, your extern_list does not define a list of ExternDecl . It defines an optional ExternDecl . I think you made that same mistake in other list productions.) (此外,您的extern_list没有定义ExternDecl列表。它定义了一个可选的ExternDecl 。我认为您在其他列表制作中犯了同样的错误。)

The syntax for a bison rule is: result: components…;野牛规则的语法是: result: components...;

As far as I see none of your rules have the semicolon.据我所知,你的规则都没有分号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM