bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: FYI: default %destructor/%printer for error, $undefined, $accept


From: Joel E. Denny
Subject: Re: FYI: default %destructor/%printer for error, $undefined, $accept
Date: Wed, 23 Aug 2006 21:28:39 -0400 (EDT)

I committed this clean-up patch.

Joel

Index: ChangeLog
===================================================================
RCS file: /sources/bison/bison/ChangeLog,v
retrieving revision 1.1554
diff -p -u -r1.1554 ChangeLog
--- ChangeLog   21 Aug 2006 21:53:18 -0000      1.1554
+++ ChangeLog   24 Aug 2006 01:23:47 -0000
@@ -1,3 +1,26 @@
+2006-08-23  Joel E. Denny  <address@hidden>
+
+       Whether the default %destructor/%printer applies to a particular symbol
+       isn't a question of whether the user *declares* that symbol (in %token,
+       for example).  It's a question of whether the user by any means
+       *defines* the symbol at all (by simply using a char token, for
+       example).  $end is defined by Bison whereas any other token with token
+       number 0 is defined by the user.  The error token is always defined by
+       Bison regardless of whether the user declares it with %token, but we
+       may one day let the user define error as a nonterminal instead.
+       * NEWS (2.3+): Say "user-defined" instead of "user-declared".
+       * doc/bison.texinfo (Freeing Discarded Symbols): Likewise, and document
+       the meaning of "user-defined".
+       * tests/actions.at (Default %printer and %destructor for user-declared
+       end token): Rename to...
+       (Default %printer and %destructor for user-defined end token): ...
+       this.
+
+       * src/symtab.c (symbol_destructor_get, symbol_printer_get): In the
+       computation of whether to apply the default, don't maintain a list of
+       every Bison-defined symbol.  Instead, just check for a first character
+       of '$', which a user symbol cannot have, and check for the error token.
+
 2006-08-21  Joel E. Denny  <address@hidden>
 
        Don't apply the default %destructor or %printer to the error token,
Index: NEWS
===================================================================
RCS file: /sources/bison/bison/NEWS,v
retrieving revision 1.156
diff -p -u -r1.156 NEWS
--- NEWS        29 Jul 2006 05:53:41 -0000      1.156
+++ NEWS        24 Aug 2006 01:23:48 -0000
@@ -24,7 +24,7 @@ Changes in version 2.3+:
      %destructor { free ($$); }
      %destructor { free ($$); printf ("%d", @$.first_line); } STRING1 string1
 
-  guarantees that, when the parser discards any user-declared symbol, it passes
+  guarantees that, when the parser discards any user-defined symbol, it passes
   its semantic value to `free'.  However, when the parser discards a `STRING1'
   or a `string1', it also prints its line number to `stdout'.  It performs only
   the second `%destructor' in this case, so it invokes `free' only once.
Index: doc/bison.texinfo
===================================================================
RCS file: /sources/bison/bison/doc/bison.texinfo,v
retrieving revision 1.200
diff -p -u -r1.200 bison.texinfo
--- doc/bison.texinfo   9 Aug 2006 17:13:46 -0000       1.200
+++ doc/bison.texinfo   24 Aug 2006 01:23:52 -0000
@@ -4014,7 +4014,7 @@ The Parser Function @code{yyparse}}).
 
 @deffn {Directive} %destructor @{ @var{code} @}
 @cindex default %destructor
-Invoke the braced @var{code} whenever the parser discards any user-declared
+Invoke the braced @var{code} whenever the parser discards any user-defined
 grammar symbol for which the user has not specifically declared any
 @code{%destructor}.
 This is known as the default @code{%destructor}.
@@ -4035,13 +4035,35 @@ For instance:
 @end smallexample
 
 @noindent
-guarantees that, when the parser discards any user-declared symbol, it passes
+guarantees that, when the parser discards any user-defined 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.
 
+Notice that a Bison-generated parser invokes the default @code{%destructor}
+only for user-defined as opposed to Bison-defined symbols.
+For example, the parser will not invoke it for the special Bison-defined
+symbols @code{$accept}, @code{$undefined}, or @code{$end} (@pxref{Table of
+Symbols, ,Bison Symbols}), none of which you can reference in your grammar.
+It also will not invoke it for the @code{error} token (@pxref{Table of Symbols,
+,error}), which is always defined by Bison regardless of whether you reference
+it in your grammar.
+However, it will invoke it for the end token (token 0) if you redefine it from
address@hidden to, for example, @code{END}:
+
address@hidden
+%token END 0
address@hidden smallexample
+
address@hidden
address@hidden
+In the future, it may be possible to redefine the @code{error} token as a
+nonterminal that captures the discarded symbols.
+In that case, the parser will invoke the default destructor for it as well.
address@hidden ignore
+
 @sp 1
 
 @cindex discarded symbols
Index: src/symtab.c
===================================================================
RCS file: /sources/bison/bison/src/symtab.c,v
retrieving revision 1.79
diff -p -u -r1.79 symtab.c
--- src/symtab.c        21 Aug 2006 21:53:18 -0000      1.79
+++ src/symtab.c        24 Aug 2006 01:23:52 -0000
@@ -168,8 +168,7 @@ symbol_destructor_get (symbol *sym)
     return sym->destructor;
 
   /* Apply the default %destructor only to user-defined symbols.  */
-  if (sym == errtoken || sym == undeftoken || sym == accept
-      || UNIQSTR_EQ (sym->tag, uniqstr_new ("$end")))
+  if (sym->tag[0] == '$' || sym == errtoken)
     return NULL;
   return default_destructor;
 }
@@ -214,8 +213,7 @@ symbol_printer_get (symbol *sym)
     return sym->printer;
 
   /* Apply the default %printer only to user-defined symbols.  */
-  if (sym == errtoken || sym == undeftoken || sym == accept
-      || UNIQSTR_EQ (sym->tag, uniqstr_new ("$end")))
+  if (sym->tag[0] == '$' || sym == errtoken)
     return NULL;
   return default_printer;
 }
Index: tests/actions.at
===================================================================
RCS file: /sources/bison/bison/tests/actions.at,v
retrieving revision 1.63
diff -p -u -r1.63 actions.at
--- tests/actions.at    21 Aug 2006 21:53:18 -0000      1.63
+++ tests/actions.at    24 Aug 2006 01:23:52 -0000
@@ -690,10 +690,10 @@ AT_CLEANUP
 
 
 ## ------------------------------------------------------------- ##
-## Default %printer and %destructor for user-declared end token. ##
+## Default %printer and %destructor for user-defined end token. ##
 ## ------------------------------------------------------------- ##
 
-AT_SETUP([Default %printer and %destructor for user-declared end token])
+AT_SETUP([Default %printer and %destructor for user-defined end token])
 
 AT_DATA_GRAMMAR([[input.y]],
 [[%error-verbose




reply via email to

[Prev in Thread] Current Thread [Next in Thread]