Manual
Manual
Manual
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 3: Bison Grammar Files 57<br />
3.7.7 Freeing Discarded Symbols<br />
During error recovery (see Chapter 6 [Error Recovery], page 83), symbols already pushed<br />
on the stack and tokens coming from the rest of the file are discarded until the parser falls<br />
on its feet. If the parser runs out of memory, or if it returns via YYABORT or YYACCEPT, all<br />
the symbols on the stack must be discarded. Even if the parser succeeds, it must discard<br />
the start symbol.<br />
When discarded symbols convey heap based information, this memory is lost. While<br />
this behavior can be tolerable for batch parsers, such as in traditional compilers, it is unacceptable<br />
for programs like shells or protocol implementations that may parse and execute<br />
indefinitely.<br />
The %destructor directive defines code that is called when a symbol is automatically<br />
discarded.<br />
%destructor { code } symbols<br />
[Directive]<br />
Invoke the braced code whenever the parser discards one of the symbols. Within<br />
code, $$ designates the semantic value associated with the discarded symbol. The<br />
additional parser parameters are also available (see Section 4.1 [The Parser Function<br />
yyparse], page 63).<br />
For instance:<br />
%union<br />
{<br />
char *string;<br />
}<br />
%token STRING<br />
%type string<br />
%destructor { free ($$); } STRING string<br />
guarantees that when a STRING or a string is discarded, its associated memory will be<br />
freed.<br />
Discarded symbols are the following:<br />
• stacked symbols popped during the first phase of error recovery,<br />
• incoming terminals during the second phase of error recovery,<br />
• the current look-ahead and the entire stack (except the current right-hand side symbols)<br />
when the parser returns immediately, and<br />
• the start symbol, when the parser succeeds.<br />
The parser can return immediately because of an explicit call to YYABORT or YYACCEPT,<br />
or failed error recovery, or memory exhaustion.<br />
Right-hand size symbols of a rule that explicitly triggers a syntax error via YYERROR are<br />
not discarded automatically. As a rule of thumb, destructors are invoked only when user<br />
actions cannot manage the memory.<br />
3.7.8 Suppressing Conflict Warnings<br />
Bison normally warns if there are any conflicts in the grammar (see Section 5.2 [Shift/Reduce<br />
Conflicts], page 72), but most real grammars have harmless shift/reduce conflicts which are<br />
resolved in a predictable way and would be difficult to eliminate. It is desirable to suppress