From d9a9b054ae1821e0f01fa876180961f5b2fa05bd Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 29 Apr 2020 07:58:51 +0200 Subject: [PATCH] all: fix the interface of yyexpected_tokens The user gives yyexpected_tokens a limit: the max number of tokens she wants to hear about. That's because an error message that reports a bazillion of possible tokens is useless. In that case yyexpected_tokens returned 0, so the user would not know if there are too many expected tokens or none (yes, that's possible). There are several ways to tell the user in which situation she's in: - return some E2MANY, a negative value. Then it makes the pattern int argsize = yypcontext_expected_tokens (ctx, arg, ARGS_MAX); if (argsize < 0) return argsize; no longer valid, as for E2MANY (i) the user must generate the error message anyway, and (ii) she should not return E2MANY - return ARGS_MAX + 1. Then it makes it dangerous for the user, as she has to iterate update `min (ARGS_MAX, argsize)`. Returning 0 is definitely simpler and safer for the user, as it tells her "this is not an error, just generate your message without a list of expecting tokens". So let's still return 0, but set arg[0] to the empty token when the list is really empty. * data/skeletons/glr.c, data/skeletons/lalr1.cc, data/skeletons/lalr1.java * data/skeletons/yacc.c (yyexpected_tokens): Put the empty symbol first if there are no possible tokens at all. * examples/c/bistromathic/parse.y: Demonstrate how to use that. --- data/skeletons/glr.c | 2 ++ data/skeletons/lalr1.cc | 2 ++ data/skeletons/lalr1.java | 2 ++ data/skeletons/yacc.c | 2 ++ doc/bison.texi | 23 ++++++++++++++--------- examples/c/bistromathic/bistromathic.test | 2 +- examples/c/bistromathic/parse.y | 9 ++++++--- 7 files changed, 29 insertions(+), 13 deletions(-) diff --git a/data/skeletons/glr.c b/data/skeletons/glr.c index ab3a0718..c5074a3e 100644 --- a/data/skeletons/glr.c +++ b/data/skeletons/glr.c @@ -2119,6 +2119,8 @@ yypcontext_expected_tokens (const yyGLRStack* yystackp, yyarg[yycount++] = YY_CAST (yysymbol_kind_t, yyx); } } + if (yyarg && yycount == 0 && 0 < yyargn) + yyarg[0] = ]b4_symbol(-2, kind)[; return yycount; }]])[ diff --git a/data/skeletons/lalr1.cc b/data/skeletons/lalr1.cc index 2b27088f..e6da4d81 100644 --- a/data/skeletons/lalr1.cc +++ b/data/skeletons/lalr1.cc @@ -1326,6 +1326,8 @@ b4_dollar_popdef])[]dnl } } ]])[ + if (yyarg && yycount == 0 && 0 < yyargn) + yyarg[0] = symbol_kind::]b4_symbol(-2, kind)[; return yycount; } diff --git a/data/skeletons/lalr1.java b/data/skeletons/lalr1.java index 1baa497d..ce29a3f3 100644 --- a/data/skeletons/lalr1.java +++ b/data/skeletons/lalr1.java @@ -944,6 +944,8 @@ b4_dollar_popdef[]dnl yyarg[yycount++] = SymbolKind.get (yyx); } } + if (yyarg != null && yycount == yyoffset && yyoffset < yyargn) + yyarg[yycount] = null; return yycount - yyoffset; } } diff --git a/data/skeletons/yacc.c b/data/skeletons/yacc.c index ef65287f..a790f6fa 100644 --- a/data/skeletons/yacc.c +++ b/data/skeletons/yacc.c @@ -1158,6 +1158,8 @@ yypcontext_expected_tokens (const yypcontext_t *yyctx, yyarg[yycount++] = YY_CAST (yysymbol_kind_t, yyx); } }]])[ + if (yyarg && yycount == 0 && 0 < yyargn) + yyarg[0] = ]b4_symbol(-2, kind)[; return yycount; } diff --git a/doc/bison.texi b/doc/bison.texi index aeb176a4..fd497d2d 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -7551,9 +7551,12 @@ Fill @var{argv} with the expected tokens, which never includes @code{YYSYMBOL_YYUNDEF}. Never put more than @var{argc} elements into @var{argv}, and on success -return the effective number of tokens stored in @var{argv}. Return 0 if -there are more than @var{argc} expected tokens, yet fill @var{argv} up to -@var{argc}. When LAC is enabled, may return a negative number on errors, +return the number of tokens stored in @var{argv}. If there are more +expected tokens than @var{argc}, fill @var{argv} up to @var{argc} and return +0. If there are no expected tokens, also return 0, but set @code{argv[0]} +to @code{YYSYMBOL_YYEMPTY}. + +When LAC is enabled, may return a negative number on errors, such as @code{YYENOMEM} on memory exhaustion. If @var{argv} is null, return the size needed to store all the possible @@ -12137,9 +12140,10 @@ Fill @var{argv} with the expected tokens, which never includes @code{symbol_kind::S_YYUNDEF}. Never put more than @var{argc} elements into @var{argv}, and on success -return the effective number of tokens stored in @var{argv}. Return 0 if -there are more than @var{argc} expected tokens, yet fill @var{argv} up to -@var{argc}. +return the number of tokens stored in @var{argv}. If there are more +expected tokens than @var{argc}, fill @var{argv} up to @var{argc} and return +0. If there are no expected tokens, also return 0, but set @code{argv[0]} +to @code{symbol_kind::S_YYEMPTY}. If @var{argv} is null, return the size needed to store all the possible values, which is always less than @code{YYNTOKENS}. @@ -13298,9 +13302,10 @@ Fill @var{argv} with the expected tokens, which never includes @code{SymbolKind.S_YYERROR}, or @code{SymbolKind.S_YYUNDEF}. Never put more than @var{argc} elements into @var{argv}, and on success -return the effective number of tokens stored in @var{argv}. Return 0 if -there are more than @var{argc} expected tokens, yet fill @var{argv} up to -@var{argc}. +return the number of tokens stored in @var{argv}. If there are more +expected tokens than @var{argc}, fill @var{argv} up to @var{argc} and return +0. If there are no expected tokens, also return 0, but set @code{argv[0]} +to @code{null}. If @var{argv} is null, return the size needed to store all the possible values, which is always less than @code{YYNTOKENS}. diff --git a/examples/c/bistromathic/bistromathic.test b/examples/c/bistromathic/bistromathic.test index 3bb83b77..495146bd 100755 --- a/examples/c/bistromathic/bistromathic.test +++ b/examples/c/bistromathic/bistromathic.test @@ -96,7 +96,7 @@ cat >input < * > '' -err: 1.1: syntax error: expected end of file or - or ( or exit or number or function or variable before *' +err: 1.1: syntax error: expected end of file or - or ( or exit or number or function etc., before *' cat >input <