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.
This commit is contained in:
Akim Demaille
2020-04-04 19:35:33 +02:00
parent 4e3c06b0f8
commit 225a67321b
3 changed files with 27 additions and 15 deletions

30
NEWS
View File

@@ -39,20 +39,30 @@ GNU Bison NEWS
int int
yyreport_syntax_error (const yypcontext_t *ctx) yyreport_syntax_error (const yypcontext_t *ctx)
{ {
enum { ARGMAX = 10 }; int res = 0;
int arg[ARGMAX];
int n = yy_syntax_error_arguments (ctx, arg, ARGMAX);
if (n == -2)
return 2; // Memory exhausted.
YY_LOCATION_PRINT (stderr, *yypcontext_location (ctx)); YY_LOCATION_PRINT (stderr, *yypcontext_location (ctx));
fprintf (stderr, ": syntax error"); fprintf (stderr, ": syntax error");
for (int i = 1; i < n; ++i) // 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", fprintf (stderr, "%s %s",
i == 1 ? "expected" : "or", yysymbol_name (arg[i])); i == 0 ? ": expected" : " or", yysymbol_name (expected[i]));
if (n) }
fprintf (stderr, " before %s", yysymbol_name (arg[0])); // 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"); fprintf (stderr, "\n");
return 0; return res;
} }
**** Token aliases internationalization **** Token aliases internationalization

View File

@@ -287,6 +287,7 @@ yyreport_syntax_error (const yypcontext_t *ctx)
int res = 0; int res = 0;
YY_LOCATION_PRINT (stderr, *yypcontext_location (ctx)); YY_LOCATION_PRINT (stderr, *yypcontext_location (ctx));
fprintf (stderr, ": syntax error"); fprintf (stderr, ": syntax error");
// Report the tokens expected at this point.
{ {
enum { TOKENMAX = 10 }; enum { TOKENMAX = 10 };
yysymbol_type_t expected[TOKENMAX]; yysymbol_type_t expected[TOKENMAX];
@@ -299,9 +300,10 @@ yyreport_syntax_error (const yypcontext_t *ctx)
fprintf (stderr, "%s %s", fprintf (stderr, "%s %s",
i == 0 ? ": expected" : " or", yysymbol_name (expected[i])); i == 0 ? ": expected" : " or", yysymbol_name (expected[i]));
} }
// Report the unexpected token.
{ {
yysymbol_type_t lookahead = yypcontext_token (ctx); yysymbol_type_t lookahead = yypcontext_token (ctx);
if (lookahead != YYEMPTY) if (lookahead != YYSYMBOL_YYEMPTY)
fprintf (stderr, " before %s", yysymbol_name (lookahead)); fprintf (stderr, " before %s", yysymbol_name (lookahead));
} }
fprintf (stderr, "\n"); fprintf (stderr, "\n");

View File

@@ -634,7 +634,7 @@ yyreport_syntax_error (const yypcontext_t *ctx]AT_PARAM_IF([, AT_PARSE_PARAMS])[
fprintf (stderr, "syntax error"); fprintf (stderr, "syntax error");
{ {
yysymbol_type_t la = yypcontext_token (ctx); yysymbol_type_t la = yypcontext_token (ctx);
if (la != YYEMPTY) if (la != YYSYMBOL_YYEMPTY)
fprintf (stderr, " on token [%s]", yysymbol_name (la)); 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]; yysymbol_type_t expected[TOKENMAX];
int n = yyexpected_tokens (ctx, expected, TOKENMAX); int n = yyexpected_tokens (ctx, expected, TOKENMAX);
/* Forward errors to yyparse. */ /* Forward errors to yyparse. */
if (n < 0) if (n <= 0)
res = n; res = n;
else if (0 < n) else
{ {
fprintf (stderr, " (expected:"); fprintf (stderr, " (expected:");
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)