[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/7] yacc.c: simplify yypull_parse
From: |
Akim Demaille |
Subject: |
[PATCH 1/7] yacc.c: simplify yypull_parse |
Date: |
Sun, 28 Jun 2020 16:52:14 +0200 |
Currently yypull_parse takes a yypstate* as argument, and accepts it
to be NULL. This does not seem to make a lot of sense: rather it is
its callers that should do that.
I believe this is historical: yypull_parse was introduced
first (c3d503425f8014b432601a33b3398446d63b5963), with yyparse being a
macro. So yyparse could hardly deal with memory allocation properly.
In 7172e23e8ffb95b8cafee24c4f36c46ca709507f that yyparse was turned
into a genuine function. At that point, it should have allocated its
own yypstate*, which would have left yypull_parse deal with only one
single non-null ypstate* argument.
Fortunately, it is nowhere documented that it is valid to pass NULL to
yypull_parse. It is now forbidden.
* data/skeletons/yacc.c (yypull_parse): Don't allocate a yypstate.
Needs a location to issue the error message.
(yyparse): Allocate the yypstate.
---
data/skeletons/yacc.c | 36 +++++++++++++++++-------------------
1 file changed, 17 insertions(+), 19 deletions(-)
diff --git a/data/skeletons/yacc.c b/data/skeletons/yacc.c
index a9c84a33..b763332f 100644
--- a/data/skeletons/yacc.c
+++ b/data/skeletons/yacc.c
@@ -1444,36 +1444,34 @@ yysyntax_error (YYPTRDIFF_T *yymsg_alloc, char **yymsg,
int
yyparse (]m4_ifset([b4_parse_param], [b4_formals(b4_parse_param)], [void])[)
{
- return yypull_parse (YY_NULLPTR]b4_user_args[);
+ yypstate *yyps = yypstate_new ();
+ if (!yyps)
+ {]b4_pure_if([b4_locations_if([[
+ static YYLTYPE yyloc_default][]b4_yyloc_default[;
+ YYLTYPE yylloc = yyloc_default;]])[
+ yyerror (]b4_yyerror_args[YY_("memory exhausted"));]], [[
+ if (!yypstate_allocated)
+ yyerror (]b4_yyerror_args[YY_("memory exhausted"));]])[
+ return 2;
+ }
+ int yystatus = yypull_parse (yyps]b4_user_args[);
+ if (!yyps)
+ yypstate_delete (yyps);
+ return yystatus;
}
int
yypull_parse (yypstate *yyps]b4_user_formals[)
-{]b4_pure_if([b4_locations_if([[
+{
+ YY_ASSERT (yyps);]b4_pure_if([b4_locations_if([[
static YYLTYPE yyloc_default][]b4_yyloc_default[;
YYLTYPE yylloc = yyloc_default;]])])[
- yypstate *yyps_local;
- if (yyps)
- yyps_local = yyps;
- else
- {
- yyps_local = yypstate_new ();
- if (!yyps_local)
- {]b4_pure_if([[
- yyerror (]b4_yyerror_args[YY_("memory exhausted"));]], [[
- if (!yypstate_allocated)
- yyerror (]b4_yyerror_args[YY_("memory exhausted"));]])[
- return 2;
- }
- }
int yystatus;
do {
]b4_pure_if([[ YYSTYPE yylval;
int ]])[yychar = ]b4_lex[;
- yystatus = yypush_parse (yyps_local]b4_pure_if([[, yychar,
&yylval]b4_locations_if([[, &yylloc]])])m4_ifset([b4_parse_param], [,
b4_args(b4_parse_param)])[);
+ yystatus = yypush_parse (yyps]b4_pure_if([[, yychar,
&yylval]b4_locations_if([[, &yylloc]])])m4_ifset([b4_parse_param], [,
b4_args(b4_parse_param)])[);
} while (yystatus == YYPUSH_MORE);
- if (!yyps)
- yypstate_delete (yyps_local);
return yystatus;
}]])[
--
2.27.0
- [PATCH 0/7] RFC: yypstate_clear (ATTENTION push parser users), Akim Demaille, 2020/06/28
- [PATCH 1/7] yacc.c: simplify yypull_parse,
Akim Demaille <=
- [PATCH 2/7] yacc.c: style changes in push mode, Akim Demaille, 2020/06/28
- [PATCH 3/7] yacc.c: declare and initialize and the same time, Akim Demaille, 2020/06/28
- [PATCH 4/7] regen, Akim Demaille, 2020/06/28
- [PATCH 5/7] yacc.c: simplify initialization of push parsers, Akim Demaille, 2020/06/28
- [PATCH 6/7] bistromathic: don't display undefined locations, Akim Demaille, 2020/06/28
- [PATCH 7/7] yacc.c: push: don't clear the parser state when accepting/rejecting, Akim Demaille, 2020/06/28