bison-patches
[Top][All Lists]
Advanced

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

news: update the yyreport_syntax_error example


From: Akim Demaille
Subject: news: update the yyreport_syntax_error example
Date: Sun, 5 Apr 2020 08:59:41 +0200

commit 225a67321b4672db197e97a580c8fcbe907428a6
Author: Akim Demaille <address@hidden>
Date:   Sat Apr 4 19:35:33 2020 +0200

    news: update the yyreport_syntax_error example
    
    * examples/c/bistromathic/parse.y, tests/local.at
    (yyreport_syntax_error): Fix use of YYSYMBOL_YYEMPTY.
    * NEWS: Update.

diff --git a/NEWS b/NEWS
index de8cfd0a..062efd83 100644
--- a/NEWS
+++ b/NEWS
@@ -39,20 +39,30 @@ GNU Bison NEWS
     int
     yyreport_syntax_error (const yypcontext_t *ctx)
     {
-      enum { ARGMAX = 10 };
-      int arg[ARGMAX];
-      int n = yy_syntax_error_arguments (ctx, arg, ARGMAX);
-      if (n == -2)
-        return 2; // Memory exhausted.
+      int res = 0;
       YY_LOCATION_PRINT (stderr, *yypcontext_location (ctx));
       fprintf (stderr, ": syntax error");
-      for (int i = 1; i < n; ++i)
-        fprintf (stderr, " %s %s",
-                 i == 1 ? "expected" : "or", yysymbol_name (arg[i]));
-      if (n)
-        fprintf (stderr, " before %s", yysymbol_name (arg[0]));
+      // Report the tokens expected at this point.
+      {
+        enum { TOKENMAX = 10 };
+        yysymbol_type_t expected[TOKENMAX];
+        int n = yyexpected_tokens (ctx, expected, TOKENMAX);
+        if (n < 0)
+          // Forward errors to yyparse.
+          res = n;
+        else
+          for (int i = 0; i < n; ++i)
+            fprintf (stderr, "%s %s",
+                     i == 0 ? ": expected" : " or", yysymbol_name 
(expected[i]));
+      }
+      // Report the unexpected token.
+      {
+        yysymbol_type_t lookahead = yypcontext_token (ctx);
+        if (lookahead != YYSYMBOL_YYEMPTY)
+          fprintf (stderr, " before %s", yysymbol_name (lookahead));
+      }
       fprintf (stderr, "\n");
-      return 0;
+      return res;
     }
 
 **** Token aliases internationalization
diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y
index 74f4d3f7..9b8fb76f 100644
--- a/examples/c/bistromathic/parse.y
+++ b/examples/c/bistromathic/parse.y
@@ -287,6 +287,7 @@ yyreport_syntax_error (const yypcontext_t *ctx)
   int res = 0;
   YY_LOCATION_PRINT (stderr, *yypcontext_location (ctx));
   fprintf (stderr, ": syntax error");
+  // Report the tokens expected at this point.
   {
     enum { TOKENMAX = 10 };
     yysymbol_type_t expected[TOKENMAX];
@@ -299,9 +300,10 @@ yyreport_syntax_error (const yypcontext_t *ctx)
         fprintf (stderr, "%s %s",
                  i == 0 ? ": expected" : " or", yysymbol_name (expected[i]));
   }
+  // Report the unexpected token.
   {
     yysymbol_type_t lookahead = yypcontext_token (ctx);
-    if (lookahead != YYEMPTY)
+    if (lookahead != YYSYMBOL_YYEMPTY)
       fprintf (stderr, " before %s", yysymbol_name (lookahead));
   }
   fprintf (stderr, "\n");
diff --git a/tests/local.at b/tests/local.at
index a6b0c99c..c34c2b1b 100644
--- a/tests/local.at
+++ b/tests/local.at
@@ -634,7 +634,7 @@ yyreport_syntax_error (const yypcontext_t 
*ctx]AT_PARAM_IF([, AT_PARSE_PARAMS])[
   fprintf (stderr, "syntax error");
   {
     yysymbol_type_t la = yypcontext_token (ctx);
-    if (la != YYEMPTY)
+    if (la != YYSYMBOL_YYEMPTY)
       fprintf (stderr, " on token [%s]", yysymbol_name (la));
   }
   {
@@ -642,9 +642,9 @@ yyreport_syntax_error (const yypcontext_t 
*ctx]AT_PARAM_IF([, AT_PARSE_PARAMS])[
     yysymbol_type_t expected[TOKENMAX];
     int n = yyexpected_tokens (ctx, expected, TOKENMAX);
     /* Forward errors to yyparse.  */
-    if (n < 0)
+    if (n <= 0)
       res = n;
-    else if (0 < n)
+    else
       {
         fprintf (stderr, " (expected:");
         for (int i = 0; i < n; ++i)




reply via email to

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