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

📄 compile3.cpp

📁 一个不算完整的编译器实现
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{
		if(temp2->name=="statement-list")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index]=temp2;
	}  
	temp2->name ="statement-list";
    //查找statement       
    int index2=hash_funct("statement"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="statement")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到statement");
    //将返回的类型传给statment-list   
    temp2->re_type=temp1->re_type;
      
}
/*22statement-list #*/

/*23statement expression-stmt*/
void statement_expression()
{
	//创建statement
    int index=hash_funct("statement");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="statement")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index]=temp2;
		tableFunct[index]->name ="statement";
	}  
	
    //查找selection-stmt
    int index2=hash_funct("expression-stmt"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="expression-stmt")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到expression-stmt");
    //将返回的类型传给statment   
    temp2->re_type=temp1->re_type;	
}

/*24statement compound-stmt*/
/*25statement selection-stmt*/
void statement_select()
{
	//创建statement
    int index=hash_funct("statement");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="statement")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index]=temp2;
		tableFunct[index]->name ="statement";
	}  
	
    //查找selection-stmt
    int index2=hash_funct("selection-stmt"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="selection-stmt")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到selection-stmt");
    //将返回的类型传给statment   
    temp2->re_type=temp1->re_type;
}
/*26statement iteration-stmt*/
void statement_iteration()
{
	//创建statement
    int index=hash_funct("statement");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="statement")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		temp2->name ="statement";
		tableFunct[index]=temp2;
	}  
    //查找iteration-stmt
    int index2=hash_funct("iteration-stmt"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="iteration-stmt")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到iteration-stmt");
    //将返回的类型传给statment   
    temp2->re_type=temp1->re_type;
}
/*27statement return-stmt*/
void statement_return()
{
   
    //创建statement
    int index=hash_funct("statement");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="statement")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		temp2->name ="statement";
		tableFunct[index]=temp2;
	}  
    //查找return-stmt
    int index2=hash_funct("return-stmt"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="return-stmt")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到return-stmt");
    //将返回的类型传给statment   
    temp2->re_type=temp1->re_type;
}
/*28expression-stmt expression ;*/
void expressionstmt_expression()
{
	//创建expression-stmt
    int index=hash_funct("expression-stmt");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="expression-stmt")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index]=temp2;
		tableFunct[index]->name ="expression-stmt";
	}  
	
    //查找selection-stmt
    int index2=hash_funct("expression"); 
    table_var* temp1=tableVar[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="expression")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到expression-stmt");
    //将返回的类型传给statment   
    temp2->re_type=temp1->type;	
}
/*29expression-stmt ;*/
void expressionstmt()
{
//创建expression-stmt
    int index=hash_funct("expression-stmt");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="expression-stmt")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index]=temp2;
		tableFunct[index]->name ="expression-stmt";
	}  
    temp2->re_type="void";	
}
/*30selection-stmt if ( expression ) statement*/
void selectstmt_if()
{
	int index=hash_funct("expression");
	bool ex=true;
	table_var* temptable=tableVar[index];
	if(temptable==NULL)
	{
		p_error("不能查找到expression!");
	}
	else
	{
		while(temptable!=NULL)
		{
			if(temptable->name=="expression")
			{
				ex=false;
				if(temptable->type!="bool")
				{
					perror("The expression type of select-stmt is wrong ");
				}
				break;
			}
			else
				temptable=temptable->next;
		}
		if(ex==true)
		{
			p_error("无法查找到expression");
		}
	}
	//构造selection-stmt节点
	int index1=hash_funct("selection-stmt");
	table_funct* temp2=tableFunct[index1];
	while(temp2!=NULL)
	{
		if(temp2->name=="selection-stmt")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index1]=temp2;
	}  
	temp2->name ="selection-stmt";
	temp2->re_type ="bool";
}
/*31selection-stmt if ( expression ) statement else statement*/
void selectstmt_else()
{
	selectstmt_if();
}
/*32iteration-stmt while ( expression ) statement*/
void iterationstmt()
{
	int index=hash_funct("expression");
	bool ex=true;
	table_var* temptable=tableVar[index];
	if(temptable==NULL)
	{
		p_error("不能查找到expression!");
	}
	else
	{
		while(temptable!=NULL)
		{
			if(temptable->name=="expression")
			{
				ex=false;
				if(temptable->type!="bool")
				{
					perror("The expression type of select-stmt is wrong ");
				}
				break;
			}
			else
				temptable=temptable->next;
		}
		if(ex==true)
		{
			p_error("无法查找到expression");
		}
	}
	//构造iteration-stmt节点
	int index1=hash_funct("iteration-stmt");
	table_funct* temp2=tableFunct[index1];
	while(temp2!=NULL)
	{
		if(temp2->name=="iteration-stmt")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index1]=temp2;
	}  
	temp2->name ="iteration-stmt";
	temp2->re_type ="bool";
}
/*33return-stmt return ;*/
void returnstmt()
{
	int index=hash_funct("return-stmt");
	table_funct* temptable=tableFunct[index];
	if(temptable!=NULL)
	{
		while(temptable!=NULL)
		{
			if(temptable->name=="return-stmt")
				break;
			else
				temptable=temptable->next;
		}
	}
	if(temptable==NULL)
	{
		temptable=new table_funct();
		tableFunct[index]=temptable;
	}
	temptable->name="return-stmt";
	temptable->re_type="void";
}
/*34return-stmt return expression ;*/
void returnstmt_expression()
{
	int index=hash_funct("return-stmt");
	table_funct* temptable=tableFunct[index];
	if(temptable!=NULL)
	{
		while(temptable!=NULL)
		{
			if(temptable->name=="return-stmt")
				break;
			else
				temptable=temptable->next;
		}
	}
	if(temptable==NULL)
	{
		temptable=new table_funct();
		tableFunct[index]=temptable;
	}

	int index2=hash_funct("expression");
	table_var* temp=tableVar[index2];
	bool b=true;
	if(temp==NULL)
		p_error("无法查找到expression");
	else
	{
		while(temp!=NULL)
		{
			if(temp->name=="expression")
			{
				temptable->re_type=temp->type;
				b=false;
				break;
			}
			else
				temp=temp->next;
		}
		if(b==true)
			p_error("无法查找到expression");
	}
	temptable->name="return-stmt";
}
/*35expression var = expression*/
void expressionvar()
{
	bool b=true;
	int index1=hash_funct("var");
	int index2=hash_funct("expression");
	table_var* temp=tableVar[index1];
	if(temp==NULL)
		p_error("无法查找到var");
	else
	{
		while(temp!=NULL)
		{
			if(temp->name =="var")
			{
				b=false;
				break;
			}
		}
		if(b==true)
			p_error("无法查找到var");
	}

	table_var* temp2=tableVar[index2];
	if(temp2==NULL)
		p_error("无法查找到expression");
	else
	{
		while(temp2!=NULL)
		{
			if(temp2->name =="expression")
			{
				b=false;
				break;
			}
			else
				temp2=temp2->next;
		}
		if(b==true)
			p_error("无法查找到expression");
	} 
	/* var = expression*/
	if(temp->type!=temp2->type)
	{
		p_error("赋值时类型不匹配!");
		temp2->type="int";
	}
	else
		temp2->type ="bool";	
}

/*36expression simple-expression*/
void simpleexpression()
{
	//找到simple-expression节点
	int index=hash_funct("simple-expression");
	table_var* temp1=tableVar[index];
	while(temp1!=NULL)
	{
		if(temp1->name=="simple-expression")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到simple-expression");
	//构造expression节点
	int index1=hash_funct("expression");
	table_var* temp2=tableVar[index1];
	while(temp2!=NULL)
	{
		if(temp2->name=="expression")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_var();
		tableVar[index1]=temp2;
	}
	temp2->name="expression";
	temp2->type=temp1->type;
	temp2->value =temp2->value ;
}
/*37var ID*/
void varID(string name)
{
	int index1=hash_funct("var");
	table_var* temp=tableVar[index1];
	while(temp!=NULL)
	{
		if(temp->name=="var")
		{
			break;
		}
		else
			temp=temp->next;
	}
	if(temp==NULL)
	{
		temp=new table_var();
		tableVar[index1]=temp;
	}
	temp->name="var";
	
	//找到name所在的位置
	int index=hash_funct(name);
	table_var* temp2=tableVar[index];
	while(temp2!=NULL)
	{
		if(temp2->name==name)

⌨️ 快捷键说明

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