yacc.c: yypstate_expected_tokens

In push parsers, when asking for the list of expected tokens at some
point, it makes no sense to build a yyparse_context_t: the yypstate
alone suffices (the only difference being the lookahead).  Instead of
forcing the user to build a useless shell around yypstate, let's offer
yypstate_expected_tokens.

See https://lists.gnu.org/r/bison-patches/2020-03/msg00025.html.

* data/skeletons/yacc.c (yypstate): Declare earlier, so that we can
use it for...
(yypstate_expected_tokens): this new function, when in push parsers.
Adjust dependencies.
* examples/c/bistromathic/parse.y: Simplify: use
yypstate_expected_tokens.
Style fixes.
Reduce scopes (reported by Joel E. Denny).
This commit is contained in:
Akim Demaille
2020-03-08 17:31:28 +01:00
parent 0c3dd3a669
commit 44ac18d136
3 changed files with 61 additions and 34 deletions

View File

@@ -332,24 +332,23 @@ int
expected_tokens (const char *input,
int *tokens, int ntokens)
{
YYDPRINTF ((stderr, "expected_tokens(\"%s\")", input));
YYDPRINTF ((stderr, "expected_tokens (\"%s\")", input));
// Parse the current state of the line.
YYLTYPE lloc;
yypstate *ps = yypstate_new ();
int status = 0;
do {
YYLTYPE lloc;
YYSTYPE lval;
int token = yylex (&input, &lval, &lloc);
// Don't let the parse know when we reach the end of input.
if (!token)
break;
status = yypush_parse (ps, token, &lval, &lloc);
} while (status == YYPUSH_MORE);
// Then query for the accepted tokens at this point.
yyparse_context_t yyctx
= {ps->yyssp, YYEMPTY, &lloc, ps->yyesa, &ps->yyes, &ps->yyes_capacity};
int res = yyexpected_tokens (&yyctx, tokens, ntokens);
int res = yypstate_expected_tokens (ps, tokens, ntokens);
yypstate_delete (ps);
return res;
}
@@ -362,7 +361,7 @@ expected_tokens (const char *input,
char **
completion (const char *text, int start, int end)
{
YYDPRINTF ((stderr, "completion(\"%.*s[%.*s]%s\")\n",
YYDPRINTF ((stderr, "completion (\"%.*s[%.*s]%s\")\n",
start, rl_line_buffer,
end - start, rl_line_buffer + start,
rl_line_buffer + end));
@@ -414,7 +413,7 @@ completion (const char *text, int start, int end)
if (yydebug)
{
fprintf (stderr, "completion(\"%.*s[%.*s]%s\") = ",
fprintf (stderr, "completion (\"%.*s[%.*s]%s\") = ",
start, rl_line_buffer,
end - start, rl_line_buffer + start,
rl_line_buffer + end);
@@ -452,7 +451,7 @@ void init_readline (void)
int main (int argc, char const* argv[])
{
// Enable parse traces on option -p.
if (argc == 2 && strcmp(argv[1], "-p") == 0)
if (argc == 2 && strcmp (argv[1], "-p") == 0)
yydebug = 1;
init_table ();
init_readline ();