bison-patches
[Top][All Lists]
Advanced

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

10-exercise-parse-param.patch


From: Akim Demaille
Subject: 10-exercise-parse-param.patch
Date: Mon, 21 Oct 2002 09:24:06 +0200

Index: ChangeLog
from  Akim Demaille  <address@hidden>
        * src/muscle_tab.c (muscle_grow): Remove trailing debugging code.
        * data/glr.c (YY_USER_FORMALS, YY_USER_ARGS): New.
        (yyuserAction, yydoAction, yyglrReduce, yyresolveValue)
        (yyresolveStates, yyresolveAction, yyresolveStack)
        (yyprocessOneStack): Use them.
        (yy_reduce_print): New.
        * tests/calc.at (_AT_DATA_CALC_Y): Exercise %parse-param.
        
        
Index: src/muscle_tab.c
--- src/muscle_tab.c Sun, 20 Oct 2002 13:37:27 +0200 akim
+++ src/muscle_tab.c Sun, 20 Oct 2002 16:39:35 +0200 akim
@@ -139,7 +139,6 @@
     {
       /* Grow the current value. */
       char *new_val;
-      fprintf (stderr, "<= %s + %s\n", entry->value, val);
       obstack_sgrow (&muscle_obstack, entry->value);
       free (entry->value);
       obstack_sgrow (&muscle_obstack, separator);
@@ -147,7 +146,6 @@
       obstack_1grow (&muscle_obstack, 0);
       new_val = obstack_finish (&muscle_obstack);
       entry->value = xstrdup (new_val);
-      fprintf (stderr, "=> %s\n", new_val);
       obstack_free (&muscle_obstack, new_val);
     }
 }
Index: data/glr.c
--- data/glr.c Sun, 20 Oct 2002 14:43:28 +0200 akim
+++ data/glr.c Sun, 20 Oct 2002 16:34:23 +0200 akim
@@ -383,9 +383,16 @@ m4_define_default([b4_header_guard],
 
 
 /* Prevent warning if -Wmissing-prototypes.  */
-]b4_c_ansi_function_decl([yyparse], [int], b4_parse_param)[
+]b4_c_ansi_function_decl([yyparse], [int], b4_parse_param)
 
-/* Error token number */
+m4_ifset([b4_parse_param],
+[#define YY_USER_FORMALS , b4_c_ansi_formals(b4_parse_param)
+#define YY_USER_ARGS    , b4_c_args(b4_parse_param)],
+[#define YY_USER_FORMALS
+#define YY_USER_ARGS])
+
+
+[/* Error token number */
 #define YYTERROR 1
 
 /* YYLLOC_DEFAULT -- Compute the default location (before the actions
@@ -581,7 +588,8 @@ m4_define_default([b4_header_guard],
  *  yyerr for YYERROR, yyabort for YYABORT. */
 static YYRESULTTAG
 yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem* yyvsp,
-             YYSTYPE* yyvalp, YYLTYPE* yylocp, yyGLRStack* yystack)
+             YYSTYPE* yyvalp, YYLTYPE* yylocp, yyGLRStack* yystack
+             YY_USER_FORMALS)
 {
   /* Avoid `unused' warnings in there are no $n. */
   (void) yystack;
@@ -983,7 +991,7 @@ m4_define_default([b4_header_guard],
  *  for userAction. */
 static inline int
 yydoAction (yyGLRStack* yystack, int yyk, yyRuleNum yyrule,
-           YYSTYPE* yyvalp, YYLTYPE* yylocp)
+           YYSTYPE* yyvalp, YYLTYPE* yylocp YY_USER_FORMALS)
 {
   int yynrhs = yyrhsLength (yyrule);
 
@@ -1005,7 +1013,8 @@ m4_define_default([b4_header_guard],
          *yyvalp = rhs[1-yynrhs].yystate.yysemantics.yysval;
          *yylocp = rhs[1-yynrhs].yystate.yyloc;
        }
-      return yyuserAction (yyrule, yynrhs, rhs, yyvalp, yylocp, yystack);
+      return yyuserAction (yyrule, yynrhs, rhs,
+                          yyvalp, yylocp, yystack YY_USER_ARGS);
     }
   else
     {
@@ -1033,10 +1042,36 @@ m4_define_default([b4_header_guard],
          *yylocp = yyrhsVals[0].yystate.yyloc;
        }
       return yyuserAction (yyrule, yynrhs, yyrhsVals + (yynrhs-1),
-                          yyvalp, yylocp, yystack);
+                          yyvalp, yylocp, yystack YY_USER_ARGS);
     }
 }
 
+#if !YYDEBUG
+# define YY_REDUCE_PRINT(K, Rule)
+#else
+# define YY_REDUCE_PRINT(K, Rule)      \
+do {                                   \
+  if (yydebug)                         \
+    yy_reduce_print (K, Rule);         \
+} while (0)
+
+/*----------------------------------------------------------.
+| Report that the RULE is going to be reduced on stack #K.  |
+`----------------------------------------------------------*/
+
+static inline void
+yy_reduce_print (size_t yyk, yyRuleNum yyrule)
+{
+  int yyi;
+  YYDPRINTF ((stderr, "Reducing stack %d by rule %d (line %d),",
+             yyk, yyrule - 1, yyrline[yyrule]));
+  /* Print the symbols being reduced, and their result.  */
+  for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++)
+    YYFPRINTF (stderr, "%s ", yytokenName (yyrhs[yyi]));
+  YYFPRINTF (stderr, " -> %s\n", yytokenName (yyr1[yyrule]));
+}
+#endif
+
 /** Pop items off stack #K of STACK according to grammar rule RULE,
  *  and push back on the resulting nonterminal symbol.  Perform the
  *  semantic action associated with RULE and store its value with the
@@ -1050,7 +1085,7 @@ m4_define_default([b4_header_guard],
  */
 static inline YYRESULTTAG
 yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule,
-             bool yyforceEval)
+             bool yyforceEval YY_USER_FORMALS)
 {
   size_t yyposn = yystack->yytops.yystates[yyk]->yyposn;
 
@@ -1059,19 +1094,8 @@ m4_define_default([b4_header_guard],
       YYSTYPE yysval;
       YYLTYPE yyloc;
 
-#if YYDEBUG
-      if (yydebug)
-       {
-         int yyi;
-         YYDPRINTF ((stderr, "Reducing stack %d by rule %d (line %d),",
-                     yyk, yyrule - 1, yyrline[yyrule]));
-         /* Print the symbols being reduced, and their result.  */
-         for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++)
-           YYFPRINTF (stderr, "%s ", yytokenName (yyrhs[yyi]));
-         YYFPRINTF (stderr, " -> %s\n", yytokenName (yyr1[yyrule]));
-       }
-#endif
-      YYCHK (yydoAction (yystack, yyk, yyrule, &yysval, &yyloc));
+      YY_REDUCE_PRINT (yyk, yyrule);
+      YYCHK (yydoAction (yystack, yyk, yyrule, &yysval, &yyloc YY_USER_ARGS));
       yyglrShift (yystack, yyk,
                  yyLRgotoState (yystack->yytops.yystates[yyk]->yylrState,
                                 yylhsNonterm (yyrule)),
@@ -1217,22 +1241,23 @@ m4_define_default([b4_header_guard],
 
 static YYRESULTTAG yyresolveValue (yySemanticOption* yyoptionList,
                                   yyGLRStack* yystack, YYSTYPE* yyvalp,
-                                  YYLTYPE* yylocp);
+                                  YYLTYPE* yylocp YY_USER_FORMALS);
 
 static YYRESULTTAG
-yyresolveStates (yyGLRState* yys, int yyn, yyGLRStack* yystack)
+yyresolveStates (yyGLRState* yys, int yyn, yyGLRStack* yystack YY_USER_FORMALS)
 {
   YYRESULTTAG yyflag;
   if (0 < yyn)
     {
       assert (yys->yypred != NULL);
-      yyflag = yyresolveStates (yys->yypred, yyn-1, yystack);
+      yyflag = yyresolveStates (yys->yypred, yyn-1, yystack YY_USER_ARGS);
       if (yyflag != yyok)
        return yyflag;
       if (! yys->yyresolved)
        {
          yyflag = yyresolveValue (yys->yysemantics.yyfirstVal, yystack,
-                              &yys->yysemantics.yysval, &yys->yyloc);
+                                  &yys->yysemantics.yysval, &yys->yyloc
+                                  YY_USER_ARGS);
          if (yyflag != yyok)
            return yyflag;
          yys->yyresolved = yytrue;
@@ -1243,14 +1268,14 @@ m4_define_default([b4_header_guard],
 
 static YYRESULTTAG
 yyresolveAction (yySemanticOption* yyopt, yyGLRStack* yystack,
-                YYSTYPE* yyvalp, YYLTYPE* yylocp)
+                YYSTYPE* yyvalp, YYLTYPE* yylocp YY_USER_FORMALS)
 {
   yyGLRStackItem yyrhsVals[YYMAXRHS];
   int yynrhs, yyi;
   yyGLRState* yys;
 
   yynrhs = yyrhsLength (yyopt->yyrule);
-  YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack));
+  YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack YY_USER_ARGS));
   for (yyi = yynrhs-1, yys = yyopt->yystate; 0 <= yyi;
        yyi -= 1, yys = yys->yypred)
     {
@@ -1260,7 +1285,7 @@ m4_define_default([b4_header_guard],
       yyrhsVals[yyi].yystate.yyloc = yys->yyloc;
     }
   return yyuserAction (yyopt->yyrule, yynrhs, yyrhsVals + (yynrhs-1),
-                      yyvalp, yylocp, yystack);
+                      yyvalp, yylocp, yystack YY_USER_ARGS);
 }
 
 #if YYDEBUG
@@ -1275,7 +1300,8 @@ m4_define_default([b4_header_guard],
     }
   };
 
-static void yyreportTree (yySemanticOption* yyx, int yyindent)
+static void
+yyreportTree (yySemanticOption* yyx, int yyindent)
 {
   int yynrhs = yyrhsLength (yyx->yyrule);
   int yyi;
@@ -1339,7 +1365,7 @@ m4_define_default([b4_header_guard],
  *  actions, and return the result. */
 static YYRESULTTAG
 yyresolveValue (yySemanticOption* yyoptionList, yyGLRStack* yystack,
-               YYSTYPE* yyvalp, YYLTYPE* yylocp)
+               YYSTYPE* yyvalp, YYLTYPE* yylocp YY_USER_FORMALS)
 {
   yySemanticOption* yybest;
   yySemanticOption* yyp;
@@ -1372,25 +1398,25 @@ m4_define_default([b4_header_guard],
   if (yymerge)
     {
       int yyprec = yydprec[yybest->yyrule];
-      YYCHK (yyresolveAction (yybest, yystack, yyvalp, yylocp));
+      YYCHK (yyresolveAction (yybest, yystack, yyvalp, yylocp YY_USER_ARGS));
       for (yyp = yybest->yynext; yyp != NULL; yyp = yyp->yynext)
        {
          if (yyprec == yydprec[yyp->yyrule])
            {
              YYSTYPE yyval1;
              YYLTYPE yydummy;
-             YYCHK (yyresolveAction (yyp, yystack, &yyval1, &yydummy));
+             YYCHK (yyresolveAction (yyp, yystack, &yyval1, &yydummy 
YY_USER_ARGS));
              *yyvalp = yyuserMerge (yymerger[yyp->yyrule], yyvalp, &yyval1);
            }
        }
       return yyok;
     }
   else
-    return yyresolveAction (yybest, yystack, yyvalp, yylocp);
+    return yyresolveAction (yybest, yystack, yyvalp, yylocp YY_USER_ARGS);
 }
 
 static YYRESULTTAG
-yyresolveStack (yyGLRStack* yystack)
+yyresolveStack (yyGLRStack* yystack YY_USER_FORMALS)
 {
   if (yystack->yysplitPoint != NULL)
     {
@@ -1401,7 +1427,8 @@ m4_define_default([b4_header_guard],
           yys != yystack->yysplitPoint;
           yys = yys->yypred, yyn += 1)
        ;
-      YYCHK (yyresolveStates (yystack->yytops.yystates[0], yyn, yystack));
+      YYCHK (yyresolveStates (yystack->yytops.yystates[0], yyn, yystack
+                             YY_USER_ARGS));
     }
   return yyok;
 }
@@ -1438,7 +1465,8 @@ m4_define_default([b4_header_guard],
 
 static YYRESULTTAG
 yyprocessOneStack (yyGLRStack* yystack, int yyk,
-                  size_t yyposn, YYSTYPE* yylvalp, YYLTYPE* yyllocp)
+                  size_t yyposn, YYSTYPE* yylvalp, YYLTYPE* yyllocp
+                  YY_USER_FORMALS)
 {
   int yyaction;
   const short* yyconflicts;
@@ -1459,7 +1487,7 @@ m4_define_default([b4_header_guard],
              yymarkStackDeleted (yystack, yyk);
              return yyok;
            }
-         YYCHK (yyglrReduce (yystack, yyk, yyrule, yyfalse));
+         YYCHK (yyglrReduce (yystack, yyk, yyrule, yyfalse YY_USER_ARGS));
        }
       else
        {
@@ -1478,9 +1506,10 @@ m4_define_default([b4_header_guard],
              int yynewStack = yysplitStack (yystack, yyk);
              YYDPRINTF ((stderr, "Splitting off stack %d from %d.\n",
                          yynewStack, yyk));
-             YYCHK (yyglrReduce (yystack, yynewStack, *yyconflicts, yyfalse));
+             YYCHK (yyglrReduce (yystack, yynewStack,
+                                 *yyconflicts, yyfalse YY_USER_ARGS));
              YYCHK (yyprocessOneStack (yystack, yynewStack, yyposn,
-                                       yylvalp, yyllocp));
+                                       yylvalp, yyllocp YY_USER_ARGS));
              yyconflicts += 1;
            }
 
@@ -1500,7 +1529,7 @@ m4_define_default([b4_header_guard],
              break;
            }
          else
-           YYCHK (yyglrReduce (yystack, yyk, -yyaction, yyfalse));
+           YYCHK (yyglrReduce (yystack, yyk, -yyaction, yyfalse YY_USER_ARGS));
        }
     }
   return yyok;
@@ -1704,7 +1733,7 @@ m4_define_default([b4_header_guard],
                  yyreportParseError (&yystack, yylvalp, yyllocp);
                  goto yyuser_error;
                }
-             YYCHK1 (yyglrReduce (&yystack, 0, yyrule, yytrue));
+             YYCHK1 (yyglrReduce (&yystack, 0, yyrule, yytrue YY_USER_ARGS));
            }
          else
            {
@@ -1738,7 +1767,7 @@ m4_define_default([b4_header_guard],
                  goto yyuser_error;
                }
              else
-               YYCHK1 (yyglrReduce (&yystack, 0, -yyaction, yytrue));
+               YYCHK1 (yyglrReduce (&yystack, 0, -yyaction, yytrue 
YY_USER_ARGS));
            }
        }
 
@@ -1748,7 +1777,7 @@ m4_define_default([b4_header_guard],
          int yyn = yystack.yytops.yysize;
          for (yys = 0; yys < yyn; yys += 1)
            YYCHK1 (yyprocessOneStack (&yystack, yys, yyposn,
-                                      yylvalp, yyllocp));
+                                      yylvalp, yyllocp YY_USER_ARGS));
          yytoken = YYEMPTY;
          yyposn += 1;
          yyremoveDeletes (&yystack);
@@ -1757,14 +1786,14 @@ m4_define_default([b4_header_guard],
              yyundeleteLastStack (&yystack);
              if (yystack.yytops.yysize == 0)
                yyFail (&yystack, "parse error");
-             YYCHK1 (yyresolveStack (&yystack));
+             YYCHK1 (yyresolveStack (&yystack YY_USER_ARGS));
              YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
              yyreportParseError (&yystack, yylvalp, yyllocp);
              goto yyuser_error;
            }
          else if (yystack.yytops.yysize == 1)
            {
-             YYCHK1 (yyresolveStack (&yystack));
+             YYCHK1 (yyresolveStack (&yystack YY_USER_ARGS));
              YYDPRINTF ((stderr, "Returning to deterministic operation.\n"));
              yycompressStack (&yystack);
              break;
@@ -1850,7 +1879,6 @@ m4_define_default([b4_header_guard],
             (long) YYINDEX (yystack->yytops.yystates[yyi]));
   fprintf (stderr, "\n");
 }
-
 ]
 
 b4_epilogue
Index: tests/calc.at
--- tests/calc.at Sun, 20 Oct 2002 13:37:27 +0200 akim
+++ tests/calc.at Sun, 20 Oct 2002 17:43:08 +0200 akim
@@ -53,14 +53,21 @@ m4_define([_AT_DATA_CALC_Y],
 char *strcat(char *dest, const char *src);
 #endif
 #include <ctype.h>
+#include <assert.h>
 
 extern void perror (const char *s);
 
 /* Exercise pre-prologue dependency to %union.  */
 typedef int value_t;
 
+value_t global_result = 0;
+int global_count = 0;
+
 %}
 
+%parse-param "value_t *result", "result"
+%parse-param "int *count",      "count"
+
 /* Exercise %union. */
 %union
 {
@@ -120,12 +127,12 @@ m4_define([_AT_DATA_CALC_Y],
 %%
 input:
   line
-| input line
+| input line         { ++*count; ++global_count; }
 ;
 
 line:
   '\n'
-| exp '\n'
+| exp '\n'           { *result = global_result = $1; }
 ;
 
 exp:
@@ -134,7 +141,7 @@ exp:
   {
      if ($1 != $3)
        fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
-     $$ = $1 == $3;
+     $$ = $1;
   }
 | exp '+' exp        { $$ = $1 + $3;        }
 | exp '-' exp        { $$ = $1 - $3;        }
@@ -286,6 +293,8 @@ exp:
 int
 main (int argc, const char **argv)
 {
+  value_t result = 0;
+  int count = 0;
   yyin = NULL;
 
   if (argc == 2)
@@ -302,7 +311,10 @@ exp:
 #if YYDEBUG
   yydebug = 1;
 #endif
-  yyparse ();
+  yyparse (&result, &count);
+  assert (global_result == result);
+  assert (global_count  == count);
+
   return 0;
 }
 ]])
@@ -435,7 +447,8 @@ m4_define([AT_CHECK_CALC],
 1 - (2 - 3) = 2
 
 2^2^3 = 256
-(2^2)^3 = 64], [486])
+(2^2)^3 = 64],
+               [486])
 
 # Some parse errors.
 _AT_CHECK_CALC_ERROR([$1], [0 0], [11],
@@ -462,11 +475,6 @@ m4_define([AT_CHECK_CALC],
 [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
 1.15-1.16: parse error, unexpected "number"
 calc: error: 0 != 1])
-
-# Add a studid example demonstrating that Bison can further improve the
-# error message.  FIXME: Fix this ridiculous message.
-_AT_CHECK_CALC_ERROR([$1], [()], [21],
-[1.2-1.3: parse error, unexpected ')', expecting "number" or '-' or '('])
 
 AT_CLEANUP
 ])# AT_CHECK_CALC




reply via email to

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