bison-patches
[Top][All Lists]
Advanced

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

Proposal for lambda'ing error recovery


From: akim
Subject: Proposal for lambda'ing error recovery
Date: Sat, 16 Nov 2002 15:02:10 +0100
User-agent: Mutt/1.3.28i

This patch is going into the direction I'd like yacc.c to take: more
functions, and less inlined code.  But it means that we need to 
pass some variable addresses, hence they no longer can be register'd.
I dobt very much that this can have a significant influence.  Any
way, this is a simple but painful functionification of the error
recovery.  The next step will be to do as we do in glr.c, imho.


Index: ChangeLog
from  Akim Demaille  <address@hidden>

        * data/yacc.c (YYPPOPSTACK, yy_status_type)
        (yy_recover_parse_error): New.
        (yyparse): Instead of inlined code for error recovery, use
        yy_recover_parse_error.  This requires that yyssp and yyvsp no
        longer be `register'.

Index: data/yacc.c
--- data/yacc.c Sat, 16 Nov 2002 13:34:09 +0100 akim
+++ data/yacc.c Sat, 16 Nov 2002 14:55:27 +0100 akim
@@ -497,6 +497,11 @@ m4_define([b4_rhs_location],
 #define YYTERROR       1
 #define YYERRCODE      256
 
+#define YYPOPSTACK   (yyvsp--, yyssp--]b4_location_if([, yylsp--])[)
+
+#define YYPPOPSTACK   \
+   ((*yyvspp)--, (*yysspp)--]b4_location_if([, (*yylspp)--])[)
+
 /* YYLLOC_DEFAULT -- Compute the default location (before the actions
    are run).  */
 
@@ -547,9 +552,10 @@ m4_define([b4_rhs_location],
     }                                                          \
 } while (0)
 
+
 /*------------------------------------------------------------------.
 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
-| TOP (cinluded).                                                   |
+| TOP (included).                                                   |
 `------------------------------------------------------------------*/
 
 ]b4_c_function_def([yy_stack_print], [static void],
@@ -683,7 +689,101 @@ m4_define([b4_rhs_location],
 #if YYDEBUG
 ]b4_yysymprint_generate([b4_c_function_def])[
 #endif /* ! YYDEBUG */
-]b4_yydestruct_generate([b4_c_function_def])
+]b4_yydestruct_generate([b4_c_function_def])[
+
+
+/*--------------------------------------------------------------------.
+| yy_recover_parse_error -- Recover from a syntax error on YYSTACK,   |
+| assuming that YYTOKENP, YYLVALP, and YYLLOCP point to the           |
+| syntactic category, semantic value, and location of the lookahead.  |
+`--------------------------------------------------------------------*/
+
+typedef enum {
+  yy_ok,
+  yy_abort,
+  yy_accept
+} yy_status_type;
+
+]b4_c_function_def([yy_recover_parse_error], [static yy_status_type],
+                   [[int *yystatep], [yystatep]],
+                   [[int *yyerrstatusp], [yyerrstatusp]],
+                   [[short *yyss],       [yyss]],
+                   [[int *yytokenp],     [yytokenp]],
+                   [[short **yysspp],    [yysspp]],
+                   [[YYSTYPE *yylvalp],  [yylvalp]],
+                   [[YYSTYPE **yyvspp],  [yyvspp]]b4_location_if([,
+                   [[YYLTYPE *yyllocp],  [yyllocp]],
+                   [[YYLTYPE **yylspp],  [yylspp]]]))[
+{
+  int yyn;
+  if (*yyerrstatusp == 3)
+    {
+      /* If just tried and failed to reuse lookahead token after an
+        error, discard it.  */
+
+      /* Return failure if at end of input.  */
+      if (*yytokenp == YYEOF)
+        {
+         /* Pop the error token.  */
+          YYPPOPSTACK;
+         /* Pop the rest of the stack.  */
+         while (yyss < *yysspp)
+           {
+             YYDSYMPRINTF ("Error: popping",
+                           yystos[**yysspp], *yyvspp, *yylspp);
+             yydestruct (yystos[**yysspp], *yyvspp]b4_location_if([, 
*yylspp])[);
+             YYPPOPSTACK;
+           }
+         return yy_abort;
+        }
+
+      YYDSYMPRINTF ("Error: discarding", *yytokenp, yylvalp, yyllocp);
+      yydestruct (*yytokenp, yylvalp]b4_location_if([, yyllocp])[);
+      *yytokenp = YYEMPTY;
+    }
+
+  /* Else will try to reuse lookahead token after shifting the error
+     token.  */
+
+  *yyerrstatusp = 3;   /* Each real token shifted decrements this.  */
+
+  for (;;)
+    {
+      yyn = yypact[*yystatep];
+      if (yyn != YYPACT_NINF)
+       {
+         yyn += YYTERROR;
+         if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
+           {
+             yyn = yytable[yyn];
+             if (0 < yyn)
+               break;
+           }
+       }
+
+      /* Pop the current state because it cannot handle the error token.  */
+      if (*yysspp == yyss)
+       return yy_abort;
+
+      YYDSYMPRINTF ("Error: popping", yystos[**yysspp], *yyvspp, *yylspp);
+      yydestruct (yystos[*yystatep], *yyvspp]b4_location_if([, *yylspp])[);
+      YYPPOPSTACK;
+      *yystatep = **yysspp;
+      YY_STACK_PRINT (yyss, *yysspp);
+    }
+
+  if (yyn == YYFINAL)
+    return yy_accept;
+
+  YYDPRINTF ((stderr, "Shifting error token, "));
+
+  *++(*yyvspp) = *yylvalp;
+]b4_location_if([  *++(*yylspp) = *yyllocp;])[
+
+  *yystatep = yyn;
+  return yy_ok;
+}
+
 
 
 /* Prevent warnings from -Wmissing-prototypes.  */
@@ -695,11 +795,11 @@ m4_define([b4_rhs_location],
 int yyparse ();
 # endif
 #else /* ! YYPARSE_PARAM */
-b4_c_function_decl([yyparse], [int], b4_parse_param)
+]b4_c_function_decl([yyparse], [int], b4_parse_param)[
 #endif /* ! YYPARSE_PARAM */
 
 
-m4_divert_push([KILL])# ======================== M4 code.
+]m4_divert_push([KILL])# ======================== M4 code.
 # b4_declare_parser_variables
 # ---------------------------
 # Declare the variables that are global, or local to YYPARSE if
@@ -719,7 +819,7 @@ m4_define([b4_declare_parser_variables],
 m4_divert_pop([KILL])dnl# ====================== End of M4 code.
 
 b4_pure_if([],
-           [b4_declare_parser_variables])
+           [b4_declare_parser_variables])[
 
 
 /*----------.
@@ -734,9 +834,9 @@ m4_define([b4_declare_parser_variables],
   void *YYPARSE_PARAM;
 # endif
 #else /* ! YYPARSE_PARAM */
-b4_c_function_def([yyparse], [int], b4_parse_param)
+]b4_c_function_def([yyparse], [int], b4_parse_param)[
 #endif
-{[
+{
   ]b4_pure_if([b4_declare_parser_variables])[
   register int yystate;
   register int yyn;
@@ -757,12 +857,12 @@ m4_define([b4_declare_parser_variables],
   /* The state stack.  */
   short        yyssa[YYINITDEPTH];
   short *yyss = yyssa;
-  register short *yyssp;
+  short *yyssp;
 
   /* The semantic value stack.  */
   YYSTYPE yyvsa[YYINITDEPTH];
   YYSTYPE *yyvs = yyvsa;
-  register YYSTYPE *yyvsp;
+  YYSTYPE *yyvsp;
 
 ]b4_location_if(
 [[  /* The location stack.  */
@@ -770,8 +870,6 @@ m4_define([b4_declare_parser_variables],
   YYLTYPE *yyls = yylsa;
   YYLTYPE *yylsp;]])[
 
-#define YYPOPSTACK   (yyvsp--, yyssp--]b4_location_if([, yylsp--])[)
-
   YYSIZE_T yystacksize = YYINITDEPTH;
 
   /* The variables used to return semantic value and location from the
@@ -907,15 +1005,9 @@ yybackup:
     }
 
   if (yytoken == YYEOF)
-    {
-      YYDPRINTF ((stderr, "Now at end of input.\n"));
-    }
+    YYDPRINTF ((stderr, "Now at end of input.\n"));
   else
-    {
-      /* We have to keep this `#if YYDEBUG', since we use variables
-        which are defined only if `YYDEBUG' is set.  */
-      YYDSYMPRINTF ("Next token is", yytoken, &yylval, &yylloc);
-    }
+    YYDSYMPRINTF ("Next token is", yytoken, &yylval, &yylloc);
 
   /* If the proper action on seeing token YYTOKEN is to reduce or to
      detect an error, take that action.  */
@@ -1080,73 +1172,21 @@ yyerrlab:
 | yyerrlab1 -- error raised explicitly by an action.  |
 `----------------------------------------------------*/
 yyerrlab1:
-  if (yyerrstatus == 3)
+  switch (yy_recover_parse_error (&yystate, &yyerrstatus, yyss,
+                                 &yytoken, &yyssp,
+                                 &yylval, &yyvsp]b4_location_if([,
+                                 &yylloc, &yylsp])[))
     {
-      /* If just tried and failed to reuse lookahead token after an
-        error, discard it.  */
-
-      /* Return failure if at end of input.  */
-      if (yytoken == YYEOF)
-        {
-         /* Pop the error token.  */
-          YYPOPSTACK;
-         /* Pop the rest of the stack.  */
-         while (yyss < yyssp)
-           {
-             YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
-             yydestruct (yystos[*yyssp], yyvsp]b4_location_if([, yylsp])[);
-             YYPOPSTACK;
-           }
-         YYABORT;
-        }
-
-      YYDSYMPRINTF ("Error: discarding", yytoken, &yylval, &yylloc);
-      yydestruct (yytoken, &yylval]b4_location_if([, &yylloc])[);
-      yytoken = YYEMPTY;
-    }
-
-  /* Else will try to reuse lookahead token after shifting the error
-     token.  */
-
-  yyerrstatus = 3;     /* Each real token shifted decrements this.  */
-
-  for (;;)
-    {
-      yyn = yypact[yystate];
-      if (yyn != YYPACT_NINF)
-       {
-         yyn += YYTERROR;
-         if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
-           {
-             yyn = yytable[yyn];
-             if (0 < yyn)
-               break;
-           }
-       }
-
-      /* Pop the current state because it cannot handle the error token.  */
-      if (yyssp == yyss)
-       YYABORT;
-
-      YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
-      yydestruct (yystos[yystate], yyvsp]b4_location_if([, yylsp])[);
-      yyvsp--;
-      yystate = *--yyssp;
-]b4_location_if([      yylsp--;])[
-      YY_STACK_PRINT (yyss, yyssp);
+      case yy_accept:
+        goto yyacceptlab;
+        break;
+      case yy_abort:
+        goto yyabortlab;
+        break;
+      default:
+        break;
     }
-
-  if (yyn == YYFINAL)
-    YYACCEPT;
-
-  YYDPRINTF ((stderr, "Shifting error token, "));
-
-  *++yyvsp = yylval;
-]b4_location_if([  *++yylsp = yylloc;])[
-
-  yystate = yyn;
   goto yynewstate;
-
 
 /*-------------------------------------.
 | yyacceptlab -- YYACCEPT comes here.  |
Index: data/glr.c
--- data/glr.c Sat, 16 Nov 2002 13:34:09 +0100 akim
+++ data/glr.c Sat, 16 Nov 2002 14:59:18 +0100 akim
@@ -1646,8 +1646,7 @@ m4_define([b4_rhs_location],
       {
        if (*yytokenp == YYEOF)
          {
-           /* Now pop stack until we find a state that shifts the
-              error token.  */
+           /* Cannot recover: flush the stack.  */
            while (yystack->yytops.yystates[0] != NULL)
              {
                yyGLRState *yys = yystack->yytops.yystates[0];
@@ -1696,7 +1695,7 @@ m4_define([b4_rhs_location],
   yyremoveDeletes (yystack);
   yycompressStack (yystack);
 
-  /* Now pop stack until we find a state that shifts the error token. */
+  /* Now pop stack until we find a state that shifts the error token.  */
   while (yystack->yytops.yystates[0] != NULL)
     {
       yyGLRState *yys = yystack->yytops.yystates[0];




reply via email to

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