简体   繁体   中英

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.

I have provided my code in the two following pastebin files:

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:

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. In my parser generator file (decafast.y), I attempt to create lists by doing something along the lines of

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. 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. 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.

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.)

The syntax for a bison rule is: result: components…;

As far as I see none of your rules have the semicolon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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