bison-patches
[Top][All Lists]
Advanced

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

yacc.c: Extracting yysyntax_error


From: Akim Demaille
Subject: yacc.c: Extracting yysyntax_error
Date: Fri, 16 Sep 2005 15:03:43 +0200
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux)

I have not applied the following patch for the following reasons:
- 2.1 is expected and changes now are not welcome
- this change is wrong because we return memory that might have
  been allocated by alloca

The last point is not too hard to address: simply put back the call to
error in yysyntax_error instead of leaving it outside.  I left it as
it is because that's really what I would have liked to have, to
increase the similarity between our skeletons (in addition to making
yyparse *much* more readable).

Paul, what do you think?
[ ] no
[ ] later
[ ] now with the call to error moved in yysyntax_error
    (this also makes it unnecessary to uncover yystrlen and yystpcpy).


Index: ChangeLog
from  Akim Demaille  <address@hidden>

        * data/yacc.c (yysyntax_error): New, extracted from yyparse.
        (yystrlen, yystpcpy): Define even if !YYERROR_VERBOSE.

Index: data/yacc.c
===================================================================
RCS file: /cvsroot/bison/bison/data/yacc.c,v
retrieving revision 1.103
diff -u -u -r1.103 yacc.c
--- data/yacc.c 9 Sep 2005 22:42:15 -0000 1.103
+++ data/yacc.c 16 Sep 2005 13:03:34 -0000
@@ -689,20 +689,18 @@
 
 
 
-#if YYERROR_VERBOSE
-
-# ifndef yystrlen
-#  if defined (__GLIBC__) && defined (_STRING_H)
-#   define yystrlen strlen
-#  else
+#ifndef yystrlen
+# if defined (__GLIBC__) && defined (_STRING_H)
+#  define yystrlen strlen
+# else
 /* Return the length of YYSTR.  */
 static YYSIZE_T
-#   if defined (__STDC__) || defined (__cplusplus)
+#  if defined (__STDC__) || defined (__cplusplus)
 yystrlen (const char *yystr)
-#   else
+#  else
 yystrlen (yystr)
      const char *yystr;
-#   endif
+#  endif
 {
   const char *yys = yystr;
 
@@ -711,23 +709,23 @@
 
   return yys - yystr - 1;
 }
-#  endif
 # endif
+#endif
 
-# ifndef yystpcpy
-#  if defined (__GLIBC__) && defined (_STRING_H) && defined (_GNU_SOURCE)
-#   define yystpcpy stpcpy
-#  else
+#ifndef yystpcpy
+# if defined (__GLIBC__) && defined (_STRING_H) && defined (_GNU_SOURCE)
+#  define yystpcpy stpcpy
+# else
 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
    YYDEST.  */
 static char *
-#   if defined (__STDC__) || defined (__cplusplus)
+#  if defined (__STDC__) || defined (__cplusplus)
 yystpcpy (char *yydest, const char *yysrc)
-#   else
+#  else
 yystpcpy (yydest, yysrc)
      char *yydest;
      const char *yysrc;
-#   endif
+#  endif
 {
   char *yyd = yydest;
   const char *yys = yysrc;
@@ -737,9 +735,10 @@
 
   return yyd - 1;
 }
-#  endif
 # endif
+#endif
 
+#if YYERROR_VERBOSE
 # ifndef yytnamerr
 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
    quotes and backslashes, so that it's suitable for yyerror.  The
@@ -790,6 +789,115 @@
 
 #endif /* YYERROR_VERBOSE */
 
+/* Return an error message about the unexpected token YYCHAR while
+   in state YYSTATE.  The message was allocated with YYSTACK_ALLOC,
+   and should be freed with YYSTACK_FREE.  Return 0 on memory
+   allocation failure.  */
+char *
+yysyntax_error (int yystate, int yychar)
+{
+  char *res = 0;
+#if YYERROR_VERBOSE
+  int yyn = yypact[yystate];
+
+  if (YYPACT_NINF < yyn && yyn < YYLAST)
+    {
+      int yytype = YYTRANSLATE (yychar);
+      YYSIZE_T yysize0 = yytnamerr (0, yytname[yytype]);
+      YYSIZE_T yysize = yysize0;
+      YYSIZE_T yysize1;
+      int yysize_overflow = 0;
+#     define YYERROR_VERBOSE_ARGS_MAXIMUM 5
+      char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
+      int yyx;
+
+#if 0
+      /* This is so xgettext sees the translatable formats that are
+         constructed on the fly.  */
+      YY_("syntax error, unexpected %s");
+      YY_("syntax error, unexpected %s, expecting %s");
+      YY_("syntax error, unexpected %s, expecting %s or %s");
+      YY_("syntax error, unexpected %s, expecting %s or %s or %s");
+      YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s");
+#endif
+      char *yyfmt;
+      char const *yyf;
+      static char const yyunexpected[] = "syntax error, unexpected %s";
+      static char const yyexpecting[] = ", expecting %s";
+      static char const yyor[] = " or %s";
+      char yyformat[sizeof yyunexpected
+                    + sizeof yyexpecting - 1
+                    + ((YYERROR_VERBOSE_ARGS_MAXIMUM - 2)
+                       * (sizeof yyor - 1))];
+      char const *yyprefix = yyexpecting;
+
+      /* Start YYX at -YYN if negative to avoid negative indexes in
+         YYCHECK.  */
+      int yyxbegin = yyn < 0 ? -yyn : 0;
+
+      /* Stay within bounds of both yycheck and yytname.  */
+      int yychecklim = YYLAST - yyn;
+      int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
+      int yycount = 1;
+
+      yyarg[0] = yytname[yytype];
+      yyfmt = yystpcpy (yyformat, yyunexpected);
+
+      for (yyx = yyxbegin; yyx < yyxend; ++yyx)
+        if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
+          {
+            if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
+              {
+                yycount = 1;
+                yysize = yysize0;
+                yyformat[sizeof yyunexpected - 1] = '\0';
+                break;
+              }
+            yyarg[yycount++] = yytname[yyx];
+            yysize1 = yysize + yytnamerr (0, yytname[yyx]);
+            yysize_overflow |= yysize1 < yysize;
+            yysize = yysize1;
+            yyfmt = yystpcpy (yyfmt, yyprefix);
+            yyprefix = yyor;
+          }
+
+      yyf = YY_(yyformat);
+      yysize1 = yysize + yystrlen (yyf);
+      yysize_overflow |= yysize1 < yysize;
+      yysize = yysize1;
+
+      if (!yysize_overflow && yysize <= YYSTACK_ALLOC_MAXIMUM)
+        res = (char *) YYSTACK_ALLOC (yysize);
+      if (res)
+        {
+          /* Avoid sprintf, as that infringes on the user's name space.
+             Don't have undefined behavior even if the translation
+             produced a string with the wrong number of "%s"s.  */
+          char *yyp = res;
+          int yyi = 0;
+          while ((*yyp = *yyf))
+            {
+              if (*yyp == '%' && yyf[1] == 's' && yyi < yycount)
+                {
+                  yyp += yytnamerr (yyp, yyarg[yyi++]);
+                  yyf += 2;
+                }
+              else
+                {
+                  yyp++;
+                  yyf++;
+                }
+            }
+        }
+    }
+  else
+#endif /* YYERROR_VERBOSE */
+    {
+      res = YYSTACK_ALLOC (yystrlen (YY_("syntax error")) + 1);
+      yystpcpy (res, YY_("syntax error"));
+    }
+  return res;
+}
 
 
 #if YYDEBUG
@@ -1158,110 +1266,17 @@
   if (!yyerrstatus)
     {
       ++yynerrs;
-#if YYERROR_VERBOSE
-      yyn = yypact[yystate];
-
-      if (YYPACT_NINF < yyn && yyn < YYLAST)
-       {
-         int yytype = YYTRANSLATE (yychar);
-         YYSIZE_T yysize0 = yytnamerr (0, yytname[yytype]);
-         YYSIZE_T yysize = yysize0;
-         YYSIZE_T yysize1;
-         int yysize_overflow = 0;
-         char *yymsg = 0;
-#        define YYERROR_VERBOSE_ARGS_MAXIMUM 5
-         char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
-         int yyx;
-
-#if 0
-         /* This is so xgettext sees the translatable formats that are
-            constructed on the fly.  */
-         YY_("syntax error, unexpected %s");
-         YY_("syntax error, unexpected %s, expecting %s");
-         YY_("syntax error, unexpected %s, expecting %s or %s");
-         YY_("syntax error, unexpected %s, expecting %s or %s or %s");
-         YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s");
-#endif
-         char *yyfmt;
-         char const *yyf;
-         static char const yyunexpected[] = "syntax error, unexpected %s";
-         static char const yyexpecting[] = ", expecting %s";
-         static char const yyor[] = " or %s";
-         char yyformat[sizeof yyunexpected
-                       + sizeof yyexpecting - 1
-                       + ((YYERROR_VERBOSE_ARGS_MAXIMUM - 2)
-                          * (sizeof yyor - 1))];
-         char const *yyprefix = yyexpecting;
-
-         /* Start YYX at -YYN if negative to avoid negative indexes in
-            YYCHECK.  */
-         int yyxbegin = yyn < 0 ? -yyn : 0;
-
-         /* Stay within bounds of both yycheck and yytname.  */
-         int yychecklim = YYLAST - yyn;
-         int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
-         int yycount = 1;
-
-         yyarg[0] = yytname[yytype];
-         yyfmt = yystpcpy (yyformat, yyunexpected);
-
-         for (yyx = yyxbegin; yyx < yyxend; ++yyx)
-           if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
-             {
-               if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
-                 {
-                   yycount = 1;
-                   yysize = yysize0;
-                   yyformat[sizeof yyunexpected - 1] = '\0';
-                   break;
-                 }
-               yyarg[yycount++] = yytname[yyx];
-               yysize1 = yysize + yytnamerr (0, yytname[yyx]);
-               yysize_overflow |= yysize1 < yysize;
-               yysize = yysize1;
-               yyfmt = yystpcpy (yyfmt, yyprefix);
-               yyprefix = yyor;
-             }
-
-         yyf = YY_(yyformat);
-         yysize1 = yysize + yystrlen (yyf);
-         yysize_overflow |= yysize1 < yysize;
-         yysize = yysize1;
-
-         if (!yysize_overflow && yysize <= YYSTACK_ALLOC_MAXIMUM)
-           yymsg = (char *) YYSTACK_ALLOC (yysize);
-         if (yymsg)
-           {
-             /* Avoid sprintf, as that infringes on the user's name space.
-                Don't have undefined behavior even if the translation
-                produced a string with the wrong number of "%s"s.  */
-             char *yyp = yymsg;
-             int yyi = 0;
-             while ((*yyp = *yyf))
-               {
-                 if (*yyp == '%' && yyf[1] == 's' && yyi < yycount)
-                   {
-                     yyp += yytnamerr (yyp, yyarg[yyi++]);
-                     yyf += 2;
-                   }
-                 else
-                   {
-                     yyp++;
-                     yyf++;
-                   }
-               }
-             yyerror (]b4_yyerror_args[yymsg);
-             YYSTACK_FREE (yymsg);
-           }
-         else
-           {
-             yyerror (]b4_yyerror_args[YY_("syntax error"));
-             goto yyexhaustedlab;
-           }
-       }
+      char *yymsg = yysyntax_error (yystate, yychar);
+      if (yymsg)
+        {
+          yyerror (]b4_yyerror_args[yymsg);
+          YYSTACK_FREE (yymsg);
+        }
       else
-#endif /* YYERROR_VERBOSE */
-       yyerror (]b4_yyerror_args[YY_("syntax error"));
+        {
+          yyerror (]b4_yyerror_args[YY_("syntax error"));
+          goto yyexhaustedlab;
+        }
     }
 
 ]b4_location_if([[  yyerror_range[0] = yylloc;]])[





reply via email to

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