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

📄 lab3.tab.c

📁 c—语言的句法分析器。读入一个C--语言程序
💻 C
📖 第 1 页 / 共 4 页
字号:
case 58:
#line 689 "lab3.y"
{
           yyval.expn.type = yyvsp[0].expn.type;
       /*    if (debug) {
               printf("term => unary_expression\n");
           }*/
       ;
    break;}
case 59:
#line 698 "lab3.y"
{
           yyval.op[0] = '*';
           if (debug) {
               printf("mulop => *\n");
           }
       ;
    break;}
case 60:
#line 705 "lab3.y"
{
           yyval.op[0] = '/';
           if (debug) {
               printf("mulop => /\n");
           }
       ;
    break;}
case 61:
#line 712 "lab3.y"
{
           yyval.op[0] = '%';
           if (debug) {
               printf("mulop => %\n");
           }
       ;
    break;}
case 62:
#line 721 "lab3.y"
{
           yyval.expn.type = INT;
           if (yyvsp[0].expn.type == INT) {
               if (yyvsp[-1].op[0] == '+') {
                   yyval.expn.value = yyvsp[0].expn.value;  
               } else {
                   yyval.expn.value = -yyvsp[0].expn.value; 
               }
           }
           if (yyvsp[0].expn.type != INT) {
               printf("line %3d error: %c :illegal, right operand is not constant\n", lineno, yyvsp[-1].op[0]);
               right = 0;
           }
           if (debug) {
               printf("unary_expression => unaryop unary_expression\n");
           }
       ;
    break;}
case 63:
#line 739 "lab3.y"
{
           yyval.expn.type = yyvsp[0].expn.type;
       /*    if (debug) {
               printf("unary_expression => factor\n");
           }*/
       ;
    break;}
case 64:
#line 748 "lab3.y"
{
           yyval.op[0] = '+';
           if (debug) {
               printf("unaryop => +\n");
           }
       ;
    break;}
case 65:
#line 755 "lab3.y"
{
           yyval.op[0] = '-';
           if (debug) {
               printf("unaryop => -\n");
           }
       ;
    break;}
case 66:
#line 764 "lab3.y"
{
           yyval.expn.type = yyvsp[-1].expn.type;
           if (debug) {
               printf("factor => ( expression )\n");
           }
       ;
    break;}
case 67:
#line 771 "lab3.y"
{
           if (yyvsp[0].sign != 0) {
               yyval.expn.type = yyvsp[0].sign->type;
               yyval.expn.symadd = yyvsp[0].sign;
           }
           if (debug) {
               printf("factor => var\n");
           }
       ;
    break;}
case 68:
#line 781 "lab3.y"
{
           yyval.expn.type = yyvsp[0].expn.type;
           if (debug) {
               printf("factor => call\n");
           }
       ;
    break;}
case 69:
#line 788 "lab3.y"
{
           yyval.expn.type = INT;
           yyval.expn.value = atoi(yyvsp[0].id);
           if (debug) {
               printf("factor => NUM\n");
           }
       ;
    break;}
case 70:
#line 796 "lab3.y"
{
           yyval.expn.type = CHAR;
           yyval.expn.value = yyvsp[0].id[1];
           if (debug) {
               printf("factor => CHAR_LITERAL\n");
           }
       ;
    break;}
case 71:
#line 804 "lab3.y"
{
           yyval.expn.type = CHAR + 100;
           yyval.expn.string = yyvsp[0].id;
           if (debug) {
               printf("factor => STRING_LITERAL\n");
           }
       ;
    break;}
case 72:
#line 814 "lab3.y"
{
           yyval.expn.type = LookUpFun(yyvsp[-3].id);
           if (0 == yyval.expn.type) {
               printf("line %3d error: %s :undeclared function\n", lineno, yyvsp[-3].id);
               right = 0;
           }
           typecheck = 0;
           free(arg_in.temp);
           if (0 == (arg_in.temp = (int *)malloc(50 * sizeof(int)))) {
               printf("malloc for argument check buffer error\n");
               exit(1);
           }
           arg_in.size = 50;
           if (debug) {
               printf("call => ID ( args )\n");
           }
       ;
    break;}
case 73:
#line 834 "lab3.y"
{/*
           if (debug) {
               printf("args => arg_list\n");
           }*/
       ;
    break;}
case 75:
#line 843 "lab3.y"
{
           if (typecheck + 1 > arg_in.size) {
               arg_in.temp = (int *)realloc(arg_in.temp,(arg_in.size + BUFFINCREMENT) * sizeof(int));
               if (0 == arg_in.temp) {
                   printf("realloc for argument check buffer error\n");
                   exit(1);
               }
               arg_in.size += BUFFINCREMENT;
           }
           arg_in.temp[typecheck++] = yyvsp[0].expn.type;
       /*    if (debug) {
               printf("arg_list => arg_list , expression\n");
           }*/
       ;
    break;}
case 76:
#line 858 "lab3.y"
{
           if (typecheck + 1 > arg_in.size) {
               arg_in.temp = (int *)realloc(arg_in.temp,(arg_in.size + BUFFINCREMENT) * sizeof(int));
               if (0 == arg_in.temp) {
                   printf("realloc for argument check buffer error\n");
                   exit(1);
               }
               arg_in.size += BUFFINCREMENT;
           }
           arg_in.temp[typecheck++] = yyvsp[0].expn.type;
       /*    if (debug) {
               printf("arg_list => expression\n");
           }*/
       ;
    break;}
case 77:
#line 875 "lab3.y"
{
           if (debug) {
               printf("calling_convention => CDECL\n");
           }
       ;
    break;}
case 78:
#line 881 "lab3.y"
{
           if (debug) {
               printf("calling_convention => STDCALL\n");
           }
       ;
    break;}
}
   /* the action file gets copied in in place of this dollarsign */
#line 543 "/usr/local/share/bison.simple"

  yyvsp -= yylen;
  yyssp -= yylen;
#ifdef YYLSP_NEEDED
  yylsp -= yylen;
#endif

#if YYDEBUG != 0
  if (yydebug)
    {
      short *ssp1 = yyss - 1;
      fprintf (stderr, "state stack now");
      while (ssp1 != yyssp)
	fprintf (stderr, " %d", *++ssp1);
      fprintf (stderr, "\n");
    }
#endif

  *++yyvsp = yyval;

#ifdef YYLSP_NEEDED
  yylsp++;
  if (yylen == 0)
    {
      yylsp->first_line = yylloc.first_line;
      yylsp->first_column = yylloc.first_column;
      yylsp->last_line = (yylsp-1)->last_line;
      yylsp->last_column = (yylsp-1)->last_column;
      yylsp->text = 0;
    }
  else
    {
      yylsp->last_line = (yylsp+yylen-1)->last_line;
      yylsp->last_column = (yylsp+yylen-1)->last_column;
    }
#endif

  /* 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 - YYNTBASE] + *yyssp;
  if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
    yystate = yytable[yystate];
  else
    yystate = yydefgoto[yyn - YYNTBASE];

  goto yynewstate;

yyerrlab:   /* here on detecting error */

  if (! yyerrstatus)
    /* If not already recovering from an error, report this error.  */
    {
      ++yynerrs;

#ifdef YYERROR_VERBOSE
      yyn = yypact[yystate];

      if (yyn > YYFLAG && yyn < YYLAST)
	{
	  int size = 0;
	  char *msg;
	  int x, count;

	  count = 0;
	  /* Start X at -yyn if nec to avoid negative indexes in yycheck.  */
	  for (x = (yyn < 0 ? -yyn : 0);
	       x < (sizeof(yytname) / sizeof(char *)); x++)
	    if (yycheck[x + yyn] == x)
	      size += strlen(yytname[x]) + 15, count++;
	  msg = (char *) malloc(size + 15);
	  if (msg != 0)
	    {
	      strcpy(msg, "parse error");

	      if (count < 5)
		{
		  count = 0;
		  for (x = (yyn < 0 ? -yyn : 0);
		       x < (sizeof(yytname) / sizeof(char *)); x++)
		    if (yycheck[x + yyn] == x)
		      {
			strcat(msg, count == 0 ? ", expecting `" : " or `");
			strcat(msg, yytname[x]);
			strcat(msg, "'");
			count++;
		      }
		}
	      yyerror(msg);
	      free(msg);
	    }
	  else
	    yyerror ("parse error; also virtual memory exceeded");
	}
      else
#endif /* YYERROR_VERBOSE */
	yyerror("parse error");
    }

  goto yyerrlab1;
yyerrlab1:   /* here on error raised explicitly by an action */

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

      /* return failure if at end of input */
      if (yychar == YYEOF)
	YYABORT;

#if YYDEBUG != 0
      if (yydebug)
	fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]);
#endif

      yychar = YYEMPTY;
    }

  /* Else will try to reuse lookahead token
     after shifting the error token.  */

  yyerrstatus = 3;		/* Each real token shifted decrements this */

  goto yyerrhandle;

yyerrdefault:  /* current state does not do anything special for the error token. */

#if 0
  /* This is wrong; only states that explicitly want error tokens
     should shift them.  */
  yyn = yydefact[yystate];  /* If its default is to accept any token, ok.  Otherwise pop it.*/
  if (yyn) goto yydefault;
#endif

yyerrpop:   /* pop the current state because it cannot handle the error token */

  if (yyssp == yyss) YYABORT;
  yyvsp--;
  yystate = *--yyssp;
#ifdef YYLSP_NEEDED
  yylsp--;
#endif

#if YYDEBUG != 0
  if (yydebug)
    {
      short *ssp1 = yyss - 1;
      fprintf (stderr, "Error: state stack now");
      while (ssp1 != yyssp)
	fprintf (stderr, " %d", *++ssp1);
      fprintf (stderr, "\n");
    }
#endif

yyerrhandle:

  yyn = yypact[yystate];
  if (yyn == YYFLAG)
    goto yyerrdefault;

  yyn += YYTERROR;
  if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
    goto yyerrdefault;

  yyn = yytable[yyn];
  if (yyn < 0)
    {
      if (yyn == YYFLAG)
	goto yyerrpop;
      yyn = -yyn;
      goto yyreduce;
    }
  else if (yyn == 0)
    goto yyerrpop;

  if (yyn == YYFINAL)
    YYACCEPT;

#if YYDEBUG != 0
  if (yydebug)
    fprintf(stderr, "Shifting error token, ");
#endif

  *++yyvsp = yylval;
#ifdef YYLSP_NEEDED
  *++yylsp = yylloc;
#endif

  yystate = yyn;
  goto yynewstate;

 yyacceptlab:
  /* YYACCEPT comes here.  */
  if (yyfree_stacks)
    {
      free (yyss);
      free (yyvs);
#ifdef YYLSP_NEEDED
      free (yyls);
#endif
    }
  return 0;

 yyabortlab:
  /* YYABORT comes here.  */
  if (yyfree_stacks)
    {
      free (yyss);
      free (yyvs);
#ifdef YYLSP_NEEDED
      free (yyls);
#endif
    }
  return 1;
}
#line 888 "lab3.y"


void info()
{
    printf("lab3.exe -- Copyright (C) 2007 cj4 No.04120004\n");
    printf("Usage: lab3 [-d] filename\n");
    printf("----------------------------------------------------\n");
    printf("filename    specifies the name of input file\n");
    printf("-d          debug mode\n");
    exit(1);
}

int yyerror(char *s)
{
    fprintf(stderr, "line %3d error: %s\n", lineno, s);
    right = 0;
    return (0);
}

int main(int argc, char *argv[])
{
    debug = 0;
    switch (argc) {
        case 3:
            if (0 == strcmp(argv[1], "-d")) {
                printf("============= debug mode =============\n");
                debug = 1;
            } else {
                info();
            }
            if (NULL == (yyin = fopen(argv[2],"r"))) {
                printf("file: %s open error!\n", argv[2]);
                exit(1);
            }
            yyparse();
            free(argtemp.temp);
            free(arg_in.temp);
            fclose(yyin);
            break;
        case 2:
            if (NULL == (yyin = fopen(argv[1],"r"))) {
                printf("file: %s open error!\n", argv[1]);
                exit(1);
            }
            yyparse();
            free(argtemp.temp);
            free(arg_in.temp);
            fclose(yyin);
            break;
        default:
            info();
    }
    if (right) {
        printf("Well done!\n");
    }
    return (0);
}

⌨️ 快捷键说明

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