]> git.saurik.com Git - bison.git/blobdiff - data/glr.c
* data/c.m4 (b4_copyright): Put the special exception for Bison
[bison.git] / data / glr.c
index aab6779ce6293769a635a2aee6a897992f062202..02e0d91044a379fc124b060a746b2ac6fa194182 100644 (file)
@@ -143,14 +143,14 @@ m4_define([b4_rhs_location],
 m4_changecom()
 m4_divert(0)dnl
 @output @output_parser_name@
-b4_copyright([Skeleton parser for GLR parsing with Bison],
+b4_copyright([Skeleton implementation for Bison GLR parsers in C],
   [2002, 2003, 2004, 2005, 2006])
 [
-/* This is the parser code for GLR (Generalized LR) parser.  */
+/* C GLR parser skeleton written by Paul Hilfinger.  */
 
 ]b4_identification
 
-m4_if(b4_prefix[], [yy], [],
+m4_if(b4_prefix, [yy], [],
 [/* Substitute the variable and function names.  */
 #define yyparse b4_prefix[]parse
 #define yylex   b4_prefix[]lex
@@ -1803,6 +1803,57 @@ yyreportAmbiguity (yySemanticOption* yyx0,
   return yyabort;
 }
 
+/** Starting at and including state S1, resolve the location for each of the
+ *  previous N1 states that is unresolved.  The first semantic option of a state
+ *  is always chosen.  */
+static void
+yyresolveLocations (yyGLRState* yys1, int yyn1,
+                   yyGLRStack *yystackp]b4_user_formals[)
+{
+  if (0 < yyn1)
+    {
+      yyresolveLocations (yys1->yypred, yyn1 - 1, yystackp]b4_user_args[);
+      if (!yys1->yyresolved)
+       {
+         yySemanticOption *yyoption;
+         yyGLRStackItem yyrhsloc[1 + YYMAXRHS];
+         int yynrhs;
+         int yychar_current;
+         YYSTYPE yylval_current;
+         YYLTYPE yylloc_current;
+         yyoption = yys1->yysemantics.yyfirstVal;
+         YYASSERT (yyoption != NULL);
+         yynrhs = yyrhsLength (yyoption->yyrule);
+         if (yynrhs > 0)
+           {
+             yyGLRState *yys;
+             int yyn;
+             yyresolveLocations (yyoption->yystate, yynrhs,
+                                 yystackp]b4_user_args[);
+             for (yys = yyoption->yystate, yyn = yynrhs;
+                  yyn > 0;
+                  yys = yys->yypred, yyn -= 1)
+               yyrhsloc[yyn].yystate.yyloc = yys->yyloc;
+           }
+         else
+           {
+             yyGLRState *yyprevious = yyoption->yystate;
+             YYASSERT (yyprevious->yyresolved);
+             yyrhsloc[0].yystate.yyloc = yyprevious->yyloc;
+           }
+         yychar_current = yychar;
+         yylval_current = yylval;
+         yylloc_current = yylloc;
+         yychar = yyoption->yyrawchar;
+         yylval = yyoption->yyval;
+         yylloc = yyoption->yyloc;
+         YYLLOC_DEFAULT ((yys1->yyloc), yyrhsloc, yynrhs);
+         yychar = yychar_current;
+         yylval = yylval_current;
+         yylloc = yylloc_current;
+       }
+    }
+}
 
 /** Resolve the ambiguity represented in state S, perform the indicated
  *  actions, and set the semantic value of S.  If result != yyok, the chain of
@@ -1837,6 +1888,7 @@ yyresolveValue (yyGLRState* yys, yyGLRStack* yystackp]b4_user_formals[)
          switch (yypreference (yybest, yyp))
            {
            case 0:
+             yyresolveLocations (yys, 1, yystackp]b4_user_args[);
              return yyreportAmbiguity (yybest, yyp]b4_pure_args[);
              break;
            case 1:
@@ -2338,9 +2390,8 @@ b4_syncline([@oline@], [@ofile@])])dnl
        {
          yySymbol yytoken_to_shift;
          size_t yys;
-         size_t yyn = yystack.yytops.yysize;
 
-         for (yys = 0; yys < yyn; yys += 1)
+         for (yys = 0; yys < yystack.yytops.yysize; yys += 1)
            yystackp->yytops.yylookaheadNeeds[yys] = yychar != YYEMPTY;
 
          /* yyprocessOneStack returns one of three things:
@@ -2359,13 +2410,23 @@ b4_syncline([@oline@], [@ofile@])])dnl
             Except in the first case, yyparse will invoke yyremoveDeletes and
             then shift the next token onto all remaining stacks.  This
             synchronization of the shift (that is, after all preceding
-            reductions on all stacks) helps prevents double destructor calls
+            reductions on all stacks) helps prevent double destructor calls
             on yylval in the event of memory exhaustion.  */
 
-         for (yys = 0; yys < yyn; yys += 1)
+         for (yys = 0; yys < yystack.yytops.yysize; yys += 1)
            YYCHK1 (yyprocessOneStack (&yystack, yys, yyposn]b4_lpure_args[));
          yyremoveDeletes (&yystack);
-         yyn = yystack.yytops.yysize;
+         if (yystack.yytops.yysize == 0)
+           {
+             yyundeleteLastStack (&yystack);
+             if (yystack.yytops.yysize == 0)
+               yyFail (&yystack][]b4_lpure_args[, YY_("syntax error"));
+             YYCHK1 (yyresolveStack (&yystack]b4_user_args[));
+             YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
+]b4_location_if([[           yystack.yyerror_range[1].yystate.yyloc = yylloc;]])[
+             yyreportSyntaxError (&yystack]b4_user_args[);
+             goto yyuser_error;
+           }
 
          /* If any yyglrShift call fails, it will fail after shifting.  Thus,
             a copy of yylval will already be on stack 0 in the event of a
@@ -2375,7 +2436,7 @@ b4_syncline([@oline@], [@ofile@])])dnl
          yytoken_to_shift = YYTRANSLATE (yychar);
          yychar = YYEMPTY;
          yyposn += 1;
-         for (yys = 0; yys < yyn; yys += 1)
+         for (yys = 0; yys < yystack.yytops.yysize; yys += 1)
            {
              int yyaction;
              const short int* yyconflicts;
@@ -2391,18 +2452,8 @@ b4_syncline([@oline@], [@ofile@])])dnl
                          (unsigned long int) yys,
                          yystack.yytops.yystates[yys]->yylrState));
            }
-         if (yystack.yytops.yysize == 0)
-           {
-             yyundeleteLastStack (&yystack);
-             if (yystack.yytops.yysize == 0)
-               yyFail (&yystack][]b4_lpure_args[, YY_("syntax error"));
-             YYCHK1 (yyresolveStack (&yystack]b4_user_args[));
-             YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
-]b4_location_if([[           yystack.yyerror_range[1].yystate.yyloc = yylloc;]])[
-             yyreportSyntaxError (&yystack]b4_user_args[);
-             goto yyuser_error;
-           }
-         else if (yystack.yytops.yysize == 1)
+
+         if (yystack.yytops.yysize == 1)
            {
              YYCHK1 (yyresolveStack (&yystack]b4_user_args[));
              YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
@@ -2546,11 +2597,8 @@ yypdumpstack (yyGLRStack* yystackp)
 b4_epilogue
 m4_if(b4_defines_flag, 0, [],
 [@output @output_header_name@
-b4_copyright([Skeleton parser for GLR parsing with Bison],
-            [2002, 2003, 2004, 2005, 2006])[
-
-/* C GLR parser skeleton written by Paul Hilfinger.  */
-]
+b4_copyright([Skeleton interface for Bison GLR parsers in C],
+  [2002, 2003, 2004, 2005, 2006])
 
 b4_shared_declarations