📄 glr.c
字号:
} yyj += 1; } yyi += 1; }}/** Shift to a new state on stack #K of STACK, corresponding to LR state * LRSTATE, at input position POSN, with (resolved) semantic value SVAL. */static inline voidyyglrShift (yyGLRStack* yystack, int yyk, yyStateNum yylrState, size_t yyposn, YYSTYPE yysval, YYLTYPE* yylocp]b4_user_formals[){ yyGLRStackItem* yynewItem; 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]b4_pure_args[);}/** Shift stack #K of YYSTACK, to a new state corresponding to LR * state YYLRSTATE, at input position YYPOSN, with the (unresolved) * semantic value of YYRHS under the action for YYRULE. */static inline voidyyglrShiftDefer (yyGLRStack* yystack, int yyk, yyStateNum yylrState, size_t yyposn, yyGLRState* rhs, yyRuleNum yyrule]b4_pure_formals[){ yyGLRStackItem* yynewItem; 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]b4_pure_args[);}/** Pop the symbols consumed by reduction #RULE from the top of stack * #K of STACK, and perform the appropriate semantic action on their * semantic values. Assumes that all ambiguities in semantic values * 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 YYRESULTTAGyydoAction (yyGLRStack* yystack, int yyk, yyRuleNum yyrule, YYSTYPE* yyvalp, YYLTYPE* yylocp]b4_user_formals[){ int yynrhs = yyrhsLength (yyrule); if (yystack->yysplitPoint == NULL) { /* Standard special case: single stack. */ yyGLRStackItem* rhs = (yyGLRStackItem*) yystack->yytops.yystates[yyk]; YYASSERT (yyk == 0); yystack->yynextFree -= yynrhs; yystack->yyspaceLeft += yynrhs; yystack->yytops.yystates[0] = & yystack->yynextFree[-1].yystate; return yyuserAction (yyrule, yynrhs, rhs, yyvalp, yylocp, yystack]b4_user_args[); } else { int yyi; yyGLRState* yys; yyGLRStackItem yyrhsVals[YYMAXRHS + YYMAXLEFT + 1]; yys = yyrhsVals[YYMAXRHS + YYMAXLEFT].yystate.yypred = yystack->yytops.yystates[yyk]; for (yyi = 0; yyi < yynrhs; yyi += 1) { yys = yys->yypred; YYASSERT (yys); } yyupdateSplit (yystack, yys); yystack->yytops.yystates[yyk] = yys; return yyuserAction (yyrule, yynrhs, yyrhsVals + YYMAXRHS + YYMAXLEFT - 1, yyvalp, yylocp, yystack]b4_user_args[); }}#if !YYDEBUG# define YY_REDUCE_PRINT(K, Rule)#else# define YY_REDUCE_PRINT(K, Rule) \do { \ if (yydebug) \ yy_reduce_print (K, Rule); \} while (0)/*----------------------------------------------------------.| Report that the RULE is going to be reduced on stack #K. |`----------------------------------------------------------*/static inline voidyy_reduce_print (size_t yyk, yyRuleNum yyrule){ int yyi; YYFPRINTF (stderr, "Reducing stack %lu by rule %d (line %lu), ", (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]));}#endif/** Pop items off stack #K of STACK according to grammar rule RULE, * and push back on the resulting nonterminal symbol. Perform the * semantic action associated with RULE and store its value with the * newly pushed state, if FORCEEVAL or if STACK is currently * unambiguous. Otherwise, store the deferred semantic action with * the new state. If the new state would have an identical input * position, LR state, and predecessor to an existing state on the stack, * it is identified with that existing state, eliminating stack #K from * the STACK. In this case, the (necessarily deferred) semantic value is * added to the options for the existing state's semantic value. */static inline YYRESULTTAGyyglrReduce (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, yybool yyforceEval]b4_pure_formals[){ size_t yyposn = yystack->yytops.yystates[yyk]->yyposn; if (yyforceEval || yystack->yysplitPoint == NULL) { YYSTYPE yysval; YYLTYPE yyloc; YY_REDUCE_PRINT (yyk, yyrule); YYCHK (yydoAction (yystack, yyk, yyrule, &yysval, &yyloc]b4_user_args[)); yyglrShift (yystack, yyk, yyLRgotoState (yystack->yytops.yystates[yyk]->yylrState, yylhsNonterm (yyrule)), yyposn, yysval, &yyloc]b4_user_args[); } else { size_t yyi; int yyn; yyGLRState* yys, *yys0 = yystack->yytops.yystates[yyk]; yyStateNum yynewLRState; for (yys = yystack->yytops.yystates[yyk], yyn = yyrhsLength (yyrule); 0 < yyn; yyn -= 1) { yys = yys->yypred; YYASSERT (yys); } yyupdateSplit (yystack, yys); yynewLRState = yyLRgotoState (yys->yylrState, yylhsNonterm (yyrule)); YYDPRINTF ((stderr, "Reduced stack %lu by rule #%d; action deferred. Now in state %d.\n", (unsigned long int) yyk, yyrule - 1, yynewLRState)); for (yyi = 0; yyi < yystack->yytops.yysize; yyi += 1) if (yyi != yyk && yystack->yytops.yystates[yyi] != NULL) { yyGLRState* yyp, *yysplit = yystack->yysplitPoint; yyp = yystack->yytops.yystates[yyi]; while (yyp != yys && yyp != yysplit && yyp->yyposn >= yyposn) { if (yyp->yylrState == yynewLRState && yyp->yypred == yys) { yyaddDeferredAction (yystack, yyp, yys0, yyrule]b4_pure_args[); yymarkStackDeleted (yystack, yyk); YYDPRINTF ((stderr, "Merging stack %lu into stack %lu.\n", (unsigned long int) yyk, (unsigned long int) yyi)); return yyok; } yyp = yyp->yypred; } } yystack->yytops.yystates[yyk] = yys; yyglrShiftDefer (yystack, yyk, yynewLRState, yyposn, yys0, yyrule]b4_pure_args[); } return yyok;}static intyysplitStack (yyGLRStack* yystack, int yyk){ if (yystack->yysplitPoint == NULL) { YYASSERT (yyk == 0); yystack->yysplitPoint = yystack->yytops.yystates[yyk]; } if (yystack->yytops.yysize >= yystack->yytops.yycapacity) { yystack->yytops.yycapacity *= 2; yystack->yytops.yystates = (yyGLRState**) YYREALLOC (yystack->yytops.yystates, (yystack->yytops.yycapacity * sizeof yystack->yytops.yystates[0])); } yystack->yytops.yystates[yystack->yytops.yysize] = yystack->yytops.yystates[yyk]; yystack->yytops.yysize += 1; return yystack->yytops.yysize-1;}/** True iff Y0 and Y1 represent identical options at the top level. * That is, they represent the same rule applied to RHS symbols * that produce the same terminal symbols. */static yyboolyyidenticalOptions (yySemanticOption* yyy0, yySemanticOption* yyy1){ if (yyy0->yyrule == yyy1->yyrule) { yyGLRState *yys0, *yys1; int yyn; for (yys0 = yyy0->yystate, yys1 = yyy1->yystate, yyn = yyrhsLength (yyy0->yyrule); yyn > 0; yys0 = yys0->yypred, yys1 = yys1->yypred, yyn -= 1) if (yys0->yyposn != yys1->yyposn) return yyfalse; return yytrue; } else return yyfalse;}/** Assuming identicalOptions (Y0,Y1), (destructively) merge the * alternative semantic values for the RHS-symbols of Y1 into the * corresponding semantic value sets of the symbols of Y0. */static voidyymergeOptionSets (yySemanticOption* yyy0, yySemanticOption* yyy1){ yyGLRState *yys0, *yys1; int yyn; for (yys0 = yyy0->yystate, yys1 = yyy1->yystate, yyn = yyrhsLength (yyy0->yyrule); yyn > 0; yys0 = yys0->yypred, yys1 = yys1->yypred, yyn -= 1) if (yys0 == yys1) break; else if (! yys0->yyresolved && ! yys1->yyresolved) { yySemanticOption* yyz; for (yyz = yys0->yysemantics.yyfirstVal; yyz->yynext != NULL; yyz = yyz->yynext) continue; yyz->yynext = yys1->yysemantics.yyfirstVal; }}/** Y0 and Y1 represent two possible actions to take in a given * parsing state; return 0 if no combination is possible, * 1 if user-mergeable, 2 if Y0 is preferred, 3 if Y1 is preferred. */static intyypreference (yySemanticOption* y0, yySemanticOption* y1){ yyRuleNum r0 = y0->yyrule, r1 = y1->yyrule; int p0 = yydprec[r0], p1 = yydprec[r1]; if (p0 == p1) { if (yymerger[r0] == 0 || yymerger[r0] != yymerger[r1]) return 0; else return 1; } if (p0 == 0 || p1 == 0) return 0; if (p0 < p1) return 3; if (p1 < p0) return 2; return 0;}static YYRESULTTAG yyresolveValue (yySemanticOption* yyoptionList, yyGLRStack* yystack, YYSTYPE* yyvalp, YYLTYPE* yylocp]b4_user_formals[);static YYRESULTTAGyyresolveStates (yyGLRState* yys, int yyn, yyGLRStack* yystack]b4_user_formals[){ YYRESULTTAG yyflag; if (0 < yyn) { YYASSERT (yys->yypred); yyflag = yyresolveStates (yys->yypred, yyn-1, yystack]b4_user_args[); if (yyflag != yyok) return yyflag; if (! yys->yyresolved) { yyflag = yyresolveValue (yys->yysemantics.yyfirstVal, yystack, &yys->yysemantics.yysval, &yys->yyloc ]b4_user_args[); if (yyflag != yyok) return yyflag; yys->yyresolved = yytrue; } } return yyok;}static YYRESULTTAGyyresolveAction (yySemanticOption* yyopt, yyGLRStack* yystack, YYSTYPE* yyvalp, YYLTYPE* yylocp]b4_user_formals[){ yyGLRStackItem yyrhsVals[YYMAXRHS + YYMAXLEFT + 1]; int yynrhs; yynrhs = yyrhsLength (yyopt->yyrule); YYCHK (yyresolveStates (yyopt->yystate, yynrhs, yystack]b4_user_args[)); yyrhsVals[YYMAXRHS + YYMAXLEFT].yystate.yypred = yyopt->yystate; return yyuserAction (yyopt->yyrule, yynrhs, yyrhsVals + YYMAXRHS + YYMAXLEFT - 1, yyvalp, yylocp, yystack]b4_user_args[);}#if YYDEBUGstatic voidyyreportTree (yySemanticOption* yyx, int yyindent){ int yynrhs = yyrhsLength (yyx->yyrule); int yyi; yyGLRState* yys; yyGLRState* yystates[YYMAXRHS]; yyGLRState yyleftmost_state; for (yyi = yynrhs, yys = yyx->yystate; 0 < yyi; yyi -= 1, yys = yys->yypred) yystates[yyi] = yys; if (yys == NULL) { yyleftmost_state.yyposn = 0; yystates[0] = &yyleftmost_state; } else yystates[0] = yys; if (yyx->yystate->yyposn < yys->yyposn + 1) YYFPRINTF (stderr, "%*s%s -> <Rule %d, empty>\n", yyindent, "", yytokenName (yylhsNonterm (yyx->yyrule)), yyx->yyrule); else YYFPRINTF (stderr, "%*s%s -> <Rule %d, tokens %lu .. %lu>\n", yyindent, "", yytokenName (yylhsNonterm (yyx->yyrule)), yyx->yyrule, (unsigned long int) (yys->yyposn + 1), (unsigned long int) yyx->yystate->yyposn); for (yyi = 1; yyi <= yynrhs; yyi += 1) { if (yystates[yyi]->yyresolved) { if (yystates[yyi-1]->yyposn+1 > yystates[yyi]->yyposn) YYFPRINTF (stderr, "%*s%s <empty>\n", yyindent+2, "", yytokenName (yyrhs[yyprhs[yyx->yyrule]+yyi-1])); else YYFPRINTF (stderr, "%*s%s <tokens %lu .. %lu>\n", yyindent+2, "", yytokenName (yyrhs[yyprhs[yyx->yyrule]+yyi-1]), (unsigned long int) (yystates[yyi - 1]->yyposn + 1), (unsigned long int) yystates[yyi]->yyposn); } else yyreportTree (yystates[yyi]->yysemantics.yyfirstVal, yyindent+2); }}#endifstatic voidyyreportAmbiguity (yySemanticOption* yyx0, yySemanticOption* yyx1, yyGLRStack* yystack]b4_pure_formals[){ /* `Unused' warnings. */ (void) yyx0; (void) yyx1;#if YYDEBUG YYFPRINTF (stderr, "Ambiguity detected.\n"); YYFPRINTF (stderr, "Option 1,\n"); yyreportTree (yyx0, 2); YYFPRINTF (stderr, "\nOption 2,\n"); yyreportTree (yyx1, 2); YYFPRINTF (stderr, "\n");#endif yyFail (yystack][]b4_pure_args[, "ambiguity detected");}/** Resolve the ambiguity represented by OPTIONLIST, perform the indicated * actions, and return the result. */static YYRESULTTAGyyresolveValue (yySemanticOption* yyoptionList, yyGLRStack* yystack, YYSTYPE* yyvalp, YYLTYPE* yylocp]b4_user_formals[){ yySemanticOption* yybest; yySemanticOption* yyp; int yymerge; yybest = yyoptionList; yymerge = 0; for (yyp = yyoptionList->yynext; yyp != NULL; yyp = yyp->yynext) { if (yyidenticalOptions (yybest, yyp)) yymergeOptionSets (yybest, yyp); else switch (yypreference (yybest, yyp)) { case 0: yyreportAmbiguity (yybest, yyp, yystack]b4_pure_args[); break; case 1: yymerge = 1; break; case 2: break; case 3: yybest = yyp; yymerge = 0; break; } } if (yymerge) { int yyprec = yydprec[yybest->yyrule]; YYCHK (yyresolveAction (yybest, yystack, yyvalp, yylocp]b4_user_args[)); for (yyp = yybest->yynext; yyp != NULL; yyp = yyp->yynext) { if (yyprec == yydprec[yyp->yyrule]) { YYSTYPE yyval1; YYLTYPE yydummy; YYCHK (yyresolveAction (yyp, yystack, &yyval1, &yydummy]b4_user_args[)); yyuserMerge (yymerger[yyp->yyrule], yyvalp, &yyval1); } } return yyok; } else return yyresolveAction (yybest, yystack, yyvalp, yylocp]b4_user_args[);}static YYRESULTTAGyyresolveStack (yyGLRStack* yystack]b4_user_formals[){ if (yystack->yysplitPoint != NULL) { yyGLRState* yys; int yyn; for (yyn = 0, yys = yystack->yytops.yystates[0]; yys != yystack->yysplitPoint; yys = yys->yypred, yyn += 1) continue; YYCHK (yyresolveStates (yystack->yytops.yystates[0], yyn, yystack ]b4_user_args[)); } return yyok;}static voidyycompressStack (yyGLRStack* yystack){ yyGLRState* yyp, *yyq, *yyr; if (yystack->yytops.yysize != 1 || yystack->yysplitPoint == NULL) return; for (yyp = yystack->yytops.yystates[0], yyq = yyp->yypred, yyr = NULL; yyp != yystack->yysplitPoint; yyr = yyp, yyp = yyq, yyq = yyp->yypred) yyp->yypred = yyr; yystack->yyspaceLeft += yystack->yynextFree - yystack->yyitems; yystack->yynextFree = ((yyGLRStackItem*) yystack->yysplitPoint) + 1; yystack->yyspaceLeft -= yystack->yynextFree - yystack->yyitems; yystack->yysplitPoint = NULL; yystack->yylastDeleted = NULL; while (yyr != NULL) { yystack->yynextFree->yystate = *yyr; yyr = yyr->yypred; yystack->yynextFree->yystate.yypred = & yystack->yynextFree[-1].yystate; yystack->yytops.yystates[0] = &yystack->yynextFree->yystate; yystack->yynextFree += 1; yystack->yyspaceLeft -= 1; }}static YYRESULTTAGyyprocessOneStack (yyGLRStack* yystack, int yyk, size_t yyposn, YYSTYPE* yylvalp, YYLTYPE* yyllocp ]b4_user_formals[){ int yyaction; const short int* yyconflicts; yyRuleNum yyrule; yySymbol* const yytokenp = yystack->yytokenp; while (yystack->yytops.yystates[yyk] != NULL) { yyStateNum yystate = yystack->yytops.yystates[yyk]->yylrState; YYDPRINTF ((stderr, "Stack %d Entering state %d\n", yyk, yystate)); YYASSERT (yystate != YYFINAL); if (yyisDefaultedState (yystate)) { yyrule = yydefaultAction (yystate); if (yyrule == 0) { YYDPRINTF ((stderr, "Stack %d dies.\n", yyk)); yymarkStackDeleted (yystack, yyk); return yyok;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -