From: Joel E. Denny Date: Tue, 21 Feb 2006 21:01:57 +0000 (+0000) Subject: * doc/bison.texinfo (Mid-Rule Actions): Explain how to bury a X-Git-Tag: v2.3b~444 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/841a773708453d8e46d69d0727654fbdba6e063e?ds=inline * doc/bison.texinfo (Mid-Rule Actions): Explain how to bury a mid-rule action inside a nonterminal symbol in order to declare a destructor for its semantic value. --- diff --git a/ChangeLog b/ChangeLog index c8d3e981..d56eb26e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2006-02-21 Joel E. Denny + + * doc/bison.texinfo (Mid-Rule Actions): Explain how to bury a + mid-rule action inside a nonterminal symbol in order to declare a + destructor for its semantic value. + 2006-02-16 Paul Eggert * data/yacc.c [(! defined yyoverflow || YYERROR_VERBOSE) && ! diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 35b978af..eb36845f 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -3317,6 +3317,46 @@ earlier action is used to restore the prior list of variables. This removes the temporary @code{let}-variable from the list so that it won't appear to exist while the rest of the program is parsed. +@findex %destructor +@cindex discarded symbols, mid-rule actions +@cindex error recovery, mid-rule actions +In the above example, if the parser initiates error recovery (@pxref{Error +Recovery}) while parsing the tokens in the embedded statement @code{stmt}, +it might discard the previous semantic context @code{$5} without +restoring it. +Thus, @code{$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. + +One solution is to bury the mid-rule action inside a nonterminal symbol and to +declare a destructor for that symbol: + +@example +@group +%type let +%destructor @{ pop_context ($$); @} let + +%% + +stmt: let stmt + @{ $$ = $2; + pop_context ($1); @} + ; + +let: LET '(' var ')' + @{ $$ = push_context (); + declare_variable ($3); @} + ; + +@end group +@end example + +@noindent +Note that the action is now at the end of its rule. +Any mid-rule action can be converted to an end-of-rule action in this way, and +this is what Bison actually does to implement mid-rule actions. + Taking action before a rule is completely recognized often leads to conflicts since the parser must commit to a parse in order to execute the action. For example, the following two rules, without mid-rule actions, @@ -3410,10 +3450,7 @@ compound: subroutine @noindent Now Bison can execute the action in the rule for @code{subroutine} without -deciding which rule for @code{compound} it will eventually use. Note that -the action is now at the end of its rule. Any mid-rule action can be -converted to an end-of-rule action in this way, and this is what Bison -actually does to implement mid-rule actions. +deciding which rule for @code{compound} it will eventually use. @node Locations @section Tracking Locations