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

📄 calculator.tab.c

📁 该课程设计通过使用flex和bison编写了一个科学计算器的程序
💻 C
📖 第 1 页 / 共 3 页
字号:

      YYDPRINTF ((stderr, "Stack size increased to %lu\n",
		  (unsigned long int) yystacksize));

      if (yyss + yystacksize - 1 <= yyssp)
	YYABORT;
    }

  YYDPRINTF ((stderr, "Entering state %d\n", yystate));

  goto yybackup;

/*-----------.
| yybackup.  |
`-----------*/
yybackup:

/* Do appropriate processing given the current state.  */
/* Read a look-ahead token if we need one and don't already have one.  */
/* yyresume: */

  /* First try to decide what to do without reference to look-ahead token.  */

  yyn = yypact[yystate];
  if (yyn == YYPACT_NINF)
    goto yydefault;

  /* Not known => get a look-ahead token if don't already have one.  */

  /* YYCHAR is either YYEMPTY or YYEOF or a valid look-ahead symbol.  */
  if (yychar == YYEMPTY)
    {
      YYDPRINTF ((stderr, "Reading a token: "));
      yychar = YYLEX;
    }

  if (yychar <= YYEOF)
    {
      yychar = yytoken = YYEOF;
      YYDPRINTF ((stderr, "Now at end of input.\n"));
    }
  else
    {
      yytoken = YYTRANSLATE (yychar);
      YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
    }

  /* If the proper action on seeing token YYTOKEN is to reduce or to
     detect an error, take that action.  */
  yyn += yytoken;
  if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
    goto yydefault;
  yyn = yytable[yyn];
  if (yyn <= 0)
    {
      if (yyn == 0 || yyn == YYTABLE_NINF)
	goto yyerrlab;
      yyn = -yyn;
      goto yyreduce;
    }

  if (yyn == YYFINAL)
    YYACCEPT;

  /* Shift the look-ahead token.  */
  YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);

  /* Discard the token being shifted unless it is eof.  */
  if (yychar != YYEOF)
    yychar = YYEMPTY;

  *++yyvsp = yylval;


  /* Count tokens shifted since error; after three, turn off error
     status.  */
  if (yyerrstatus)
    yyerrstatus--;

  yystate = yyn;
  goto yynewstate;


/*-----------------------------------------------------------.
| yydefault -- do the default action for the current state.  |
`-----------------------------------------------------------*/
yydefault:
  yyn = yydefact[yystate];
  if (yyn == 0)
    goto yyerrlab;
  goto yyreduce;


/*-----------------------------.
| yyreduce -- Do a reduction.  |
`-----------------------------*/
yyreduce:
  /* yyn is the number of a rule to reduce with.  */
  yylen = yyr2[yyn];

  /* If YYLEN is nonzero, implement the default value of the action:
     `$$ = $1'.

     Otherwise, the following line sets YYVAL to garbage.
     This behavior is undocumented and Bison
     users should not rely upon it.  Assigning to YYVAL
     unconditionally makes the parser a bit smaller, and it avoids a
     GCC warning that YYVAL may be used uninitialized.  */
  yyval = yyvsp[1-yylen];


  YY_REDUCE_PRINT (yyn);
  switch (yyn)
    {
        case 6:
#line 26 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {{(yyvsp[-3].tptr)->value.var.a=(yyvsp[-1].val).a;(yyvsp[-3].tptr)->value.var.b=(yyvsp[-1].val).b;}
                     if((yyvsp[-1].val).b==0){yyerrok;};}
    break;

  case 7:
#line 28 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {Display((yyvsp[-2].val));;}
    break;

  case 8:
#line 29 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {yyerrok;;}
    break;

  case 9:
#line 31 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(yyvsp[0].val).a;(yyval.val).b =1;;}
    break;

  case 10:
#line 32 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(yyvsp[0].tptr)->value.var.a;(yyval.val).b=(yyvsp[0].tptr)->value.var.b;;}
    break;

  case 11:
#line 33 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(yyvsp[0].val).a;(yyval.val).b=(yyvsp[0].val).b;(yyvsp[-2].tptr)->value.var.a=(yyvsp[0].val).a;(yyvsp[-2].tptr)->value.var.b=(yyvsp[0].val).b;;}
    break;

  case 12:
#line 34 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(*((yyvsp[-3].tptr)->value.fnctptr))((yyvsp[-1].val).a/(yyvsp[-1].val).b);(yyval.val).b=1;;}
    break;

  case 13:
#line 35 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(yyvsp[-2].val).a*(yyvsp[0].val).b+(yyvsp[0].val).a*(yyvsp[-2].val).b;(yyval.val).b=(yyvsp[-2].val).b*(yyvsp[0].val).b;;}
    break;

  case 14:
#line 36 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(yyvsp[-2].val).a*(yyvsp[0].val).b-(yyvsp[0].val).a*(yyvsp[-2].val).b;(yyval.val).b=(yyvsp[-2].val).b*(yyvsp[0].val).b;;}
    break;

  case 15:
#line 37 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(yyvsp[-2].val).a*(yyvsp[0].val).a;(yyval.val).b=(yyvsp[-2].val).b*(yyvsp[0].val).b;;}
    break;

  case 16:
#line 38 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(yyvsp[-2].val).a*(yyvsp[0].val).b;(yyval.val).b=(yyvsp[-2].val).b*(yyvsp[0].val).a;;}
    break;

  case 17:
#line 39 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=-(yyvsp[0].val).a;(yyval.val).b=(yyvsp[0].val).b;;}
    break;

  case 18:
#line 40 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=pow((yyvsp[-2].val).a,(yyvsp[0].val).a);(yyval.val).b =1;;}
    break;

  case 19:
#line 41 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"
    {(yyval.val).a=(yyvsp[-1].val).a;(yyval.val).b=(yyvsp[-1].val).b;;}
    break;


      default: break;
    }

/* Line 1126 of yacc.c.  */
#line 1202 "Calculator.tab.c"

  yyvsp -= yylen;
  yyssp -= yylen;


  YY_STACK_PRINT (yyss, yyssp);

  *++yyvsp = yyval;


  /* Now `shift' the result of the reduction.  Determine what state
     that goes to, based on the state we popped back to and the rule
     number reduced by.  */

  yyn = yyr1[yyn];

  yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
  if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
    yystate = yytable[yystate];
  else
    yystate = yydefgoto[yyn - YYNTOKENS];

  goto yynewstate;


/*------------------------------------.
| yyerrlab -- here on detecting error |
`------------------------------------*/
yyerrlab:
  /* If not already recovering from an error, report this error.  */
  if (!yyerrstatus)
    {
      ++yynerrs;
#if YYERROR_VERBOSE
      yyn = yypact[yystate];

      if (YYPACT_NINF < yyn && yyn < YYLAST)
	{
	  int yytype = YYTRANSLATE (yychar);
	  YYSIZE_T yysize0 = yytnamerr (0, yytname[yytype]);
	  YYSIZE_T yysize = yysize0;
	  YYSIZE_T yysize1;
	  int yysize_overflow = 0;
	  char *yymsg = 0;
#	  define YYERROR_VERBOSE_ARGS_MAXIMUM 5
	  char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
	  int yyx;

#if 0
	  /* This is so xgettext sees the translatable formats that are
	     constructed on the fly.  */
	  YY_("syntax error, unexpected %s");
	  YY_("syntax error, unexpected %s, expecting %s");
	  YY_("syntax error, unexpected %s, expecting %s or %s");
	  YY_("syntax error, unexpected %s, expecting %s or %s or %s");
	  YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s");
#endif
	  char *yyfmt;
	  char const *yyf;
	  static char const yyunexpected[] = "syntax error, unexpected %s";
	  static char const yyexpecting[] = ", expecting %s";
	  static char const yyor[] = " or %s";
	  char yyformat[sizeof yyunexpected
			+ sizeof yyexpecting - 1
			+ ((YYERROR_VERBOSE_ARGS_MAXIMUM - 2)
			   * (sizeof yyor - 1))];
	  char const *yyprefix = yyexpecting;

	  /* Start YYX at -YYN if negative to avoid negative indexes in
	     YYCHECK.  */
	  int yyxbegin = yyn < 0 ? -yyn : 0;

	  /* Stay within bounds of both yycheck and yytname.  */
	  int yychecklim = YYLAST - yyn;
	  int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
	  int yycount = 1;

	  yyarg[0] = yytname[yytype];
	  yyfmt = yystpcpy (yyformat, yyunexpected);

	  for (yyx = yyxbegin; yyx < yyxend; ++yyx)
	    if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
	      {
		if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
		  {
		    yycount = 1;
		    yysize = yysize0;
		    yyformat[sizeof yyunexpected - 1] = '\0';
		    break;
		  }
		yyarg[yycount++] = yytname[yyx];
		yysize1 = yysize + yytnamerr (0, yytname[yyx]);
		yysize_overflow |= yysize1 < yysize;
		yysize = yysize1;
		yyfmt = yystpcpy (yyfmt, yyprefix);
		yyprefix = yyor;
	      }

	  yyf = YY_(yyformat);
	  yysize1 = yysize + yystrlen (yyf);
	  yysize_overflow |= yysize1 < yysize;
	  yysize = yysize1;

	  if (!yysize_overflow && yysize <= YYSTACK_ALLOC_MAXIMUM)
	    yymsg = (char *) YYSTACK_ALLOC (yysize);
	  if (yymsg)
	    {
	      /* Avoid sprintf, as that infringes on the user's name space.
		 Don't have undefined behavior even if the translation
		 produced a string with the wrong number of "%s"s.  */
	      char *yyp = yymsg;
	      int yyi = 0;
	      while ((*yyp = *yyf))
		{
		  if (*yyp == '%' && yyf[1] == 's' && yyi < yycount)
		    {
		      yyp += yytnamerr (yyp, yyarg[yyi++]);
		      yyf += 2;
		    }
		  else
		    {
		      yyp++;
		      yyf++;
		    }
		}
	      yyerror (yymsg);
	      YYSTACK_FREE (yymsg);
	    }
	  else
	    {
	      yyerror (YY_("syntax error"));
	      goto yyexhaustedlab;
	    }
	}
      else
#endif /* YYERROR_VERBOSE */
	yyerror (YY_("syntax error"));
    }



  if (yyerrstatus == 3)
    {
      /* If just tried and failed to reuse look-ahead token after an
	 error, discard it.  */

      if (yychar <= YYEOF)
        {
	  /* Return failure if at end of input.  */
	  if (yychar == YYEOF)
	    YYABORT;
        }
      else
	{
	  yydestruct ("Error: discarding", yytoken, &yylval);
	  yychar = YYEMPTY;
	}
    }

  /* Else will try to reuse look-ahead token after shifting the error
     token.  */
  goto yyerrlab1;


/*---------------------------------------------------.
| yyerrorlab -- error raised explicitly by YYERROR.  |
`---------------------------------------------------*/
yyerrorlab:

  /* Pacify compilers like GCC when the user code never invokes
     YYERROR and the label yyerrorlab therefore never appears in user
     code.  */
  if (0)
     goto yyerrorlab;

yyvsp -= yylen;
  yyssp -= yylen;
  yystate = *yyssp;
  goto yyerrlab1;


/*-------------------------------------------------------------.
| yyerrlab1 -- common code for both syntax error and YYERROR.  |
`-------------------------------------------------------------*/
yyerrlab1:
  yyerrstatus = 3;	/* Each real token shifted decrements this.  */

  for (;;)
    {
      yyn = yypact[yystate];
      if (yyn != YYPACT_NINF)
	{
	  yyn += YYTERROR;
	  if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
	    {
	      yyn = yytable[yyn];
	      if (0 < yyn)
		break;
	    }
	}

      /* Pop the current state because it cannot handle the error token.  */
      if (yyssp == yyss)
	YYABORT;


      yydestruct ("Error: popping", yystos[yystate], yyvsp);
      YYPOPSTACK;
      yystate = *yyssp;
      YY_STACK_PRINT (yyss, yyssp);
    }

  if (yyn == YYFINAL)
    YYACCEPT;

  *++yyvsp = yylval;


  /* Shift the error token. */
  YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);

  yystate = yyn;
  goto yynewstate;


/*-------------------------------------.
| yyacceptlab -- YYACCEPT comes here.  |
`-------------------------------------*/
yyacceptlab:
  yyresult = 0;
  goto yyreturn;

/*-----------------------------------.
| yyabortlab -- YYABORT comes here.  |
`-----------------------------------*/
yyabortlab:
  yyresult = 1;
  goto yyreturn;

#ifndef yyoverflow
/*-------------------------------------------------.
| yyexhaustedlab -- memory exhaustion comes here.  |
`-------------------------------------------------*/
yyexhaustedlab:
  yyerror (YY_("memory exhausted"));
  yyresult = 2;
  /* Fall through.  */
#endif

yyreturn:
  if (yychar != YYEOF && yychar != YYEMPTY)
     yydestruct ("Cleanup: discarding lookahead",
		 yytoken, &yylval);
  while (yyssp != yyss)
    {
      yydestruct ("Cleanup: popping",
		  yystos[*yyssp], yyvsp);
      YYPOPSTACK;
    }
#ifndef yyoverflow
  if (yyss != yyssa)
    YYSTACK_FREE (yyss);
#endif
  return yyresult;
}


#line 43 "C:\\Documents and Settings\\Administrator\\桌面\\编译原理课程设计\\Calculator.y"

#include<ctype.h>
int yylex(void)
{
  int c;
  while((c=getchar())==' '||c=='\t');

  if(c==EOF) return 0;
  if(c=='.'||isdigit(c))
  { 
    ungetc(c,stdin);
    scanf("%lf",&yylval.val);
    return NUM;
   }
   if(isalpha(c))
   {
     symrec *s;
     static char *symbuf=0;
     static int length=0;
     int i;
     if(length==0)
     length=40,symbuf=(char *)malloc(length+1);
     i=0;
     do{
        if(i==length)
        {
          length*=2;
          symbuf=(char *)realloc(symbuf,length+1);
        }
        symbuf[i++]=c;
        c=getchar();
        }while(isalnum(c));
       ungetc(c,stdin);
       symbuf[i]='\0';
       s=getsym(symbuf);
       if(s==0)
       s=putsym(symbuf,VAR);
       yylval.tptr=s;
       return s->type;
      }
     return c;
 }
    


⌨️ 快捷键说明

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