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

📄 parser_tab.c

📁 一个面向对像语言的编译器
💻 C
📖 第 1 页 / 共 5 页
字号:

  if (yyn == YYFINAL)
    YYACCEPT;

  /* Shift the lookahead token.  */

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

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

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

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

  yystate = yyn;
  goto yynewstate;

/* Do the default action for the current state.  */
yydefault:

  yyn = yydefact[yystate];
  if (yyn == 0)
    goto yyerrlab;

/* Do a reduction.  yyn is the number of a rule to reduce with.  */
yyreduce:
  yylen = yyr2[yyn];
  yyval = yyvsp[1-yylen]; /* implement default value of the action */

#if YYDEBUG != 0
  if (yydebug)
    {
      int i;

      fprintf (stderr, "Reducing via rule %d (line %d), ",
	       yyn, yyrline[yyn]);

      /* Print the symbols being reduced, and their result.  */
      for (i = yyprhs[yyn]; yyrhs[i] > 0; i++)
	fprintf (stderr, "%s ", yytname[yyrhs[i]]);
      fprintf (stderr, " -> %s\n", yytname[yyr1[yyn]]);
    }
#endif


  switch (yyn) {

case 1:
#line 113 "parser.y"
{
						//在退出全局作用域之前检查链接错误,在此只检查那些函数调用但没有定义的情况
						int numElements;
						Declaration *decl;
						
						//所有调用过的函数的个数
						numElements=FunUsedList->NumElements();

						for(int i=0; i<numElements; i++)
						{
							decl=FunUsedList->Nth(i);
							//如果函数没有定义
							if(!decl->FunIsDefined())
							{
								//类中定义的函数的调用
								if(decl->GetScope()->IsClassScope())
								{
									numerr++;
									ReportError(NULL,err_meth_undefined,decl->GetName(), decl->GetScope()->GetClassType()->GetClassName());
								}
								//全局函数的调用
								if(decl->GetScope()->IsGlobalScope())
								{
									//没有定义而调用则报错
									numerr++;
									ReportError(NULL,err_fn_undefined,decl->GetName());
								}
							}
						}
						
						//下面单独处理main函数的情况
						Scope *tScope=scopes->GetCurrentScope();
						Declaration *dec1;
						bool flag=false;

						dec1=tScope->FirstDecla();
						while(dec1!=NULL)
						{
							//是否有main函数
							if(strcmp(dec1->GetName(),"main")==0&&dec1->FunIsDefined())
								flag=true;
							//下一个
							dec1=tScope->NextDecla();
						}
							
						//没有main函数定义则报错
						if(!flag)
								ReportError(NULL,err_fn_undefined,"main");

						 scopes->PopScope(); 
							
						  numErrors=numErrors+numerr;
                          if (numErrors == 0) cg->Emit(); ;
    break;}
case 2:
#line 169 "parser.y"
{ if(yyvsp[0].decl) yyvsp[-1].declList->Append(yyvsp[0].decl); yyval.declList = yyvsp[-1].declList; ;
    break;}
case 3:
#line 171 "parser.y"
{yyval.declList=new DeclList(); FunUsedList=new DeclList();;
    break;}
case 4:
#line 174 "parser.y"
{yyval.decl=yyvsp[0].decl;;
    break;}
case 5:
#line 175 "parser.y"
{yyval.decl=yyvsp[0].decl;;
    break;}
case 6:
#line 176 "parser.y"
{yyval.decl=yyvsp[0].decl;;
    break;}
case 7:
#line 177 "parser.y"
{yyval.decl=yyvsp[0].decl;;
    break;}
case 8:
#line 181 "parser.y"
{
							Declaration *debug=yyvsp[-1].decl;
							scopes->Declare(yyvsp[-1].decl); 
							
						//	if($1->GetType()->IsArrayType())
						//		$1->ValidateAddr();
							
							Scope *tScope=scopes->GetCurrentScope();
							//local中的变量声明,设置local offset
							if(tScope->IsLocalScope())
							{
								cg->SetLocalOffset(yyvsp[-1].decl);
							}
							
							
							if(tScope->IsGlobalScope())
							{
								//生成全局变量
								cg->GenGVar(yyvsp[-1].decl);
							}

							if(tScope->IsClassScope())
							{
								//如果是类域里面变量,设置变量在类域的offset
								cg->SetIVarOffset(yyvsp[-1].decl);
							}

							yyval.decl=yyvsp[-1].decl;	
						;
    break;}
case 9:
#line 213 "parser.y"
{ if (yyvsp[-1].type->IsEquivalentTo(Type::doubleType)) {
                            ReportError(&yylsp[-1], err_no_code_gen_for_doubles);
                            yyvsp[-1].type = Type::errorType;
                          }
                          yyval.decl = BuildVarDecl(yyvsp[0].identifier, yyvsp[-1].type, yylsp[0].first_line);
                        ;
    break;}
case 10:
#line 222 "parser.y"
{ yyval.type = Type::intType; ;
    break;}
case 11:
#line 224 "parser.y"
{ yyval.type = Type::voidType;;
    break;}
case 12:
#line 226 "parser.y"
{ yyval.type = Type::boolType;;
    break;}
case 13:
#line 228 "parser.y"
{ yyval.type = Type::stringType;;
    break;}
case 14:
#line 230 "parser.y"
{ yyval.type = Type::doubleType;;
    break;}
case 15:
#line 232 "parser.y"
{ yyval.type = TypeForClassName(yyvsp[0].identifier);;
    break;}
case 16:
#line 234 "parser.y"
{ yyval.type = Type::NewArrayType(yyvsp[-2].type); ;
    break;}
case 17:
#line 239 "parser.y"
{
							Declaration *d = BuildClassDecl(yyvsp[-1].identifier,yyvsp[0].type,yylsp[-1].first_line);
							int maxParentIVarOffset=0, maxParentMethodOffset=-4;
							Declaration *decl;
							bool flag1=false,flag2=false;

							yyval.decl=d;
							scopes->Declare(d);
							yyval.type = d->GetType();
							scopes->PushScope(yyval.type->GetClassScope());
							
							//初始化类中变量和成员方法的offset
							cg->InitIVarOffset();
							cg->InitMethodOffset();

					
							if(yyvsp[0].type!=NULL)
							{//继承有父类

								//拷贝父类成员
								scopes->GetCurrentScope()->CopyFieldsFromParent(yyvsp[0].type->GetClassScope());
								Scope *temp;
								int offset;

								temp=scopes->GetCurrentScope();
								decl=temp->FirstDecla();

								//下面查找父类中成员变量和方法的offset, 以便设置本类中新定义的成员的offset
								while(decl!=NULL)
								{
									if(decl->IsVarDecl())
									{
										offset=decl->GetOffset();
										if(offset>maxParentIVarOffset)
											maxParentIVarOffset=offset;
									}

									if(decl->IsFunctionDecl())
									{
										offset=decl->GetOffset();
										if(offset>maxParentMethodOffset)
											maxParentMethodOffset=offset;
									}

									decl=temp->NextDecla();
								}
								if(maxParentIVarOffset!=0)
									cg->SetTopIVarOffset(maxParentIVarOffset+VarSize);
								
								if(maxParentMethodOffset!=-4)
								cg->SetTopMethodOffset(maxParentMethodOffset+VarSize);
							}
						 ;
    break;}
case 18:
#line 293 "parser.y"
{
							Scope *tScope;
							DeclList *tMethodList, *methodList;
							Declaration *decl;
							int num;

							tScope=scopes->GetCurrentScope();
							tMethodList=new DeclList();
							methodList=new DeclList();
							cg->InitMethodOffset();

							decl=tScope->FirstDecla();
							//将类中(包括父类)所有的成员方法加入tMethodList中
							while(decl!=NULL)
							{
								if(decl->IsFunctionDecl())
								{
									tMethodList->Append(decl);
								}
								
								decl=tScope->NextDecla();	
							}
							
							num=tMethodList->NumElements();
							
							//将所得到的所有的成员方法按声明的先后顺序加入vTable中
							for(int i=0; i<num; i++)
							{
								for(int j=0; j<num; j++)
								{
									decl=tMethodList->Nth(j);
									if(decl->GetOffset()==4*i)
										methodList->Append(decl);
								}
							}		
					
							cg->GenVTable(yyvsp[-5].identifier,methodList);
							
							Declaration *classDecl=scopes->Lookup(yyvsp[-5].identifier);
							int size=cg->GetStackFrameSize(ClassStackSize);
							//设置要为该类分配对象空间的时候的大小
							classDecl->SetStackFrameSize(size);

							scopes->PopScope(); 
						;
    break;}
case 19:
#line 341 "parser.y"
{ yyval.type = TypeForClassName(yyvsp[0].identifier); ;
    break;}
case 20:
#line 343 "parser.y"
{ yyval.type = NULL; ;
    break;}
case 21:
#line 347 "parser.y"
{ if(yyvsp[0].decl) yyvsp[-1].declList->Append(yyvsp[0].decl); yyval.declList = yyvsp[-1].declList; ;
    break;}
case 22:
#line 349 "parser.y"
{yyval.declList=new DeclList();;
    break;}
case 23:
#line 353 "parser.y"
{yyval.decl=yyvsp[0].decl;;
    break;}
case 24:
#line 355 "parser.y"
{yyval.decl=yyvsp[0].decl;;
    break;}
case 25:
#line 357 "parser.y"
{yyval.decl=yyvsp[0].decl;;
    break;}
case 26:
#line 361 "parser.y"
{ 
							Declaration * decl=BuildFnDecl(yyvsp[-4].identifier, yyvsp[-5].type, yyvsp[-2].declList, DeclOnly, &yylsp[-4]);
							Scope *tScope;
							Declaration *dec1;

							tScope=scopes->GetCurrentScope();

							//如果是在类域中的函数声明设置它的offset
							if(tScope->IsClassScope())
							{
								dec1=tScope->Lookup(yyvsp[-4].identifier);
								if(dec1!=NULL)
								{
									//如果前面还有声明,offset设置为前面声明的offset
									if(dec1->IsFunctionDecl())
										decl->SetOffset(dec1->GetOffset());
									else
										cg->SetMethodOffset(decl);
								}
								else	
									cg->SetMethodOffset(decl);
							}
							yyval.decl=decl;
							scopes->Declare(decl);
							
						;
    break;}
case 27:
#line 390 "parser.y"
{yyval.declList=yyvsp[0].declList;;
    break;}
case 28:
#line 392 "parser.y"
{ yyval.declList = new DeclList; ;
    break;}
case 29:
#line 396 "parser.y"
{ yyvsp[-2].declList->Append(yyvsp[0].decl); yyval.declList = yyvsp[-2].declList; ;
    break;}
case 30:
#line 398 "parser.y"
{	
							yyval.declList = new DeclList();
							yyval.declList->Append(yyvsp[0].decl); 
						;
    break;}
case 31:
#line 405 "parser.y"
{
							Declaration *d = BuildFnDecl(yyvsp[-3].identifier, yyvsp[-4].type, yyvsp[-1].declList, FullDefn, &yylsp[-3]);
							Declaration *mythis;
							Scope *tScope;
							bool flag=false;

							if(scopes->GetCurrentScope()->IsClassScope())
							{
								flag=true;
							}

							if(scopes->GetCurrentScope()->IsGlobalScope())
							{
								tScope=scopes->GetCurrentScope();
								Declaration *temp=tScope->Lookup(yyvsp[-3].identifier);
								if(temp!=NULL)
									temp->MakeFunDefined();

⌨️ 快捷键说明

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