]> git.saurik.com Git - bison.git/blobdiff - data/glr.c
Formatting changes.
[bison.git] / data / glr.c
index d956a3796ab1a776d36366c9c296d2883b9f7553..f8a32a82293deb3063ee60c18a4352e0ff3c56e8 100644 (file)
@@ -1,22 +1,21 @@
-m4_divert(-1)                                                       -*- C -*-
+                                                                    -*- C -*-
 
 # GLR skeleton for Bison
-# Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
+# Inc.
 
-# This program is free software; you can redistribute it and/or modify
+# This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
+# the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
-
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-
+#
 # You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-# 02110-1301  USA
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 m4_include(b4_pkgdatadir/[c.m4])
@@ -35,6 +34,13 @@ m4_define_default([b4_stack_depth_init],  [200])
 ## Pure/impure interfaces.  ##
 ## ------------------------ ##
 
+b4_define_flag_if([pure])
+# If glr.cc is including this file and thus has already set b4_pure_flag, don't
+# change the value of b4_pure_flag, and don't record a use of api.pure.
+m4_ifndef([b4_pure_flag],
+[b4_percent_define_default([[api.pure]], [[false]])
+ m4_define([b4_pure_flag],
+           [b4_percent_define_flag_if([[api.pure]], [[1]], [[0]])])])
 
 # b4_user_formals
 # ---------------
@@ -103,7 +109,15 @@ m4_define([b4_pure_formals],
 # --------------------
 # Expansion of $<TYPE>$.
 m4_define([b4_lhs_value],
-[((*yyvalp)[]m4_ifval([$1], [.$1]))])
+[b4_symbol_value([(*yyvalp)], [$1])])
+
+
+# b4_rhs_data(RULE-LENGTH, NUM)
+# -----------------------------
+# Expand to the semantic stack place that contains value and location
+# of symbol number NUM in a rule of length RULE-LENGTH.
+m4_define([b4_rhs_data],
+[((yyGLRStackItem const *)yyvsp)@{YYFILL (($2) - ($1))@}.yystate])
 
 
 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
@@ -111,7 +125,7 @@ m4_define([b4_lhs_value],
 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
 # symbols on RHS.
 m4_define([b4_rhs_value],
-[(((yyGLRStackItem const *)yyvsp)@{YYFILL (($2) - ($1))@}.yystate.yysemantics.yysval[]m4_ifval([$3], [.$3]))])
+[b4_symbol_value([b4_rhs_data([$1], [$2]).yysemantics.yysval], [$3])])
 
 
 
@@ -131,7 +145,7 @@ m4_define([b4_lhs_location],
 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
 # on RHS.
 m4_define([b4_rhs_location],
-[(((yyGLRStackItem const *)yyvsp)@{YYFILL (($2) - ($1))@}.yystate.yyloc)])
+[(b4_rhs_data([$1], [$2]).yyloc)])
 
 
 
@@ -141,8 +155,8 @@ m4_define([b4_rhs_location],
 
 # We do want M4 expansion after # for CPP macros.
 m4_changecom()
-m4_divert(0)dnl
-@output @output_parser_name@
+m4_divert_push(0)dnl
+@output(b4_parser_file_name@)@
 b4_copyright([Skeleton implementation for Bison GLR parsers in C],
   [2002, 2003, 2004, 2005, 2006])
 [
@@ -150,6 +164,7 @@ b4_copyright([Skeleton implementation for Bison GLR parsers in C],
 
 ]b4_identification
 
+b4_percent_code_get([[top]])[]dnl
 m4_if(b4_prefix, [yy], [],
 [/* Substitute the variable and function names.  */
 #define yyparse b4_prefix[]parse
@@ -162,27 +177,27 @@ m4_if(b4_prefix, [yy], [],
 #define yylloc  b4_prefix[]lloc])[
 
 /* Copy the first part of user declarations.  */
-]b4_pre_prologue
+]b4_user_pre_prologue
 
 dnl # b4_shared_declarations
 dnl # ----------------------
 dnl # Declaration that might either go into the header (if --defines)
 dnl # or open coded in the parser body.
 m4_define([b4_shared_declarations],
-[m4_ifdef([b4_start_header],
-[[/* Copy the %start-header blocks.  */
-]b4_start_header])[]dnl
+[b4_percent_code_get([[requires]])[]dnl
 
 b4_token_enums(b4_tokens)
 
-[#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
+[#ifndef YYSTYPE
 ]m4_ifdef([b4_stype],
-[typedef union b4_union_name
-b4_user_stype
-       YYSTYPE;],
-[typedef int YYSTYPE;])[
-# define YYSTYPE_IS_DECLARED 1
-# define YYSTYPE_IS_TRIVIAL 1
+[[typedef union ]b4_union_name[
+{
+]b4_user_stype[
+} YYSTYPE;
+# define YYSTYPE_IS_TRIVIAL 1]],
+[m4_if(b4_tag_seen_flag, 0,
+[[typedef int YYSTYPE;
+# define YYSTYPE_IS_TRIVIAL 1]])])[
 #endif
 
 #if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
@@ -201,12 +216,10 @@ typedef struct YYLTYPE
 # define YYLTYPE_IS_TRIVIAL 1
 #endif
 
-]m4_ifdef([b4_end_header],
-[[/* Copy the %end-header blocks.  */
-]b4_end_header])[]dnl
+]b4_percent_code_get([[provides]])[]dnl
 ])
 
-b4_defines_if([#include @output_header_name@],
+b4_defines_if([[#include "@basename(]b4_spec_defines_file[@)"]],
              [b4_shared_declarations])[
 
 /* Enabling traces.  */
@@ -235,12 +248,12 @@ b4_defines_if([#include @output_header_name@],
 static YYSTYPE yyval_default;
 
 /* Copy the second part of user declarations.  */
-]b4_user_post_prologue[
+]b4_user_post_prologue
+b4_percent_code_get[]dnl
 
-#include <stdio.h>
+[#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdarg.h>
 
 #ifndef YY_
 # if YYENABLE_NLS
@@ -345,8 +358,7 @@ static YYSTYPE yyval_default;
 #define YYMAXUTOK   ]b4_user_token_number_max[
 
 #define YYTRANSLATE(YYX)                                               \
-  ((YYX <= 0) ? YYEOF :                                                        \
-   (unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
+  ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
 
 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX.  */
 static const ]b4_int_type_for([b4_translate])[ yytranslate[] =
@@ -1440,12 +1452,12 @@ yy_reduce_print (yyGLRStack* yystackp, size_t yyk, yyRuleNum yyrule,
   /* The symbols being reduced.  */
   for (yyi = 0; yyi < yynrhs; yyi++)
     {
-      fprintf (stderr, "   $%d = ", yyi + 1);
+      YYFPRINTF (stderr, "   $%d = ", yyi + 1);
       yy_symbol_print (stderr, yyrhs[yyprhs[yyrule] + yyi],
                       &]b4_rhs_value(yynrhs, yyi + 1)[
                       ]b4_locations_if([, &]b4_rhs_location(yynrhs, yyi + 1))[]dnl
                       b4_user_args[);
-      fprintf (stderr, "\n");
+      YYFPRINTF (stderr, "\n");
     }
 }
 #endif
@@ -2039,11 +2051,19 @@ yyprocessOneStack (yyGLRStack* yystackp, size_t yyk,
            {
              YYDPRINTF ((stderr, "Reading a token: "));
              yychar = YYLEX;
+           }
+
+         if (yychar <= YYEOF)
+           {
+             yychar = yytoken = YYEOF;
+             YYDPRINTF ((stderr, "Now at end of input.\n"));
+           }
+         else
+           {
              yytoken = YYTRANSLATE (yychar);
              YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
            }
-         else
-           yytoken = YYTRANSLATE (yychar);
+
          yygetLRActions (yystate, yytoken, &yyaction, &yyconflicts);
 
          while (*yyconflicts != 0)
@@ -2209,8 +2229,16 @@ yyrecoverSyntaxError (yyGLRStack* yystackp]b4_user_formals[)
          }
        YYDPRINTF ((stderr, "Reading a token: "));
        yychar = YYLEX;
-       yytoken = YYTRANSLATE (yychar);
-       YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
+       if (yychar <= YYEOF)
+         {
+           yychar = yytoken = YYEOF;
+           YYDPRINTF ((stderr, "Now at end of input.\n"));
+         }
+       else
+         {
+           yytoken = YYTRANSLATE (yychar);
+           YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
+         }
        yyj = yypact[yystackp->yytops.yystates[0]->yylrState];
        if (yyis_pact_ninf (yyj))
          return;
@@ -2260,7 +2288,8 @@ yyrecoverSyntaxError (yyGLRStack* yystackp]b4_user_formals[)
            }
        }
 ]b4_locations_if([[      yystackp->yyerror_range[1].yystate.yyloc = yys->yyloc;]])[
-      yydestroyGLRState ("Error: popping", yys]b4_user_args[);
+      if (yys->yypred != NULL)
+       yydestroyGLRState ("Error: popping", yys]b4_user_args[);
       yystackp->yytops.yystates[0] = yys->yypred;
       yystackp->yynextFree -= 1;
       yystackp->yyspaceLeft += 1;
@@ -2303,8 +2332,8 @@ yyrecoverSyntaxError (yyGLRStack* yystackp]b4_user_formals[)
   yylval = yyval_default;
 ]b4_locations_if([
 #if YYLTYPE_IS_TRIVIAL
-  yylloc.first_line   = yylloc.last_line   = 1;
-  yylloc.first_column = yylloc.last_column = 0;
+  yylloc.first_line   = yylloc.last_line   = ]b4_location_initial_line[;
+  yylloc.first_column = yylloc.last_column = ]b4_location_initial_column[;
 #endif
 ])
 m4_ifdef([b4_initial_action], [
@@ -2361,19 +2390,26 @@ m4_popdef([b4_at_dollar])])dnl
                {
                  YYDPRINTF ((stderr, "Reading a token: "));
                  yychar = YYLEX;
+               }
+
+             if (yychar <= YYEOF)
+               {
+                 yychar = yytoken = YYEOF;
+                 YYDPRINTF ((stderr, "Now at end of input.\n"));
+               }
+             else
+               {
                  yytoken = YYTRANSLATE (yychar);
                  YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
                }
-             else
-               yytoken = YYTRANSLATE (yychar);
+
              yygetLRActions (yystate, yytoken, &yyaction, &yyconflicts);
              if (*yyconflicts != 0)
                break;
              if (yyisShiftAction (yyaction))
                {
                  YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
-                 if (yychar != YYEOF)
-                   yychar = YYEMPTY;
+                 yychar = YYEMPTY;
                  yyposn += 1;
                  yyglrShift (&yystack, 0, yyaction, yyposn, &yylval, &yylloc);
                  if (0 < yystack.yyerrState)
@@ -2489,7 +2525,7 @@ m4_popdef([b4_at_dollar])])dnl
   goto yyreturn;
 
  yyreturn:
-  if (yychar != YYEOF && yychar != YYEMPTY)
+  if (yychar != YYEMPTY)
     yydestruct ("Cleanup: discarding lookahead",
                YYTRANSLATE (yychar),
                &yylval]b4_locations_if([, &yylloc])[]b4_user_args[);
@@ -2511,7 +2547,8 @@ m4_popdef([b4_at_dollar])])dnl
                  {
                    yyGLRState *yys = yystates[yyk];
 ]b4_locations_if([[                yystack.yyerror_range[1].yystate.yyloc = yys->yyloc;]]
-)[                 yydestroyGLRState ("Cleanup: popping", yys]b4_user_args[);
+)[                 if (yys->yypred != NULL)
+                     yydestroyGLRState ("Cleanup: popping", yys]b4_user_args[);
                    yystates[yyk] = yys->yypred;
                    yystack.yynextFree -= 1;
                    yystack.yyspaceLeft += 1;
@@ -2527,7 +2564,7 @@ m4_popdef([b4_at_dollar])])dnl
 }
 
 /* DEBUGGING ONLY */
-#ifdef YYDEBUG
+#if YYDEBUG
 static void yypstack (yyGLRStack* yystackp, size_t yyk)
   __attribute__ ((__unused__));
 static void yypdumpstack (yyGLRStack* yystackp) __attribute__ ((__unused__));
@@ -2538,19 +2575,20 @@ yy_yypstack (yyGLRState* yys)
   if (yys->yypred)
     {
       yy_yypstack (yys->yypred);
-      fprintf (stderr, " -> ");
+      YYFPRINTF (stderr, " -> ");
     }
-  fprintf (stderr, "%d@@%lu", yys->yylrState, (unsigned long int) yys->yyposn);
+  YYFPRINTF (stderr, "%d@@%lu", yys->yylrState,
+             (unsigned long int) yys->yyposn);
 }
 
 static void
 yypstates (yyGLRState* yyst)
 {
   if (yyst == NULL)
-    fprintf (stderr, "<null>");
+    YYFPRINTF (stderr, "<null>");
   else
     yy_yypstack (yyst);
-  fprintf (stderr, "\n");
+  YYFPRINTF (stderr, "\n");
 }
 
 static void
@@ -2570,46 +2608,54 @@ yypdumpstack (yyGLRStack* yystackp)
   size_t yyi;
   for (yyp = yystackp->yyitems; yyp < yystackp->yynextFree; yyp += 1)
     {
-      fprintf (stderr, "%3lu. ", (unsigned long int) (yyp - yystackp->yyitems));
+      YYFPRINTF (stderr, "%3lu. ",
+                 (unsigned long int) (yyp - yystackp->yyitems));
       if (*(yybool *) yyp)
        {
-         fprintf (stderr, "Res: %d, LR State: %d, posn: %lu, pred: %ld",
-                  yyp->yystate.yyresolved, yyp->yystate.yylrState,
-                  (unsigned long int) yyp->yystate.yyposn,
-                  (long int) YYINDEX (yyp->yystate.yypred));
+         YYFPRINTF (stderr, "Res: %d, LR State: %d, posn: %lu, pred: %ld",
+                    yyp->yystate.yyresolved, yyp->yystate.yylrState,
+                    (unsigned long int) yyp->yystate.yyposn,
+                    (long int) YYINDEX (yyp->yystate.yypred));
          if (! yyp->yystate.yyresolved)
-           fprintf (stderr, ", firstVal: %ld",
-                    (long int) YYINDEX (yyp->yystate.yysemantics.yyfirstVal));
+           YYFPRINTF (stderr, ", firstVal: %ld",
+                      (long int) YYINDEX (yyp->yystate
+                                             .yysemantics.yyfirstVal));
        }
       else
        {
-         fprintf (stderr, "Option. rule: %d, state: %ld, next: %ld",
-                  yyp->yyoption.yyrule - 1,
-                  (long int) YYINDEX (yyp->yyoption.yystate),
-                  (long int) YYINDEX (yyp->yyoption.yynext));
+         YYFPRINTF (stderr, "Option. rule: %d, state: %ld, next: %ld",
+                    yyp->yyoption.yyrule - 1,
+                    (long int) YYINDEX (yyp->yyoption.yystate),
+                    (long int) YYINDEX (yyp->yyoption.yynext));
        }
-      fprintf (stderr, "\n");
+      YYFPRINTF (stderr, "\n");
     }
-  fprintf (stderr, "Tops:");
+  YYFPRINTF (stderr, "Tops:");
   for (yyi = 0; yyi < yystackp->yytops.yysize; yyi += 1)
-    fprintf (stderr, "%lu: %ld; ", (unsigned long int) yyi,
-            (long int) YYINDEX (yystackp->yytops.yystates[yyi]));
-  fprintf (stderr, "\n");
+    YYFPRINTF (stderr, "%lu: %ld; ", (unsigned long int) yyi,
+              (long int) YYINDEX (yystackp->yytops.yystates[yyi]));
+  YYFPRINTF (stderr, "\n");
 }
 #endif
 ]
 
 b4_epilogue
-b4_defines_if(
-[@output @output_header_name@
+dnl
+dnl glr.cc produces its own header.
+dnl
+m4_if(b4_skeleton, ["glr.c"],
+[b4_defines_if(
+[@output(b4_spec_defines_file@)@
 b4_copyright([Skeleton interface for Bison GLR parsers in C],
   [2002, 2003, 2004, 2005, 2006])
 
 b4_shared_declarations
 
-extern YYSTYPE b4_prefix[]lval;
+b4_pure_if([],
+[[extern YYSTYPE b4_prefix][lval;]])
 
 b4_locations_if([b4_pure_if([],
 [extern YYLTYPE b4_prefix[]lloc;])
 ])
-])
+])])
+m4_divert_pop(0)