Next: The Start-Symbol, Previous: Printing Semantic Values, Up: Bison Declarations [Contents][Index]
Bison normally warns if there are any conflicts in the grammar
(see 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 deterministic 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
You may wish to be more specific in your
specification of expected conflicts. To this end, you can also attach
%expect
and %expect-rr
modifiers to individual rules.
The interpretation of these modifiers differs from their use as
declarations. When attached to rules, they indicate the number of states
in which the rule is involved in a conflict. You will need to consult the
output resulting from -v to determine appropriate numbers to use.
For example, for the following grammar fragment, the first rule for
empty_dims
appears in two states in which the ‘[’ token is a
lookahead. Having determined that, you can document this fact with an
%expect
modifier as follows:
dims: empty_dims | '[' expr ']' dims ; empty_dims: %empty %expect 2 | empty_dims '[' ']' ;
Mid-rule actions generate implicit rules that are also subject to conflicts
(see Conflicts due to Midrule Actions). To attach
an %expect
or %expect-rr
annotation to an implicit
mid-rule action’s rule, put it before the action. For example,
%glr-parser %expect-rr 1 %% clause: "condition" %expect-rr 1 { value_mode(); } '(' exprs ')' | "condition" %expect-rr 1 { class_mode(); } '(' types ')' ;
Here, the appropriate mid-rule action will not be determined until after the ‘(’ token is shifted. Thus, the two actions will clash with each other, and we should expect one reduce/reduce conflict for each.
In general, using %expect
involves these steps:
%expect
. Use the -v option
to get a verbose list of where the conflicts occur. Bison will also
print the number of conflicts.
%expect
declaration, copying the number n from the
number that Bison printed. With GLR parsers, add an
%expect-rr
declaration as well.
%expect-rr
or %expect
modifiers
as appropriate. Rules that are in conflict appear in the output listing
surrounded by square brackets or, in the case of reduce/reduce conflicts,
as reductions having the same lookahead symbol as a square-bracketed
reduction in the same state.
Now Bison will report an error if you introduce an unexpected conflict, but will keep silent otherwise.
Next: The Start-Symbol, Previous: Printing Semantic Values, Up: Bison Declarations [Contents][Index]