bison-patches
[Top][All Lists]
Advanced

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

Re: undefining YYRHSLOC in the GLR parser


From: Paul Eggert
Subject: Re: undefining YYRHSLOC in the GLR parser
Date: Sun, 12 Dec 2004 01:32:57 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Frank Heckenbach <address@hidden> writes:

> The version contains the problem I reported here 2004-11-19 which
> Akim Demaille promised to revert:

Sorry, I hadn't paid attention to that issue.  I thought Akim had
looked into it, since he made further changes to glr.c after that
exchange.

For lack of a better idea I installed this patch.  Can you please try
it?  Thanks.

2004-12-12  Paul Eggert  <address@hidden>

        * data/glr.c (YYRHSLOC): Don't have two definitions, one for
        the normal case and one for the error case.  Just use the
        first one uniformly.  Problem reported by Frank Heckenbach.
        (YYLLOC_DEFAULT): Use the conventions of yacc.c, so we can
        use exactly the same macro in both places.
        (yyerror_range): Now of type yyGLRStackItem, not YYLTYPE,
        so that the normal-case YYRHSLOC works for the error case too.
        All uses changed.
        * data/yacc.c (YYRHSLOC): New macro, taken from glr.c.
        (YYLLOC_DEFAULT): Use the same macro as glr.c.
        * doc/bison.texinfo (Location Default Action): Don't claim that
        we have an array of locations.  Use the same macro for both glr
        and lalr parsers.  Mention YYRHSLOC.  Mention what happens when
        the index is 0.

Index: data/glr.c
===================================================================
RCS file: /cvsroot/bison/bison/data/glr.c,v
retrieving revision 1.83
diff -p -u -r1.83 glr.c
--- data/glr.c  11 Dec 2004 06:07:11 -0000      1.83
+++ data/glr.c  12 Dec 2004 09:18:11 -0000
@@ -424,24 +424,25 @@ static const ]b4_int_type_for([b4_stos])
    the previous symbol: RHS[0] (always defined).  */
 
 ]b4_location_if([[
+#define YYRHSLOC(Rhs, K) ((Rhs)[K].yystate.yyloc)
 #ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(yyCurrent, yyRhs, YYN)                         \
-do {                                                                   \
-  if (YYN)                                                             \
-    {                                                                  \
-      (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_column  = YYRHSLOC(yyRhs, YYN).last_column;     \
-    }                                                                  \
-  else                                                                 \
-    {                                                                  \
-      (yyCurrent).first_line   = YYRHSLOC(yyRhs, 0).last_line;         \
-      (yyCurrent).first_column = YYRHSLOC(yyRhs, 0).last_column;       \
-      (yyCurrent).last_line    = YYRHSLOC(yyRhs, 0).last_line;         \
-      (yyCurrent).last_column  = YYRHSLOC(yyRhs, 0).last_column;       \
-    }                                                                  \
-} while(0)
+# define YYLLOC_DEFAULT(Current, Rhs, N)                               \
+    do                                                                 \
+      if (N)                                                           \
+       {                                                               \
+         (Current).first_line   = YYRHSLOC(Rhs, 1).first_line;         \
+         (Current).first_column = YYRHSLOC(Rhs, 1).first_column;       \
+         (Current).last_line    = YYRHSLOC(Rhs, N).last_line;          \
+         (Current).last_column  = YYRHSLOC(Rhs, N).last_column;        \
+       }                                                               \
+      else                                                             \
+       {                                                               \
+         (Current).first_line   = (Current).last_line   =              \
+           YYRHSLOC(Rhs, 0).last_line;                                 \
+         (Current).first_column = (Current).last_column =              \
+           YYRHSLOC(Rhs, 0).last_column;                               \
+       }                                                               \
+    while (0)
 
 /* YY_LOCATION_PRINT -- Print the location on the stream.
    This macro was not mandated originally: define only if we know
@@ -454,7 +455,7 @@ do {                                                        
                \
 #endif
 ]],[
 #ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(yyCurrent, yyRhs, YYN) ((void) 0)
+# define YYLLOC_DEFAULT(Current, Rhs, N) ((void) 0)
 #endif
 ])[
 
@@ -612,7 +613,7 @@ struct yyGLRStack {
   int yyerrflag;
   int yyerrState;
 ]b4_location_if([[  /* To compute the location of the error token,  */
-  YYLTYPE yyerror_range[2];]])[
+  yyGLRStackItem yyerror_range[2];]])[
 ]b4_pure_if(
 [
   int yyerrcnt;
@@ -736,9 +737,7 @@ yyuserAction (yyRuleNum yyn, int yyrhsle
     *yyvalp = yyval_default;
   else
     *yyvalp = yyvsp[YYFILL (1-yyrhslen)].yystate.yysemantics.yysval;
-# define YYRHSLOC(yyRhs,YYK) ((yyRhs)[YYK].yystate.yyloc)
   YYLLOC_DEFAULT (*yylocp, yyvsp - yyrhslen, yyrhslen);
-# undef YYRHSLOC
 ]
   switch (yyn)
     {
@@ -1723,7 +1722,7 @@ yyrecoverSyntaxError (yyGLRStack* yystac
            while (yystack->yytops.yystates[0] != NULL)
              {
                yyGLRState *yys = yystack->yytops.yystates[0];
-]b4_location_if([[             yystack->yyerror_range[0] = yys->yyloc;]])[
+]b4_location_if([[             yystack->yyerror_range[0].yystate.yyloc = 
yys->yyloc;]])[
                yydestruct ("Error: popping",
                             yystos[yys->yylrState],
                            &yys->yysemantics.yysval]b4_location_if([, 
&yys->yyloc])[);
@@ -1785,10 +1784,8 @@ yyrecoverSyntaxError (yyGLRStack* yystac
            {
              /* Shift the error token having adjusted its location.  */
              YYLTYPE yyerrloc;]b4_location_if([[
-             yystack->yyerror_range[1] = *yyllocp;
-#             define YYRHSLOC(yyRhs,YYK) ((yyRhs)[YYK])
-             YYLLOC_DEFAULT (yyerrloc, yystack->yyerror_range - 1, 2);
-#             undef YYRHSLOC]])[
+             yystack->yyerror_range[1].yystate.yyloc = *yyllocp;
+             YYLLOC_DEFAULT (yyerrloc, yystack->yyerror_range - 1, 2);]])[
              YY_SYMBOL_PRINT ("Shifting", yystos[yytable[yyj]],
                               yylvalp, &yyerrloc);
              yyglrShift (yystack, 0, yytable[yyj],
@@ -1797,7 +1794,7 @@ yyrecoverSyntaxError (yyGLRStack* yystac
              break;
            }
        }
-]b4_location_if([[      yystack->yyerror_range[0] = yys->yyloc;]])[
+]b4_location_if([[      yystack->yyerror_range[0].yystate.yyloc = 
yys->yyloc;]])[
       yydestruct ("Error: popping",
                  yystos[yys->yylrState],
                  &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[);
@@ -1896,7 +1893,7 @@ b4_syncline(address@hidden@], address@hidden@])])dnl
              yyrule = yydefaultAction (yystate);
              if (yyrule == 0)
                {
-]b4_location_if([[               yystack.yyerror_range[0] = *yyllocp;]])[
+]b4_location_if([[               yystack.yyerror_range[0].yystate.yyloc = 
*yyllocp;]])[
                  yyreportSyntaxError (&yystack, yylvalp, 
yyllocp]b4_user_args[);
                  goto yyuser_error;
                }
@@ -1927,7 +1924,7 @@ b4_syncline(address@hidden@], address@hidden@])])dnl
                }
              else if (yyisErrorAction (yyaction))
                {
-]b4_location_if([[               yystack.yyerror_range[0] = *yyllocp;]])[
+]b4_location_if([[               yystack.yyerror_range[0].yystate.yyloc = 
*yyllocp;]])[
                  yyreportSyntaxError (&yystack, yylvalp, 
yyllocp]b4_user_args[);
                  goto yyuser_error;
                }
@@ -1953,7 +1950,7 @@ b4_syncline(address@hidden@], address@hidden@])])dnl
                yyFail (&yystack][]b4_lpure_args[, "syntax error");
              YYCHK1 (yyresolveStack (&yystack]b4_user_args[));
              YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
-]b4_location_if([[           yystack.yyerror_range[0] = *yyllocp;]])[
+]b4_location_if([[           yystack.yyerror_range[0].yystate.yyloc = 
*yyllocp;]])[
              yyreportSyntaxError (&yystack, yylvalp, yyllocp]b4_user_args[);
              goto yyuser_error;
            }
Index: data/yacc.c
===================================================================
RCS file: /cvsroot/bison/bison/data/yacc.c,v
retrieving revision 1.74
diff -p -u -r1.74 yacc.c
--- data/yacc.c 5 Oct 2004 08:39:17 -0000       1.74
+++ data/yacc.c 12 Dec 2004 09:18:11 -0000
@@ -510,22 +510,25 @@ while (0)
    If N is 0, then set CURRENT to the empty location which ends
    the previous symbol: RHS[0] (always defined).  */
 
+#define YYRHSLOC(Rhs, K) ((Rhs)[K])
 #ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(Current, Rhs, N)                                    \
-do {                                                                        \
-  if (N)                                                                    \
-    {                                                                       \
-      (Current).first_line   = (Rhs)[1].first_line;                         \
-      (Current).first_column = (Rhs)[1].first_column;                       \
-      (Current).last_line    = (Rhs)[N].last_line;                          \
-      (Current).last_column  = (Rhs)[N].last_column;                        \
-    }                                                                       \
-  else                                                                      \
-    {                                                                       \
-      (Current).first_line   = (Current).last_line   = (Rhs)[0].last_line;   \
-      (Current).first_column = (Current).last_column = (Rhs)[0].last_column; \
-    }                                                                       \
-} while(0)
+# define YYLLOC_DEFAULT(Current, Rhs, N)                               \
+    do                                                                 \
+      if (N)                                                           \
+       {                                                               \
+         (Current).first_line   = YYRHSLOC(Rhs, 1).first_line;         \
+         (Current).first_column = YYRHSLOC(Rhs, 1).first_column;       \
+         (Current).last_line    = YYRHSLOC(Rhs, N).last_line;          \
+         (Current).last_column  = YYRHSLOC(Rhs, N).last_column;        \
+       }                                                               \
+      else                                                             \
+       {                                                               \
+         (Current).first_line   = (Current).last_line   =              \
+           YYRHSLOC(Rhs, 0).last_line;                                 \
+         (Current).first_column = (Current).last_column =              \
+           YYRHSLOC(Rhs, 0).last_column;                               \
+       }                                                               \
+    while (0)
 #endif
 
 
Index: doc/bison.texinfo
===================================================================
RCS file: /cvsroot/bison/bison/doc/bison.texinfo,v
retrieving revision 1.134
diff -p -u -r1.134 bison.texinfo
--- doc/bison.texinfo   22 Oct 2004 18:10:59 -0000      1.134
+++ doc/bison.texinfo   12 Dec 2004 09:18:12 -0000
@@ -3446,38 +3446,40 @@ dedicated code from semantic actions.
 
 The @code{YYLLOC_DEFAULT} macro takes three parameters.  The first one is
 the location of the grouping (the result of the computation).  When a
-rule is matched, the second parameter is an array holding locations of
+rule is matched, the second parameter identifies locations of
 all right hand side elements of the rule being matched, and the third
 parameter is the size of the rule's right hand side.  When processing
-a syntax error, the second parameter is an array holding locations of
+a syntax error, the second parameter identifies locations of
 the symbols that were discarded during error processing, and the third
 parameter is the number of discarded symbols.
 
-By default, @code{YYLLOC_DEFAULT} is defined this way for simple
address@hidden(1) parsers:
+By default, @code{YYLLOC_DEFAULT} is defined this way:
 
address@hidden
address@hidden
 @group
-# define YYLLOC_DEFAULT(Current, Rhs, N)               \
-   ((Current).first_line   = (Rhs)[1].first_line,      \
-    (Current).first_column = (Rhs)[1].first_column,    \
-    (Current).last_line    = (Rhs)[N].last_line,       \
-    (Current).last_column  = (Rhs)[N].last_column)
+# define YYLLOC_DEFAULT(Current, Rhs, N)                                \
+    do                                                                  \
+      if (N)                                                            \
+        @{                                                               \
+          (Current).first_line   = YYRHSLOC(Rhs, 1).first_line;         \
+          (Current).first_column = YYRHSLOC(Rhs, 1).first_column;       \
+          (Current).last_line    = YYRHSLOC(Rhs, N).last_line;          \
+          (Current).last_column  = YYRHSLOC(Rhs, N).last_column;        \
+        @}                                                               \
+      else                                                              \
+        @{                                                               \
+          (Current).first_line   = (Current).last_line   =              \
+            YYRHSLOC(Rhs, 0).last_line;                                 \
+          (Current).first_column = (Current).last_column =              \
+            YYRHSLOC(Rhs, 0).last_column;                               \
+        @}                                                               \
+    while (0)
 @end group
address@hidden example
-
address@hidden
-and like this for @acronym{GLR} parsers:
address@hidden smallexample
 
address@hidden
address@hidden
-# define YYLLOC_DEFAULT(yyCurrent, yyRhs, YYN)                 \
-   ((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_column  = YYRHSLOC(yyRhs, YYN).last_column)
address@hidden group
address@hidden example
+where @code{YYRHSLOC (rhs, k)} is the location of the @var{k}th symbol
+in @var{rhs} when @var{k} is positive, and the location of the symbol
+just before the reduction when @var{k} is zero.
 
 When defining @code{YYLLOC_DEFAULT}, you should consider that:
 
@@ -3487,8 +3489,10 @@ All arguments are free of side-effects. 
 result) should be modified by @code{YYLLOC_DEFAULT}.
 
 @item
-For consistency with semantic actions, valid indexes for the location
-array range from 1 to @var{n}.
+For consistency with semantic actions, valid indexes within the
+right hand side range from 1 to @var{n}.  When @var{n} is zero, only 0 is a
+valid index, and it refers to the symbol just before the reduction.
+During error processing @var{n} is always positive.
 
 @item
 Your macro should parenthesize its arguments, if need be, since the




reply via email to

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