X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/b0400cc6ef2e204e20964f38fd745e6f65e5c1ed..5dac0025f6cef52c95cf1b8d04c5047980a73160:/data/glr.c diff --git a/data/glr.c b/data/glr.c index 7191458f..461a6313 100644 --- a/data/glr.c +++ b/data/glr.c @@ -243,10 +243,6 @@ b4_syncline([@oline@], [@ofile@]) # define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) #endif -#if ! defined (__GNUC__) -# define inline -#endif - /* YYFINAL -- State number of the termination state. */ #define YYFINAL ]b4_final_state_number[ /* YYLAST -- Last index in YYTABLE. */ @@ -364,7 +360,7 @@ static const ]b4_int_type_for([b4_pgoto])[ yypgoto[] = /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If positive, shift that token. If negative, reduce the rule which number is the opposite. If zero, do what YYDEFACT says. - If YYTABLE_NINF, parse error. */ + If YYTABLE_NINF, syntax error. */ #define YYTABLE_NINF ]b4_table_ninf[ static const ]b4_int_type_for([b4_table])[ yytable[] = { @@ -395,6 +391,13 @@ static const ]b4_int_type_for([b4_check])[ yycheck[] = ]b4_check[ }; +/* YYSTOS[STATE-NUM] -- The (internal number of the) accessing + symbol of state STATE-NUM. */ +static const ]b4_int_type_for([b4_stos])[ yystos[] = +{ + ]b4_stos[ +}; + /* Prevent warning if -Wmissing-prototypes. */ ]b4_c_ansi_function_decl([yyparse], [int], b4_parse_param)[ @@ -451,12 +454,37 @@ do { \ if (yydebug) \ YYFPRINTF Args; \ } while (0) + +]b4_yysymprint_generate([b4_c_ansi_function_def])[ + +# define YYDSYMPRINT(Args) \ +do { \ + if (yydebug) \ + yysymprint Args; \ +} while (0) + +# define YYDSYMPRINTF(Title, Token, Value, Location) \ +do { \ + if (yydebug) \ + { \ + YYFPRINTF (stderr, "%s ", Title); \ + yysymprint (stderr, \ + Token, Value]b4_location_if([, Location])[); \ + YYFPRINTF (stderr, "\n"); \ + } \ +} while (0) + /* Nonzero means print parse trace. It is left uninitialized so that multiple parsers can coexist. */ int yydebug; + #else /* !YYDEBUG */ + /* Avoid empty `if' bodies. */ -# define YYDPRINTF(Args) {} +# define YYDPRINTF(Args) {} +# define YYDSYMPRINT(Args) {} +# define YYDSYMPRINTF(Title, Token, Value, Location) {} + #endif /* !YYDEBUG */ /* YYINITDEPTH -- initial size of the parser's stacks. */ @@ -566,12 +594,12 @@ static void yyfreeGLRStack (yyGLRStack* yystack); static void yyFail (yyGLRStack* yystack]b4_pure_formals[, const char* yyformat, ...) { + yystack->yyerrflag = 1; if (yyformat != NULL) { char yymsg[256]; va_list yyap; va_start (yyap, yyformat); - yystack->yyerrflag = 1; vsprintf (yymsg, yyformat, yyap); yyerror (]b4_yyerror_args[yymsg); } @@ -611,8 +639,6 @@ yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem* yyvsp, *yyvalp = yyvsp[1-yyrhslen].yystate.yysemantics.yysval; *yylocp = yyvsp[1-yyrhslen].yystate.yyloc; } -# undef yyval -# define yyval (*yyvalp) # undef yyerrok # define yyerrok (yystack->yyerrState = 0) # undef YYACCEPT @@ -639,7 +665,6 @@ yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem* yyvsp, } return yyok; -# undef yyval # undef yyerrok # undef YYABORT # undef YYACCEPT @@ -669,6 +694,8 @@ yyuserMerge (int yyn, YYSTYPE* yy0, YYSTYPE* yy1) [ /* Bison grammar-table manipulation. */ +]b4_yydestruct_generate([b4_c_ansi_function_def])[ + /** Number of symbols composing the right hand side of rule #RULE. */ static inline int yyrhsLength (yyRuleNum yyrule) @@ -839,8 +866,7 @@ yyexpandGLRStack (yyGLRStack* yystack]b4_pure_formals[) size_t yyn; yysize = yystack->yynextFree - yystack->yyitems; if (YYMAXDEPTH <= yysize) - yyFail (yystack][]b4_pure_args[, - "parsing stack overflow (%d items)", yysize); + yyFail (yystack][]b4_pure_args[, "parser stack overflow"); yynewSize = 2*yysize; if (YYMAXDEPTH < yynewSize) yynewSize = YYMAXDEPTH; @@ -887,8 +913,7 @@ yyexpandGLRStack (yyGLRStack* yystack]b4_pure_formals[) #else - yyFail (yystack][]b4_lpure_args[, - "parsing stack overflow (%d items)", yysize); + yyFail (yystack][]b4_lpure_args[, "parser stack overflow"); #endif } @@ -1007,7 +1032,7 @@ yyglrShiftDefer (yyGLRStack* yystack, int yyk, yyStateNum yylrState, * have been previously resolved. Set *VALP to the resulting value, * and *LOCP to the computed location (if any). Return value is as * for userAction. */ -static inline int +static inline YYRESULTTAG yydoAction (yyGLRStack* yystack, int yyk, yyRuleNum yyrule, YYSTYPE* yyvalp, YYLTYPE* yylocp]b4_user_formals[) { @@ -1081,12 +1106,12 @@ static inline void yy_reduce_print (size_t yyk, yyRuleNum yyrule) { int yyi; - YYDPRINTF ((stderr, "Reducing stack %d by rule %d (line %d),", + 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]; 0 <= yyrhs[yyi]; yyi++) YYFPRINTF (stderr, "%s ", yytokenName (yyrhs[yyi])); - YYFPRINTF (stderr, " -> %s\n", yytokenName (yyr1[yyrule])); + YYFPRINTF (stderr, "-> %s\n", yytokenName (yyr1[yyrule])); } #endif @@ -1153,7 +1178,7 @@ yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, yymarkStackDeleted (yystack, yyk); YYDPRINTF ((stderr, "Merging stack %d into stack %d.\n", yyk, yyi)); - return 0; + return yyok; } yyp = yyp->yypred; } @@ -1161,7 +1186,7 @@ yyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, yystack->yytops.yystates[yyk] = yys; yyglrShiftDefer (yystack, yyk, yynewLRState, yyposn, yys0, yyrule]b4_pure_args[); } - return 0; + return yyok; } static int @@ -1507,8 +1532,7 @@ yyprocessOneStack (yyGLRStack* yystack, int yyk, YYDPRINTF ((stderr, "Reading a token: ")); yychar = YYLEX; *yytokenp = YYTRANSLATE (yychar); - YYDPRINTF ((stderr, "Next token is %s\n", - yytokenName (*yytokenp))); + YYDSYMPRINTF ("Next token is", *yytokenp, yylvalp, yyllocp); } yygetLRActions (yystate, *yytokenp, &yyaction, &yyconflicts); @@ -1570,7 +1594,7 @@ yyreportParseError (yyGLRStack* yystack, yycount = 0; /* Start YYX at -YYN if negative to avoid negative indexes in YYCHECK. */ - yysize = sizeof ("parse error, unexpected ") + yysize = sizeof ("syntax error, unexpected ") + strlen (yytokenName (*yytokenp)); yyprefix = ", expecting "; for (yyx = yyn < 0 ? -yyn : 0; yyx < yytname_size && yycount <= 5; @@ -1579,7 +1603,8 @@ yyreportParseError (yyGLRStack* yystack, yysize += strlen (yytokenName (yyx)) + strlen (yyprefix), yycount += 1, yyprefix = " or "; yymsg = yyp = (char*) malloc (yysize); - sprintf (yyp, "parse error, unexpected %s", yytokenName (*yytokenp)); + sprintf (yyp, "syntax error, unexpected %s", + yytokenName (*yytokenp)); yyp += strlen (yyp); if (yycount < 5) { @@ -1597,7 +1622,7 @@ yyreportParseError (yyGLRStack* yystack, } else #endif - yyerror (]b4_lyyerror_args["parse error"); + yyerror (]b4_lyyerror_args["syntax error"); yynerrs += 1; } } @@ -1621,14 +1646,32 @@ yyrecoverParseError (yyGLRStack* yystack, while (yytrue) { if (*yytokenp == YYEOF) - yyFail (yystack][]b4_lpure_args[, NULL); + { + /* Now pop stack until we find a state that shifts the + error token. */ + while (yystack->yytops.yystates[0] != NULL) + { + yyGLRState *yys = yystack->yytops.yystates[0]; + YYDSYMPRINTF ("Error: popping", + yystos[yys->yylrState], + &yys->yysemantics.yysval, &yys->yyloc); + yydestruct (yystos[yys->yylrState], + &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[); + yystack->yytops.yystates[0] = yys->yypred; + yystack->yynextFree -= 1; + yystack->yyspaceLeft += 1; + } + yyFail (yystack][]b4_lpure_args[, NULL); + } if (*yytokenp != YYEMPTY) - YYDPRINTF ((stderr, "Discarding token %s\n", - yytokenName (*yytokenp))); + { + YYDSYMPRINTF ("Error: discarding", *yytokenp, yylvalp, yyllocp); + yydestruct (*yytokenp, yylvalp]b4_location_if([, yyllocp])[); + } YYDPRINTF ((stderr, "Reading a token: ")); yychar = YYLEX; *yytokenp = YYTRANSLATE (yychar); - YYDPRINTF ((stderr, "Next token is %s\n", yytokenName (*yytokenp))); + YYDSYMPRINTF ("Next token is", *yytokenp, yylvalp, yyllocp); yyj = yypact[yystack->yytops.yystates[0]->yylrState]; if (yyis_pact_ninf (yyj)) /* Something's not right; we shouldn't be here. */ @@ -1657,20 +1700,28 @@ yyrecoverParseError (yyGLRStack* yystack, /* Now pop stack until we find a state that shifts the error token. */ while (yystack->yytops.yystates[0] != NULL) { - yyj = yypact[yystack->yytops.yystates[0]->yylrState]; + yyGLRState *yys = yystack->yytops.yystates[0]; + yyj = yypact[yys->yylrState]; if (! yyis_pact_ninf (yyj)) { yyj += YYTERROR; if (0 <= yyj && yyj <= YYLAST && yycheck[yyj] == YYTERROR && yyisShiftAction (yytable[yyj])) { + YYDPRINTF ((stderr, "Shifting error token, ")); yyglrShift (yystack, 0, yytable[yyj], - yystack->yytops.yystates[0]->yyposn, - *yylvalp, yyllocp]b4_user_args[); + yys->yyposn, *yylvalp, yyllocp]b4_user_args[); + YYDPRINTF ((stderr, "Entering state %d\n", + yystack->yytops.yystates[0]->yylrState)); break; } } - yystack->yytops.yystates[0] = yystack->yytops.yystates[0]->yypred; + YYDSYMPRINTF ("Error: popping", + yystos[yys->yylrState], + &yys->yysemantics.yysval, &yys->yyloc); + yydestruct (yystos[yys->yylrState], + &yys->yysemantics.yysval]b4_location_if([, &yys->yyloc])[); + yystack->yytops.yystates[0] = yys->yypred; yystack->yynextFree -= 1; yystack->yyspaceLeft += 1; } @@ -1760,8 +1811,7 @@ yyrecoverParseError (yyGLRStack* yystack, YYDPRINTF ((stderr, "Reading a token: ")); yychar = YYLEX; yytoken = YYTRANSLATE (yychar); - YYDPRINTF ((stderr, "Next token is %s\n", - yytokenName (yytoken))); + YYDSYMPRINTF ("Next token is", yytoken, yylvalp, yyllocp); } yygetLRActions (yystate, yytoken, &yyaction, &yyconflicts); if (*yyconflicts != 0) @@ -1804,7 +1854,7 @@ yyrecoverParseError (yyGLRStack* yystack, { yyundeleteLastStack (&yystack); if (yystack.yytops.yysize == 0) - yyFail (&yystack][]b4_lpure_args[, "parse error"); + yyFail (&yystack][]b4_lpure_args[, "syntax error"); YYCHK1 (yyresolveStack (&yystack]b4_user_args[)); YYDPRINTF ((stderr, "Returning to deterministic operation.\n")); yyreportParseError (&yystack, yylvalp, yyllocp]b4_user_args[);