X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/df9a1ba4de98c4deeea6925f67c0b9bac3a9a408..422494834c997d34b85f31f22bcc54f8859876c4:/data/glr.c diff --git a/data/glr.c b/data/glr.c index 578ad116..f427cfa9 100644 --- a/data/glr.c +++ b/data/glr.c @@ -34,6 +34,16 @@ m4_define_default([b4_stack_depth_init], [200]) ## ------------------------ ## +# b4_user_formals +# --------------- +# The possible parse-params formal arguments preceded by a comma. +# +# This is not shared with yacc.c in c.m4 because GLR relies on ISO C +# formal argument declarations. +m4_define([b4_user_formals], +[m4_ifset([b4_parse_param], [, b4_c_ansi_formals(b4_parse_param)])]) + + # b4_lex_param # ------------ # Accumule in b4_lex_param all the yylex arguments. @@ -44,15 +54,10 @@ b4_location_if([, [[YYLTYPE *], [yyllocp]]])])dnl m4_ifdef([b4_lex_param], [, ]b4_lex_param))) -# b4_user_formals -# --------------- -m4_define([b4_user_formals], -[m4_ifset([b4_parse_param], [, b4_c_ansi_formals(b4_parse_param)])]) - - # b4_yyerror_args # --------------- -# Arguments passed to yyerror: user args plus yylloc. +# Optional effective arguments passed to yyerror: user args plus yylloc, and +# a trailing comma. m4_define([b4_yyerror_args], [b4_pure_if([b4_location_if([yylocp, ])])dnl m4_ifset([b4_parse_param], [b4_c_args(b4_parse_param), ])]) @@ -68,18 +73,11 @@ m4_ifset([b4_parse_param], [b4_c_args(b4_parse_param), ])]) # b4_pure_args # ------------ -# Arguments needed by yyerror: user args plus yylloc. +# Same as b4_yyerror_args, but with a leading comma. m4_define([b4_pure_args], [b4_pure_if([b4_location_if([, yylocp])])[]b4_user_args]) -# b4_pure_formals -# --------------- -# Arguments passed to yyerror: user formals plus yyllocp. -m4_define([b4_pure_formals], -[b4_pure_if([b4_location_if([, YYLTYPE *yylocp])])[]b4_user_formals]) - - # b4_lpure_args # ------------- # Same as above, but on the look-ahead, hence yyllocp instead of yylocp. @@ -87,6 +85,13 @@ m4_define([b4_lpure_args], [b4_pure_if([b4_location_if([, yyllocp])])[]b4_user_args]) +# b4_pure_formals +# --------------- +# Arguments passed to yyerror: user formals plus yyllocp. +m4_define([b4_pure_formals], +[b4_pure_if([b4_location_if([, YYLTYPE *yylocp])])[]b4_user_formals]) + + # b4_lpure_formals # ---------------- # Same as above, but on the look-ahead, hence yyllocp instead of yylocp. @@ -111,7 +116,7 @@ m4_define([b4_lhs_value], # Expansion of $NUM, where the current rule has RULE-LENGTH # symbols on RHS. m4_define([b4_rhs_value], -[(((yyGLRStackItem const *)yyvsp)@{YYFILL (m4_eval([$2 - $1]))@}.yystate.yysemantics.yysval[]m4_ifval([$3], [.$3]))]) +[(((yyGLRStackItem const *)yyvsp)@{YYFILL ($2 - $1)@}.yystate.yysemantics.yysval[]m4_ifval([$3], [.$3]))]) @@ -131,7 +136,13 @@ m4_define([b4_lhs_location], # Expansion of @NUM, where the current rule has RULE-LENGTH symbols # on RHS. m4_define([b4_rhs_location], -[(((yyGLRStackItem const *)yyvsp)@{YYFILL (m4_eval([$2 - $1]))@}.yystate.yyloc)]) +[(((yyGLRStackItem const *)yyvsp)@{YYFILL ($2 - $1)@}.yystate.yyloc)]) + + + +## -------------- ## +## Output files. ## +## -------------- ## # We do want M4 expansion after # for CPP macros. m4_changecom() @@ -143,6 +154,7 @@ b4_copyright([Skeleton parser for GLR parsing with Bison], /* This is the parser code for GLR (Generalized LR) parser. */ ]b4_identification + m4_if(b4_prefix[], [yy], [], [/* Substitute the variable and function names. */ #define yyparse b4_prefix[]parse @@ -152,29 +164,21 @@ m4_if(b4_prefix[], [yy], [], #define yychar b4_prefix[]char #define yydebug b4_prefix[]debug #define yynerrs b4_prefix[]nerrs -#define yylloc b4_prefix[]lloc]) +#define yylloc b4_prefix[]lloc]) -b4_token_defines(b4_tokens) +dnl # b4_shared_declarations +dnl # ---------------------- +dnl # Declaration that might either go into the header (if --defines) +dnl # or open coded in the parser body. +m4_define([b4_shared_declarations], +[b4_token_enums(b4_tokens)[ /* Copy the first part of user declarations. */ -b4_pre_prologue[ - -/* Enabling traces. */ -#ifndef YYDEBUG -# define YYDEBUG ]b4_debug[ -#endif - -/* Enabling verbose error messages. */ -#ifdef YYERROR_VERBOSE -# undef YYERROR_VERBOSE -# define YYERROR_VERBOSE 1 -#else -# define YYERROR_VERBOSE ]b4_error_verbose[ -#endif +]b4_pre_prologue[ #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@])], @@ -198,6 +202,29 @@ typedef struct YYLTYPE # define YYLTYPE_IS_DECLARED 1 # define YYLTYPE_IS_TRIVIAL 1 #endif +]]) + +m4_if(b4_defines_flag, 0, + [b4_shared_declarations], + [#include @output_header_name@])[ + +/* Enabling traces. */ +#ifndef YYDEBUG +# define YYDEBUG ]b4_debug[ +#endif + +/* Enabling verbose error messages. */ +#ifdef YYERROR_VERBOSE +# undef YYERROR_VERBOSE +# define YYERROR_VERBOSE 1 +#else +# define YYERROR_VERBOSE ]b4_error_verbose[ +#endif + +/* Enabling the token table. */ +#ifndef YYTOKEN_TABLE +# define YYTOKEN_TABLE ]b4_token_table[ +#endif /* Default (constant) value used for initialization for null right-hand sides. Unlike the standard yacc.c template, @@ -216,7 +243,6 @@ b4_syncline([@oline@], [@ofile@]) #include #include #include -#include #ifndef YY_ # if YYENABLE_NLS @@ -230,6 +256,9 @@ b4_syncline([@oline@], [@ofile@]) # endif #endif +/* Suppress unused-variable warnings by "using" E. */ +#define YYUSE(e) do {;} while (/*CONSTCOND*/ yyfalse && (e)) + #ifndef YYFREE # define YYFREE free #endif @@ -250,33 +279,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 +363,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[] = { @@ -463,7 +495,7 @@ static const ]b4_int_type_for([b4_stos])[ yystos[] = (Current).first_column = (Current).last_column = \ YYRHSLOC (Rhs, 0).last_column; \ } \ - while (0) + while (/*CONSTCOND*/ 0) /* YY_LOCATION_PRINT -- Print the location on the stream. This macro was not mandated originally: define only if we know @@ -508,7 +540,7 @@ typedef enum { yyok, yyaccept, yyabort, yyerr } YYRESULTTAG; #define YYCHK(YYE) \ do { YYRESULTTAG yyflag = YYE; if (yyflag != yyok) return yyflag; } \ - while (0) + while (/*CONSTCOND*/ 0) #if YYDEBUG @@ -520,19 +552,20 @@ typedef enum { yyok, yyaccept, yyabort, yyerr } YYRESULTTAG; do { \ if (yydebug) \ YYFPRINTF Args; \ -} while (0) +} while (/*CONSTCOND*/ 0) ]b4_yysymprint_generate([b4_c_ansi_function_def])[ -# define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ -do { \ - if (yydebug) \ - { \ - YYFPRINTF (stderr, "%s ", Title); \ - yysymprint (stderr, \ - Type, Value]b4_location_if([, Location])[); \ - } \ -} while (0) +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ +do { \ + if (yydebug) \ + { \ + YYFPRINTF (stderr, "%s ", Title); \ + yysymprint (stderr, \ + Type, Value]b4_location_if([, Location])[]b4_user_args[); \ + YYFPRINTF (stderr, "\n"); \ + } \ +} while (/*CONSTCOND*/ 0) /* Nonzero means print parse trace. It is left uninitialized so that multiple parsers can coexist. */ @@ -578,6 +611,21 @@ int yydebug; # endif #endif +#if YYSTACKEXPANDABLE +# define YY_RESERVE_GLRSTACK(Yystack) \ + do { \ + if (Yystack->yyspaceLeft < YYHEADROOM) \ + yyexpandGLRStack (Yystack); \ + } while (/*CONSTCOND*/ 0) +#else +# define YY_RESERVE_GLRSTACK(Yystack) \ + do { \ + if (Yystack->yyspaceLeft < YYHEADROOM) \ + yyMemoryExhausted (Yystack); \ + } while (/*CONSTCOND*/ 0) +#endif + + #if YYERROR_VERBOSE # ifndef yystpcpy @@ -600,6 +648,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 +774,7 @@ struct yyGLRStack { int yyrawchar; ])[ yySymbol* yytokenp; - jmp_buf yyexception_buffer; + YYJMP_BUF yyexception_buffer; yyGLRStackItem* yyitems; yyGLRStackItem* yynextFree; size_t yyspaceLeft; @@ -689,18 +785,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 @@ -717,9 +817,8 @@ yytokenName (yySymbol yytoken) /** Fill in YYVSP[YYLOW1 .. YYLOW0-1] from the chain of states starting * 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; + * containing the pointer to the next state in the chain. */ +static void yyfillin (yyGLRStackItem *, int, int) __attribute__ ((__unused__)); static void yyfillin (yyGLRStackItem *yyvsp, int yylow0, int yylow1) { @@ -737,10 +836,10 @@ yyfillin (yyGLRStackItem *yyvsp, int yylow0, int yylow1) } /* Do nothing if YYNORMAL or if *YYLOW <= YYLOW1. Otherwise, fill in - YYVSP[YYLOW1 .. *YYLOW-1] as in yyfillin and set *YYLOW = YYLOW1. - For convenience, always return 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 +863,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 @@ -816,9 +916,8 @@ b4_syncline([@oline@], [@ofile@]) static void yyuserMerge (int yyn, YYSTYPE* yy0, YYSTYPE* yy1) { - /* `Use' the arguments. */ - (void) yy0; - (void) yy1; + YYUSE (yy0); + YYUSE (yy1); switch (yyn) { @@ -831,13 +930,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]b4_user_formals[) +{ + if (yys->yyresolved) + yydestruct (yymsg, yystos[yys->yylrState], + &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[]b4_user_args[); + else + { +#if YYDEBUG + if (yydebug) + { + YYFPRINTF (stderr, "%s unresolved ", yymsg); + yysymprint (stderr, yystos[yys->yylrState], + &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[]b4_user_args[); + 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]b4_user_args[); + } + } +} + /** Left-hand-side symbol for rule #RULE. */ static inline yySymbol yylhsNonterm (yyRuleNum yyrule) @@ -846,9 +976,9 @@ yylhsNonterm (yyRuleNum yyrule) } #define yyis_pact_ninf(yystate) \ - ]m4_if(m4_eval(b4_pact_ninf < b4_pact_min), 1, - 0, - ((yystate) == YYPACT_NINF))[ + ]m4_if(m4_eval(b4_pact_ninf < b4_pact_min), [1], + [0], + [((yystate) == YYPACT_NINF)])[ /** True iff LR state STATE has only a default reduction (regardless * of token). */ @@ -866,9 +996,9 @@ yydefaultAction (yyStateNum yystate) } #define yyis_table_ninf(yytable_value) \ - ]m4_if(m4_eval(b4_table_ninf < b4_table_min), 1, - 0, - ((yytable_value) == YYTABLE_NINF))[ + ]m4_if(m4_eval(b4_table_ninf < b4_table_min), [1], + [0], + [((yytable_value) == YYTABLE_NINF)])[ /** Set *YYACTION to the action to take in YYSTATE on seeing YYTOKEN. * Result R means @@ -925,21 +1055,32 @@ yyisErrorAction (int yyaction) /* GLRStates */ +/** Return a fresh GLRStackItem. Callers should call + * YY_RESERVE_GLRSTACK afterwards to make sure there is sufficient + * headroom. */ + +static inline yyGLRStackItem* +yynewGLRStackItem (yyGLRStack* yystack, yybool yyisState) +{ + yyGLRStackItem* yynewItem = yystack->yynextFree; + yystack->yyspaceLeft -= 1; + yystack->yynextFree += 1; + yynewItem->yystate.yyisState = yyisState; + return yynewItem; +} + static void yyaddDeferredAction (yyGLRStack* yystack, yyGLRState* yystate, yyGLRState* rhs, yyRuleNum yyrule) { - yySemanticOption* yynewItem; - yynewItem = &yystack->yynextFree->yyoption; - yystack->yyspaceLeft -= 1; - yystack->yynextFree += 1; - yynewItem->yyisState = yyfalse; - yynewItem->yystate = rhs; - yynewItem->yyrule = yyrule; - yynewItem->yynext = yystate->yysemantics.yyfirstVal; - yystate->yysemantics.yyfirstVal = yynewItem; - if (yystack->yyspaceLeft < YYHEADROOM) - yyexpandGLRStack (yystack); + yySemanticOption* yynewOption = + &yynewGLRStackItem (yystack, yyfalse)->yyoption; + yynewOption->yystate = rhs; + yynewOption->yyrule = yyrule; + yynewOption->yynext = yystate->yysemantics.yyfirstVal; + yystate->yysemantics.yyfirstVal = yynewOption; + + YY_RESERVE_GLRSTACK (yystack); } /* GLRStacks */ @@ -970,14 +1111,19 @@ 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) \ + +#if YYSTACKEXPANDABLE +# define YYRELOC(YYFROMITEMS,YYTOITEMS,YYX,YYTYPE) \ &((YYTOITEMS) - ((YYFROMITEMS) - (yyGLRStackItem*) (YYX)))->YYTYPE /** If STACK is expandable, extend it. WARNING: Pointers into the @@ -988,7 +1134,6 @@ yyinitGLRStack (yyGLRStack* yystack, size_t yysize) static void yyexpandGLRStack (yyGLRStack* yystack) { -#if YYSTACKEXPANDABLE yyGLRStackItem* yynewItems; yyGLRStackItem* yyp0, *yyp1; size_t yysize, yynewSize; @@ -1041,11 +1186,8 @@ yyexpandGLRStack (yyGLRStack* yystack) yystack->yyitems = yynewItems; yystack->yynextFree = yynewItems + yysize; yystack->yyspaceLeft = yynewSize - yysize; - -#else - yyMemoryExhausted (yystack); -#endif } +#endif static void yyfreeGLRStack (yyGLRStack* yystack) @@ -1123,21 +1265,17 @@ yyglrShift (yyGLRStack* yystack, size_t yyk, yyStateNum yylrState, size_t yyposn, YYSTYPE yysval, YYLTYPE* yylocp) { - yyGLRStackItem* yynewItem; + yyGLRState* yynewState = &yynewGLRStackItem (yystack, yytrue)->yystate; - yynewItem = yystack->yynextFree; - yystack->yynextFree += 1; - yystack->yyspaceLeft -= 1; - yynewItem->yystate.yyisState = yytrue; - yynewItem->yystate.yylrState = yylrState; - yynewItem->yystate.yyposn = yyposn; - yynewItem->yystate.yyresolved = yytrue; - yynewItem->yystate.yypred = yystack->yytops.yystates[yyk]; - yystack->yytops.yystates[yyk] = &yynewItem->yystate; - yynewItem->yystate.yysemantics.yysval = yysval; - yynewItem->yystate.yyloc = *yylocp; - if (yystack->yyspaceLeft < YYHEADROOM) - yyexpandGLRStack (yystack); + yynewState->yylrState = yylrState; + yynewState->yyposn = yyposn; + yynewState->yyresolved = yytrue; + yynewState->yypred = yystack->yytops.yystates[yyk]; + yynewState->yysemantics.yysval = yysval; + yynewState->yyloc = *yylocp; + yystack->yytops.yystates[yyk] = yynewState; + + YY_RESERVE_GLRSTACK (yystack); } /** Shift stack #K of YYSTACK, to a new state corresponding to LR @@ -1147,19 +1285,17 @@ static inline void yyglrShiftDefer (yyGLRStack* yystack, size_t yyk, yyStateNum yylrState, size_t yyposn, yyGLRState* rhs, yyRuleNum yyrule) { - yyGLRStackItem* yynewItem; + yyGLRState* yynewState = &yynewGLRStackItem (yystack, yytrue)->yystate; - yynewItem = yystack->yynextFree; - yynewItem->yystate.yyisState = yytrue; - yynewItem->yystate.yylrState = yylrState; - yynewItem->yystate.yyposn = yyposn; - yynewItem->yystate.yyresolved = yyfalse; - yynewItem->yystate.yypred = yystack->yytops.yystates[yyk]; - yynewItem->yystate.yysemantics.yyfirstVal = NULL; - yystack->yytops.yystates[yyk] = &yynewItem->yystate; - yystack->yynextFree += 1; - yystack->yyspaceLeft -= 1; - yyaddDeferredAction (yystack, &yynewItem->yystate, rhs, yyrule); + yynewState->yylrState = yylrState; + yynewState->yyposn = yyposn; + yynewState->yyresolved = yyfalse; + yynewState->yypred = yystack->yytops.yystates[yyk]; + yynewState->yysemantics.yyfirstVal = NULL; + yystack->yytops.yystates[yyk] = yynewState; + + /* Invokes YY_RESERVE_GLRSTACK. */ + yyaddDeferredAction (yystack, yynewState, rhs, yyrule); } /** Pop the symbols consumed by reduction #RULE from the top of stack @@ -1187,11 +1323,18 @@ yydoAction (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, } else { + /* At present, doAction is never called in nondeterministic + * mode, so this branch is never taken. It is here in + * anticipation of a future feature that will allow immediate + * evaluation of selected actions in nondeterministic mode. */ int yyi; yyGLRState* yys; yyGLRStackItem yyrhsVals[YYMAXRHS + YYMAXLEFT + 1]; yys = yyrhsVals[YYMAXRHS + YYMAXLEFT].yystate.yypred - = yystack->yytops.yystates[yyk]; + = yystack->yytops.yystates[yyk];]b4_location_if([[ + if (yynrhs == 0) + /* Set default location. */ + yyrhsVals[YYMAXRHS + YYMAXLEFT - 1].yystate.yyloc = yys->yyloc;]])[ for (yyi = 0; yyi < yynrhs; yyi += 1) { yys = yys->yypred; @@ -1205,29 +1348,42 @@ yydoAction (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, } #if !YYDEBUG -# define YY_REDUCE_PRINT(K, Rule) +# define YY_REDUCE_PRINT(Args) #else -# define YY_REDUCE_PRINT(K, Rule) \ +# define YY_REDUCE_PRINT(Args) \ do { \ if (yydebug) \ - yy_reduce_print (K, Rule); \ -} while (0) + yy_reduce_print Args; \ +} while (/*CONSTCOND*/ 0) /*----------------------------------------------------------. | Report that the RULE is going to be reduced on stack #K. | `----------------------------------------------------------*/ static inline void -yy_reduce_print (size_t yyk, yyRuleNum yyrule) +yy_reduce_print (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, + YYSTYPE* yyvalp, YYLTYPE* yylocp]b4_user_formals[) { + int yynrhs = yyrhsLength (yyrule); + yybool yynormal __attribute__ ((__unused__)) = + (yystack->yysplitPoint == NULL); + yyGLRStackItem* yyvsp = (yyGLRStackItem*) yystack->yytops.yystates[yyk]; + int yylow = 1; int yyi; - YYFPRINTF (stderr, "Reducing stack %lu by rule %d (line %lu), ", + YYFPRINTF (stderr, "Reducing stack %lu by rule %d (line %lu):\n", (unsigned long int) yyk, yyrule - 1, (unsigned long int) yyrline[yyrule]); /* Print the symbols being reduced, and their result. */ - for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++) - YYFPRINTF (stderr, "%s ", yytokenName (yyrhs[yyi])); - YYFPRINTF (stderr, "-> %s\n", yytokenName (yyr1[yyrule])); + //for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++) + for (yyi = 0; yyi < yynrhs; yyi++) + { + fprintf (stderr, " $%d = ", yyi); + yysymprint (stderr, yyrhs[yyprhs[yyrule] + yyi], + &]b4_rhs_value(yynrhs, yyi)[ + ]b4_location_if([, &]b4_rhs_location(yynrhs, yyi))[]dnl + b4_user_args[); + fprintf (stderr, "\n"); + } } #endif @@ -1253,8 +1409,9 @@ yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, YYSTYPE yysval; YYLTYPE yyloc; - YY_REDUCE_PRINT (yyk, yyrule); + YY_REDUCE_PRINT ((yystack, yyk, yyrule, &yysval, &yyloc]b4_user_args[)); YYCHK (yydoAction (yystack, yyk, yyrule, &yysval, &yyloc]b4_user_args[)); + YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyrule], &yysval, &yyloc); yyglrShift (yystack, yyk, yyLRgotoState (yystack->yytops.yystates[yyk]->yylrState, yylhsNonterm (yyrule)), @@ -1465,7 +1622,10 @@ yyresolveAction (yySemanticOption* yyopt, yyGLRStack* yystack, yynrhs = yyrhsLength (yyopt->yyrule); YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack]b4_user_args[)); - yyrhsVals[YYMAXRHS + YYMAXLEFT].yystate.yypred = yyopt->yystate; + yyrhsVals[YYMAXRHS + YYMAXLEFT].yystate.yypred = yyopt->yystate;]b4_location_if([[ + if (yynrhs == 0) + /* Set default location. */ + yyrhsVals[YYMAXRHS + YYMAXLEFT - 1].yystate.yyloc = yyopt->yystate->yyloc;]])[ return yyuserAction (yyopt->yyrule, yynrhs, yyrhsVals + YYMAXRHS + YYMAXLEFT - 1, yyvalp, yylocp, yystack]b4_user_args[); @@ -1519,13 +1679,15 @@ 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[) { - /* `Unused' warnings. */ - (void) yyx0; - (void) yyx1; + YYUSE (yyx0); + YYUSE (yyx1); #if YYDEBUG YYFPRINTF (stderr, "Ambiguity detected.\n"); @@ -1546,35 +1708,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 +1855,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 +1877,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; } @@ -1724,9 +1900,8 @@ static void yyreportSyntaxError (yyGLRStack* yystack, YYSTYPE* yylvalp, YYLTYPE* yyllocp]b4_user_formals[) { - /* `Unused' warnings. */ - (void) yylvalp; - (void) yyllocp; + YYUSE (yylvalp); + YYUSE (yyllocp); if (yystack->yyerrState == 0) { @@ -1736,7 +1911,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 +1953,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 +1976,7 @@ yyreportSyntaxError (yyGLRStack* yystack, { if (*yyp == '%' && yyf[1] == 's' && yyi < yycount) { - yyp = yystpcpy (yyp, yyarg[yyi++]); + yyp += yytnamerr (yyp, yyarg[yyi++]); yyf += 2; } else @@ -1856,13 +2031,12 @@ yyrecoverSyntaxError (yyGLRStack* yystack, yyerror_range[2].yystate.yyloc = *yyllocp; YYLLOC_DEFAULT (yys->yyloc, yyerror_range, 2);]])[ yydestruct ("Error: discarding", - *yytokenp, yylvalp]b4_location_if([, yyllocp])[); + *yytokenp, yylvalp]b4_location_if([, yyllocp])[]b4_user_args[); } YYDPRINTF ((stderr, "Reading a token: ")); 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 +2079,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 +2086,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]b4_user_args[); yystack->yytops.yystates[0] = yys->yypred; yystack->yynextFree -= 1; yystack->yyspaceLeft += 1; @@ -1927,7 +2098,7 @@ yyrecoverSyntaxError (yyGLRStack* yystack, #define YYCHK1(YYE) \ do { \ switch (YYE) { \ - default: \ + case yyok: \ break; \ case yyabort: \ goto yyabortlab; \ @@ -1935,8 +2106,10 @@ yyrecoverSyntaxError (yyGLRStack* yystack, goto yyacceptlab; \ case yyerr: \ goto yyuser_error; \ + default: \ + goto yybuglab; \ } \ - } while (0) + } while (/*CONSTCOND*/ 0) /*----------. @@ -1982,10 +2155,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 +2201,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 +2208,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 +2265,10 @@ b4_syncline([@oline@], [@ofile@])])dnl yyresult = 0; goto yyreturn; + yybuglab: + YYASSERT (yyfalse); + /* Fall through. */ + yyabortlab: yyresult = 1; goto yyreturn; @@ -2103,30 +2280,36 @@ b4_syncline([@oline@], [@ofile@])])dnl yyreturn: if (yytoken != YYEOF && yytoken != YYEMPTY) - yydestruct ("Error: discarding lookahead", - yytoken, yylvalp]b4_location_if([, yyllocp])[); + yydestruct ("Cleanup: discarding lookahead", + yytoken, yylvalp]b4_location_if([, yyllocp])[]b4_user_args[); - /* Now pop stack until empty, destroying its entries as we go. */ - while (yystack.yytops.yystates[0] != NULL) + /* 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 *yys = yystack.yytops.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])[); - yystack.yytops.yystates[0] = yys->yypred; - yystack.yynextFree -= 1; - yystack.yyspaceLeft += 1; + 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;]] +)[ yydestroyGLRState ("Cleanup: popping", yys]b4_user_args[); + 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) @@ -2199,39 +2382,11 @@ b4_epilogue m4_if(b4_defines_flag, 0, [], [@output @output_header_name@ b4_copyright([Skeleton parser for GLR parsing with Bison], - [2002, 2003, 2004, 2005]) - -b4_token_defines(b4_tokens) + [2002, 2003, 2004, 2005]) -#if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED) -m4_ifdef([b4_stype], -[b4_syncline([b4_stype_line], [b4_filename]) -typedef union m4_bregexp(b4_stype, [^{], [YYSTYPE ])b4_stype YYSTYPE; -/* Line __line__ of glr.c. */ -b4_syncline([@oline@], [@ofile@])], -[typedef int YYSTYPE;]) -# define YYSTYPE_IS_DECLARED 1 -# define YYSTYPE_IS_TRIVIAL 1 -#endif - -b4_pure_if([], -[extern YYSTYPE b4_prefix[]lval;]) +b4_shared_declarations -#if ! defined (YYLTYPE) && ! defined (YYLTYPE_IS_DECLARED) -typedef struct YYLTYPE -{ -b4_location_if([ - int first_line; - int first_column; - int last_line; - int last_column; -],[ - char yydummy; -]) -} YYLTYPE; -# define YYLTYPE_IS_DECLARED 1 -# define YYLTYPE_IS_TRIVIAL 1 -#endif +extern YYSTYPE b4_prefix[]lval; b4_location_if([b4_pure_if([], [extern YYLTYPE b4_prefix[]lloc;])