[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/6] glr.c: log the execution of deferred actions
From: |
Akim Demaille |
Subject: |
[PATCH 4/6] glr.c: log the execution of deferred actions |
Date: |
Sat, 2 Jan 2021 16:30:29 +0100 |
Currently deferred reductions are not "verbose" at all: only immediate
reductions are displayed in the YYDEBUG traces. I don't understand
why. Besides it seems actually simpler the install the reduction
traces right around the user action inside yyuserAction rather that
around calls to yyuserAction.
This only trouble is that yyuserAction does not know the stack number
it works on, so we have to pass it. And pass -1 when we are actually
running on a temporary stack.
The glr example, on "T(x) + y;" as input, adds these logs, which
allow to see when the `<cast>` is built:
Stack 0 Entering state 26
Reduced stack 0 by rule 7 (line 108); action deferred. Now in state 7.
Stack 0 Entering state 7
Reading a token
Next token is token '+' (1.6: )
Stack 1 Entering state 27
Reduced stack 1 by rule 13 (line 123); action deferred. Now in state 12.
Stack 1 Entering state 12
Next token is token '+' (1.6: )
Stack 1 dies.
Removing dead stacks.
On stack 0, shifting token '+' (1.6: )
Stack 0 now in state #14
+Reducing stack -1 by rule 6 (line 107):
+ $1 = token identifier (1.3: x)
+-> $$ = nterm expr (1.3: x)
+Reducing stack -1 by rule 7 (line 108):
+ $1 = token typename (1.0: T)
+ $2 = token '(' (1.2: )
+ $3 = nterm expr (1.3: x)
+ $4 = token ')' (1.4: )
+-> $$ = nterm expr (1.0-3: <cast>(x,T))
Returning to deterministic operation.
* data/skeletons/glr.c (yyuserAction): Take yyk as a new argument.
Rename argument yyn as yyrule for clarity.
Log before and after the user action.
Adjust callers to not call YY_REDUCE_PRINT and YY_SYMBOL_PRINT.
---
NEWS | 5 +++++
data/skeletons/glr.c | 28 +++++++++++++++-------------
2 files changed, 20 insertions(+), 13 deletions(-)
diff --git a/NEWS b/NEWS
index 61534bc0..e12ba6fa 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,11 @@ GNU Bison NEWS
Prefer value_type to semantic_type to denote the semantic value type,
specified by the `api.value.type` %define variable.
+*** GLR traces
+
+ There were not debug traces for deferred calls to user actions. They are
+ logged now.
+
** New features
*** Option -H, --header and directive %header
diff --git a/data/skeletons/glr.c b/data/skeletons/glr.c
index a39ebead..598b4464 100644
--- a/data/skeletons/glr.c
+++ b/data/skeletons/glr.c
@@ -900,14 +900,15 @@ yyfill (yyGLRStackItem *yyvsp, int *yylow, int yylow1,
yybool yynormal)
* (@@$). Returns yyok for normal return, yyaccept for YYACCEPT,
* yyerr for YYERROR, yyabort for YYABORT. */
static YYRESULTTAG
-yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem* yyvsp,
- yyGLRStack* yystackp,
+yyuserAction (yyRuleNum yyrule, int yyrhslen, yyGLRStackItem* yyvsp,
+ yyGLRStack* yystackp, YYPTRDIFF_T yyk,
YYSTYPE* yyvalp]b4_locuser_formals[)
{
- yybool yynormal YY_ATTRIBUTE_UNUSED = yystackp->yysplitPoint == YY_NULLPTR;
+ const yybool yynormal YY_ATTRIBUTE_UNUSED = yystackp->yysplitPoint ==
YY_NULLPTR;
int yylow = 1;
]b4_parse_param_use([yyvalp], [yylocp])dnl
-[ YYUSE (yyrhslen);
+[ YYUSE (yyk);
+ YYUSE (yyrhslen);
# undef yyerrok
# define yyerrok (yystackp->yyerrState = 0)
# undef YYACCEPT
@@ -933,14 +934,17 @@ yyuserAction (yyRuleNum yyn, int yyrhslen,
yyGLRStackItem* yyvsp,
*yyvalp = yyvsp[YYFILL
(1-yyrhslen)].yystate.yysemantics.yyval;]b4_locations_if([[
/* Default location. */
YYLLOC_DEFAULT ((*yylocp), (yyvsp - yyrhslen), yyrhslen);
- yystackp->yyerror_range[1].yystate.yyloc = *yylocp;
-]])[]b4_glr_cc_if([[
+ yystackp->yyerror_range[1].yystate.yyloc = *yylocp;]])[
+ /* If yyk == -1, we are running a deferred action on a temporary
+ stack. In that case, YY_REDUCE_PRINT must not play with YYFILL,
+ so pretend the stack is "normal". */
+ YY_REDUCE_PRINT ((yynormal || yyk == -1, yyvsp, yyk,
yyrule]b4_user_args[));]b4_glr_cc_if([[
#if YY_EXCEPTIONS
typedef ]b4_namespace_ref[::]b4_parser_class[::syntax_error syntax_error;
try
{
#endif // YY_EXCEPTIONS]])[
- switch (yyn)
+ switch (yyrule)
{
]b4_user_actions[
default: break;
@@ -955,6 +959,7 @@ yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem*
yyvsp,
YYERROR;
}
#endif // YY_EXCEPTIONS]])[
+ YY_SYMBOL_PRINT ("-> $$ =", yylhsNonterm (yyrule), yyvalp, yylocp);
return yyok;
# undef yyerrok
@@ -1446,8 +1451,7 @@ yydoAction (yyGLRStack* yystackp, YYPTRDIFF_T yyk,
yyRuleNum yyrule,
yystackp->yynextFree -= yynrhs;
yystackp->yyspaceLeft += yynrhs;
yystackp->yytops.yystates[0] = & yystackp->yynextFree[-1].yystate;
- YY_REDUCE_PRINT ((yytrue, yyrhs, yyk, yyrule]b4_user_args[));
- return yyuserAction (yyrule, yynrhs, yyrhs, yystackp,
+ return yyuserAction (yyrule, yynrhs, yyrhs, yystackp, yyk,
yyvalp]b4_locuser_args[);
}
else
@@ -1466,9 +1470,8 @@ yydoAction (yyGLRStack* yystackp, YYPTRDIFF_T yyk,
yyRuleNum yyrule,
}
yyupdateSplit (yystackp, yys);
yystackp->yytops.yystates[yyk] = yys;
- YY_REDUCE_PRINT ((yyfalse, yyrhsVals + YYMAXRHS + YYMAXLEFT - 1, yyk,
yyrule]b4_user_args[));
return yyuserAction (yyrule, yynrhs, yyrhsVals + YYMAXRHS + YYMAXLEFT -
1,
- yystackp, yyvalp]b4_locuser_args[);
+ yystackp, yyk, yyvalp]b4_locuser_args[);
}
}
@@ -1501,7 +1504,6 @@ yyglrReduce (yyGLRStack* yystackp, YYPTRDIFF_T yyk,
yyRuleNum yyrule,
YY_CAST (long, yyk), yyrule - 1, yyrline[yyrule]));
if (yyflag != yyok)
return yyflag;
- YY_SYMBOL_PRINT ("-> $$ =", yylhsNonterm (yyrule), &yyval, &yyloc);
yyglrShift (yystackp, yyk,
yyLRgotoState (yystackp->yytops.yystates[yyk]->yylrState,
yylhsNonterm (yyrule)),
@@ -1752,7 +1754,7 @@ yyresolveAction (yySemanticOption* yyopt, yyGLRStack*
yystackp,
yylloc = yyopt->yyloc;])[
yyflag = yyuserAction (yyopt->yyrule, yynrhs,
yyrhsVals + YYMAXRHS + YYMAXLEFT - 1,
- yystackp, yyvalp]b4_locuser_args[);
+ yystackp, -1, yyvalp]b4_locuser_args[);
yychar = yychar_current;
yylval = yylval_current;]b4_locations_if([
yylloc = yylloc_current;])[
--
2.29.2
- [PATCH 0/6] glr: various improvements, Akim Demaille, 2021/01/02
- [PATCH 1/6] glr.c: example: several improvements, Akim Demaille, 2021/01/02
- [PATCH 2/6] glr.c: example: use the exact same display as in the C++ example, Akim Demaille, 2021/01/02
- [PATCH 3/6] glr.c: reorder routines, Akim Demaille, 2021/01/02
- [PATCH 4/6] glr.c: log the execution of deferred actions,
Akim Demaille <=
- [PATCH 5/6] glr2.cc: minor clean up, Akim Demaille, 2021/01/02
- [PATCH 6/6] glr2.cc: log the deferred action too, Akim Demaille, 2021/01/02