From 44ac18d13681e05f38626427783bb5aad7d08020 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 8 Mar 2020 17:31:28 +0100 Subject: 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). --- examples/c/bistromathic/parse.y | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y index b91fd094..45bcfd62 100644 --- a/examples/c/bistromathic/parse.y +++ b/examples/c/bistromathic/parse.y @@ -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 (); -- cgit v1.2.1