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

📄 yufa.cpp

📁 本程序是实现C-的一个小型编译器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
						sibling3->TOKEN.code=curToken.code;
						strcpy(sibling3->TOKEN.name,curToken.name);
						NextToken();
						sibling3->Rightsibling=local_declarations();
						sibling4=sibling3->Rightsibling;
						sibling4->father=Tree;
						sibling4->Rightsibling=NULL;
					}
					else error(2,curToken.LineofPro);//缺少";"
				}
				else error(3,curToken.LineofPro);//缺少"]"
			}
			else error(4,curToken.LineofPro);//缺少"NUM"
		}
		else error(10,curToken.LineofPro);//缺少"["
	return Tree;
}

//15.statement_list->空字|statement statement_list
CTreeNode* statement_list()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=15;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if((curToken.code==18)||(curToken.code==24)||(curToken.code==26)\
	||(curToken.code==27)||(curToken.code==2)||(curToken.code==4)||(curToken.code==6))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=statement();
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		LeftTree->Rightsibling=statement_list();
		sibling=LeftTree->Rightsibling;
		sibling->father=Tree;
		sibling->Rightsibling=NULL;
	}
	else 
		Tree->Leftchild=NULL;//空字符处理
	return Tree;
}

//16.statement->compound_stmt|expression_stmt|selection_stmt|iteration_stmt|return_stmt
CTreeNode*  statement()
{		
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=16;
	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=compound_stmt();
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		LeftTree->Rightsibling=NULL;
	}
	if(((strcmp(curToken.name,";")==0))||(curToken.code==26)||(curToken.code==27)||((strcmp(curToken.name,"("))==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=expression_stmt();
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		LeftTree->Rightsibling=NULL;
	}
	if((strcmp(curToken.name,"if")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=selection_stmt();
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		LeftTree->Rightsibling=NULL;
	}	
	if((strcmp(curToken.name,"while")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=iteration_stmt();
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		LeftTree->Rightsibling=NULL;
	}
	if((strcmp(curToken.name,"return")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=return_stmt();    
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		LeftTree->Rightsibling=NULL;
	}	
//	else error(11,curToken.LineofPro);//复合句语法错误
	return Tree;
}

//17.expression_stmt->expression;|;
CTreeNode* expression_stmt()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=17;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if((curToken.code==26)||(curToken.code==27)||(strcmp(curToken.name,"(")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=expression();
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		if((strcmp(curToken.name,";")==0))
		{
			LeftTree->Rightsibling=sibling;
			sibling->father=Tree;
			sibling->Leftchild=NULL;
			sibling->Rightsibling=NULL;
			sibling->FID=17;
			sibling->Node_Number=k++;
			sibling->TOKEN.LineofPro=curToken.LineofPro;
			sibling->TOKEN.code=curToken.code;
			strcpy(sibling->TOKEN.name,curToken.name);
			NextToken();
		}
		else error(2,curToken.LineofPro);//缺少";"
	}
	else 
		if((strcmp(curToken.name,";")==0))
		{
			SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
			Tree->Leftchild=LeftTree;
			LeftTree->Leftchild=NULL;
			LeftTree->father=Tree;
			LeftTree->Rightsibling=NULL;
			LeftTree->FID=17;
			LeftTree->Node_Number=k++;
			LeftTree->TOKEN.LineofPro=curToken.LineofPro;
			LeftTree->TOKEN.code=curToken.code;
			strcpy(LeftTree->TOKEN.name,curToken.name);
			NextToken();
		}
		else error(2,curToken.LineofPro);//缺少";"
	return Tree;
}

//18.selection_stmt->if (expression) statement selection_stmt1 
CTreeNode* selection_stmt()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=18;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if((strcmp(curToken.name,"if")==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));
		SyntaxTree sibling4=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling5=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=LeftTree;
		LeftTree->Leftchild=NULL;
		LeftTree->father=Tree;
		LeftTree->FID=18;
		LeftTree->Node_Number=k++;
		LeftTree->TOKEN.LineofPro=curToken.LineofPro;
		LeftTree->TOKEN.code=curToken.code;
		strcpy(LeftTree->TOKEN.name,curToken.name);
		True_address=nextquad;//真出口
		NextToken();
		if((strcmp(curToken.name,"(")==0))
		{
			LeftTree->Rightsibling=sibling1;
			sibling1->Leftchild=NULL;
			sibling1->father=Tree;
			sibling1->FID=18;
			sibling1->Node_Number=k++;
			sibling1->TOKEN.LineofPro=curToken.LineofPro;
			sibling1->TOKEN.code=curToken.code;
			strcpy(sibling1->TOKEN.name,curToken.name);
			stack[R++]='(';	
			NextToken();
			sibling1->Rightsibling=expression();
			sibling2=sibling1->Rightsibling;
			sibling2->father=Tree;
			if((strcmp(curToken.name,")")==0))
			{
				sibling2->Rightsibling=sibling3;	
				sibling3->Leftchild=NULL;
				sibling3->father=Tree;
				sibling3->FID=18;
				sibling3->Node_Number=k++;
				sibling3->TOKEN.LineofPro=curToken.LineofPro;
				sibling3->TOKEN.code=curToken.code;
				strcpy(sibling3->TOKEN.name,curToken.name);
				stack[--R]='\0';	
				False_address=EquPush("\0","\0","\0",nextquad);//假出口	
				BackPatch(True_address,nextquad);//回填
				True_address=nextquad;//将下一个出口作为真出口
				NextToken();
				sibling3->Rightsibling=statement();
				sibling4=sibling3->Rightsibling;
				sibling4->father=Tree;
				sibling4->Rightsibling=selection_stmt1();
				sibling5=sibling4->Rightsibling;
				sibling5->father=Tree;
				sibling5->Rightsibling=NULL;
			}
			else error(12,curToken.LineofPro);//表达式缺少")"
		}
		else error(13,curToken.LineofPro);//表达式缺少"("
	}
	return Tree;
}

//19.selection_stmt1->空字|else statement
CTreeNode* selection_stmt1()
{ 
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=19;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	BackPatch(False_address,nextquad);//回填
	False_address=nextquad;
	if((strcmp(curToken.name,"else")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=LeftTree;
		LeftTree->Leftchild=NULL;
		LeftTree->father=Tree;
		LeftTree->FID=19;
		LeftTree->Node_Number=k++;
		LeftTree->TOKEN.LineofPro=curToken.LineofPro;
		LeftTree->TOKEN.code=curToken.code;
		strcpy(LeftTree->TOKEN.name,curToken.name);
		NextToken();
		LeftTree->Rightsibling=statement(); 
		sibling=LeftTree->Rightsibling;
		sibling->father=Tree;
		sibling->Rightsibling=NULL;
		BackPatch(False_address,nextquad);//回填假出口
		BackPatch(True_address,nextquad);//回填真出口
	}
	else 
	{	Tree->Leftchild=NULL;//空字符处理
		BackPatch(True_address,nextquad);//回填
	}
	return Tree;
}
	
//20.iteration_stmt->while (expression) statement
CTreeNode* iteration_stmt()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=20;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	True_address=nextquad;//真出口
	if((strcmp(curToken.name,"while")==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));
		SyntaxTree sibling4=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=LeftTree;
		LeftTree->Leftchild=NULL;
		LeftTree->father=Tree;
		LeftTree->FID=20;
		LeftTree->Node_Number=k++;
		LeftTree->TOKEN.LineofPro=curToken.LineofPro;
		LeftTree->TOKEN.code=curToken.code;
		strcpy(LeftTree->TOKEN.name,curToken.name);
		NextToken();
		if((strcmp(curToken.name,"(")==0))
		{
			LeftTree->Rightsibling=sibling1;
			sibling1->Leftchild=NULL;
			sibling1->father=Tree;
			sibling1->FID=20;
			sibling1->Node_Number=k++;
			sibling1->TOKEN.LineofPro=curToken.LineofPro;
			sibling1->TOKEN.code=curToken.code;
			strcpy(sibling1->TOKEN.name,curToken.name);
			NextToken();
			sibling1->Rightsibling=expression();
			sibling2=sibling1->Rightsibling;
			sibling2->father=Tree;
			if((strcmp(curToken.name,")")==0))
			{
				sibling2->Rightsibling=sibling3;	
				sibling3->Leftchild=NULL;
				sibling3->father=Tree;
				sibling3->FID=20;
				sibling3->Node_Number=k++;
				sibling3->TOKEN.LineofPro=curToken.LineofPro;
				sibling3->TOKEN.code=curToken.code;
				strcpy(sibling3->TOKEN.name,curToken.name);
				False_address=EquPush("\0","\0","\0",nextquad);//假出口
				BackPatch(True_address,nextquad);//回填
				True_address=nextquad;//将下一个出口作为真出口
				NextToken();
				sibling3->Rightsibling=statement();
				BackPatch(False_address,nextquad);//回填假出口
				BackPatch(True_address,nextquad);//回填真出口
				sibling4=sibling3->Rightsibling;
				sibling4->father=Tree;
				sibling4->Rightsibling=NULL;
			}
			else error(12,curToken.LineofPro);//表达式缺少")"
		}
		else error(13,curToken.LineofPro);//表达式缺少"("
	}
	return Tree;
}

//21.return_stmt->return return_stmt1
CTreeNode* return_stmt()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=21;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if((strcmp(curToken.name,"return")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=LeftTree;
		LeftTree->Leftchild=NULL;
		LeftTree->father=Tree;
		LeftTree->FID=21;
		LeftTree->Node_Number=k++;
		LeftTree->TOKEN.LineofPro=curToken.LineofPro;
		LeftTree->TOKEN.code=curToken.code;
		strcpy(LeftTree->TOKEN.name,curToken.name);
		strcpy(OP,"return");
		NextToken();
		LeftTree->Rightsibling=return_stmt1();   
		sibling=LeftTree->Rightsibling;
		sibling->father=Tree;
		sibling->Rightsibling=NULL;
	}
	else error(17,curToken.LineofPro);//缺少"return"
	return Tree;
}

//22.return_stmt1->;|expression;
CTreeNode* return_stmt1()
{
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=22;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if((curToken.code==26)||(curToken.code==27)||(strcmp(curToken.name,"(")==0))
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		SyntaxTree sibling=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=expression();  
		LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		if((strcmp(curToken.name,";")==0))
		{		
			LeftTree->Rightsibling=sibling;
			sibling->father=Tree;
			sibling->Leftchild=NULL;
			sibling->Rightsibling=NULL;
			sibling->FID=22;
			sibling->Node_Number=k++;
			sibling->TOKEN.LineofPro=curToken.LineofPro;
			sibling->TOKEN.code=curToken.code;
			strcpy(sibling->TOKEN.name,curToken.name);
			NextToken();
		}
		else error(2,curToken.LineofPro);//缺少";"
	}
	else 
		if((strcmp(curToken.name,";")==0))
		{
			SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
			Tree->Leftchild=LeftTree;
			LeftTree->Leftchild=NULL;
			LeftTree->father=Tree;
			LeftTree->Rightsibling=NULL;
			LeftTree->FID=22;
			LeftTree->Node_Number=k++;
			LeftTree->TOKEN.LineofPro=curToken.LineofPro;
			LeftTree->TOKEN.code=curToken.code;
			strcpy(LeftTree->TOKEN.name,curToken.name);
			NextToken();
		}
		else error(2,curToken.LineofPro);//缺少";"
	return Tree;
}

//23.expression->var=expression|simple_expression
CTreeNode* expression()
{		
	int length=0,i=0;
	SyntaxTree Tree=(SyntaxTree)malloc(sizeof(CTreeNode));
	Tree->FID=23;
	Tree->Node_Number=k++;
	Tree->TOKEN.LineofPro=0;	
	Tree->TOKEN.code=0;
	strcpy(Tree->TOKEN.name,"\0");
	if((curToken.code==27)||(strcmp(curToken.name,"(")==0))//NUM
	{
		SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
		Tree->Leftchild=simple_expression();
	    LeftTree=Tree->Leftchild;
		LeftTree->father=Tree;
		LeftTree->Rightsibling=NULL;
	}
	else
		if(curToken.code==26)//ID
		{	
			NextToken();
			length++;
			if(((strcmp(curToken.name,"[")==0))||((strcmp(curToken.name,"=")==0)))
			{
				SyntaxTree LeftTree=(SyntaxTree)malloc(sizeof(CTreeNode));
				SyntaxTree sibling1=(SyntaxTree)malloc(sizeof(CTreeNode));

⌨️ 快捷键说明

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