]> git.saurik.com Git - bison.git/blobdiff - data/yacc.c
Use #define to handle the %name-prefix.
[bison.git] / data / yacc.c
index 42c7261680a01c7de2c36b63c0d87ceab8e24fcd..7331b65c3e3296183af281715173b338cb91abb3 100644 (file)
@@ -153,8 +153,7 @@ b4_copyright([Skeleton parser for Yacc-like parsing with Bison],
 
 ]b4_identification
 m4_if(b4_prefix[], [yy], [],
-[/* If NAME_PREFIX is specified substitute the variables and functions
-   names.  */
+[/* Substitute the variable and function names.  */
 #define yyparse b4_prefix[]parse
 #define yylex   b4_prefix[]lex
 #define yyerror b4_prefix[]error
@@ -501,20 +500,53 @@ do                                                                \
     }                                                          \
 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].
+   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)               \
-   ((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)
 #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.  */
+
+#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)
+# else
+#  define YY_LOCATION_PRINT(File, Loc) ((void) 0)
+# endif
+#endif
+
+
 /* YYLEX -- calling `yylex' with the right arguments.  */
 
 #ifdef YYLEX_PARAM
@@ -770,7 +802,8 @@ b4_c_function_def([yyparse], [int], b4_parse_param)
   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--])[)
 
@@ -799,17 +832,26 @@ b4_c_function_def([yyparse], [int], b4_parse_param)
 
   yyssp = yyss;
   yyvsp = yyvs;
-]b4_location_if([  yylsp = yyls;])[
-]m4_ifdef([b4_initial_action], [
-m4_pushdef([b4_at_dollar],     [(*yylsp)])dnl
-m4_pushdef([b4_dollar_dollar], [(*yyvsp)])dnl
+]b4_location_if([[  yylsp = yyls;
+#if YYLTYPE_IS_TRIVIAL
+  /* Initialize the default location before parsing starts.  */
+  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],     [yylloc])dnl
+m4_pushdef([b4_dollar_dollar], [yylval])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([@oline@], [@ofile@])])dnl
-
+[
+  yyvsp[0] = yylval;
+]b4_location_if([[    yylsp[0] = yylloc;
+]])
 [  goto yysetstate;
 
 /*------------------------------------------------------------.
@@ -946,7 +988,7 @@ yybackup:
     YYACCEPT;
 
   /* Shift the look-ahead token.  */
-  YYDPRINTF ((stderr, "Shifting token %s, ", yytname[yytoken]));
+  YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
 
   /* Discard the token being shifted unless it is eof.  */
   if (yychar != YYEOF)
@@ -992,8 +1034,8 @@ yyreduce:
   yyval = yyvsp[1-yylen];
 
 ]b4_location_if(
-[  /* Default location. */
-  YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);])[
+[[  /* Default location. */
+  YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);]])[
   YY_REDUCE_PRINT (yyn);
   switch (yyn)
     ]{
@@ -1097,7 +1139,7 @@ yyerrlab:
        yyerror (]b4_yyerror_args["syntax error");
     }
 
-]b4_location_if([  yylerrsp = yylsp;])[
+]b4_location_if([[  yyerror_range[0] = yylloc;]])[
 
   if (yyerrstatus == 3)
     {
@@ -1111,6 +1153,7 @@ yyerrlab:
          if (yychar == YYEOF)
             for (;;)
               {
+]b4_location_if([[                 yyerror_range[0] = *yylsp;]])[
                 YYPOPSTACK;
                 if (yyssp == yyss)
                   YYABORT;
@@ -1122,7 +1165,6 @@ yyerrlab:
        {
          yydestruct ("Error: discarding", yytoken, &yylval]b4_location_if([, &yylloc])[);
          yychar = YYEMPTY;
-]b4_location_if([        *++yylerrsp = yylloc;])[
        }
     }
 
@@ -1143,13 +1185,12 @@ yyerrorlab:
      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 +1217,7 @@ yyerrlab1:
       if (yyssp == yyss)
        YYABORT;
 
+]b4_location_if([[      yyerror_range[0] = *yylsp;]])[
       yydestruct ("Error: popping", yystos[yystate], yyvsp]b4_location_if([, yylsp])[);
       YYPOPSTACK;
       yystate = *yyssp;
@@ -1185,11 +1227,16 @@ yyerrlab1:
   if (yyn == YYFINAL)
     YYACCEPT;
 
-  YYDPRINTF ((stderr, "Shifting error token, "));
-
   *++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);
 
   yystate = yyn;
   goto yynewstate;