www.delorie.com/gnu/docs/bison/bison_63.html   search  
 
Buy the book!


Bison 1.875

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.7.5 Freeing Discarded Symbols

Some symbols can be discarded by the parser, typically during error recovery (see section 6. Error Recovery). Basically, during error recovery, embarrassing symbols already pushed on the stack, and embarrassing tokens coming from the rest of the file are thrown away until the parser falls on its feet. If these symbols convey heap based information, this memory is lost. While this behavior is tolerable for batch parsers, such as in compilers, it is unacceptable for parsers that can possibility "never end" such as shells, or implementations of communication protocols.

The %destructor directive allows for the definition of code that is called when a symbol is thrown away.

Directive: %destructor { code } symbols
Declare that the code must be invoked for each of the symbols that will be discarded by the parser. The code should use $$ to designate the semantic value associated to the symbols. The additional parser parameters are also avaible (see section The Parser Function yyparse).

Warning: as of Bison 1.875, this feature is still considered as experimental, as there was not enough user feedback. In particular, the syntax might still change.

For instance:

 
%union
{
  char *string;
}
%token <string> STRING
%type  <string> string
%destructor { free ($$); } STRING string

guarantees that when a STRING or a string will be discarded, its associated memory will be freed.

Note that in the future, Bison might also consider that right hand side members that are not mentioned in the action can be destroyed. For instance, in:

 
comment: "/*" STRING "*/";

the parser is entitled to destroy the semantic value of the string. Of course, this will not apply to the default action; compare:

 
typeless: string;  // $$ = $1 does not apply; $1 is destroyed.
typefull: string;  // $$ = $1 applies, $1 is not destroyed.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003