bison-patches
[Top][All Lists]
Advanced

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

Re: symbol declarations after rules


From: Joel E. Denny
Subject: Re: symbol declarations after rules
Date: Fri, 23 Jun 2006 16:17:33 -0400 (EDT)

On Thu, 22 Jun 2006, Joel E. Denny wrote:

> However, I was imagining a user who just wants to add a 
> %before-definitions and/or %after-definitions to an existing grammar file.  
> Should he be forced to convert all his (other) prologue blocks to 
> %before-header and %after-header?
> 
> I guess we could go with the restriction and re-evaluate if someone 
> complains.

I'll add this restriction in a separate patch.  That'll make it easier to 
remember what to do if we want to back it out later.

> > One notational thought.  Perhaps we should change "before-definitions"
> > to "start-header", and "after-definitions" to "end-header"?  That
> > might help people remember better the relationships among the
> > %*-header directives.  Also, it will give them the pattern "%*-header"
> > to remember the concepts by.

I committed the following.

Joel

Index: ChangeLog
===================================================================
RCS file: /sources/bison/bison/ChangeLog,v
retrieving revision 1.1511
diff -p -u -r1.1511 ChangeLog
--- ChangeLog   22 Jun 2006 19:46:05 -0000      1.1511
+++ ChangeLog   23 Jun 2006 20:04:15 -0000
@@ -1,3 +1,47 @@
+2006-06-23  Joel E. Denny  <address@hidden>
+
+       Rename %before-definitions to %start-header and %after-definitions to
+       %end-header.  Don't use these declarations to separate pre-prologue
+       blocks from post-prologue blocks.  Add new order-independent
+       declarations %before-header and %after-header as alternatives to the
+       traditional Yacc pre-prologue and post-prologue blocks.  Discussed at
+       <http://lists.gnu.org/archive/html/bison-patches/2006-06/msg00110.html>.
+       * NEWS (2.3+): Update for these changes.
+       * data/glr.c (b4_before_definitions): Update to...
+       (b4_start_header): ... this.
+       (b4_after_definitions): Update to...
+       (b4_end_header): ... this.
+       * data/glr.cc: Likewise.
+       * data/lalr1.cc: Likewise.
+       * data/yacc.c: Likewise.
+       * doc/bison.texinfo (The prologue): Update names, and replace remaining
+       prologue blocks with %*-header declarations.
+       (Calc++ Parser): Likewise.
+       (Bison Declaration Summary): Update names.
+       (Bison Symbols): Update description.
+       * src/parse-gram.y (PERCENT_AFTER_DEFINITIONS): Update to...
+       (PERCENT_END_HEADER): ... this.
+       (PERCENT_BEFORE_DEFINITIONS): Update to...
+       (PERCENT_START_HEADER): ... this.
+       (PERCENT_AFTER_HEADER, PERCENT_BEFORE_HEADER): New tokens.
+       (declaration): Update token names and m4 macro names.
+       When parsing %end-header and %start-header, invoke translate_code
+       before muscle_code_grow, and no longer set global booleans to remember
+       whether these declarations have been seen.
+       Parse new %after-header and %before-header.
+       * src/reader.c (before_definitions, after_definitions): Remove.
+       (prologue_augment): Accept a new bool argument to specify whether to
+       augment the pre-prologue or post-prologue.
+       * src/reader.h (before_definitions, after_definitions): Remove these
+       extern's.
+       (prologue_augment): Add new bool argument.
+       * src/scan-gram.l (PERCENT_AFTER_DEFINITIONS): Update to...
+       (PERCENT_END_HEADER): ... this.
+       (PERCENT_BEFORE_DEFINITIONS): Update to...
+       (PERCENT_START_HEADER): ... this.
+       (PERCENT_AFTER_HEADER, PERCENT_BEFORE_HEADER): New tokens.
+       * tests/actions.at (Printers and Destructors): Update names.
+
 2006-06-22  Joel E. Denny  <address@hidden>
 
        Add comparison operators for C++ location classes.  Discussed at
Index: NEWS
===================================================================
RCS file: /sources/bison/bison/NEWS,v
retrieving revision 1.152
diff -p -u -r1.152 NEWS
--- NEWS        21 Jun 2006 01:37:01 -0000      1.152
+++ NEWS        23 Jun 2006 20:04:15 -0000
@@ -9,18 +9,16 @@ Changes in version 2.3+:
   helps to sanitize the global namespace during preprocessing, but POSIX Yacc
   requires them.  Bison still generates an enum for token names in all cases.
 
-* Handling of prologue blocks is now more consistent but potentially backward
-  incompatible.
+* Handling of traditional Yacc prologue blocks is now more consistent but
+  potentially incompatible with previous releases of Bison.
 
   As before, you declare prologue blocks in your grammar file with the
   `%{ ... %}' syntax.  To generate the pre-prologue, Bison concatenates all
-  prologue blocks that you've declared before any %union.  If you've declared a
-  %union, Bison concatenates all prologue blocks that you've declared after it
-  to generate the post-prologue.  (The new %before-definitions and
-  %after-definitions have a similar effect as %union on the prologues.  See
-  below.)
+  prologue blocks that you've declared before the first %union.  To generate
+  the post-prologue, Bison concatenates all prologue blocks that you've
+  declared after the first %union.  
 
-  Previous versions of Bison inserted the pre-prologue into both the header
+  Previous releases of Bison inserted the pre-prologue into both the header
   file and the code file in all cases except for LALR(1) parsers in C.  In the
   latter case, Bison inserted it only into the code file.  For parsers in C++,
   the point of insertion was before any token definitions (which associate
@@ -30,38 +28,50 @@ Changes in version 2.3+:
   Now, Bison never inserts the pre-prologue into the header file.  In the code
   file, it always inserts it before the token definitions.
 
-* Bison now provides the %before-definitions and %after-definitions directives.
-
-  For example, in your grammar file:
-
-    %{
-      /* A pre-prologue block.  For Yacc portability, Bison no longer puts this
-       * in the header file.  In the code file, Bison inserts it before any
-       * %before-definitions blocks.  */
-    %}
-    %before-definitions {
-      /* Bison inserts this into both the header file and code file.  In both
-       * files, the point of insertion is before any Bison-generated token,
-       * semantic type, location type, and class definitions.  This is a good
-       * place to define %union dependencies, for example.  */
+* Bison now provides a more flexible alternative to the traditional Yacc
+  prologue blocks: %before-header, %start-header, %end-header, and
+  %after-header.
+
+  For example, the following declaration order in the grammar file reflects the
+  order in which Bison will output these code blocks.  However, you are free to
+  declare these code blocks in your grammar file in whatever order is most
+  convenient for you:
+
+    %before-header {
+      /* Bison treats this block like a pre-prologue block: it inserts it into
+       * the code file before the contents of the header file.  It does *not*
+       * insert it into the header file.  This is a good place to put
+       * #include's that you want at the top of your code file.  A common
+       * example is `#include "system.h"'.  */
+    }
+    %start-header {
+      /* Bison inserts this block into both the header file and the code file.
+       * In both files, the point of insertion is before any Bison-generated
+       * token, semantic type, location type, and class definitions.  This is a
+       * good place to define %union dependencies, for example.  */
     }
     %union {
-      /* With previous versions of Bison, the first %union in your grammar file
-       * separated the pre-prologue blocks from the post-prologue blocks.  Now,
-       * the first %union, %before-definitions, or %after-definitions does
-       * that.  */
+      /* Unlike the traditional Yacc prologue blocks, the output order for the
+       * new %*-header blocks is not affected by their declaration position
+       * relative to any %union in the grammar file.  */
     }
-    %after-definitions {
-      /* If you want something in the header file and in the code file and it
-       * depends on any of the Bison-generated definitions in the header file,
-       * put it here.  */
+    %end-header {
+      /* Bison inserts this block into both the header file and the code file.
+       * In both files, the point of insertion is after the Bison-generated
+       * definitions.  This is a good place to declare or define public
+       * functions or data structures that depend on the Bison-generated
+       * definitions.  */
     }
-    %{
-      /* A post-prologue block.  If you want something in the code file but not
-       * in the header file and it depends on Bison-generated definitions, put
-       * it here.  In the code file, Bison inserts it after any
-       * %after-definitions blocks.  */
-    %}
+    %after-header {
+      /* Bison treats this block like a post-prologue block: it inserts it into
+       * the code file after the contents of the header file.  It does *not*
+       * insert it into the header file.  This is a good place to declare or
+       * define internal functions or data structures that depend on the
+       * Bison-generated definitions.  */
+    }
+
+  If you have multiple occurrences of any one of the above declarations, Bison
+  will concatenate the contents in declaration order.
 
 * The option `--report=look-ahead' has been changed to `--report=lookahead'.
   The old spelling still works, but is not documented and may be removed
Index: data/glr.c
===================================================================
RCS file: /sources/bison/bison/data/glr.c,v
retrieving revision 1.181
diff -p -u -r1.181 glr.c
--- data/glr.c  21 Jun 2006 01:37:01 -0000      1.181
+++ data/glr.c  23 Jun 2006 20:04:16 -0000
@@ -169,9 +169,9 @@ dnl # ----------------------
 dnl # Declaration that might either go into the header (if --defines)
 dnl # or open coded in the parser body.
 m4_define([b4_shared_declarations],
-[m4_ifdef([b4_before_definitions],
-[[/* Copy the %before-definitions blocks.  */
-]b4_before_definitions])[]dnl
+[m4_ifdef([b4_start_header],
+[[/* Copy the %start-header blocks.  */
+]b4_start_header])[]dnl
 
 b4_token_enums(b4_tokens)
 
@@ -203,9 +203,9 @@ typedef struct YYLTYPE
 # define YYLTYPE_IS_TRIVIAL 1
 #endif
 
-]m4_ifdef([b4_after_definitions],
-[[/* Copy the %after-definitions blocks.  */
-]b4_after_definitions])[]dnl
+]m4_ifdef([b4_end_header],
+[[/* Copy the %end-header blocks.  */
+]b4_end_header])[]dnl
 ])
 
 b4_defines_if([#include @address@hidden,
Index: data/glr.cc
===================================================================
RCS file: /sources/bison/bison/data/glr.cc,v
retrieving revision 1.21
diff -p -u -r1.21 glr.cc
--- data/glr.cc 21 Jun 2006 01:37:01 -0000      1.21
+++ data/glr.cc 23 Jun 2006 20:04:16 -0000
@@ -235,9 +235,9 @@ namespace ]b4_namespace[
   class location;
 }
 
-]m4_ifdef([b4_before_definitions],
-[[/* Copy the %before-definitions blocks.  */
-]b4_before_definitions])[]dnl
+]m4_ifdef([b4_start_header],
+[[/* Copy the %start-header blocks.  */
+]b4_start_header])[]dnl
 
 [/* Line __line__ of glr.cc.  */
 ]b4_syncline(address@hidden@], address@hidden@])[
@@ -386,8 +386,8 @@ m4_ifset([b4_global_tokens_and_yystype],
 
 }
 
-]m4_ifdef([b4_after_definitions],
-[[/* Copy the %after-definitions blocks.  */
-]b4_after_definitions])[]dnl
+]m4_ifdef([b4_end_header],
+[[/* Copy the %end-header blocks.  */
+]b4_end_header])[]dnl
 
 [#endif /* ! defined PARSER_HEADER_H */]
Index: data/lalr1.cc
===================================================================
RCS file: /sources/bison/bison/data/lalr1.cc,v
retrieving revision 1.134
diff -p -u -r1.134 lalr1.cc
--- data/lalr1.cc       21 Jun 2006 01:37:01 -0000      1.134
+++ data/lalr1.cc       23 Jun 2006 20:04:17 -0000
@@ -53,9 +53,9 @@ namespace ]b4_namespace[
   class location;
 }
 
-]m4_ifdef([b4_before_definitions],
-[[/* Copy the %before-definitions blocks.  */
-]b4_before_definitions])[]dnl
+]m4_ifdef([b4_start_header],
+[[/* Copy the %start-header blocks.  */
+]b4_start_header])[]dnl
 
 [/* Line __line__ of lalr1.cc.  */
 ]b4_syncline(address@hidden@], address@hidden@])[
@@ -300,9 +300,9 @@ b4_error_verbose_if([, int tok])[);
 # define YYSTYPE b4_namespace::b4_parser_class_name::semantic_type
 #endif
 ])
-m4_ifdef([b4_after_definitions],
-[[/* Copy the %after-definitions blocks.  */
-]b4_after_definitions])[]dnl
+m4_ifdef([b4_end_header],
+[[/* Copy the %end-header blocks.  */
+]b4_end_header])[]dnl
 
 [#endif /* ! defined PARSER_HEADER_H */]
 ])dnl
Index: data/yacc.c
===================================================================
RCS file: /sources/bison/bison/data/yacc.c,v
retrieving revision 1.145
diff -p -u -r1.145 yacc.c
--- data/yacc.c 21 Jun 2006 01:37:01 -0000      1.145
+++ data/yacc.c 23 Jun 2006 20:04:17 -0000
@@ -181,9 +181,9 @@ b4_locations_if([#define yylloc b4_prefi
 # define YYTOKEN_TABLE ]b4_token_table[
 #endif
 
-]m4_ifdef([b4_before_definitions],
-[[/* Copy the %before-definitions blocks.  */
-]b4_before_definitions])[]dnl
+]m4_ifdef([b4_start_header],
+[[/* Copy the %start-header blocks.  */
+]b4_start_header])[]dnl
 
 b4_token_enums_defines(b4_tokens)[
 
@@ -213,9 +213,9 @@ typedef struct YYLTYPE
 # define YYLTYPE_IS_TRIVIAL 1
 #endif])
 
-m4_ifdef([b4_after_definitions],
-[[/* Copy the %after-definitions blocks.  */
-]b4_after_definitions])[]dnl
+m4_ifdef([b4_end_header],
+[[/* Copy the %end-header blocks.  */
+]b4_end_header])[]dnl
 
 [/* Copy the second part of user declarations.  */
 ]b4_post_prologue
@@ -1498,9 +1498,9 @@ b4_defines_if(
 b4_copyright([Skeleton interface for Bison's Yacc-like parsers in C],dnl '
   [1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006])
 
-m4_ifdef([b4_before_definitions],
-[[/* Copy the %before-definitions blocks.  */
-]b4_before_definitions])[]dnl
+m4_ifdef([b4_start_header],
+[[/* Copy the %start-header blocks.  */
+]b4_start_header])[]dnl
 
 b4_token_enums_defines(b4_tokens)
 
@@ -1538,7 +1538,7 @@ typedef struct YYLTYPE
           [extern YYLTYPE b4_prefix[]lloc;])
 )dnl b4_locations_if
 
-m4_ifdef([b4_after_definitions],
-[[/* Copy the %after-definitions blocks.  */
-]b4_after_definitions])[]dnl
+m4_ifdef([b4_end_header],
+[[/* Copy the %end-header blocks.  */
+]b4_end_header])[]dnl
 ])dnl b4_defines_if
Index: doc/bison.texinfo
===================================================================
RCS file: /sources/bison/bison/doc/bison.texinfo,v
retrieving revision 1.194
diff -p -u -r1.194 bison.texinfo
--- doc/bison.texinfo   21 Jun 2006 01:37:01 -0000      1.194
+++ doc/bison.texinfo   23 Jun 2006 20:04:21 -0000
@@ -2664,19 +2664,22 @@ can be done with two @var{Prologue} bloc
 @dots{}
 @end smallexample
 
address@hidden %before-definitions
address@hidden %before-header
address@hidden %start-header
address@hidden %after-header
 If you've instructed Bison to generate a header file (@pxref{Table of Symbols,
 ,%defines}), you probably want @code{#include "ptypes.h"} to appear
 in that header file as well.
-In that case, use @code{%before-definitions} instead of a @var{Prologue}
-section (@pxref{Table of Symbols, ,%before-definitions}):
+In that case, use @code{%before-header}, @code{%start-header}, and
address@hidden instead of @var{Prologue} sections
+(@pxref{Table of Symbols, ,%start-header}):
 
 @smallexample
address@hidden
+%before-header @{
   #include <stdio.h>
address@hidden
address@hidden
 
-%before-definitions @{
+%start-header @{
   #include "ptypes.h"
 @}
 %union @{
@@ -2684,10 +2687,10 @@ section (@pxref{Table of Symbols, ,%befo
   tree t;  /* @address@hidden is defined in @file{ptypes.h}.} */
 @}
 
address@hidden
+%after-header @{
   static void print_token_value (FILE *, int, YYSTYPE);
   #define YYPRINT(F, N, L) print_token_value (F, N, L)
address@hidden
address@hidden
 
 @dots{}
 @end smallexample
@@ -4228,11 +4231,11 @@ typically needs to be able to refer to t
 and to the token type codes.  @xref{Token Values, ,Semantic Values of
 Tokens}.
 
address@hidden %before-definitions
address@hidden %after-definitions
-If you have declared @code{%before-definitions} or @code{%after-definitions},
-the output header also contains their code.
address@hidden of Symbols, ,%before-definitions}.
address@hidden %start-header
address@hidden %end-header
+If you have declared @code{%start-header} or @code{%end-header}, the output
+header also contains their code.
address@hidden of Symbols, ,%start-header}.
 @end deffn
 
 @deffn {Directive} %destructor
@@ -7462,18 +7465,18 @@ the grammar for.
 @end example
 
 @noindent
address@hidden %before-definitions
address@hidden %start-header
 Then come the declarations/inclusions needed to define the
 @code{%union}.  Because the parser uses the parsing driver and
 reciprocally, both cannot include the header of the other.  Because the
 driver's header needs detailed knowledge about the parser class (in
 particular its inner types), it is the parser's header which will simply
 use a forward declaration of the driver.
address@hidden of Symbols, ,%before-definitions}.
address@hidden of Symbols, ,%start-header}.
 
 @comment file: calc++-parser.yy
 @example
-%before-definitions @{
+%start-header @{
 # include <string>
 class calcxx_driver;
 @}
@@ -7532,15 +7535,15 @@ them.
 @end example
 
 @noindent
-The code between @address@hidden and @address@hidden after the introduction of 
the
address@hidden is output in the @file{*.cc} file; it needs detailed
-knowledge about the driver.
address@hidden %after-header
+The code between @samp{%after-header @{} and @address@hidden is output in the
address@hidden file; it needs detailed knowledge about the driver.
 
 @comment file: calc++-parser.yy
 @example
address@hidden
+%after-header @{
 # include "calc++-driver.hh"
address@hidden
address@hidden
 @end example
 
 
@@ -8208,54 +8211,79 @@ $end}, where @var{start} is the start sy
 Start-Symbol}.  It cannot be used in the grammar.
 @end deffn
 
address@hidden {Directive} %after-definitions @address@hidden@}
address@hidden {Directive} %after-header @address@hidden@}
+Specifies code to be inserted into the code file after the contents of the
+header file.
address@hidden of Symbols, ,%start-header}.
address@hidden deffn
+
address@hidden {Directive} %before-header @address@hidden@}
+Specifies code to be inserted into the code file before the contents of the
+header file.
address@hidden of Symbols, ,%start-header}.
address@hidden deffn
+
address@hidden {Directive} %end-header @address@hidden@}
 Specifies code to be inserted both into the header file (if generated;
 @pxref{Table of Symbols, ,%defines}) and into the code file after any
 Bison-generated definitions.
-For details, @xref{Table of Symbols, ,%before-definitions}.
address@hidden of Symbols, ,%start-header}.
 @end deffn
 
address@hidden {Directive} %before-definitions @address@hidden@}
address@hidden {Directive} %start-header @address@hidden@}
 Specifies code to be inserted both into the header file (if generated;
 @pxref{Table of Symbols, ,%defines}) and into the code file before any
 Bison-generated definitions.
 
 @cindex Prologue
address@hidden %after-definitions
address@hidden %before-header
 @findex %union
-For example, in your grammar file:
address@hidden %end-header
address@hidden %after-header
+For example, the following declaration order in the grammar file reflects the
+order in which Bison will output these code blocks.  However, you are free to
+declare these code blocks in your grammar file in whatever order is most
+convenient for you:
 
 @smallexample
address@hidden
-  /* A pre-prologue block.  For Yacc portability, Bison does not put
-   * this in the header file.  In the code file, Bison inserts it
-   * before any %before-definitions blocks.  */
address@hidden
-%before-definitions @{
-  /* Bison inserts this into both the header file and code file.  In
-   * both files, the point of insertion is before any Bison-generated
-   * token, semantic type, location type, and class definitions.
-   * This is a good place to define %union dependencies, for
-   * example.  */
+%before-header @{
+  /* Bison treats this block like a pre-prologue block: it inserts it
+   * into the code file before the contents of the header file.  It
+   * does *not* insert it into the header file.  This is a good place
+   * to put #include's that you want at the top of your code file.  A
+   * common example is `#include "system.h"'.  */
address@hidden
+%start-header @{
+  /* Bison inserts this block into both the header file and the code
+   * file.  In both files, the point of insertion is before any
+   * Bison-generated token, semantic type, location type, and class
+   * definitions.  This is a good place to define %union
+   * dependencies, for example.  */
 @}
 %union @{
-  /* The first %union, %before-definitions, or %after-definitions
-   * in your grammar file separates the pre-prologue blocks from the
-   * post-prologue blocks.  */
+  /* Unlike the traditional Yacc prologue blocks, the output order
+   * for the %*-header blocks is not affected by their declaration
+   * position relative to any %union in the grammar file.  */
 @}
-%after-definitions @{
-  /* If you want something in the header file and in the code file
-   * and it depends on any of the Bison-generated definitions in the
-   * header file, put it here.  */
+%end-header @{
+  /* Bison inserts this block into both the header file and the code
+   * file.  In both files, the point of insertion is after the
+   * Bison-generated definitions.  This is a good place to declare or
+   * define public functions or data structures that depend on the
+   * Bison-generated definitions.  */
address@hidden
+%after-header @{
+  /* Bison treats this block like a post-prologue block: it inserts
+   * it into the code file after the contents of the header file.  It
+   * does *not* insert it into the header file.  This is a good place
+   * to declare or define internal functions or data structures that
+   * depend on the Bison-generated definitions.  */
 @}
address@hidden
-  /* A post-prologue block.  If you want something in the code file
-   * but not in the header file and it depends on Bison-generated
-   * definitions, put it here.  In the code file, Bison inserts it
-   * after any %after-definitions blocks.  */
address@hidden
 @end smallexample
 
+If you have multiple occurrences of any one of the above declarations, Bison
+will concatenate the contents in declaration order.
+
 @xref{Prologue, ,The Prologue}.
 @end deffn
 
Index: src/parse-gram.y
===================================================================
RCS file: /sources/bison/bison/src/parse-gram.y,v
retrieving revision 1.79
diff -p -u -r1.79 parse-gram.y
--- src/parse-gram.y    21 Jun 2006 01:37:01 -0000      1.79
+++ src/parse-gram.y    23 Jun 2006 20:04:22 -0000
@@ -130,14 +130,13 @@ static int current_prec = 0;
 `----------------------*/
 
 %token
-  PERCENT_AFTER_DEFINITIONS
-                         "%after-definitions"
-  PERCENT_BEFORE_DEFINITIONS
-                         "%before-definitions"
+  PERCENT_AFTER_HEADER    "%after-header"
+  PERCENT_BEFORE_HEADER   "%before-header"
   PERCENT_DEBUG           "%debug"
   PERCENT_DEFAULT_PREC    "%default-prec"
   PERCENT_DEFINE          "%define"
   PERCENT_DEFINES         "%defines"
+  PERCENT_END_HEADER      "%end-header"
   PERCENT_ERROR_VERBOSE   "%error-verbose"
   PERCENT_EXPECT          "%expect"
   PERCENT_EXPECT_RR      "%expect-rr"
@@ -157,6 +156,7 @@ static int current_prec = 0;
   PERCENT_REQUIRE        "%require"
   PERCENT_SKELETON        "%skeleton"
   PERCENT_START           "%start"
+  PERCENT_START_HEADER    "%start-header"
   PERCENT_TOKEN_TABLE     "%token-table"
   PERCENT_VERBOSE         "%verbose"
   PERCENT_YACC            "%yacc"
@@ -214,21 +214,21 @@ declarations:
 
 declaration:
   grammar_declaration
-| PROLOGUE                         { prologue_augment (translate_code ($1, @1),
-                                                      @1); }
-| "%after-definitions" "{...}"
+| PROLOGUE
+    {
+      prologue_augment (translate_code ($1, @1), @1, typed);
+    }
+| "%after-header" "{...}"
     {
-      after_definitions = true;
       /* Remove the '{', and replace the '}' with '\n'.  */
       $2[strlen ($2) - 1] = '\n';
-      muscle_code_grow ("after_definitions", $2+1, @2);
+      prologue_augment (translate_code ($2+1, @2), @2, true);
     }
-| "%before-definitions" "{...}"
+| "%before-header" "{...}"
     {
-      before_definitions = true;
       /* Remove the '{', and replace the '}' with '\n'.  */
       $2[strlen ($2) - 1] = '\n';
-      muscle_code_grow ("before_definitions", $2+1, @2);
+      prologue_augment (translate_code ($2+1, @2), @2, false);
     }
 | "%debug"                                 { debug_flag = true; }
 | "%define" string_content
@@ -238,6 +238,12 @@ declaration:
     }
 | "%define" string_content string_content  { muscle_insert ($2, $3); }
 | "%defines"                               { defines_flag = true; }
+| "%end-header" "{...}"
+    {
+      /* Remove the '{', and replace the '}' with '\n'.  */
+      $2[strlen ($2) - 1] = '\n';
+      muscle_code_grow ("end_header", translate_code ($2+1, @2), @2);
+    }
 | "%error-verbose"                         { error_verbose = true; }
 | "%expect" INT                            { expected_sr_conflicts = $2; }
 | "%expect-rr" INT                        { expected_rr_conflicts = $2; }
@@ -261,6 +267,12 @@ declaration:
 | "%pure-parser"                           { pure_parser = true; }
 | "%require" string_content                { version_check (&@2, $2); }
 | "%skeleton" string_content               { skeleton = $2; }
+| "%start-header" "{...}"
+    {
+      /* Remove the '{', and replace the '}' with '\n'.  */
+      $2[strlen ($2) - 1] = '\n';
+      muscle_code_grow ("start_header", translate_code ($2+1, @2), @2);
+    }
 | "%token-table"                           { token_table_flag = true; }
 | "%verbose"                               { report_flag = report_states; }
 | "%yacc"                                  { yacc_flag = true; }
Index: src/reader.c
===================================================================
RCS file: /sources/bison/bison/src/reader.c,v
retrieving revision 1.256
diff -p -u -r1.256 reader.c
--- src/reader.c        21 Jun 2006 01:37:01 -0000      1.256
+++ src/reader.c        23 Jun 2006 20:04:22 -0000
@@ -44,10 +44,8 @@ static symbol_list *grammar = NULL;
 static bool start_flag = false;
 merger_list *merge_functions;
 
-/* Was %union, %before-definitions, or %after-definitions seen?  */
+/* Was %union seen?  */
 bool typed = false;
-bool before_definitions = false;
-bool after_definitions = false;
 
 /* Should rules have a default precedence?  */
 bool default_prec = true;
@@ -70,17 +68,16 @@ grammar_start_symbol_set (symbol *sym, l
 }
 
 
-/*---------------------------------------------------------------------------.
-| There are two prologues: one before the first %union, %before-definitions, |
-| or %after-definitions; and one after.  Augment the current one.            |
-`---------------------------------------------------------------------------*/
+/*---------------------------------------------------------------------.
+| There are two prologues: one before the first %union and one after.  |
+|  Augment the one specified by POST.                                  |
+`---------------------------------------------------------------------*/
 
 void
-prologue_augment (const char *prologue, location loc)
+prologue_augment (const char *prologue, location loc, bool post)
 {
   struct obstack *oout =
-    !(typed || before_definitions || after_definitions)
-    ? &pre_prologue_obstack : &post_prologue_obstack;
+    !post ? &pre_prologue_obstack : &post_prologue_obstack;
 
   obstack_fgrow1 (oout, "]b4_syncline(%d, [[", loc.start.line);
   /* FIXME: Protection of M4 characters missing here.  See
Index: src/reader.h
===================================================================
RCS file: /sources/bison/bison/src/reader.h,v
retrieving revision 1.51
diff -p -u -r1.51 reader.h
--- src/reader.h        21 Jun 2006 01:37:01 -0000      1.51
+++ src/reader.h        23 Jun 2006 20:04:22 -0000
@@ -43,7 +43,7 @@ char const *token_name (int type);
 
 /* From reader.c. */
 void grammar_start_symbol_set (symbol *sym, location loc);
-void prologue_augment (const char *prologue, location loc);
+void prologue_augment (const char *prologue, location loc, bool post);
 void grammar_current_rule_begin (symbol *lhs, location loc);
 void grammar_current_rule_end (location loc);
 void grammar_midrule_action (void);
@@ -58,10 +58,8 @@ void free_merger_functions (void);
 
 extern merger_list *merge_functions;
 
-/* Was %union, %before-definitions, or %after-definitions seen?  */
+/* Was %union seen?  */
 extern bool typed;
-extern bool before_definitions;
-extern bool after_definitions;
 
 /* Should rules have a default precedence?  */
 extern bool default_prec;
Index: src/scan-gram.l
===================================================================
RCS file: /sources/bison/bison/src/scan-gram.l,v
retrieving revision 1.93
diff -p -u -r1.93 scan-gram.l
--- src/scan-gram.l     21 Jun 2006 01:37:01 -0000      1.93
+++ src/scan-gram.l     23 Jun 2006 20:04:22 -0000
@@ -164,8 +164,8 @@ splice       (\\[ \f\t\v]*\n)*
   `----------------------------*/
 <INITIAL>
 {
-  "%after-definitions"              return PERCENT_AFTER_DEFINITIONS;
-  "%before-definitions"             return PERCENT_BEFORE_DEFINITIONS;
+  "%after-header"                   return PERCENT_AFTER_HEADER;
+  "%before-header"                  return PERCENT_BEFORE_HEADER;
   "%binary"                        return PERCENT_NONASSOC;
   "%debug"                         return PERCENT_DEBUG;
   "%default"[-_]"prec"             return PERCENT_DEFAULT_PREC;
@@ -173,6 +173,7 @@ splice       (\\[ \f\t\v]*\n)*
   "%defines"                       return PERCENT_DEFINES;
   "%destructor"                            return PERCENT_DESTRUCTOR;
   "%dprec"                         return PERCENT_DPREC;
+  "%end-header"                     return PERCENT_END_HEADER;
   "%error"[-_]"verbose"            return PERCENT_ERROR_VERBOSE;
   "%expect"                        return PERCENT_EXPECT;
   "%expect"[-_]"rr"                return PERCENT_EXPECT_RR;
@@ -199,6 +200,7 @@ splice       (\\[ \f\t\v]*\n)*
   "%right"                         return PERCENT_RIGHT;
   "%skeleton"                      return PERCENT_SKELETON;
   "%start"                         return PERCENT_START;
+  "%start-header"                   return PERCENT_START_HEADER;
   "%term"                          return PERCENT_TOKEN;
   "%token"                         return PERCENT_TOKEN;
   "%token"[-_]"table"              return PERCENT_TOKEN_TABLE;
Index: tests/actions.at
===================================================================
RCS file: /sources/bison/bison/tests/actions.at,v
retrieving revision 1.59
diff -p -u -r1.59 actions.at
--- tests/actions.at    21 Jun 2006 01:37:01 -0000      1.59
+++ tests/actions.at    23 Jun 2006 20:04:23 -0000
@@ -172,7 +172,7 @@ m4_if([$1$2$3], $[1]$[2]$[3], [],
 # helping macros.  So don't put any directly in the Bison file.
 AT_BISON_OPTION_PUSHDEFS([$5])
 AT_DATA_GRAMMAR([[input.y]],
-[[%before-definitions {
+[[%start-header {
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
@@ -190,7 +190,7 @@ m4_ifval([$6], [%union
   int ival;
 }])
 AT_LALR1_CC_IF([%define "global_tokens_and_yystype"])
-m4_ifval([$6], [[%after-definitions {]], [[%before-definitions {]])
+m4_ifval([$6], [[%end-header {]], [[%start-header {]])
 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;
                 m4_ifval([$6], , [#define YYSTYPE int])])
 [static int yylex (]AT_LEX_FORMALS[);




reply via email to

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