06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

58 Bison 2.3<br />

the warning about these conflicts unless the number of conflicts changes. You can do this<br />

with the %expect declaration.<br />

The declaration looks like this:<br />

%expect n<br />

Here n is a decimal integer. The declaration says there should be n shift/reduce conflicts<br />

and no reduce/reduce conflicts. Bison reports an error if the number of shift/reduce conflicts<br />

differs from n, or if there are any reduce/reduce conflicts.<br />

For normal LALR(1) parsers, reduce/reduce conflicts are more serious, and should be<br />

eliminated entirely. Bison will always report reduce/reduce conflicts for these parsers. With<br />

GLR parsers, however, both kinds of conflicts are routine; otherwise, there would be no<br />

need to use GLR parsing. Therefore, it is also possible to specify an expected number of<br />

reduce/reduce conflicts in GLR parsers, using the declaration:<br />

%expect-rr n<br />

In general, using %expect involves these steps:<br />

• Compile your grammar without %expect. Use the ‘-v’ option to get a verbose list of<br />

where the conflicts occur. Bison will also print the number of conflicts.<br />

• Check each of the conflicts to make sure that Bison’s default resolution is what you<br />

really want. If not, rewrite the grammar and go back to the beginning.<br />

• Add an %expect declaration, copying the number n from the number which Bison<br />

printed. With GLR parsers, add an %expect-rr declaration as well.<br />

Now Bison will warn you if you introduce an unexpected conflict, but will keep silent<br />

otherwise.<br />

3.7.9 The Start-Symbol<br />

Bison assumes by default that the start symbol for the grammar is the first nonterminal<br />

specified in the grammar specification section. The programmer may override this restriction<br />

with the %start declaration as follows:<br />

%start symbol<br />

3.7.10 A Pure (Reentrant) Parser<br />

A reentrant program is one which does not alter in the course of execution; in other words, it<br />

consists entirely of pure (read-only) code. Reentrancy is important whenever asynchronous<br />

execution is possible; for example, a nonreentrant program may not be safe to call from a<br />

signal handler. In systems with multiple threads of control, a nonreentrant program must<br />

be called only within interlocks.<br />

Normally, Bison generates a parser which is not reentrant. This is suitable for most<br />

uses, and it permits compatibility with Yacc. (The standard Yacc interfaces are inherently<br />

nonreentrant, because they use statically allocated variables for communication with yylex,<br />

including yylval and yylloc.)<br />

Alternatively, you can generate a pure, reentrant parser. The Bison declaration %pureparser<br />

says that you want the parser to be reentrant. It looks like this:<br />

%pure-parser

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!