Enable declaration of default %printer/%destructor. Make the parser

use these for all user-declared grammar symbols for which the user does
not declare a specific %printer/%destructor.  Thus, the parser uses it
for token 0 if the user declares it but not if Bison generates it as
$end.  Discussed starting at
<http://lists.gnu.org/archive/html/bison-patches/2006-02/msg00064.html>,
<http://lists.gnu.org/archive/html/bison-patches/2006-06/msg00091.html>,
and
<http://lists.gnu.org/archive/html/bison-patches/2006-07/msg00019.html>.
* NEWS (2.3+): Mention.
* doc/bison.texinfo (Actions in Mid-Rule): It's no longer impossible to
declare a %destructor for a mid-rule's semantic value.  It's just
impossible to declare one specific to it.
(Freeing Discarded Symbols): Mention that @$ can be used in %destructor
code.  Describe default %destructor form.
* src/parse-gram.y (grammar_declaration): Parse default
%printer/%destructor declarations.
* src/output.c (symbol_destructors_output): Use symbol_destructor_get
and symbol_destructor_location_get rather than accessing the destructor
and destructor_location members of struct symbol.
(symbol_printers_output): Likewise but for %printer's.
* src/reader.c (symbol_should_be_used): Likewise but for %destructor's
again.
* src/symtab.c (default_destructor, default_destructor_location,
default_printer, default_printer_location): New static global
variables to record the default %destructor and %printer.
(symbol_destructor_get, symbol_destructor_location_get,
symbol_printer_get, symbol_printer_location_get): New functions to
compute the appropriate %destructor and %printer for a symbol.
(default_destructor_set, default_printer_set): New functions to set the
default %destructor and %printer.
* src/symtab.h: Prototype all those new functions.
* tests/actions.at (Default %printer and %destructor): New test to
check that the right %printer and %destructor are called, that they're
not called for $end, and that $$ and @$ work correctly.
(Default %printer and %destructor for user-declared end token): New
test to check that the default %printer and %destructor are called for
a user-declared end token.
* tests/input.at (Default %printer and %destructor redeclared, Unused
values with default %destructor): New tests to check related grammar
warnings and errors.
This commit is contained in:
Joel E. Denny
2006-07-29 05:53:42 +00:00
parent 868d2d9638
commit ec5479ce35
12 changed files with 771 additions and 276 deletions

View File

@@ -3348,8 +3348,8 @@ it might discard the previous semantic context @code{$<context>5} without
restoring it.
Thus, @code{$<context>5} needs a destructor (@pxref{Destructor Decl, , Freeing
Discarded Symbols}).
However, Bison currently provides no means to declare a destructor for a
mid-rule action's semantic value.
However, Bison currently provides no means to declare a destructor specific to
a particular mid-rule action's semantic value.
One solution is to bury the mid-rule action inside a nonterminal symbol and to
declare a destructor for that symbol:
@@ -4007,26 +4007,40 @@ symbol is automatically discarded.
Invoke the braced @var{code} whenever the parser discards one of the
@var{symbols}.
Within @var{code}, @code{$$} designates the semantic value associated
with the discarded symbol. The additional parser parameters are also
available (@pxref{Parser Function, , The Parser Function
@code{yyparse}}).
with the discarded symbol, and @code{@@$} designates its location.
The additional parser parameters are also available (@pxref{Parser Function, ,
The Parser Function @code{yyparse}}).
@end deffn
@deffn {Directive} %destructor @{ @var{code} @}
@cindex default %destructor
Invoke the braced @var{code} whenever the parser discards any user-declared
grammar symbol for which the user has not specifically declared any
@code{%destructor}.
This is known as the default @code{%destructor}.
As in the previous form, @code{$$}, @code{@@$}, and the additional parser
parameters are available.
@end deffn
For instance:
@smallexample
%union
@{
char *string;
@}
%token <string> STRING
%type <string> string
%destructor @{ free ($$); @} STRING string
%union @{ char *string; @}
%token <string> STRING1
%token <string> STRING2
%type <string> string1
%type <string> string2
%destructor @{ free ($$); @}
%destructor @{ free ($$); printf ("%d", @@$.first_line); @} STRING1 string1
@end smallexample
@noindent
guarantees that when a @code{STRING} or a @code{string} is discarded,
its associated memory will be freed.
guarantees that, when the parser discards any user-declared symbol, it passes
its semantic value to @code{free}.
However, when the parser discards a @code{STRING1} or a @code{string1}, it also
prints its line number to @code{stdout}.
It performs only the second @code{%destructor} in this case, so it invokes
@code{free} only once.
@sp 1