bison-patches
[Top][All Lists]
Advanced

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

Re: belt-and-suspenders for 'location zero' bugs


From: Akim Demaille
Subject: Re: belt-and-suspenders for 'location zero' bugs
Date: Mon, 20 Sep 2004 14:24:34 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

>>> "Paul" == Paul Eggert <address@hidden> writes:

 > I realize that we added %initial-action on August 25 to address the
 > problem of the "location" of the empty token before parsing starts,
 > but in reviewing and responding to another bug report on this subject
 > <http://mail.gnu.org/archive/html/bug-bison/2003-11/msg00018.html> I'm
 > becoming more inclined to also set that location to a known value
 > before the user gets hold of it, so that the errors are less likely to
 > occur.  This is the approach taken by glr.c.  It can't hurt the
 > correctness of existing code, since it merely sets a value that is
 > currently uninitialized.  And it will lower the number of bug reports
 > in the future.  Here's a proposed patch.

 > 2003-12-11  Paul Eggert  <address@hidden>

 >      * data/yacc.c (yyloc_default): New static var.
 >      (yyparse): Use it to provide an initial value for "location zero".

 > --- yacc.c.~1.62.~   Mon Aug 25 08:16:24 2003
 > +++ yacc.c   Thu Dec 11 13:25:29 2003
 > @@ -206,6 +206,10 @@ typedef struct YYLTYPE
 >  # define YYLTYPE_IS_DECLARED 1
 >  # define YYLTYPE_IS_TRIVIAL 1
 >  #endif
 > +
 > +/* Default (constant) value used to initialize the default location
 > +   just before parsing starts.  */
 > +static YYLTYPE yyloc_default;
 >  ])[
 
 >  /* Copy the second part of user declarations.  */
 > @@ -806,7 +810,8 @@ b4_c_function_def([yyparse], [int], b4_p
 
 >    yyssp = yyss;
 >    yyvsp = yyvs;
 > -]b4_location_if([  yylsp = yyls;])[
 > +]b4_location_if([  yylsp = yyls;
 > +  *yylsp = yyloc_default;])[
 >  ]m4_ifdef([b4_initial_action], [
 >  m4_pushdef([b4_at_dollar],     [(*yylsp)])dnl
 >  m4_pushdef([b4_dollar_dollar], [(*yyvsp)])dnl

I am now basically hitting this issue to have yacc.c and lalr1.cc
agree on locations.  But the initial line number should be 1.  Also,
if someone is not using the stock YYLTYPE, then she should ensure the
initialization herself.

So I propose (and installed) the following patch instead.  Now the
location tracking in actions.at agree bw C++ and C.  Of course, the
case of GLR remains to be addressed...

The value of the location is not right in every case: there remains to
make sure that its "width" is null.

Index: ChangeLog
from  Akim Demaille  <address@hidden>

        * data/yacc.c (YY_LOCATION_PRINT): Use YYLTYPE_IS_TRIVIAL as a
        clearer criterion to define it.
        (parse): Initialize the initial location when YYLTYPE_IS_TRIVIAL.
        When reducing on an empty RHS, use the latest stacked location as
        location.
        yylloc is not always available.
        * data/glr.c: Likewise.
        Also, honor initial-actions.

Index: data/glr.c
===================================================================
RCS file: /cvsroot/bison/bison/data/glr.c,v
retrieving revision 1.76
diff -u -u -r1.76 glr.c
--- data/glr.c 20 Sep 2004 09:32:55 -0000 1.76
+++ data/glr.c 20 Sep 2004 12:19:23 -0000
@@ -427,9 +427,9 @@
 
 #ifndef YYLLOC_DEFAULT
 # define YYLLOC_DEFAULT(yyCurrent, yyRhs, YYN)                 \
-   ((yyCurrent).first_line = YYRHSLOC(yyRhs, 1).first_line,    \
+   ((yyCurrent).first_line   = YYRHSLOC(yyRhs, 1).first_line,  \
     (yyCurrent).first_column = YYRHSLOC(yyRhs, 1).first_column,        \
-    (yyCurrent).last_line = YYRHSLOC(yyRhs, YYN).last_line,    \
+    (yyCurrent).last_line    = YYRHSLOC(yyRhs, YYN).last_line, \
     (yyCurrent).last_column  = YYRHSLOC(yyRhs, YYN).last_column)
 
 /* YY_LOCATION_PRINT -- Print the location on the stream.
@@ -720,7 +720,7 @@
   if (yyrhslen == 0)
     {
       *yyvalp = yyval_default;
-      *yylocp = yyloc_default;
+      *yylocp = yyvsp[0].yystate.yyloc;
     }
   else
     {
@@ -1825,7 +1825,24 @@
   if (setjmp (yystack.yyexception_buffer) != 0)
     goto yyDone;
 
-  yyglrShift (&yystack, 0, 0, 0, yyval_default, &yyloc_default]b4_user_args[);
+  yylval = yyval_default;
+]b4_location_if([
+#if YYLTYPE_IS_TRIVIAL
+  yylloc.first_line   = yylloc.last_line   = 1;
+  yylloc.first_column = yylloc.last_column = 0;
+#endif
+])
+m4_ifdef([b4_initial_action], [
+m4_pushdef([b4_at_dollar],     [(yylval)])dnl
+m4_pushdef([b4_dollar_dollar], [(yylloc)])dnl
+  /* User initialization code. */
+  b4_initial_action
+m4_popdef([b4_dollar_dollar])dnl
+m4_popdef([b4_at_dollar])dnl
+/* Line __line__ of glr.c.  */
+b4_syncline(address@hidden@], address@hidden@])])dnl
+[
+  yyglrShift (&yystack, 0, 0, 0, yylval, &yylloc]b4_user_args[);
   yytoken = YYEMPTY;
   yyposn = 0;
 
Index: data/yacc.c
===================================================================
RCS file: /cvsroot/bison/bison/data/yacc.c,v
retrieving revision 1.69
diff -u -u -r1.69 yacc.c
--- data/yacc.c 20 Sep 2004 09:32:55 -0000 1.69
+++ data/yacc.c 20 Sep 2004 12:19:23 -0000
@@ -514,18 +514,21 @@
     (Current).first_column = (Rhs)[1].first_column,    \
     (Current).last_line    = (Rhs)[N].last_line,       \
     (Current).last_column  = (Rhs)[N].last_column)
+#endif
+
 
 /* YY_LOCATION_PRINT -- Print the location on the stream.
    This macro was not mandated originally: define only if we know
    we won't break user code: when these are the locations we know.  */
 
-# define YY_LOCATION_PRINT(File, Loc)                  \
-    fprintf (File, "%d.%d-%d.%d",                      \
-             (Loc).first_line, (Loc).first_column,     \
-             (Loc).last_line,  (Loc).last_column)
-#endif
-
 #ifndef YY_LOCATION_PRINT
+# if YYLTYPE_IS_TRIVIAL
+#  define YY_LOCATION_PRINT(File, Loc)                 \
+     fprintf (File, "%d.%d-%d.%d",                     \
+              (Loc).first_line, (Loc).first_column,    \
+              (Loc).last_line,  (Loc).last_column)
+# endif
+#else
 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
 #endif
 
@@ -815,8 +818,14 @@
 
   yyssp = yyss;
   yyvsp = yyvs;
-]b4_location_if([  yylsp = yyls;])[
-]m4_ifdef([b4_initial_action], [
+]b4_location_if([[  yylsp = yyls;
+#if YYLTYPE_IS_TRIVIAL
+  /* Initialize the default location before parsing starts.  */
+  yyls[0].first_line   = yyls[0].last_line   = 1;
+  yyls[0].first_column = yyls[0].last_column = 0;
+#endif
+]])
+m4_ifdef([b4_initial_action], [
 m4_pushdef([b4_at_dollar],     [(*yylsp)])dnl
 m4_pushdef([b4_dollar_dollar], [(*yyvsp)])dnl
   /* User initialization code. */
@@ -1008,8 +1017,11 @@
   yyval = yyvsp[1-yylen];
 
 ]b4_location_if(
-[  /* Default location. */
-  YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);])[
+[[  /* Default location. */
+  if (yylen)
+    YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);
+  else
+    yyloc = yylsp[0];]])[
   YY_REDUCE_PRINT (yyn);
   switch (yyn)
     ]{






reply via email to

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