X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/6fdb09caac6ee472315f0cf849cb2e105d931266..b50d2359d7608270561abd5c3cbf6e84834c2019:/data/glr.c diff --git a/data/glr.c b/data/glr.c index 219dd3f5..0f466131 100644 --- a/data/glr.c +++ b/data/glr.c @@ -154,7 +154,7 @@ m4_if(b4_prefix[], [yy], [], #define yynerrs b4_prefix[]nerrs #define yylloc b4_prefix[]lloc]) -b4_token_defines(b4_tokens) +b4_token_enums(b4_tokens) /* Copy the first part of user declarations. */ b4_pre_prologue[ @@ -172,9 +172,14 @@ b4_pre_prologue[ # define YYERROR_VERBOSE ]b4_error_verbose[ #endif +/* Enabling the token table. */ +#ifndef YYTOKEN_TABLE +# define YYTOKEN_TABLE ]b4_token_table[ +#endif + #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED) ]m4_ifdef([b4_stype], -[b4_syncline([b4_stype_line], [b4_filename]) +[b4_syncline([b4_stype_line], [b4_file_name]) typedef union m4_bregexp(b4_stype, [^{], [YYSTYPE ])b4_stype YYSTYPE; /* Line __line__ of glr.c. */ b4_syncline([@oline@], [@ofile@])], @@ -216,7 +221,6 @@ b4_syncline([@oline@], [@ofile@]) #include #include #include -#include #ifndef YY_ # if YYENABLE_NLS @@ -250,33 +254,36 @@ b4_syncline([@oline@], [@ofile@]) #define yytrue 1 #define yyfalse 0 +#ifndef YYSETJMP +# include +# define YYJMP_BUF jmp_buf +# define YYSETJMP(env) setjmp (env) +# define YYLONGJMP(env, val) longjmp (env, val) +#endif + /*-----------------. | GCC extensions. | `-----------------*/ #ifndef __attribute__ /* This feature is available in gcc versions 2.5 and later. */ -# if !defined (__GNUC__) || __GNUC__ < 2 || \ -(__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ +# if (!defined (__GNUC__) || __GNUC__ < 2 \ + || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__) # define __attribute__(Spec) /* empty */ -# else -]b4_location_if([# define YYOPTIONAL_LOC(Name) Name],[ -# if defined (__cplusplus) -# define YYOPTIONAL_LOC(Name) /* empty */ -# else -# define YYOPTIONAL_LOC(Name) Name ATTRIBUTE_UNUSED -# endif])[ # endif #endif +]b4_location_if([#define YYOPTIONAL_LOC(Name) Name],[ +#ifdef __cplusplus +# define YYOPTIONAL_LOC(Name) /* empty */ +#else +# define YYOPTIONAL_LOC(Name) Name __attribute__ ((__unused__)) +#endif])[ + #ifndef YYASSERT # define YYASSERT(condition) ((void) ((condition) || (abort (), 0))) #endif -#ifndef ATTRIBUTE_UNUSED -# define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) -#endif - /* YYFINAL -- State number of the termination state. */ #define YYFINAL ]b4_final_state_number[ /* YYLAST -- Last index in YYTABLE. */ @@ -331,8 +338,8 @@ static const ]b4_int_type_for([b4_rline])[ yyrline[] = }; #endif -#if (YYDEBUG) || YYERROR_VERBOSE -/* YYTNME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. +#if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE +/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. First, the terminals, then, starting at YYNTOKENS, nonterminals. */ static const char *const yytname[] = { @@ -531,6 +538,7 @@ do { \ YYFPRINTF (stderr, "%s ", Title); \ yysymprint (stderr, \ Type, Value]b4_location_if([, Location])[); \ + YYFPRINTF (stderr, "\n"); \ } \ } while (0) @@ -600,6 +608,54 @@ yystpcpy (char *yydest, const char *yysrc) # endif # endif +# ifndef yytnamerr +/* Copy to YYRES the contents of YYSTR after stripping away unnecessary + quotes and backslashes, so that it's suitable for yyerror. The + heuristic is that double-quoting is unnecessary unless the string + contains an apostrophe, a comma, or backslash (other than + backslash-backslash). YYSTR is taken from yytname. If YYRES is + null, do not copy; instead, return the length of what the result + would have been. */ +static size_t +yytnamerr (char *yyres, const char *yystr) +{ + if (*yystr == '"') + { + size_t yyn = 0; + char const *yyp = yystr; + + for (;;) + switch (*++yyp) + { + case '\'': + case ',': + goto do_not_strip_quotes; + + case '\\': + if (*++yyp != '\\') + goto do_not_strip_quotes; + /* Fall through. */ + default: + if (yyres) + yyres[yyn] = *yyp; + yyn++; + break; + + case '"': + if (yyres) + yyres[yyn] = '\0'; + return yyn; + } + do_not_strip_quotes: ; + } + + if (! yyres) + return strlen (yystr); + + return yystpcpy (yyres, yystr) - yyres; +} +# endif + #endif /* !YYERROR_VERBOSE */ /** State numbers, as in LALR(1) machine */ @@ -678,7 +734,7 @@ struct yyGLRStack { int yyrawchar; ])[ yySymbol* yytokenp; - jmp_buf yyexception_buffer; + YYJMP_BUF yyexception_buffer; yyGLRStackItem* yyitems; yyGLRStackItem* yynextFree; size_t yyspaceLeft; @@ -689,18 +745,22 @@ struct yyGLRStack { static void yyexpandGLRStack (yyGLRStack* yystack); +static void yyFail (yyGLRStack* yystack]b4_pure_formals[, const char* yymsg) + __attribute__ ((__noreturn__)); static void yyFail (yyGLRStack* yystack]b4_pure_formals[, const char* yymsg) { if (yymsg != NULL) yyerror (]b4_yyerror_args[yymsg); - longjmp (yystack->yyexception_buffer, 1); + YYLONGJMP (yystack->yyexception_buffer, 1); } +static void yyMemoryExhausted (yyGLRStack* yystack) + __attribute__ ((__noreturn__)); static void yyMemoryExhausted (yyGLRStack* yystack) { - longjmp (yystack->yyexception_buffer, 2); + YYLONGJMP (yystack->yyexception_buffer, 2); } #if YYDEBUG || YYERROR_VERBOSE @@ -719,7 +779,7 @@ yytokenName (yySymbol yytoken) * at YYVSP[YYLOW0].yystate.yypred. Leaves YYVSP[YYLOW1].yystate.yypred * containing the pointer to the next state in the chain. Assumes * YYLOW1 < YYLOW0. */ -static void yyfillin (yyGLRStackItem *, int, int) ATTRIBUTE_UNUSED; +static void yyfillin (yyGLRStackItem *, int, int) __attribute__ ((__unused__)); static void yyfillin (yyGLRStackItem *yyvsp, int yylow0, int yylow1) { @@ -740,7 +800,7 @@ yyfillin (yyGLRStackItem *yyvsp, int yylow0, int yylow1) YYVSP[YYLOW1 .. *YYLOW-1] as in yyfillin and set *YYLOW = YYLOW1. For convenience, always return YYLOW1. */ static inline int yyfill (yyGLRStackItem *, int *, int, yybool) - ATTRIBUTE_UNUSED; + __attribute__ ((__unused__)); static inline int yyfill (yyGLRStackItem *yyvsp, int *yylow, int yylow1, yybool yynormal) { @@ -764,7 +824,8 @@ yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem* yyvsp, yyGLRStack* yystack ]b4_user_formals[) { - yybool yynormal ATTRIBUTE_UNUSED = (yystack->yysplitPoint == NULL); + yybool yynormal __attribute__ ((__unused__)) = + (yystack->yysplitPoint == NULL); int yylow; # undef yyerrok @@ -831,13 +892,44 @@ yyuserMerge (int yyn, YYSTYPE* yy0, YYSTYPE* yy1) ]b4_yydestruct_generate([b4_c_ansi_function_def])[ -/** Number of symbols composing the right hand side of rule #RULE. */ +/** Number of symbols composing the right hand side of rule #RULE. */ static inline int yyrhsLength (yyRuleNum yyrule) { return yyr2[yyrule]; } +static void +yydestroyGLRState (char const *yymsg, yyGLRState *yys) +{ + if (yys->yyresolved) + yydestruct (yymsg, yystos[yys->yylrState], + &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[); + else + { +#if YYDEBUG + if (yydebug) + { + YYFPRINTF (stderr, "%s unresolved ", yymsg); + yysymprint (stderr, yystos[yys->yylrState], + &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[); + YYFPRINTF (stderr, "\n"); + } +#endif + + if (yys->yysemantics.yyfirstVal) + { + yySemanticOption *yyoption = yys->yysemantics.yyfirstVal; + yyGLRState *yyrh; + int yyn; + for (yyrh = yyoption->yystate, yyn = yyrhsLength (yyoption->yyrule); + yyn > 0; + yyrh = yyrh->yypred, yyn -= 1) + yydestroyGLRState (yymsg, yyrh); + } + } +} + /** Left-hand-side symbol for rule #RULE. */ static inline yySymbol yylhsNonterm (yyRuleNum yyrule) @@ -970,11 +1062,14 @@ yyinitGLRStack (yyGLRStack* yystack, size_t yysize) yystack->yyerrState = 0; yynerrs = 0; yystack->yyspaceLeft = yysize; - yystack->yynextFree = yystack->yyitems = + yystack->yyitems = (yyGLRStackItem*) YYMALLOC (yysize * sizeof yystack->yynextFree[0]); + if (!yystack->yyitems) + return yyfalse; + yystack->yynextFree = yystack->yyitems; yystack->yysplitPoint = NULL; yystack->yylastDeleted = NULL; - return yyinitStateSet (&yystack->yytops) && yystack->yyitems; + return yyinitStateSet (&yystack->yytops); } #define YYRELOC(YYFROMITEMS,YYTOITEMS,YYX,YYTYPE) \ @@ -1519,6 +1614,9 @@ yyreportTree (yySemanticOption* yyx, int yyindent) } #endif +static void yyreportAmbiguity (yySemanticOption* yyx0, yySemanticOption* yyx1, + yyGLRStack* yystack]b4_pure_formals[) + __attribute__ ((__noreturn__)); static void yyreportAmbiguity (yySemanticOption* yyx0, yySemanticOption* yyx1, yyGLRStack* yystack]b4_pure_formals[) @@ -1546,35 +1644,49 @@ yyresolveValue (yySemanticOption* yyoptionList, yyGLRStack* yystack, YYSTYPE* yyvalp, YYLTYPE* yylocp]b4_user_formals[) { yySemanticOption* yybest; - yySemanticOption* yyp; + yySemanticOption** yypp; yybool yymerge; yybest = yyoptionList; yymerge = yyfalse; - for (yyp = yyoptionList->yynext; yyp != NULL; yyp = yyp->yynext) + for (yypp = &yyoptionList->yynext; *yypp != NULL; ) { + yySemanticOption* yyp = *yypp; + if (yyidenticalOptions (yybest, yyp)) - yymergeOptionSets (yybest, yyp); + { + yymergeOptionSets (yybest, yyp); + *yypp = yyp->yynext; + } else - switch (yypreference (yybest, yyp)) - { - case 0: - yyreportAmbiguity (yybest, yyp, yystack]b4_pure_args[); - break; - case 1: - yymerge = yytrue; - break; - case 2: - break; - case 3: - yybest = yyp; - yymerge = yyfalse; - break; - } + { + switch (yypreference (yybest, yyp)) + { + case 0: + yyreportAmbiguity (yybest, yyp, yystack]b4_pure_args[); + break; + case 1: + yymerge = yytrue; + break; + case 2: + break; + case 3: + yybest = yyp; + yymerge = yyfalse; + break; + default: + /* This cannot happen so it is not worth a YYASSERT (yyfalse), + but some compilers complain if the default case is + omitted. */ + break; + } + yypp = &yyp->yynext; + } } if (yymerge) { + yySemanticOption* yyp; int yyprec = yydprec[yybest->yyrule]; YYCHK (yyresolveAction (yybest, yystack, yyvalp, yylocp]b4_user_args[)); for (yyp = yybest->yynext; yyp != NULL; yyp = yyp->yynext) @@ -1679,7 +1791,6 @@ yyprocessOneStack (yyGLRStack* yystack, size_t yyk, yychar = YYLEX; *yytokenp = YYTRANSLATE (yychar); YY_SYMBOL_PRINT ("Next token is", *yytokenp, yylvalp, yyllocp); - YYDPRINTF ((stderr, "\n")); } yygetLRActions (yystate, *yytokenp, &yyaction, &yyconflicts); @@ -1702,7 +1813,8 @@ yyprocessOneStack (yyGLRStack* yystack, size_t yyk, YY_SYMBOL_PRINT ("shifting", *yytokenp, yylvalp, yyllocp); yyglrShift (yystack, yyk, yyaction, yyposn+1, *yylvalp, yyllocp); - YYDPRINTF ((stderr, ", now in state #%d\n", + YYDPRINTF ((stderr, "Stack %lu now in state #%d\n", + (unsigned long int) yyk, yystack->yytops.yystates[yyk]->yylrState)); break; } @@ -1736,7 +1848,7 @@ yyreportSyntaxError (yyGLRStack* yystack, yyn = yypact[yystack->yytops.yystates[0]->yylrState]; if (YYPACT_NINF < yyn && yyn < YYLAST) { - size_t yysize0 = strlen (yytokenName (*yytokenp)); + size_t yysize0 = yytnamerr (NULL, yytokenName (*yytokenp)); size_t yysize = yysize0; size_t yysize1; yybool yysize_overflow = yyfalse; @@ -1778,7 +1890,7 @@ yyreportSyntaxError (yyGLRStack* yystack, break; } yyarg[yycount++] = yytokenName (yyx); - yysize1 = yysize + strlen (yytokenName (yyx)); + yysize1 = yysize + yytnamerr (NULL, yytokenName (yyx)); yysize_overflow |= yysize1 < yysize; yysize = yysize1; yyfmt = yystpcpy (yyfmt, yyprefix); @@ -1801,7 +1913,7 @@ yyreportSyntaxError (yyGLRStack* yystack, { if (*yyp == '%' && yyf[1] == 's' && yyi < yycount) { - yyp = yystpcpy (yyp, yyarg[yyi++]); + yyp += yytnamerr (yyp, yyarg[yyi++]); yyf += 2; } else @@ -1862,7 +1974,6 @@ yyrecoverSyntaxError (yyGLRStack* yystack, yychar = YYLEX; *yytokenp = YYTRANSLATE (yychar); YY_SYMBOL_PRINT ("Next token is", *yytokenp, yylvalp, yyllocp); - YYDPRINTF ((stderr, "\n")); yyj = yypact[yystack->yytops.yystates[0]->yylrState]; if (yyis_pact_ninf (yyj)) return; @@ -1905,7 +2016,6 @@ yyrecoverSyntaxError (yyGLRStack* yystack, YYLLOC_DEFAULT (yyerrloc, yystack->yyerror_range, 2);]])[ YY_SYMBOL_PRINT ("Shifting", yystos[yytable[yyj]], yylvalp, &yyerrloc); - YYDPRINTF ((stderr, "\n")); yyglrShift (yystack, 0, yytable[yyj], yys->yyposn, *yylvalp, &yyerrloc); yys = yystack->yytops.yystates[0]; @@ -1913,9 +2023,7 @@ yyrecoverSyntaxError (yyGLRStack* yystack, } } ]b4_location_if([[ yystack->yyerror_range[1].yystate.yyloc = yys->yyloc;]])[ - yydestruct ("Error: popping", - yystos[yys->yylrState], - &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[); + yydestroyGLRState ("Error: popping", yys); yystack->yytops.yystates[0] = yys->yypred; yystack->yynextFree -= 1; yystack->yyspaceLeft += 1; @@ -1927,7 +2035,7 @@ yyrecoverSyntaxError (yyGLRStack* yystack, #define YYCHK1(YYE) \ do { \ switch (YYE) { \ - default: \ + case yyok: \ break; \ case yyabort: \ goto yyabortlab; \ @@ -1935,6 +2043,8 @@ yyrecoverSyntaxError (yyGLRStack* yystack, goto yyacceptlab; \ case yyerr: \ goto yyuser_error; \ + default: \ + goto yybuglab; \ } \ } while (0) @@ -1982,10 +2092,12 @@ b4_syncline([@oline@], [@ofile@])])dnl [ if (! yyinitGLRStack (&yystack, YYINITDEPTH)) goto yyexhaustedlab; - switch (setjmp (yystack.yyexception_buffer)) + switch (YYSETJMP (yystack.yyexception_buffer)) { + case 0: break; case 1: goto yyabortlab; case 2: goto yyexhaustedlab; + default: goto yybuglab; } yystack.yytokenp = &yytoken; yyglrShift (&yystack, 0, 0, 0, yylval, &yylloc); @@ -2026,7 +2138,6 @@ b4_syncline([@oline@], [@ofile@])])dnl yychar = YYLEX; yytoken = YYTRANSLATE (yychar); YY_SYMBOL_PRINT ("Next token is", yytoken, yylvalp, yyllocp); - YYDPRINTF ((stderr, "\n")); } yygetLRActions (yystate, yytoken, &yyaction, &yyconflicts); if (*yyconflicts != 0) @@ -2034,7 +2145,6 @@ b4_syncline([@oline@], [@ofile@])])dnl if (yyisShiftAction (yyaction)) { YY_SYMBOL_PRINT ("Shifting", yytoken, yylvalp, yyllocp); - YYDPRINTF ((stderr, "\n")); if (yytoken != YYEOF) yytoken = YYEMPTY; yyposn += 1; @@ -2092,6 +2202,10 @@ b4_syncline([@oline@], [@ofile@])])dnl yyresult = 0; goto yyreturn; + yybuglab: + YYASSERT (yyfalse); + /* Fall through. */ + yyabortlab: yyresult = 1; goto yyreturn; @@ -2103,34 +2217,36 @@ b4_syncline([@oline@], [@ofile@])])dnl yyreturn: if (yytoken != YYEOF && yytoken != YYEMPTY) - yydestruct ("Error: discarding lookahead", + yydestruct ("Cleanup: discarding lookahead", yytoken, yylvalp]b4_location_if([, yyllocp])[); - /* Now pop stack until empty, destroying its entries as we go. */ - { - yyGLRState** yystates = yystack.yytops.yystates; - if (yystates) - while (yystates[0]) - { - yyGLRState *yys = yystates[0]; + /* If the stack is well-formed, pop the stack until it is empty, + destroying its entries as we go. But free the stack regardless + of whether it is well-formed. */ + if (yystack.yyitems) + { + yyGLRState** yystates = yystack.yytops.yystates; + if (yystates) + while (yystates[0]) + { + yyGLRState *yys = yystates[0]; ]b4_location_if([[ yystack.yyerror_range[1].yystate.yyloc = yys->yyloc;]] -)[ yydestruct ("Error: popping", - yystos[yys->yylrState], - &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[); - yystates[0] = yys->yypred; - yystack.yynextFree -= 1; - yystack.yyspaceLeft += 1; - } - } +)[ yydestroyGLRState ("Cleanup: popping", yys); + yystates[0] = yys->yypred; + yystack.yynextFree -= 1; + yystack.yyspaceLeft += 1; + } + yyfreeGLRStack (&yystack); + } - yyfreeGLRStack (&yystack); return yyresult; } /* DEBUGGING ONLY */ #ifdef YYDEBUG -static void yypstack (yyGLRStack* yystack, size_t yyk) ATTRIBUTE_UNUSED; -static void yypdumpstack (yyGLRStack* yystack) ATTRIBUTE_UNUSED; +static void yypstack (yyGLRStack* yystack, size_t yyk) + __attribute__ ((__unused__)); +static void yypdumpstack (yyGLRStack* yystack) __attribute__ ((__unused__)); static void yy_yypstack (yyGLRState* yys) @@ -2205,11 +2321,11 @@ m4_if(b4_defines_flag, 0, [], b4_copyright([Skeleton parser for GLR parsing with Bison], [2002, 2003, 2004, 2005]) -b4_token_defines(b4_tokens) +b4_token_enums(b4_tokens) #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED) m4_ifdef([b4_stype], -[b4_syncline([b4_stype_line], [b4_filename]) +[b4_syncline([b4_stype_line], [b4_file_name]) typedef union m4_bregexp(b4_stype, [^{], [YYSTYPE ])b4_stype YYSTYPE; /* Line __line__ of glr.c. */ b4_syncline([@oline@], [@ofile@])],