yacc.c: use negative numbers for errors in auxiliary functions

yyparse returns 0, 1, 2 since ages (accept, reject, memory exhausted).
Some of our auxiliary functions such as yy_lac and
yyreport_syntax_error also need to return error codes and also use 0,
1, 2.  Because it uses yy_lac, yyexpected_tokens also needs to return
"problem", "memory exhausted", but in case of success, it needs to
return the number of tokens, so it cannot use 1 and 2 as error code.
Currently it uses -1 and -2, which is later converted into 1 and 2 as
yacc.c expects it.

Let's simplify this and use consistently -1 and -2 for auxiliary
functions that are not exposed (or not yet exposed) to the user.  In
particular this will save the user from having to convert
yyexpected_tokens's -2 into yyreport_syntax_error's 2: both return -1
or -2.

* data/skeletons/yacc.c (yy_lac, yyreport_syntax_error)
(yy_lac_stack_realloc): Return -1, -2 for errors instead of 1, 2.
Adjust callers.
* examples/c/bistromathic/parse.y (yyreport_syntax_error): Do take
error codes into account.
Issue a syntax error message even if we ran out of memory.
* src/parse-gram.y, tests/local.at (yyreport_syntax_error): Adjust.
This commit is contained in:
Akim Demaille
2020-03-21 08:39:31 +01:00
parent 1079595b2a
commit 84b1972c96
4 changed files with 36 additions and 29 deletions

View File

@@ -285,19 +285,24 @@ int
yyreport_syntax_error (const yyparse_context_t *ctx)
{
enum { ARGMAX = 10 };
int res = 0;
int arg[ARGMAX];
int n = yysyntax_error_arguments (ctx, arg, ARGMAX);
if (n == -2)
return 2;
if (n < 0)
// Forward errors to yyparse.
res = n;
YY_LOCATION_PRINT (stderr, *yyparse_context_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]));
if (n >= 0)
{
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]));
}
fprintf (stderr, "\n");
return 0;
return res;
}
// Called by yyparse on error.