user4811
user4811

Reputation: 183

How to get rid of warnings in Bison?

I get a lot of warnings in my project. Here is one excerpt of my grammar file:

stmtlist : stmt ';'           { printf(".."); }
         | stmtlist stmt ';'  { printf(".."); }
;
  1. line -> warning: unused value: $1
  2. line -> warning: unused value: $2

The problem is that I don't want to use $1 and $2. Is there a better solution to get rid of these warnings than to call Bison with "-Wno-other"???

I also get the warning that $$ is not set in some rules. How can I solve this issue? Should I always set $$ to a value? What if I have rules which clean the parsing stack and perform some actions. $$ always puts a new value on the parsing stack, right?

Upvotes: 1

Views: 2195

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126518

Well, having values that are computed and not used is a kind of code smell. Having values that are not set and then are used is definitely a problem. Combining the two, you get value that are sometimes set and sometimes uninitialized and then sometimes used, which is just an accident waiting to happen if not an outright bug.

So what you need to figure out is why do you have these unused and uninitialized things? In your example it looks like you've declared a %type for stmt, but you aren't using the value in these rules. If you're never using the value from stmt, then the right thing to do is get rid of the %type for it -- that way it has no value, so bison won't warn you about not using a non-existent value.

In the case of setting $$ -- bison pushes the value in $$ onto the parse value stack when it reduces the rule. So if you don't set $$, that means it pushes garbage. Now if you then ignore that garbage (as with your stmt rules above), then its ok, but if you use the value it might be a problem.

So it may be the case that you can fix your warnings by just removing the %type declaration you don't need -- if what you're doing is declaring a value type for some non-terminal, but then not setting the value (causes a warning) and not using the value (causes another warning).

Upvotes: 2

Related Questions