bison-patches
[Top][All Lists]
Advanced

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

FYI: yacc.c catches up with lalr1.cc for error's location, and introduci


From: Akim Demaille
Subject: FYI: yacc.c catches up with lalr1.cc for error's location, and introducing YY_LOCATION_PRINT
Date: Mon, 20 Sep 2004 11:32:45 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

yacc.c is still not getting the same results as lalr1.cc, but that's
because of the location of "empty" (i.e., when reducing a rule with an
empty rhs).  I'm looking into it, fix, and then move on completing the
places where freeing the stack is wanted.

I'm pretty unable to apply the corresponding changes in glr...

Index: ChangeLog
from  Akim Demaille  <address@hidden>

        * data/yacc.c (YY_LOCATION_PRINT): New.
        Define when we know YYLTYPE's structure, i.e., when the default
        YYLLOC_DEFAULT is used.
        * data/c.m4 (b4_yysymprint_generate): Use it.
        * data/lalr1.cc (YYLLOC_DEFAULT): Stop relying on the initial
        value of the result.
        (error_start_): Replace with...
        (error_range_): this location array.
        This allows to replace code relying on the implementation of
        locations by portable code.
        * data/yacc.c (yylerrsp): Replace with...
        (yyerror_range): this.
        Every time a token is popped, update yyerror_range[0], to have an
        accurate location for the error token.
        * data/glr.c (YY_LOCATION_PRINT): New.
        (yyprocessOneStack): Fix an invocation of YY_SYMBOL_PRINT:
        deference a pointer.
        * tests/actions.at (_AT_CHECK_PRINTER_AND_DESTRUCTOR): No longer
        report the location in %printers.

        * src/scan-skel.l: Instead of abort, report error messages to ease
        understanding skeleton scanning failures.

Index: data/c.m4
===================================================================
RCS file: /cvsroot/bison/bison/data/c.m4,v
retrieving revision 1.24
diff -u -u -r1.24 c.m4
--- data/c.m4 6 Sep 2004 10:29:52 -0000 1.24
+++ data/c.m4 20 Sep 2004 09:30:41 -0000
@@ -396,23 +396,27 @@
   (void) yyvaluep;
 b4_location_if([  (void) yylocationp;
 ])dnl
-
+[
   if (yytype < YYNTOKENS)
     {
-      YYFPRINTF (yyoutput, "token %s (", yytname[[yytype]]);
+      YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
 # ifdef YYPRINT
-      YYPRINT (yyoutput, yytoknum[[yytype]], *yyvaluep);
+      YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
 # endif
     }
   else
-    YYFPRINTF (yyoutput, "nterm %s (", yytname[[yytype]]);
+    YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
 
+]b4_location_if([  YY_LOCATION_PRINT (yyoutput, *yylocationp);
+  fprintf (yyoutput, ": ");
+])dnl
+[
   switch (yytype)
     {
-m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
-      default:
+]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
+[      default:
         break;
     }
   YYFPRINTF (yyoutput, ")");
 }
-])
+]])
Index: data/glr.c
===================================================================
RCS file: /cvsroot/bison/bison/data/glr.c,v
retrieving revision 1.75
diff -u -u -r1.75 glr.c
--- data/glr.c 16 Sep 2004 14:14:27 -0000 1.75
+++ data/glr.c 20 Sep 2004 09:30:41 -0000
@@ -420,8 +420,7 @@
 /* Error token number */
 #define YYTERROR 1
 
-/* YYLLOC_DEFAULT -- Compute the default location (before the actions
-   are run).  */
+/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].  */
 
 ]b4_location_if([[
 #define YYRHSLOC(yyRhs,YYK) ((yyRhs)[YYK].yystate.yyloc)
@@ -432,6 +431,15 @@
     (yyCurrent).first_column = YYRHSLOC(yyRhs, 1).first_column,        \
     (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.
+   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 YYLLOC_DEFAULT
@@ -439,6 +447,10 @@
 #endif
 ])[
 
+#ifndef YY_LOCATION_PRINT
+# define YY_LOCATION_PRINT(File, Loc) ((void) 0)
+#endif
+
 
 /* YYLEX -- calling `yylex' with the right arguments.  */
 #define YYLEX ]b4_c_function_call([yylex], [int], b4_lex_param)[
@@ -1579,7 +1591,7 @@
          if (yyisShiftAction (yyaction))
            {
              YYDPRINTF ((stderr, "On stack %d, ", yyk));
-             YY_SYMBOL_PRINT ("shifting", yytokenp, yylvalp, yyllocp);
+             YY_SYMBOL_PRINT ("shifting", *yytokenp, yylvalp, yyllocp);
              yyglrShift (yystack, yyk, yyaction, yyposn+1,
                          *yylvalp, yyllocp]b4_user_args[);
              YYDPRINTF ((stderr, ", now in state #%d\n",
Index: data/lalr1.cc
===================================================================
RCS file: /cvsroot/bison/bison/data/lalr1.cc,v
retrieving revision 1.55
diff -u -u -r1.55 lalr1.cc
--- data/lalr1.cc 16 Sep 2004 14:41:18 -0000 1.55
+++ data/lalr1.cc 20 Sep 2004 09:30:41 -0000
@@ -173,8 +173,11 @@
 ]/* Line __line__ of lalr1.cc.  */
 b4_syncline(address@hidden@], address@hidden@])[
 #ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(Current, Rhs, N) \
-   ((Current).end = Rhs[N].end)
+# define YYLLOC_DEFAULT(Current, Rhs, N)       \
+do {                                           \
+   ((Current).begin = (Rhs)[1].begin);         \
+   ((Current).end   = (Rhs)[N].end);           \
+} while (0)
 #endif
 
 namespace yy
@@ -323,8 +326,8 @@
     /* Semantic value and location of look-ahead token.  */
     SemanticType value;
     LocationType location;
-    /* Beginning of the last erroneous token popped off.  */
-    Position error_start_;
+    /// The locations where the error started and ended.
+    Location error_range_[2];
 
     /* @@$ and $$.  */
     SemanticType yyval;
@@ -604,7 +607,7 @@
   /* If not already recovering from an error, report this error.  */
   report_syntax_error_ ();
 
-  error_start_ = location.begin;
+  error_range_[0] = location;
   if (errstatus_ == 3)
     {
       /* If just tried and failed to reuse look-ahead token after an
@@ -618,7 +621,7 @@
          if (looka_ == eof_)
             for (;;)
               {
-                 error_start_ = location_stack_[0].begin;
+                 error_range_[0] = location_stack_[0];
                  pop ();
                 if (state_stack_.height () == 1)
                   YYABORT;
@@ -652,7 +655,7 @@
      goto yyerrorlab;
 #endif
 
-  error_start_ = location_stack_[len_ - 1].begin;
+  error_range_[0] = location_stack_[len_ - 1];
   pop (len_);
   state_ = state_stack_[0];
   goto yyerrlab1;
@@ -681,10 +684,9 @@
       if (state_stack_.height () == 1)
        YYABORT;
 
+      error_range_[0] = location_stack_[0];
       destruct_ ("Error: popping",
                  stos_[state_], &semantic_stack_[0], &location_stack_[0]);
-      error_start_ = location_stack_[0].begin;
-
       pop ();
       state_ = state_stack_[0];
       YY_STACK_PRINT ();
@@ -693,13 +695,12 @@
   if (n_ == final_)
     goto yyacceptlab;
 
-  {
-    Location errloc;
-    errloc.begin = error_start_;
-    errloc.end = location.end;
-    semantic_stack_.push (value);
-    location_stack_.push (errloc);
-  }
+  error_range_[1] = location;
+  // Using LOCATION is tempting, but would change the location of
+  // the look-ahead.  YYLOC is available though.
+  YYLLOC_DEFAULT (yyloc, error_range_ - 1, 2);
+  semantic_stack_.push (value);
+  location_stack_.push (yyloc);
 
   /* Shift the error token. */
   YY_SYMBOL_PRINT ("Shifting", stos_[n_],
Index: data/yacc.c
===================================================================
RCS file: /cvsroot/bison/bison/data/yacc.c,v
retrieving revision 1.68
diff -u -u -r1.68 yacc.c
--- data/yacc.c 16 Sep 2004 14:14:27 -0000 1.68
+++ data/yacc.c 20 Sep 2004 09:30:41 -0000
@@ -501,11 +501,12 @@
     }                                                          \
 while (0)
 
+
 #define YYTERROR       1
 #define YYERRCODE      256
 
-/* YYLLOC_DEFAULT -- Compute the default location (before the actions
-   are run).  */
+
+/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].  */
 
 #ifndef YYLLOC_DEFAULT
 # define YYLLOC_DEFAULT(Current, Rhs, N)               \
@@ -513,8 +514,22 @@
     (Current).first_column = (Rhs)[1].first_column,    \
     (Current).last_line    = (Rhs)[N].last_line,       \
     (Current).last_column  = (Rhs)[N].last_column)
+
+/* 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
+# define YY_LOCATION_PRINT(File, Loc) ((void) 0)
 #endif
 
+
 /* YYLEX -- calling `yylex' with the right arguments.  */
 
 #ifdef YYLEX_PARAM
@@ -770,7 +785,8 @@
   YYLTYPE yylsa[YYINITDEPTH];
   YYLTYPE *yyls = yylsa;
   YYLTYPE *yylsp;
-  YYLTYPE *yylerrsp;]])[
+  /* The locations where the error started and ended. */
+  YYLTYPE yyerror_range[2];]])[
 
 #define YYPOPSTACK   (yyvsp--, yyssp--]b4_location_if([, yylsp--])[)
 
@@ -1097,7 +1113,7 @@
        yyerror (]b4_yyerror_args["syntax error");
     }
 
-]b4_location_if([  yylerrsp = yylsp;])[
+]b4_location_if([[  yyerror_range[0] = yylloc;]])[
 
   if (yyerrstatus == 3)
     {
@@ -1111,6 +1127,7 @@
          if (yychar == YYEOF)
             for (;;)
               {
+]b4_location_if([[                 yyerror_range[0] = *yylsp;]])[
                 YYPOPSTACK;
                 if (yyssp == yyss)
                   YYABORT;
@@ -1122,7 +1139,6 @@
        {
          yydestruct ("Error: discarding", yytoken, &yylval]b4_location_if([, 
&yylloc])[);
          yychar = YYEMPTY;
-]b4_location_if([        *++yylerrsp = yylloc;])[
        }
     }
 
@@ -1143,13 +1159,12 @@
      goto yyerrorlab;
 #endif
 
-  yyvsp -= yylen;
+]b4_location_if([[  yyerror_range[0] = yylsp[1-yylen];
+  yylsp -= yylen;
+  ]])[yyvsp -= yylen;
   yyssp -= yylen;
   yystate = *yyssp;
-  ]b4_location_if([yylerrsp = yylsp;
-  *++yylerrsp = yyloc;
-  yylsp -= yylen;
-  ])[goto yyerrlab1;
+  goto yyerrlab1;
 
 
 /*-------------------------------------------------------------.
@@ -1176,6 +1191,7 @@
       if (yyssp == yyss)
        YYABORT;
 
+]b4_location_if([[      yyerror_range[0] = *yylsp;]])[
       yydestruct ("Error: popping", yystos[yystate], yyvsp]b4_location_if([, 
yylsp])[);
       YYPOPSTACK;
       yystate = *yyssp;
@@ -1186,8 +1202,12 @@
     YYACCEPT;
 
   *++yyvsp = yylval;
-]b4_location_if([  YYLLOC_DEFAULT (yyloc, yylsp, yylerrsp - yylsp);
-  *++yylsp = yyloc;])[
+]b4_location_if([[
+  yyerror_range[1] = yylloc;
+  /* Using YYLLOC is tempting, but would change the location of
+     the look-ahead.  YYLOC is available though. */
+  YYLLOC_DEFAULT (yyloc, yyerror_range - 1, 2);
+  *++yylsp = yyloc;]])[
 
   /* Shift the error token. */
   YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
Index: src/scan-skel.l
===================================================================
RCS file: /cvsroot/bison/bison/src/scan-skel.l,v
retrieving revision 1.24
diff -u -u -r1.24 scan-skel.l
--- src/scan-skel.l 2 Feb 2003 10:55:20 -0000 1.24
+++ src/scan-skel.l 20 Sep 2004 09:30:41 -0000
@@ -1,6 +1,6 @@
 /* Scan Bison Skeletons.                                       -*- C -*-
 
-   Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
 
    This file is part of Bison, the GNU Compiler Compiler.
 
@@ -55,7 +55,7 @@
       else if (strcmp (filename, "@output_parser_name@") == 0)
        filename = parser_file_name;
       else
-       abort ();
+       fatal ("invalid token in skeleton: %s", yytext);
     }
 
   XFREE (outname);
@@ -74,7 +74,8 @@
 "@output_parser_name@" QPUTS (parser_file_name);
 "@output_header_name@"  QPUTS (spec_defines_file);
 
-"@"       abort ();
+  /* This pattern must not match more than the previous @ patterns. */
address@hidden@\n]* fatal ("invalid @ in skeleton: %s", yytext);
 \n        lineno++; ECHO;
 address@hidden    ECHO;
 
Index: tests/actions.at
===================================================================
RCS file: /cvsroot/bison/bison/tests/actions.at,v
retrieving revision 1.36
diff -u -u -r1.36 actions.at
--- tests/actions.at 16 Sep 2004 14:14:27 -0000 1.36
+++ tests/actions.at 20 Sep 2004 09:30:42 -0000
@@ -197,7 +197,7 @@
 %printer
   {
     ]AT_LALR1_CC_IF([cdebug_ << $$;],
-                    [fprintf (yyoutput, "address@hidden", $$, RANGE (@$))])[;
+                    [fprintf (yyoutput, "%d", $$)])[;
   }
   input line thing 'x' 'y'
 




reply via email to

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