X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/ea99527d23f1f4e1e4f358f9f495b2e4e26602af..39912f52635a7b7709b6a890a41e066340a89111:/data/glr.c diff --git a/data/glr.c b/data/glr.c index 523c092d..e631cb97 100644 --- a/data/glr.c +++ b/data/glr.c @@ -19,6 +19,33 @@ m4_include([c.m4]) # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA # 02111-1307 USA + +## ---------------- ## +## Default values. ## +## ---------------- ## + +# Stack parameters. +m4_define_default([b4_stack_depth_max], [10000]) +m4_define_default([b4_stack_depth_init], [200]) + +# Location type. +m4_define_default([b4_location_type], [yyltype]) + +# Accumule in b4_lex_param all the yylex arguments. +# Yes, this is quite ugly... +m4_define([b4_lex_param], +m4_dquote(b4_pure_if([[[[YYSTYPE *]], [[yylvalp]]][]dnl +b4_location_if([, [[YYLTYPE *], [yyllocp]]])])dnl +m4_ifdef([b4_lex_param], [, ]b4_lex_param))) + +m4_define_default([b4_parse_param]) + + +## ----------------- ## +## Semantic Values. ## +## ----------------- ## + + # b4_lhs_value([TYPE]) # -------------------- # Expansion of $$. @@ -39,15 +66,6 @@ m4_define([b4_rhs_value], ## Locations. ## ## ----------- ## -# b4_location_if(IF-TRUE, IF-FALSE) -# --------------------------------- -# Expand IF-TRUE, if locations are used, IF-FALSE otherwise. -m4_define([b4_location_if], -[m4_if(b4_locations_flag, [1], - [$1], - [$2])]) - - # b4_lhs_location() # ----------------- # Expansion of @$. @@ -64,19 +82,6 @@ m4_define([b4_rhs_location], -## -------------- ## -## %pure-parser. ## -## -------------- ## - -# b4_pure_if(IF-TRUE, IF-FALSE) -# ----------------------------- -# Expand IF-TRUE, if %pure-parser, IF-FALSE otherwise. -m4_define([b4_pure_if], -[m4_if(b4_pure, [1], - [$1], - [$2])]) - - ## ------------------- ## ## Output file names. ## ## ------------------- ## @@ -174,7 +179,7 @@ typedef struct yyltype int last_line; int last_column;])[ } yyltype; -# define YYLTYPE ]b4_ltype[ +# define YYLTYPE ]b4_location_type[ # define YYLTYPE_IS_TRIVIAL 1 #endif @@ -198,6 +203,12 @@ static YYLTYPE yyloc_default; # define yyfalse 0 #endif +#if defined (__STDC__) || defined (__cplusplus) + typedef signed char yysigned_char; +#else + typedef short yysigned_char; +#endif + /*-----------------. | GCC extensions. | `-----------------*/ @@ -220,6 +231,7 @@ static YYLTYPE yyloc_default; /* YYFINAL -- State number of the termination state. */ #define YYFINAL ]b4_final_state_number[ +/* YYLAST -- Last index in YYTABLE. */ #define YYLAST ]b4_last[ /* YYNTOKENS -- Number of terminals. */ @@ -403,18 +415,10 @@ int yyparse (void); #endif /* YYLEX -- calling `yylex' with the right arguments. */ +#define YYLEX ]b4_c_function_call([yylex], [int], b4_lex_param)[ ]b4_pure_if( [ -#ifdef YYLEX_PARAM -# define YYLEX yylex (yylvalp, b4_location_if([yyllocp, ])YYLEX_PARAM) -#else -# define YYLEX yylex (yylvalp[]b4_location_if([, yyllocp])) -#endif], -[#define YYLEX yylex ()]) - -b4_pure_if( -[ #undef yynerrs #define yynerrs (yystack->yyerrcnt) #undef yychar @@ -456,7 +460,7 @@ int yydebug; /* YYINITDEPTH -- initial size of the parser's stacks. */ #ifndef YYINITDEPTH -# define YYINITDEPTH ]b4_initdepth[ +# define YYINITDEPTH ]b4_stack_depth_init[ #endif /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only @@ -471,7 +475,7 @@ int yydebug; #endif #ifndef YYMAXDEPTH -# define YYMAXDEPTH ]b4_maxdepth[ +# define YYMAXDEPTH ]b4_stack_depth_max[ #endif /* Minimum number of free items on the stack allowed after an @@ -736,7 +740,7 @@ yyLRgotoState (yyStateNum yystate, yySymbol yylhs) static inline bool yyisShiftAction (int yyaction) { - return yyaction > 0; + return 0 < yyaction; } static inline bool @@ -757,14 +761,14 @@ yyhasResolvedValue (yyGLRState* yystate) static void yyaddDeferredAction (yyGLRStack* yystack, yyGLRState* yystate, - yyGLRState* yyrhs, yyRuleNum yyrule) + yyGLRState* rhs, yyRuleNum yyrule) { yySemanticOption* yynewItem; yynewItem = &yystack->yynextFree->yyoption; yystack->yyspaceLeft -= 1; yystack->yynextFree += 1; yynewItem->yyisState = yyfalse; - yynewItem->yystate = yyrhs; + yynewItem->yystate = rhs; yynewItem->yyrule = yyrule; yynewItem->yynext = yystate->yysemantics.yyfirstVal; yystate->yysemantics.yyfirstVal = yynewItem; @@ -822,14 +826,14 @@ yyexpandGLRStack (yyGLRStack* yystack) size_t yysize, yynewSize; size_t yyn; yysize = yystack->yynextFree - yystack->yyitems; - if (yysize >= YYMAXDEPTH) + if (YYMAXDEPTH <= yysize) yyFail (yystack, "parsing stack overflow (%d items)", yysize); yynewSize = 2*yysize; - if (yynewSize > YYMAXDEPTH) + if (YYMAXDEPTH < yynewSize) yynewSize = YYMAXDEPTH; yyinitGLRStack (&yynewStack, yynewSize); for (yyp0 = yystack->yyitems, yyp1 = yynewStack.yyitems, yyn = yysize; - yyn > 0; + 0 < yyn; yyn -= 1, yyp0 += 1, yyp1 += 1) { *yyp1 = *yyp0; @@ -967,7 +971,7 @@ yyglrShift (yyGLRStack* yystack, int yyk, yyStateNum yylrState, size_t yyposn, * the (unresolved) semantic value of RHS under the action for RULE. */ static inline void yyglrShiftDefer (yyGLRStack* yystack, int yyk, yyStateNum yylrState, - size_t yyposn, yyGLRState* yyrhs, yyRuleNum yyrule) + size_t yyposn, yyGLRState* rhs, yyRuleNum yyrule) { yyGLRStackItem* yynewItem; @@ -981,7 +985,7 @@ yyglrShiftDefer (yyGLRStack* yystack, int yyk, yyStateNum yylrState, yystack->yytops.yystates[yyk] = &yynewItem->yystate; yystack->yynextFree += 1; yystack->yyspaceLeft -= 1; - yyaddDeferredAction (yystack, &yynewItem->yystate, yyrhs, yyrule); + yyaddDeferredAction (yystack, &yynewItem->yystate, rhs, yyrule); } /** Pop the symbols consumed by reduction #RULE from the top of stack @@ -999,7 +1003,7 @@ yydoAction (yyGLRStack* yystack, int yyk, yyRuleNum yyrule, if (yystack->yysplitPoint == NULL) { /* Standard special case: single stack. */ - yyGLRStackItem* yyrhs = (yyGLRStackItem*) yystack->yytops.yystates[yyk]; + yyGLRStackItem* rhs = (yyGLRStackItem*) yystack->yytops.yystates[yyk]; assert (yyk == 0); yystack->yynextFree -= yynrhs; yystack->yyspaceLeft += yynrhs; @@ -1011,17 +1015,17 @@ yydoAction (yyGLRStack* yystack, int yyk, yyRuleNum yyrule, } else { - *yyvalp = yyrhs[1-yynrhs].yystate.yysemantics.yysval; - *yylocp = yyrhs[1-yynrhs].yystate.yyloc; + *yyvalp = rhs[1-yynrhs].yystate.yysemantics.yysval; + *yylocp = rhs[1-yynrhs].yystate.yyloc; } - return yyuserAction (yyrule, yynrhs, yyrhs, yyvalp, yylocp, yystack); + return yyuserAction (yyrule, yynrhs, rhs, yyvalp, yylocp, yystack); } else { int yyi; yyGLRState* yys; yyGLRStackItem yyrhsVals[YYMAXRHS]; - for (yyi = yynrhs-1, yys = yystack->yytops.yystates[yyk]; yyi >= 0; + for (yyi = yynrhs-1, yys = yystack->yytops.yystates[yyk]; 0 <= yyi; yyi -= 1, yys = yys->yypred) { assert (yys->yypred != NULL); @@ -1075,7 +1079,7 @@ yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, YYDPRINTF ((stderr, "Reducing stack %d by rule %d (line %d),", yyk, yyrule - 1, yyrline[yyrule])); /* Print the symbols being reduced, and their result. */ - for (yyi = yyprhs[yyrule]; yyrhs[yyi] >= 0; yyi++) + for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++) YYFPRINTF (stderr, "%s ", yytokenName (yyrhs[yyi])); YYFPRINTF (stderr, " -> %s\n", yytokenName (yyr1[yyrule])); } @@ -1096,7 +1100,7 @@ yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, yyStateNum yynewLRState; for (yys = yystack->yytops.yystates[yyk], yyn = yyrhsLength (yyrule); - yyn > 0; yyn -= 1) + 0 < yyn; yyn -= 1) { yys = yys->yypred; assert (yys != NULL); @@ -1219,7 +1223,7 @@ yypreference (yySemanticOption* y0, yySemanticOption* y1) return 0; if (p0 < p1) return 3; - if (p0 > p1) + if (p1 < p0) return 2; return 0; } @@ -1232,7 +1236,7 @@ static YYRESULTTAG yyresolveStates (yyGLRState* yys, int yyn, yyGLRStack* yystack) { YYRESULTTAG yyflag; - if (yyn > 0) + if (0 < yyn) { assert (yys->yypred != NULL); yyflag = yyresolveStates (yys->yypred, yyn-1, yystack); @@ -1260,7 +1264,7 @@ yyresolveAction (yySemanticOption* yyopt, yyGLRStack* yystack, yynrhs = yyrhsLength (yyopt->yyrule); YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack)); - for (yyi = yynrhs-1, yys = yyopt->yystate; yyi >= 0; + for (yyi = yynrhs-1, yys = yyopt->yystate; 0 <= yyi; yyi -= 1, yys = yys->yypred) { assert (yys->yypred != NULL); @@ -1273,7 +1277,16 @@ yyresolveAction (yySemanticOption* yyopt, yyGLRStack* yystack, } #if YYDEBUG -static yyGLRState YYLEFTMOST_STATE = { 0, 0, -1, NULL, 0, { NULL } }; +static yyGLRState YYLEFTMOST_STATE = + { + 0, 0, -1, NULL, 0, { NULL }, + /* yyloc. */ + { +#if YYLTYPE_IS_TRIVIAL +]b4_location_if([ 0, 0, 0, 0])[ +#endif + } + }; static void yyreportTree (yySemanticOption* yyx, int yyindent) { @@ -1282,14 +1295,14 @@ static void yyreportTree (yySemanticOption* yyx, int yyindent) yyGLRState* yys; yyGLRState* yystates[YYMAXRHS]; - for (yyi = yynrhs, yys = yyx->yystate; yyi > 0; yyi -= 1, yys = yys->yypred) + for (yyi = yynrhs, yys = yyx->yystate; 0 < yyi; yyi -= 1, yys = yys->yypred) yystates[yyi] = yys; if (yys == NULL) yystates[0] = &YYLEFTMOST_STATE; else yystates[0] = yys; - if (yys->yyposn+1 > yyx->yystate->yyposn) + if (yyx->yystate->yyposn < yys->yyposn + 1) YYFPRINTF (stderr, "%*s%s -> \n", yyindent, "", yytokenName (yylhsNonterm (yyx->yyrule)), yyx->yyrule); @@ -1537,15 +1550,17 @@ yyreportParseError (yyGLRStack* yystack, YYSTYPE* yylvalp, YYLTYPE* yyllocp) yysize += strlen (yytokenName (yyx)) + strlen (yyprefix), yycount += 1, yyprefix = " or "; yymsg = yyp = (char*) malloc (yysize); - yyp += sprintf (yyp, "parse error, unexpected %s", - yytokenName (*yytokenp)); + sprintf (yyp, "parse error, unexpected %s", + yytokenName (*yytokenp)); + yyp += strlen (yyp); if (yycount < 5) { yyprefix = ", expecting "; for (yyx = yyn < 0 ? -yyn : 0; yyx < yytname_size; yyx += 1) if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) { - yyp += sprintf (yyp, "%s%s", yyprefix, yytokenName (yyx)); + sprintf (yyp, "%s%s", yyprefix, yytokenName (yyx)); + yyp += strlen (yyp); yyprefix = " or "; } } @@ -1676,9 +1691,9 @@ yyparse (YYPARSE_PARAM_ARG) while (yytrue) { - /* For efficiency, we have two loops, of which the first of which - * is specialized to deterministic operation (single stack, no - * potential ambiguity). */ + /* For efficiency, we have two loops, the first of which is + specialized to deterministic operation (single stack, no + potential ambiguity). */ /* Standard mode */ while (yytrue) @@ -1721,7 +1736,7 @@ yyparse (YYPARSE_PARAM_ARG) yytoken = YYEMPTY; yyposn += 1; yyglrShift (&yystack, 0, yyaction, yyposn, yylval, yyllocp); - if (yystack.yyerrState > 0) + if (0 < yystack.yyerrState) yystack.yyerrState -= 1; YYDPRINTF ((stderr, "Entering state %d\n", yystack.yytops.yystates[0]->yylrState));