⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glr.c

📁 GNU的词法/语法分析器bison源码
💻 C
📖 第 1 页 / 共 5 页
字号:
/** True iff LR state STATE has only a default reduction (regardless *  of token). */static inline yyboolyyisDefaultedState (yyStateNum yystate){  return yyis_pact_ninf (yypact[yystate]);}/** The default reduction for STATE, assuming it has one. */static inline yyRuleNumyydefaultAction (yyStateNum yystate){  return yydefact[yystate];}#define yyis_table_ninf(yytable_value) \  ]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 *    R < 0:  Reduce on rule -R. *    R = 0:  Error. *    R > 0:  Shift to state R. *  Set *CONFLICTS to a pointer into yyconfl to 0-terminated list of *  conflicting reductions. */static inline voidyygetLRActions (yyStateNum yystate, int yytoken,	        int* yyaction, const short int** yyconflicts){  int yyindex = yypact[yystate] + yytoken;  if (yyindex < 0 || YYLAST < yyindex || yycheck[yyindex] != yytoken)    {      *yyaction = -yydefact[yystate];      *yyconflicts = yyconfl;    }  else if (! yyis_table_ninf (yytable[yyindex]))    {      *yyaction = yytable[yyindex];      *yyconflicts = yyconfl + yyconflp[yyindex];    }  else    {      *yyaction = 0;      *yyconflicts = yyconfl + yyconflp[yyindex];    }}static inline yyStateNumyyLRgotoState (yyStateNum yystate, yySymbol yylhs){  int yyr;  yyr = yypgoto[yylhs - YYNTOKENS] + yystate;  if (0 <= yyr && yyr <= YYLAST && yycheck[yyr] == yystate)    return yytable[yyr];  else    return yydefgoto[yylhs - YYNTOKENS];}static inline yyboolyyisShiftAction (int yyaction){  return 0 < yyaction;}static inline yyboolyyisErrorAction (int yyaction){  return yyaction == 0;}				/* GLRStates */static voidyyaddDeferredAction (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);}				/* GLRStacks *//** Initialize SET to a singleton set containing an empty stack. */static yyboolyyinitStateSet (yyGLRStateSet* yyset){  yyset->yysize = 1;  yyset->yycapacity = 16;  yyset->yystates = (yyGLRState**) YYMALLOC (16 * sizeof yyset->yystates[0]);  if (! yyset->yystates)    return yyfalse;  yyset->yystates[0] = NULL;  return yytrue;}static void yyfreeStateSet (yyGLRStateSet* yyset){  YYFREE (yyset->yystates);}/** Initialize STACK to a single empty stack, with total maximum *  capacity for all stacks of SIZE. */static yyboolyyinitGLRStack (yyGLRStack* yystack, size_t yysize){  yystack->yyerrState = 0;  yynerrs = 0;  yystack->yyspaceLeft = yysize;  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);}#define YYRELOC(YYFROMITEMS,YYTOITEMS,YYX,YYTYPE) \  &((YYTOITEMS) - ((YYFROMITEMS) - (yyGLRStackItem*) (YYX)))->YYTYPE/** If STACK is expandable, extend it.  WARNING: Pointers into the    stack from outside should be considered invalid after this call.    We always expand when there are 1 or fewer items left AFTER an    allocation, so that we can avoid having external pointers exist    across an allocation. */static voidyyexpandGLRStack (yyGLRStack* yystack){#if YYSTACKEXPANDABLE  yyGLRStackItem* yynewItems;  yyGLRStackItem* yyp0, *yyp1;  size_t yysize, yynewSize;  size_t yyn;  yysize = yystack->yynextFree - yystack->yyitems;  if (YYMAXDEPTH <= yysize)    yyMemoryExhausted (yystack);  yynewSize = 2*yysize;  if (YYMAXDEPTH < yynewSize)    yynewSize = YYMAXDEPTH;  yynewItems = (yyGLRStackItem*) YYMALLOC (yynewSize * sizeof yynewItems[0]);  if (! yynewItems)    yyMemoryExhausted (yystack);  for (yyp0 = yystack->yyitems, yyp1 = yynewItems, yyn = yysize;       0 < yyn;       yyn -= 1, yyp0 += 1, yyp1 += 1)    {      *yyp1 = *yyp0;      if (*(yybool *) yyp0)	{	  yyGLRState* yys0 = &yyp0->yystate;	  yyGLRState* yys1 = &yyp1->yystate;	  if (yys0->yypred != NULL)	    yys1->yypred =	      YYRELOC (yyp0, yyp1, yys0->yypred, yystate);	  if (! yys0->yyresolved && yys0->yysemantics.yyfirstVal != NULL)	    yys1->yysemantics.yyfirstVal =	      YYRELOC(yyp0, yyp1, yys0->yysemantics.yyfirstVal, yyoption);	}      else	{	  yySemanticOption* yyv0 = &yyp0->yyoption;	  yySemanticOption* yyv1 = &yyp1->yyoption;	  if (yyv0->yystate != NULL)	    yyv1->yystate = YYRELOC (yyp0, yyp1, yyv0->yystate, yystate);	  if (yyv0->yynext != NULL)	    yyv1->yynext = YYRELOC (yyp0, yyp1, yyv0->yynext, yyoption);	}    }  if (yystack->yysplitPoint != NULL)    yystack->yysplitPoint = YYRELOC (yystack->yyitems, yynewItems,				 yystack->yysplitPoint, yystate);  for (yyn = 0; yyn < yystack->yytops.yysize; yyn += 1)    if (yystack->yytops.yystates[yyn] != NULL)      yystack->yytops.yystates[yyn] =	YYRELOC (yystack->yyitems, yynewItems,		 yystack->yytops.yystates[yyn], yystate);  YYFREE (yystack->yyitems);  yystack->yyitems = yynewItems;  yystack->yynextFree = yynewItems + yysize;  yystack->yyspaceLeft = yynewSize - yysize;#else  yyMemoryExhausted (yystack);#endif}static voidyyfreeGLRStack (yyGLRStack* yystack){  YYFREE (yystack->yyitems);  yyfreeStateSet (&yystack->yytops);}/** Assuming that S is a GLRState somewhere on STACK, update the *  splitpoint of STACK, if needed, so that it is at least as deep as *  S. */static inline voidyyupdateSplit (yyGLRStack* yystack, yyGLRState* yys){  if (yystack->yysplitPoint != NULL && yystack->yysplitPoint > yys)    yystack->yysplitPoint = yys;}/** Invalidate stack #K in STACK. */static inline voidyymarkStackDeleted (yyGLRStack* yystack, size_t yyk){  if (yystack->yytops.yystates[yyk] != NULL)    yystack->yylastDeleted = yystack->yytops.yystates[yyk];  yystack->yytops.yystates[yyk] = NULL;}/** Undelete the last stack that was marked as deleted.  Can only be    done once after a deletion, and only when all other stacks have    been deleted. */static voidyyundeleteLastStack (yyGLRStack* yystack){  if (yystack->yylastDeleted == NULL || yystack->yytops.yysize != 0)    return;  yystack->yytops.yystates[0] = yystack->yylastDeleted;  yystack->yytops.yysize = 1;  YYDPRINTF ((stderr, "Restoring last deleted stack as stack #0.\n"));  yystack->yylastDeleted = NULL;}static inline voidyyremoveDeletes (yyGLRStack* yystack){  size_t yyi, yyj;  yyi = yyj = 0;  while (yyj < yystack->yytops.yysize)    {      if (yystack->yytops.yystates[yyi] == NULL)	{	  if (yyi == yyj)	    {	      YYDPRINTF ((stderr, "Removing dead stacks.\n"));	    }	  yystack->yytops.yysize -= 1;	}      else	{	  yystack->yytops.yystates[yyj] = yystack->yytops.yystates[yyi];	  if (yyj != yyi)	    {	      YYDPRINTF ((stderr, "Rename stack %lu -> %lu.\n",			  (unsigned long int) yyi, (unsigned long int) yyj));	    }	  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, size_t yyk, yyStateNum yylrState,	    size_t yyposn,	    YYSTYPE yysval, YYLTYPE* yylocp){  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);}/** 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, size_t yyk, yyStateNum yylrState,		 size_t yyposn, yyGLRState* rhs, yyRuleNum yyrule){  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);}/** 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, size_t 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_user_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);    }  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);		    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);    }  return yyok;}static size_tyysplitStack (yyGLRStack* yystack, size_t yyk){  if (yystack->yysplitPoint == NULL)    {      YYASSERT (yyk == 0);      yystack->yysplitPoint = yystack->yytops.yystates[yyk];    }  if (yystack->yytops.yysize >= yystack->yytops.yycapacity)    {      yyGLRState** yynewStates;      if (! ((yystack->yytops.yycapacity	      <= (YYSIZEMAX / (2 * sizeof yynewStates[0])))	     && (yynewStates =		 (yyGLRState**) YYREALLOC (yystack->yytops.yystates,					   ((yystack->yytops.yycapacity *= 2)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -