bison-patches
[Top][All Lists]
Advanced

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

Re: The Location handling


From: Akim Demaille
Subject: Re: The Location handling
Date: Mon, 25 Aug 2003 17:16:46 +0200
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.3 (gnu/linux)

 >> another means to fix bison would be to introduce something like
 >> 
 >> %initial-action {
 >> @$ = WHAT-EVER;
 >> lfprintf (stderr, @$, "Hello world\n");
 >> @$.last_line = -42;
 >> }
 >> 
 >> and it would be cumulative, i.e., except for the empty line, the
 >> paragraph above would be the same as
 >> 
 >> %initial-action { @$ = WHAT-EVER; }
 >> %initial-action { lfprintf (stderr, @$, "Hello world\n"); }
 >> %initial-action { @$.last_line = -42; }
 >> 
 >> I'm OK with experiencing this scheme, but, as with %initial-location,
 >> I vote for a solution used by Bison for Bison, and not published in
 >> any way (err, except the code :).

 > I like this idea better, for the reasons mentioned above.  And once we
 > have %initial-action, we don't need %initial-location.

This is what I'm installing to address this issue.  Please note that


                  *CVS Bison requires CVS Bison*

Make sure to compile using the parser as provided under CVS.  In case
of problem, to ease the bootstrapping, fetch a recent copy from

               http://www.lrde.epita.fr/~akim/downdload/

Things that remain to be done:

1. document
2. apply to GLR
3. apply to C++

Index: ChangeLog
from  Akim Demaille  <address@hidden>

        When reducing initial empty rules, Bison parser read an initial
        location that is not defined.  This results in garbage, and that
        affects Bison's own parser.  Therefore we need (i) to extend Bison
        to support a means to initialize this location, and (ii) to use
        this CVS Bison to fix CVS Bison's parser.

        * src/reader.h, reader.c (epilogue_augment): Remove, replace
        with...
        * src/muscle_tab.h, src/muscle_tab.c (muscle_code_grow): this.
        * src/parse-gram.y: Adjust.
        (%initial-action): New.
        (%error-verbose): Since we require CVS Bison, there is no reason
        not to use it.
        * src/scan-gram.l: Adjust.
        * src/Makefile.am (YACC): New, to make sure we use our own parser.
        * data/yacc.c (yyparse): Use b4_initial_action.

 2003-08-25  Akim Demaille  <address@hidden>
Index: README-cvs
===================================================================
RCS file: /cvsroot/bison/bison/README-cvs,v
retrieving revision 1.9
diff -u -u -r1.9 README-cvs
--- README-cvs 27 Sep 2002 13:24:19 -0000 1.9
+++ README-cvs 25 Aug 2003 15:07:28 -0000
@@ -11,9 +11,11 @@

 - Automake 1.7

-- Autoconf 2.54
+- Autoconf 2.57

-- Gettext 0.11.5
+- Gettext 0.12.1
+
+- CVS Bison itself

 Only building the initial full source tree will be a bit painful,
 later, a plain `cvs update -P & make' should be sufficient.
@@ -65,7 +67,7 @@

 -----

-Copyright (C) 2002 Free Software Foundation, Inc.
+Copyright (C) 2002, 2003 Free Software Foundation, Inc.

 This file is part of GNU Bison.

Index: data/yacc.c
===================================================================
RCS file: /cvsroot/bison/bison/data/yacc.c,v
retrieving revision 1.61
diff -u -u -r1.61 yacc.c
--- data/yacc.c 25 Aug 2003 08:29:27 -0000 1.61
+++ data/yacc.c 25 Aug 2003 15:07:28 -0000
@@ -807,7 +807,17 @@
   yyssp = yyss;
   yyvsp = yyvs;
 ]b4_location_if([  yylsp = yyls;])[
-  goto yysetstate;
+]m4_ifdef([b4_initial_action], [
+m4_pushdef([b4_at_dollar],     [(*yylsp)])dnl
+m4_pushdef([b4_dollar_dollar], [(*yyvsp)])dnl
+  /* User initialization code. */
+  b4_initial_action
+m4_popdef([b4_dollar_dollar])dnl
+m4_popdef([b4_at_dollar])dnl
+/* Line __line__ of yacc.c.  */
+b4_syncline(address@hidden@], address@hidden@])])dnl
+
+[  goto yysetstate;

 /*------------------------------------------------------------.
 | yynewstate -- Push a new state, which is found in yystate.  |
Index: src/Makefile.am
===================================================================
RCS file: /cvsroot/bison/bison/src/Makefile.am,v
retrieving revision 1.61
diff -u -u -r1.61 Makefile.am
--- src/Makefile.am 9 Jan 2003 12:45:08 -0000 1.61
+++ src/Makefile.am 25 Aug 2003 15:07:29 -0000
@@ -28,6 +28,10 @@

 LDADD = ../lib/libbison.a $(LIBINTL)

+# Use our own Bison to build the parser.  Of course, you ought to
+# keep a sane version of Bison nearby...
+YACC = ../tests/bison -y
+
 bin_PROGRAMS = bison
 bin_SCRIPTS = $(YACC_SCRIPT)
 EXTRA_SCRIPTS = yacc
Index: src/muscle_tab.c
===================================================================
RCS file: /cvsroot/bison/bison/src/muscle_tab.c,v
retrieving revision 1.30
diff -u -u -r1.30 muscle_tab.c
--- src/muscle_tab.c 13 Dec 2002 08:29:05 -0000 1.30
+++ src/muscle_tab.c 25 Aug 2003 15:07:29 -0000
@@ -154,6 +154,26 @@
 }


+/*------------------------------------------------------------------.
+| Append VALUE to the current value of KEY, using muscle_grow.  But |
+| in addition, issue a synchronization line for the location LOC.   |
+`------------------------------------------------------------------*/
+
+void
+muscle_code_grow (const char *key, const char *val, location loc)
+{
+  char *extension = NULL;
+  obstack_fgrow1 (&muscle_obstack, "]b4_syncline([[%d]], [[", loc.start.line);
+  MUSCLE_OBSTACK_SGROW (&muscle_obstack,
+                       quotearg_style (c_quoting_style, loc.start.file));
+  obstack_sgrow (&muscle_obstack, "]])[\n");
+  obstack_sgrow (&muscle_obstack, val);
+  obstack_1grow (&muscle_obstack, 0);
+  extension = obstack_finish (&muscle_obstack);
+  muscle_grow (key, extension, "");
+}
+
+
 /*-------------------------------------------------------------------.
 | MUSCLE is an M4 list of pairs.  Create or extend it with the pair  |
 | (A1, A2).  Note that because the muscle values are output *double* |
Index: src/muscle_tab.h
===================================================================
RCS file: /cvsroot/bison/bison/src/muscle_tab.h,v
retrieving revision 1.12
diff -u -u -r1.12 muscle_tab.h
--- src/muscle_tab.h 24 May 2003 19:16:02 -0000 1.12
+++ src/muscle_tab.h 25 Aug 2003 15:07:29 -0000
@@ -21,6 +21,8 @@
 #ifndef MUSCLE_TAB_H_
 # define MUSCLE_TAB_H_

+# include "location.h"
+
 void muscle_init (void);
 void muscle_insert (const char *key, char *value);
 char *muscle_find (const char *key);
@@ -85,6 +87,13 @@
    associated value.  VALUE and SEPARATOR are copied.  */

 void muscle_grow (const char *key, const char *value, const char *separator);
+
+
+/* Append VALUE to the current value of KEY, using muscle_grow.  But
+   in addition, issue a synchronization line for the location LOC.  */
+
+void muscle_code_grow (const char *key, const char *value, location loc);
+

 /* MUSCLE is an M4 list of pairs.  Create or extend it with the pair
    (A1, A2).  Note that because the muscle values are output *double*
Index: src/parse-gram.y
===================================================================
RCS file: /cvsroot/bison/bison/src/parse-gram.y,v
retrieving revision 1.42
diff -u -u -r1.42 parse-gram.y
--- src/parse-gram.y 24 May 2003 19:16:02 -0000 1.42
+++ src/parse-gram.y 25 Aug 2003 15:07:29 -0000
@@ -25,7 +25,7 @@
 %defines
 %locations
 %pure-parser
-// %error-verbose
+%error-verbose
 %defines
 %name-prefix="gram_"

@@ -42,9 +42,6 @@
 #include "reader.h"
 #include "symlist.h"

-/* Produce verbose syntax errors.  */
-#define YYERROR_VERBOSE 1
-
 #define YYLLOC_DEFAULT(Current, Rhs, N)  (Current) = lloc_default (Rhs, N)
 static YYLTYPE lloc_default (YYLTYPE const *, int);

@@ -69,6 +66,14 @@
 int current_prec = 0;
 %}

+%initial-action
+{
+  /* Bison's grammar can initial empty locations, hence a default
+     location is needed. */
+  @$.start.file   = @$.end.file   = current_file;
+  @$.start.line   = @$.end.line   = 1;
+  @$.start.column = @$.end.column = 0;
+}

 /* Only NUMBERS have a value.  */
 %union
@@ -109,26 +114,28 @@
 `----------------------*/

 %token
-  PERCENT_DEBUG         "%debug"
-  PERCENT_DEFINE        "%define"
-  PERCENT_DEFINES       "%defines"
-  PERCENT_ERROR_VERBOSE "%error-verbose"
-  PERCENT_EXPECT        "%expect"
-  PERCENT_FILE_PREFIX   "%file-prefix"
-  PERCENT_GLR_PARSER    "%glr-parser"
-  PERCENT_LEX_PARAM     "%lex-param {...}"
-  PERCENT_LOCATIONS     "%locations"
-  PERCENT_NAME_PREFIX   "%name-prefix"
-  PERCENT_NO_LINES      "%no-lines"
-  PERCENT_NONDETERMINISTIC_PARSER    "%nondeterministic-parser"
-  PERCENT_OUTPUT        "%output"
-  PERCENT_PARSE_PARAM   "%parse-param {...}"
-  PERCENT_PURE_PARSER   "%pure-parser"
-  PERCENT_SKELETON      "%skeleton"
-  PERCENT_START         "%start"
-  PERCENT_TOKEN_TABLE   "%token-table"
-  PERCENT_VERBOSE       "%verbose"
-  PERCENT_YACC          "%yacc"
+  PERCENT_DEBUG           "%debug"
+  PERCENT_DEFINE          "%define"
+  PERCENT_DEFINES         "%defines"
+  PERCENT_ERROR_VERBOSE   "%error-verbose"
+  PERCENT_EXPECT          "%expect"
+  PERCENT_FILE_PREFIX     "%file-prefix"
+  PERCENT_GLR_PARSER      "%glr-parser"
+  PERCENT_INITIAL_ACTION  "%initial-action {...}"
+  PERCENT_LEX_PARAM       "%lex-param {...}"
+  PERCENT_LOCATIONS       "%locations"
+  PERCENT_NAME_PREFIX     "%name-prefix"
+  PERCENT_NO_LINES        "%no-lines"
+  PERCENT_NONDETERMINISTIC_PARSER
+                          "%nondeterministic-parser"
+  PERCENT_OUTPUT          "%output"
+  PERCENT_PARSE_PARAM     "%parse-param {...}"
+  PERCENT_PURE_PARSER     "%pure-parser"
+  PERCENT_SKELETON        "%skeleton"
+  PERCENT_START           "%start"
+  PERCENT_TOKEN_TABLE     "%token-table"
+  PERCENT_VERBOSE         "%verbose"
+  PERCENT_YACC            "%yacc"
 ;

 %token TYPE            "type"
@@ -145,6 +152,7 @@

 %type <chars> STRING string_content
              "%destructor {...}"
+             "%initial-action {...}"
              "%lex-param {...}"
              "%parse-param {...}"
              "%printer {...}"
@@ -181,21 +189,28 @@
 | "%error-verbose"                         { error_verbose = true; }
 | "%expect" INT                            { expected_conflicts = $2; }
 | "%file-prefix" "=" string_content        { spec_file_prefix = $3; }
-| "%glr-parser"                           { nondeterministic_parser = true;
-                                             glr_parser = true; }
+| "%glr-parser"
+  {
+    nondeterministic_parser = true;
+    glr_parser = true;
+  }
+| "%initial-action {...}"
+  {
+    muscle_code_grow ("initial_action", $1, @1);
+  }
 | "%lex-param {...}"                      { add_param ("lex_param", $1, @1); }
 | "%locations"                             { locations_flag = true; }
 | "%name-prefix" "=" string_content        { spec_name_prefix = $3; }
 | "%no-lines"                              { no_lines_flag = true; }
 | "%nondeterministic-parser"              { nondeterministic_parser = true; }
 | "%output" "=" string_content             { spec_outfile = $3; }
-| "%parse-param {...}"                  { add_param ("parse_param", $1, @1); }
+| "%parse-param {...}"                    { add_param ("parse_param", $1, @1); 
}
 | "%pure-parser"                           { pure_parser = true; }
 | "%skeleton" string_content               { skeleton = $2; }
 | "%token-table"                           { token_table_flag = true; }
 | "%verbose"                               { report_flag = report_states; }
 | "%yacc"                                  { yacc_flag = true; }
-| ";"
+| /*FIXME: Err?  What is this horror doing here? */ ";"
 ;

 grammar_declaration:
@@ -400,7 +415,7 @@
   /* Nothing.  */
 | "%%" EPILOGUE
     {
-      epilogue_augment ($2, @2);
+      muscle_code_grow ("epilogue", $2, @2);
       scanner_last_string_free ();
     }
 ;
Index: src/reader.c
===================================================================
RCS file: /cvsroot/bison/bison/src/reader.c,v
retrieving revision 1.232
diff -u -u -r1.232 reader.c
--- src/reader.c 24 May 2003 19:16:02 -0000 1.232
+++ src/reader.c 25 Aug 2003 15:07:29 -0000
@@ -78,29 +78,6 @@
   obstack_sgrow (oout, prologue);
 }

-
-
-
-/*----------------------.
-| Handle the epilogue.  |
-`----------------------*/
-
-void
-epilogue_augment (const char *epilogue, location loc)
-{
-  char *extension = NULL;
-  obstack_fgrow1 (&muscle_obstack, "]b4_syncline([[%d]], [[", loc.start.line);
-  MUSCLE_OBSTACK_SGROW (&muscle_obstack,
-                       quotearg_style (c_quoting_style, loc.start.file));
-  obstack_sgrow (&muscle_obstack, "]])[\n");
-  obstack_sgrow (&muscle_obstack, epilogue);
-  obstack_1grow (&muscle_obstack, 0);
-  extension = obstack_finish (&muscle_obstack);
-  muscle_grow ("epilogue", extension, "");
-  obstack_free (&muscle_obstack, extension);
-}
-
-
 

 /*-------------------------------------------------------------------.
Index: src/reader.h
===================================================================
RCS file: /cvsroot/bison/bison/src/reader.h,v
retrieving revision 1.40
diff -u -u -r1.40 reader.h
--- src/reader.h 10 Jun 2003 02:44:58 -0000 1.40
+++ src/reader.h 25 Aug 2003 15:07:29 -0000
@@ -56,7 +56,6 @@
 /* From reader.c. */
 void grammar_start_symbol_set (symbol *sym, location loc);
 void prologue_augment (const char *prologue, location loc);
-void epilogue_augment (const char *epilogue, location loc);
 void grammar_symbol_append (symbol *sym, location loc);
 void grammar_rule_begin (symbol *lhs, location loc);
 void grammar_rule_end (location loc);
Index: src/scan-gram.l
===================================================================
RCS file: /cvsroot/bison/bison/src/scan-gram.l,v
retrieving revision 1.62
diff -u -u -r1.62 scan-gram.l
--- src/scan-gram.l 10 Jun 2003 02:44:58 -0000 1.62
+++ src/scan-gram.l 25 Aug 2003 15:07:34 -0000
@@ -190,6 +190,7 @@
   "%expect"               return PERCENT_EXPECT;
   "%file-prefix"          return PERCENT_FILE_PREFIX;
   "%fixed"[-_]"output"[-_]"files"   return PERCENT_YACC;
+  "%initial-action"       token_type = PERCENT_INITIAL_ACTION; BEGIN 
SC_PRE_CODE;
   "%glr-parser"           return PERCENT_GLR_PARSER;
   "%left"                 return PERCENT_LEFT;
   "%lex-param"           token_type = PERCENT_LEX_PARAM; BEGIN SC_PRE_CODE;
@@ -649,8 +650,8 @@

 %%

-/* Keeps track of the maximum number of semantic values to the left of
-   a handle (those referenced by $0, $-1, etc.) are required by the
+/* Keeps track of the maximum number of semantic values to the left of
+   a handle (those referenced by $0, $-1, etc.) are required by the
    semantic actions of this grammar. */
 int max_left_semantic_context = 0;

@@ -807,10 +808,10 @@
 }


-/*-----------------------------------------------------------------.
-| Dispatch onto handle_action_dollar, or handle_destructor_dollar, |
-| depending upon TOKEN_TYPE.                                       |
-`-----------------------------------------------------------------*/
+/*----------------------------------------------------------------.
+| Map `$?' onto the proper M4 symbol, depending on its TOKEN_TYPE |
+| (are we in an action?).                                         |
+`----------------------------------------------------------------*/

 static void
 handle_dollar (int token_type, char *text, location loc)
@@ -823,6 +824,7 @@
       break;

     case PERCENT_DESTRUCTOR:
+    case PERCENT_INITIAL_ACTION:
     case PERCENT_PRINTER:
       if (text[1] == '$')
        {
@@ -875,10 +877,10 @@
 }


-/*-------------------------------------------------------------------.
-| Dispatch onto handle_action_at, or handle_destructor_at, depending |
-| upon CODE_KIND.                                                    |
-`-------------------------------------------------------------------*/
+/*----------------------------------------------------------------.
+| Map address@hidden' onto the proper M4 symbol, depending on its TOKEN_TYPE |
+| (are we in an action?).                                         |
+`----------------------------------------------------------------*/

 static void
 handle_at (int token_type, char *text, location loc)
@@ -889,6 +891,7 @@
       handle_action_at (text, loc);
       return;

+    case PERCENT_INITIAL_ACTION:
     case PERCENT_DESTRUCTOR:
     case PERCENT_PRINTER:
       if (text[1] == '$')




reply via email to

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