bison-patches
[Top][All Lists]
Advanced

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

Re: [PATCH] more on prologue alternatives


From: Paolo Bonzini
Subject: Re: [PATCH] more on prologue alternatives
Date: Wed, 04 Oct 2006 15:14:07 +0200
User-agent: Thunderbird 1.5.0.7 (Macintosh/20060909)


1. %code{...} is the most common place for parser implementation code.  In
C/C++, it goes in the code file.  In Java, it goes in the parser class.

2. %requires{...} is a place to put dependency code for externally exposed
definitions required by Bison.  In C/C++, it's for dependencies of YYSTYPE
and YYLTYPE.  In Java, it's for import directives.

3. %provides{...} is a place for additional externally exposed definitions.
In C/C++, it goes at the bottom of the header file so that it can depend on
YYSTYPE, YYLTYPE, and yytokentype.  In Java, I suppose it could be used to
define other package-visible classes in the same java file.
This would be completely ok.
Great.  Paul?  Akim?
As a small prod, here's the patch to implement the renaming.

Paolo
2006-10-04  Paolo Bonzini  <address@hidden>

        * data/c.m4 (b4_user_requires, b4_user_provides): New.
        (b4_user_start_header): Remove.
        * data/glr.c: Use new macros instead of b4_*start_header
        and b4_*end_header.
        * data/glr.cc: Likewise.
        * data/lalr1.cc: Likewise.
        * data/push.c: Likewise.
        * data/yacc.c: Likewise.

        * doc/bison.texinfo: Remove %before-header, rename
        %{start,end,after}-header to %requires, %provides, %code.

        * src/parse-gram.y: Likewise (also rename token names accordingly).
        * src/scan-gram.l: Likewise.
        * tests/actions.at: Likewise.

Index: data/c.m4
===================================================================
RCS file: /sources/bison/bison/data/c.m4,v
retrieving revision 1.66
diff -u -p -r1.66 c.m4
--- data/c.m4   15 Sep 2006 15:56:26 -0000      1.66
+++ data/c.m4   4 Oct 2006 09:00:30 -0000
@@ -464,14 +464,16 @@ m4_define([b4_define_user_code],
 # b4_user_actions
 # b4_user_initial_action
 # b4_user_post_prologue
-# b4_user_start_header
+# b4_user_provides
+# b4_user_requires
 # b4_user_stype
 # ----------------------
 # Macros that issue user code, ending with synclines.
 b4_define_user_code([actions])
 b4_define_user_code([initial_action])
 b4_define_user_code([post_prologue])
-b4_define_user_code([start_header])
+b4_define_user_code([provides])
+b4_define_user_code([requires])
 b4_define_user_code([stype])
 
 
Index: data/glr.c
===================================================================
RCS file: /sources/bison/bison/data/glr.c,v
retrieving revision 1.188
diff -u -p -r1.188 glr.c
--- data/glr.c  11 Aug 2006 19:50:14 -0000      1.188
+++ data/glr.c  4 Oct 2006 09:00:31 -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_start_header],
-[[/* Copy the %start-header blocks.  */
-]b4_start_header])[]dnl
+[m4_ifdef([b4_requires],
+[[/* Copy the %requires blocks.  */
+]b4_user_requires])[]dnl
 
 b4_token_enums(b4_tokens)
 
@@ -202,9 +202,9 @@ typedef struct YYLTYPE
 # define YYLTYPE_IS_TRIVIAL 1
 #endif
 
-]m4_ifdef([b4_end_header],
-[[/* Copy the %end-header blocks.  */
-]b4_end_header])[]dnl
+]m4_ifdef([b4_provides],
+[[/* Copy the %provides blocks.  */
+]b4_user_provides])[]dnl
 ])
 
 b4_defines_if([#include @address@hidden,
Index: data/glr.cc
===================================================================
RCS file: /sources/bison/bison/data/glr.cc,v
retrieving revision 1.27
diff -u -p -r1.27 glr.cc
--- data/glr.cc 13 Sep 2006 17:41:19 -0000      1.27
+++ data/glr.cc 4 Oct 2006 09:00:31 -0000
@@ -225,9 +225,9 @@ b4_copyright([Skeleton interface for Bis
 #ifndef PARSER_HEADER_H
 # define PARSER_HEADER_H
 
-]m4_ifdef([b4_start_header],
-[[/* Copy the %start-header blocks.  */
-]b4_user_start_header])[
+]m4_ifdef([b4_requires],
+[[/* Copy the %requires blocks.  */
+]b4_user_requires])[
 
 #include <string>
 #include <iostream>
@@ -373,8 +373,8 @@ m4_ifset([b4_global_tokens_and_yystype],
 
 }
 
-]m4_ifdef([b4_end_header],
-[[/* Copy the %end-header blocks.  */
-]b4_end_header])[]dnl
+]m4_ifdef([b4_provides],
+[[/* Copy the %provides blocks.  */
+]b4_user_provides])[]dnl
 
 [#endif /* ! defined PARSER_HEADER_H */]
Index: data/lalr1.cc
===================================================================
RCS file: /sources/bison/bison/data/lalr1.cc,v
retrieving revision 1.144
diff -u -p -r1.144 lalr1.cc
--- data/lalr1.cc       13 Sep 2006 18:37:24 -0000      1.144
+++ data/lalr1.cc       4 Oct 2006 09:00:31 -0000
@@ -43,9 +43,9 @@ dnl FIXME: This is wrong, we want comput
 #ifndef PARSER_HEADER_H
 # define PARSER_HEADER_H
 
-]m4_ifdef([b4_start_header],
-[[/* Copy the %start-header blocks.  */
-]b4_user_start_header])[
+]m4_ifdef([b4_requires],
+[[/* Copy the %requires blocks.  */
+]b4_user_requires])[
 
 #include <string>
 #include <iostream>
@@ -295,9 +295,9 @@ b4_error_verbose_if([, int tok])[);
 # define YYSTYPE b4_namespace::b4_parser_class_name::semantic_type
 #endif
 ])
-m4_ifdef([b4_end_header],
-[[/* Copy the %end-header blocks.  */
-]b4_end_header])[]dnl
+m4_ifdef([b4_provides],
+[[/* Copy the %provides blocks.  */
+]b4_user_provides])[]dnl
 
 [#endif /* ! defined PARSER_HEADER_H */]
 ])dnl
Index: data/push.c
===================================================================
RCS file: /sources/bison/bison/data/push.c,v
retrieving revision 1.3
diff -u -p -r1.3 push.c
--- data/push.c 21 Sep 2006 17:45:21 -0000      1.3
+++ data/push.c 4 Oct 2006 09:00:31 -0000
@@ -188,9 +188,9 @@ b4_push_if([
 # define YYTOKEN_TABLE ]b4_token_table[
 #endif
 
-]m4_ifdef([b4_start_header],
-[[/* Copy the %start-header blocks.  */
-]b4_start_header])[]dnl
+]m4_ifdef([b4_requires],
+[[/* Copy the %requires blocks.  */
+]b4_user_requires])[]dnl
 
 b4_token_enums_defines(b4_tokens)[
 
@@ -220,9 +220,9 @@ typedef struct YYLTYPE
 # define YYLTYPE_IS_TRIVIAL 1
 #endif])
 
-m4_ifdef([b4_end_header],
-[[/* Copy the %end-header blocks.  */
-]b4_end_header])[]dnl
+m4_ifdef([b4_provides],
+[[/* Copy the %provides blocks.  */
+]b4_user_provides])[]dnl
 
 [/* Copy the second part of user declarations.  */
 ]b4_user_post_prologue[
@@ -1741,9 +1741,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_start_header],
-[[/* Copy the %start-header blocks.  */
-]b4_start_header])[]dnl
+m4_ifdef([b4_requires],
+[[/* Copy the %requires blocks.  */
+]b4_user_requires])[]dnl
 
 b4_token_enums_defines(b4_tokens)
 
@@ -1783,7 +1783,7 @@ enum { YYPUSH_MORE = 4 };])[
           [extern YYLTYPE b4_prefix[]lloc;])
 )dnl b4_locations_if
 
-m4_ifdef([b4_end_header],
-[[/* Copy the %end-header blocks.  */
-]b4_end_header])[]dnl
+m4_ifdef([b4_provides],
+[[/* Copy the %provides blocks.  */
+]b4_user_provides])[]dnl
 ])dnl b4_defines_if
Index: data/yacc.c
===================================================================
RCS file: /sources/bison/bison/data/yacc.c,v
retrieving revision 1.153
diff -u -p -r1.153 yacc.c
--- data/yacc.c 11 Aug 2006 19:50:14 -0000      1.153
+++ data/yacc.c 4 Oct 2006 09:00:31 -0000
@@ -181,9 +181,9 @@ b4_locations_if([#define yylloc b4_prefi
 # define YYTOKEN_TABLE ]b4_token_table[
 #endif
 
-]m4_ifdef([b4_start_header],
-[[/* Copy the %start-header blocks.  */
-]b4_start_header])[]dnl
+]m4_ifdef([b4_requires],
+[[/* Copy the %requires blocks.  */
+]b4_user_requires])[]dnl
 
 b4_token_enums_defines(b4_tokens)[
 
@@ -213,9 +213,9 @@ typedef struct YYLTYPE
 # define YYLTYPE_IS_TRIVIAL 1
 #endif])
 
-m4_ifdef([b4_end_header],
-[[/* Copy the %end-header blocks.  */
-]b4_end_header])[]dnl
+m4_ifdef([b4_provides],
+[[/* Copy the %provides blocks.  */
+]b4_user_provides])[]dnl
 
 [/* Copy the second part of user declarations.  */
 ]b4_user_post_prologue[
@@ -1489,9 +1489,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_start_header],
-[[/* Copy the %start-header blocks.  */
-]b4_start_header])[]dnl
+m4_ifdef([b4_requires],
+[[/* Copy the %requires blocks.  */
+]b4_user_requires])[]dnl
 
 b4_token_enums_defines(b4_tokens)
 
@@ -1529,7 +1529,7 @@ typedef struct YYLTYPE
           [extern YYLTYPE b4_prefix[]lloc;])
 )dnl b4_locations_if
 
-m4_ifdef([b4_end_header],
-[[/* Copy the %end-header blocks.  */
-]b4_end_header])[]dnl
+m4_ifdef([b4_provides],
+[[/* Copy the %provides blocks.  */
+]b4_user_provides])[]dnl
 ])dnl b4_defines_if
Index: doc/bison.texinfo
===================================================================
RCS file: /sources/bison/bison/doc/bison.texinfo,v
retrieving revision 1.205
diff -u -p -r1.205 bison.texinfo
--- doc/bison.texinfo   11 Sep 2006 18:56:58 -0000      1.205
+++ doc/bison.texinfo   4 Oct 2006 09:00:32 -0000
@@ -2674,22 +2674,17 @@ of feature test macros like @code{_GNU_S
 feature test macros can affect the behavior of Bison-generated
 @code{#include} directives.
 
address@hidden %before-header
address@hidden %start-header
address@hidden %after-header
address@hidden %requires
address@hidden %code
 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-header}, @code{%start-header}, and
address@hidden instead of @var{Prologue} sections
-(@pxref{Table of Symbols, ,%start-header}):
+In that case, use @code{%requires}, @code{%provides}, and
address@hidden instead of @var{Prologue} sections
+(@pxref{Table of Symbols, ,%requires}):
 
 @smallexample
-%before-header @{
-  #include <stdio.h>
address@hidden
-
-%start-header @{
+%requires @{
   #include "ptypes.h"
 @}
 %union @{
@@ -2697,7 +2692,9 @@ In that case, use @code{%before-header},
   tree t;  /* @address@hidden is defined in @file{ptypes.h}.} */
 @}
 
-%after-header @{
+%code @{
+  #include <stdio.h>
+
   static void print_token_value (FILE *, int, YYSTYPE);
   #define YYPRINT(F, N, L) print_token_value (F, N, L)
 @}
@@ -4327,11 +4324,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 %start-header
address@hidden %end-header
-If you have declared @code{%start-header} or @code{%end-header}, the output
address@hidden %requires
address@hidden %provides
+If you have declared @code{%requires} or @code{%provides}, the output
 header also contains their code.
address@hidden of Symbols, ,%start-header}.
address@hidden of Symbols, ,%requires}.
 @end deffn
 
 @deffn {Directive} %destructor
@@ -7560,18 +7557,18 @@ the grammar for.
 @end example
 
 @noindent
address@hidden %start-header
address@hidden %requires
 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, ,%start-header}.
address@hidden of Symbols, ,%requires}.
 
 @comment file: calc++-parser.yy
 @example
-%start-header @{
+%requires @{
 # include <string>
 class calcxx_driver;
 @}
@@ -7630,13 +7627,13 @@ them.
 @end example
 
 @noindent
address@hidden %after-header
-The code between @samp{%after-header @{} and @address@hidden is output in the
address@hidden %code
+The code between @samp{%code @{} and @address@hidden is output in the
 @file{*.cc} file; it needs detailed knowledge about the driver.
 
 @comment file: calc++-parser.yy
 @example
-%after-header @{
+%code @{
 # include "calc++-driver.hh"
 @}
 @end example
@@ -8308,49 +8305,35 @@ $end}, where @var{start} is the start sy
 Start-Symbol}.  It cannot be used in the grammar.
 @end deffn
 
address@hidden {Directive} %after-header @address@hidden@}
address@hidden {Directive} %code @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 of Symbols, ,%requires}.
 @end 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@}
address@hidden {Directive} %provides @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.
address@hidden of Symbols, ,%start-header}.
address@hidden of Symbols, ,%requires}.
 @end deffn
 
address@hidden {Directive} %start-header @address@hidden@}
address@hidden {Directive} %requires @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 %before-header
 @findex %union
address@hidden %end-header
address@hidden %after-header
address@hidden %provides
address@hidden %code
 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
-%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 @{
+%requires @{
   /* 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
@@ -8359,17 +8342,17 @@ convenient for you:
 @}
 %union @{
   /* 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.  */
+   * for %requires, %provides or %code blocks is not affected by their
+   * declaration position relative to any %union in the grammar file.  */
 @}
-%end-header @{
+%provides @{
   /* 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.  */
 @}
-%after-header @{
+%code @{
   /* 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
Index: src/parse-gram.y
===================================================================
RCS file: /sources/bison/bison/src/parse-gram.y,v
retrieving revision 1.92
diff -u -p -r1.92 parse-gram.y
--- src/parse-gram.y    15 Sep 2006 16:34:48 -0000      1.92
+++ src/parse-gram.y    4 Oct 2006 09:00:34 -0000
@@ -133,13 +133,11 @@ static int current_prec = 0;
 `----------------------*/
 
 %token
-  PERCENT_AFTER_HEADER    "%after-header"
-  PERCENT_BEFORE_HEADER   "%before-header"
+  PERCENT_CODE            "%code"
   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"
@@ -155,12 +153,13 @@ static int current_prec = 0;
                          "%nondeterministic-parser"
   PERCENT_OUTPUT          "%output"
   PERCENT_PARSE_PARAM     "%parse-param"
+  PERCENT_PROVIDES        "%provides"
   PERCENT_PURE_PARSER     "%pure-parser"
   PERCENT_PUSH_PARSER     "%push-parser"
   PERCENT_REQUIRE        "%require"
+  PERCENT_REQUIRES        "%requires"
   PERCENT_SKELETON        "%skeleton"
   PERCENT_START           "%start"
-  PERCENT_START_HEADER    "%start-header"
   PERCENT_TOKEN_TABLE     "%token-table"
   PERCENT_VERBOSE         "%verbose"
   PERCENT_YACC            "%yacc"
@@ -222,12 +221,11 @@ prologue_declarations:
 prologue_declaration:
   grammar_declaration
 | "%{...%}"     { prologue_augment (translate_code ($1, @1), @1, union_seen); }
-| "%after-header" braceless        { prologue_augment ($2, @2, true); }
-| "%before-header" braceless       { prologue_augment ($2, @2, false); }
+| "%code" braceless                { prologue_augment ($2, @2, true); }
 | "%debug"                         { debug_flag = true; }
 | "%define" STRING content.opt     { muscle_insert ($2, $3); }
 | "%defines"                       { defines_flag = true; }
-| "%end-header" braceless          { muscle_code_grow ("end_header", $2, @2); }
+| "%provides" braceless            { muscle_code_grow ("provides", $2, @2); }
 | "%error-verbose"                 { error_verbose = true; }
 | "%expect" INT                    { expected_sr_conflicts = $2; }
 | "%expect-rr" INT                { expected_rr_conflicts = $2; }
@@ -252,7 +250,7 @@ prologue_declaration:
 | "%push-parser"                { push_parser = true; }
 | "%require" STRING             { version_check (&@2, $2); }
 | "%skeleton" STRING            { skeleton = $2; }
-| "%start-header" braceless     { muscle_code_grow ("start_header", $2, @2); }
+| "%requires" braceless         { muscle_code_grow ("requires", $2, @2); }
 | "%token-table"                { token_table_flag = true; }
 | "%verbose"                    { report_flag = report_states; }
 | "%yacc"                       { yacc_flag = true; }
Index: src/scan-gram.l
===================================================================
RCS file: /sources/bison/bison/src/scan-gram.l,v
retrieving revision 1.103
diff -u -p -r1.103 scan-gram.l
--- src/scan-gram.l     15 Sep 2006 15:56:26 -0000      1.103
+++ src/scan-gram.l     4 Oct 2006 09:00:34 -0000
@@ -156,16 +156,14 @@ splice     (\\[ \f\t\v]*\n)*
   `----------------------------*/
 <INITIAL>
 {
-  "%after-header"                   return PERCENT_AFTER_HEADER;
-  "%before-header"                  return PERCENT_BEFORE_HEADER;
   "%binary"                        return PERCENT_NONASSOC;
+  "%code"                           return PERCENT_CODE;
   "%debug"                         return PERCENT_DEBUG;
   "%default"[-_]"prec"             return PERCENT_DEFAULT_PREC;
   "%define"                        return PERCENT_DEFINE;
   "%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;
@@ -187,13 +185,14 @@ splice     (\\[ \f\t\v]*\n)*
   "%parse-param"                   return PERCENT_PARSE_PARAM;
   "%prec"                          return PERCENT_PREC;
   "%printer"                       return PERCENT_PRINTER;
+  "%provides"                       return PERCENT_PROVIDES;
   "%pure"[-_]"parser"              return PERCENT_PURE_PARSER;
   "%push"[-_]"parser"              return PERCENT_PUSH_PARSER;
   "%require"                       return PERCENT_REQUIRE;
+  "%requires"                       return PERCENT_REQUIRES;
   "%right"                         return PERCENT_RIGHT;
   "%skeleton"                      return PERCENT_SKELETON;
   "%start"                         return PERCENT_START;
-  "%start-header"                   return PERCENT_START_HEADER;
   "%symbol-default"                 return PERCENT_SYMBOL_DEFAULT;
   "%term"                          return PERCENT_TOKEN;
   "%token"                         return PERCENT_TOKEN;
Index: tests/actions.at
===================================================================
RCS file: /sources/bison/bison/tests/actions.at,v
retrieving revision 1.69
diff -u -p -r1.69 actions.at
--- tests/actions.at    13 Sep 2006 19:39:14 -0000      1.69
+++ tests/actions.at    4 Oct 2006 09:00:34 -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]],
-[[%start-header {
+[[%requires {
 #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], [[%end-header {]], [[%after-header {]])
+m4_ifval([$6], [[%provides {]], [[%code {]])
 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
 [static int yylex (]AT_LEX_FORMALS[);
 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])

reply via email to

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