[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
3.7.8 Suppressing Conflict Warnings
Bison normally warns if there are any conflicts in the grammar
(see section Shift/Reduce Conflicts), but most real grammars
have harmless shift/reduce conflicts which are resolved in a predictable
way and would be difficult to eliminate. It is desirable to suppress
the warning about these conflicts unless the number of conflicts
changes. You can do this with the %expect
declaration.
The declaration looks like this:
%expect n |
Here n is a decimal integer. The declaration says there should be n shift/reduce conflicts and no reduce/reduce conflicts. Bison reports an error if the number of shift/reduce conflicts differs from n, or if there are any reduce/reduce conflicts.
For normal LALR(1) parsers, reduce/reduce conflicts are more serious, and should be eliminated entirely. Bison will always report reduce/reduce conflicts for these parsers. With GLR parsers, however, both kinds of conflicts are routine; otherwise, there would be no need to use GLR parsing. Therefore, it is also possible to specify an expected number of reduce/reduce conflicts in GLR parsers, using the declaration:
%expect-rr n |
In general, using %expect
involves these steps:
-
Compile your grammar without
%expect
. Use the ‘-v’ option to get a verbose list of where the conflicts occur. Bison will also print the number of conflicts. - Check each of the conflicts to make sure that Bison's default resolution is what you really want. If not, rewrite the grammar and go back to the beginning.
-
Add an
%expect
declaration, copying the number n from the number which Bison printed. With GLR parsers, add an%expect-rr
declaration as well.
Now Bison will warn you if you introduce an unexpected conflict, but will keep silent otherwise.