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

📄 yufa.cpp

📁 本程序是实现C-的一个小型编译器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			case 10:printf("param->int ID param1");
					break;
			case 11:printf("param1->[]|^");
					break;
			case 12:printf("compound_stmt->{local_declarations statement_list}");
					break;
			case 13:printf("local_declarations->type_specifier ID local_declarations1|^");
					break;
			case 14:printf("local_declarations1->; local_declarations|[NUM];local_declarations");
				    break;
			case 15:printf("statement_list->statement statement_list|^");
					break;
			case 16:printf("statement->复合句");
				    break;
			case 17:printf("expression_stmt->expression;|;");
					break;
            case 18:printf("selection_stmt->if (expression) statement selection_stmt1");
					break;
			case 19:printf("selection_stmt1->else statement|^");
					break;
			case 20:printf("iteration_stmt->while (expression) statement");
					break;
			case 21:printf("return_stmt->return return_stmt1");
					break;
			case 22:printf("return_stmt1->;|expression;");
					break;
			case 23:printf("expression->var=expression|simple_expression");
				    break;
			case 24:printf("var->ID var1");
					break;
			case 25:printf("var1->[expression]|^");
				    break;
			case 26:printf("simple_expression->additive_expression simple_expression1");
				    break;
			case 27:printf("simple_expression1->relop additive_expression|^");
					break;
            case 28:printf("relop-><=|<|>|>=|==|!=");
					break;
			case 29:printf("additive_expression->term additive_expression1");
					break;
			case 30:printf("additive_expressionn1->addop term additive_expression1 |^");
					break;
			case 31:printf("addop->+|-");
					break;
			case 32:printf("term->factor term1");
					break;
			case 33:printf("term1->mulop factor term1|^");
				    break;
			case 34:printf("mulop->*|/");
					break;
			case 35:printf("factor->(expression)|var|call|NUM");
				    break;
			case 36:printf("call->ID(args)");
					break;
			case 37:printf("args->arg_list|^");
					break;
            case 38:printf("arg_list->expression arg_list1");
					break;
			case 39:printf("arg_list1->, expression arg_list1|^");
					break;
		}
		printf("\n");
		if(PrintTree(m_pProgram->Leftchild))
		{
			if(PrintTree(m_pProgram->Rightsibling))
				return true;
		}
		return false;
	}
	else
		return true;
}

void Print()
{
	printf("结点编号: ");
	printf("token行号: ");
	printf("单词code:    ");
	printf("token:  ");
	printf("父结点编号:   ");
	printf("产生式:\n");
}

void PrintVT()
{
	printf("变量编号:变量类型:  变量名:  变量种类:   所在程序体:\n");
	for(int i=1;i<TopVT;i++)
	{
		printf("%-10d",i);
		printf("%-10s",VT[i].type);
		printf("%-9s",VT[i].name);
		switch(VT[i].kind)
		{
			case 1:printf("全局变量    ");
			       break;
			case 2:printf("函数形参    ");
				   break;
			case 3:printf("局部变量    ");
		}
		switch(VT[i].scope)
		{
			case 11:printf("第一个函数体第一层\n");
					break;
			case 21:printf("第二个函数体第一层\n");
					break;
		}
	}
}

void PrintGFT()
{
	printf("函数编号:函数返回类型:  函数名:    形参起始位置:  局部变量起始位置:\n");
	for(int i=1;i<TopGFT;i++)
	{
		printf("%-10d",i);
		printf("%-15s",GFT[i].type);
		printf("%-10s",GFT[i].function_name);
		printf("%-15d",GFT[i].parameter_variable);
		printf("%-20d\n",GFT[i].local_variable);
	}
}

void advance()
{
	fscanf(TokenFile,"%d  %d   %s",&CurrentToken.LineofPro,&CurrentToken.code,CurrentToken.name);
}

void NextToken()
{
	curToken.LineofPro=CurToken[j].LineofPro;
	curToken.code=CurToken[j].code;
	strcpy(curToken.name,CurToken[j].name);
	j++;
}

//符号表出错处理
void Symble_Table_error(int sementic,int line)
{
	switch(sementic)
	{
		case 1:printf("第%d行全局变量重复或者数据类型为void\n",line);
			   break;
		case 2:printf("第%d行同一函数体有两个相同的形参\n",line);
			   break;
		case 3:printf("第%d行同一函数体定义了相同的局部变量\n",line);
			   break;
		case 4:printf("第%d行局部变量未在函数体里定义过\n",line);
			   break;
		case 5:printf("第%d行调用函数未定义过\n",line);
			   break;
	}
}

//语法出错处理种类
void error(int syntax_error,int line)	
{
	switch(syntax_error)
	{
		case 1: printf("第%d行没有标识符\n",line);
	            break;
		case 2: printf("第%d行缺少';'\n",line);
			    break;
		case 3: printf("第%d行缺少']'\n",line);
			    break;
		case 4: printf("第%d行缺少NUM\n",line);
			    break;
		case 5: printf("第%d行缺少')'\n",line);
			    break;
		case 6: printf("第%d行缺少'('\n",line);
			    break;
		case 7: printf("第%d行缺少数据类型\n",line);
			    break;
		case 8: printf("第%d行缺少'}'\n",line);
			    break;
		case 9: printf("第%d行缺少'{'\n",line);
			    break;
		case 10:printf("第%d行缺少'['\n",line);
			    break;
		case 11:printf("第%d行复合句语法错误\n",line);
			    break;
		case 12:printf("第%d行表达式缺少')'\n",line);
			    break;
		case 13:printf("第%d行表达式缺少'('\n",line);
			    break;
		case 14:printf("第%d行缺少'+'或'-'\n",line);
                break;
		case 15:printf("第%d行缺少'*'或'/'\n",line);
			    break;
		case 16:printf("第%d行表达式错误\n",line);
			    break;
		case 17:printf("第%d行缺少return\n",line);
			    break;
		case 18:printf("第%d行缺少比较运算符\n",line);
			    break;
	}
}
	
void main_error()
{
	for(int i=1;i<TopGFT;i++)
	{
		if((strcmp(GFT[i].function_name,"main"))!=0)
			;
	    else 
			break;
	}
	if(i==TopGFT)//没有找到main函数
		printf("程序中没有main函数!\n");
}

void parser()
{   
	if((TokenFile=fopen("token.txt","r"))==NULL)
	{
		printf("打不开文件token.txt!\n");
		exit(0);
	}
	printf("       *****************************************************************\n");
	printf("                               语法分析器\n");
	printf("       *****************************************************************\n");
	for(int i=1;i<=Token_Number;i++)
	{
		advance();
		CurToken[i].LineofPro=CurrentToken.LineofPro;
		CurToken[i].code=CurrentToken.code;
		strcpy(CurToken[i].name,CurrentToken.name);
	}
	NextToken();
	InitTree();//初始化语法树
	Init_expression();//初始化表达式栈
	BuildSyntaxTree();
	Print();
	PrintTree(m_pProgram);
	printf("\n\n\n\n");
	printf("       *****************************************************************\n");
	printf("                               符号表\n");
	printf("       *****************************************************************\n");
	main_error();
	printf("变量名表:\n");
	PrintVT();
    printf("\n\n函数名表:\n");
	PrintGFT();
	printf("\n\n\n\n");
	printf("       *****************************************************************\n");
	printf("                                四元式\n");
	printf("       *****************************************************************\n");
	Print_equ();
	fclose(TokenFile);
}

//每次只先处理产生式前面的非终结符,如果遇到终结符则处理,如果是空字符则设置左孩子为空
//1.program->declaration_list
CTreeNode* program()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=1;
	Tree->Node_Number=k++;
	Tree->Leftchild=declaration_list();	
	Tree->Rightsibling=NULL;
	Tree->father=NULL;
	Tree->TOKEN.LineofPro=0;
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	LeftTree=Tree->Leftchild;  //处理左孩子的父结点
	LeftTree->Rightsibling=NULL;  //处理右兄弟结点
	LeftTree->father=Tree;
	return Tree;
}

//2.declaration_list->declaration declaration_list1
CTreeNode* declaration_list()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
	SyntaxTree sibling=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=2;
	Tree->Node_Number=k++;
	Tree->Leftchild=declaration();
	Tree->TOKEN.LineofPro=0;
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	LeftTree=Tree->Leftchild; //处理左孩子的父结点
	LeftTree->father=Tree;
	LeftTree->Rightsibling=declaration_list1();
	sibling=LeftTree->Rightsibling;
	sibling->father=Tree;
	sibling->Rightsibling=NULL;
	return Tree;
}

//3.declaration_list1->declaration declaration_list1|空字
CTreeNode* declaration_list1()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=3;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if(((strcmp(curToken.name,"int"))==0)||(strcmp(curToken.name,"void")==0)||(strcmp(curToken.name,"{")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=declaration();
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		LeftTree->Rightsibling=declaration_list1();
		sibling=LeftTree->Rightsibling;
		sibling->father=Tree;
		sibling->Rightsibling=NULL;
	}
	else 
		Tree->Leftchild=NULL;
	return Tree;
}

//4.declaration->type_specifier ID declaration1		 
CTreeNode* declaration()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=4;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if(((strcmp(curToken.name,"int"))==0)||(strcmp(curToken.name,"void")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling1=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling2=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=type_specifier();
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		if(curToken.code==26)//ID
		{
			LeftTree->Rightsibling=sibling1;
			sibling1->Leftchild=NULL;
			sibling1->father=Tree;
			sibling1->FID=4;
			sibling1->Node_Number=k++;
			sibling1->TOKEN.LineofPro=curToken.LineofPro;
			sibling1->TOKEN.code=curToken.code;
			strcpy(sibling1->TOKEN.name,curToken.name);
			strcpy(variable,curToken.name);
			NextToken();
			sibling1->Rightsibling=declaration1();
			sibling2=sibling1->Rightsibling;
			sibling2->father=Tree;
			sibling2->Rightsibling=NULL;
		}
		else error(1,curToken.LineofPro);//语法错误,没有ID
	}
	else error(7,curToken.LineofPro);//缺少数据类型
	return Tree;
}

//5.declaration1->;|[NUM];|(params) compound_stmt
CTreeNode* declaration1()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=5;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if((strcmp(curToken.name,";"))==0)
	{	
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=LeftTree;
		LeftTree->Leftchild=NULL;
		LeftTree->Rightsibling=NULL;
		LeftTree->father=Tree;
		LeftTree->FID=5;
		LeftTree->Node_Number=k++;
		LeftTree->TOKEN.LineofPro=curToken.LineofPro;
		LeftTree->TOKEN.code=curToken.code;
		strcpy(LeftTree->TOKEN.name,curToken.name);
		st_insert(variable,status,curToken.name);
		NextToken();
	}
	else
		if((strcmp(curToken.name,"["))==0)
		{
			SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
			SyntaxTree sibling1=(SyntaxTree)malloc(sizeof(CTreeNode));
			SyntaxTree sibling2=(SyntaxTree)malloc(sizeof(CTreeNode));
			SyntaxTree sibling3=(SyntaxTree)malloc(sizeof(CTreeNode));
			Tree->Leftchild=LeftTree;	
			LeftTree->Leftchild=NULL;
			LeftTree->father=Tree;
			LeftTree->FID=5;
			LeftTree->Node_Number=k++;
			LeftTree->TOKEN.LineofPro=curToken.LineofPro;
			LeftTree->TOKEN.code=curToken.code;
			strcpy(LeftTree->TOKEN.name,curToken.name);
			NextToken();
			if(curToken.code==27)//NUM
			{
				LeftTree->Rightsibling=sibling1;
				sibling1->Leftchild=NULL;
				sibling1->father=Tree;
				sibling1->FID=5;
				sibling1->Node_Number=k++;
				sibling1->TOKEN.LineofPro=curToken.LineofPro;
				sibling1->TOKEN.code=curToken.code;
				strcpy(sibling1->TOKEN.name,curToken.name);
				NextToken();
				if((strcmp(curToken.name,"]"))==0)
				{
					sibling1->Rightsibling=sibling2;
					sibling2->Leftchild=NULL;
					sibling2->father=Tree;
					sibling2->FID=5;
					sibling2->Node_Number=k++;
					sibling2->TOKEN.LineofPro=curToken.LineofPro;
					sibling2->TOKEN.code=curToken.code;
					strcpy(sibling2->TOKEN.name,curToken.name);
					NextToken();
					if((strcmp(curToken.name,";"))==0)
					{
						sibling2->Rightsibling=sibling3;
						sibling3->Leftchild=NULL;
						sibling3->father=Tree;
						sibling3->Rightsibling=NULL;
						sibling3->FID=5;
						sibling3->Node_Number=k++;
						sibling3->TOKEN.LineofPro=curToken.LineofPro;
						sibling3->TOKEN.code=curToken.code;
						strcpy(sibling3->TOKEN.name,curToken.name);
						NextToken();
					}
					else error(2,curToken.LineofPro);//缺少";"
				}
				else error(3,curToken.LineofPro);//缺少"]"

⌨️ 快捷键说明

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