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

📄 compile3.cpp

📁 一个不算完整的编译器实现
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		if(temp1->name=="type-specifier")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到type-specifier!");  
	//新建ID
	int index=hash_funct(name);
	table_var* temp2=tableVar[index];
	while(temp2!=NULL)
	{
		if(temp2->name==name)
		{
			p_error("变量已经存在,重复声明");
			break;
		}
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_var();
		tableVar[index]=temp2;
	}  
	//temp2->type =temp1->type ;
	temp2->type=stack_type.top();
	stack_type.pop();
	temp2->name =name;
	temp2->address =offset;
	offset+=1;//应该是sizeof(type-specifier.type);
	
}
/*7var-declaration type-specifier ID [ NUM ] ;*/
void typeArray(string name,int num)
{                                             
	int index=hash_funct(name);
	if(tableVar[index]==NULL && tableFunct[index]==NULL)
	{//未声明
		if(num<0)
			p_error("数组越界");
		else
		{
			tableVar[index]=new table_var();
			tableVar[index]->name=name;
			tableVar[index]->address=offset;
			offset+=num;//应该为num*sizeof(type-specifier.type)
			//tableVar[index]->type=tableVar[hash_funct("type-specifier")]->type;
			tableVar[index]->type=stack_type.top();
			stack_type.pop();
			tableVar[index]->level=level;
		}
	}
	else
	{//已声明

		bool b=true;
		table_var* temptable=tableVar[index];
		while(temptable!=NULL)
		{
			if(temptable->name==name)
			{
				p_error("The array has been declared!");
				b=false;
				break;
			}
			else
			{
				temptable=temptable->next;
			}

		}
		if(b==true)
		{
			temptable=new table_var();
			temptable->name=name;
			//temptable->type=tableVar[hash_funct("type-specifier")]->type;
			tableVar[index]->type=stack_type.top();
			stack_type.pop();
			temptable->address=offset;
			offset+=num;//应该是num*sizeof(type-specifier.type);
			temptable->level=level;
			tableVar[index]=temptable;
		}	
	}
	
}
/*8type-specifier int*/
void  typeInt()
{
	//新建type-specifier节点
	int index=hash_funct("type-specifier");
	table_var* temp2=tableVar[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="type-specifier")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_var();
		tableVar[index]=temp2;
	}  

	temp2->type="int";
	temp2->name="type-specifier";
	stack_type.push("int");
}
/*9type-specifier void*/
void typeVoid()
{
	int index=hash_funct("type-specifier");
	table_var* temp2=tableVar[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="type-specifier")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_var();
		tableVar[index]=temp2;
	}  
	temp2->type="void";
	temp2->name="type-specifier";
	stack_type.push("void");
}
/*10type-specifier bool*/
void typeBool()
{
	int index=hash_funct("type-specifier");
	table_var* temp2=tableVar[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="type-specifier")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_var();
		tableVar[index]=temp2;
	}  
	temp2->type="bool";
	temp2->name="type-specifier";
	stack_type.push("bool");
}
/*11fun-declaration type-specifier ID ( params ) compound-stmt*/
void fundecl(string name)
{
	int index=hash_funct(name);
	if(tableFunct[index]==NULL &&tableVar[index]==NULL)
	{//符号表中不存在
		tableFunct[index]=new table_funct();
		tableFunct[index]->name=name;
		//tableFunct[index]->re_type=tableVar[hash_funct("type-specifier")]->type;
		tableFunct[index]->re_type=stack_type.top();
		stack_type.pop();
		int count_param=0;
		count_param=tableFunct[hash_funct("params")]->size_param;
		for(int i=0;i<count_param;i++)
		{
			tableFunct[index]->param_type.push_back(tableFunct[hash_funct("params")]->param_type[i]);
		}
	}
	else
	{
		bool b=true;
		//检查函数符号表
		table_funct* temptable=tableFunct[index];
		while(temptable!=NULL)
		{
			if(temptable->name==name)
			{
				p_error("The function has been declared!");
				b=false;
				break;
			}
			else
			{
				temptable=temptable->next;
			}

		}
		//检查变量符号表
		table_var* temp=tableVar[index];
		while(temp!=NULL)
		{
			if(temp->name==name)
			{
				p_error("函数名和变量名冲突");
				b=false;
				break;
			}
			else
			{
				temp=temp->next;
			}

		}

		if(b==true)
		{
			tableFunct[index]=new table_funct();
			tableFunct[index]->name=name;
			tableFunct[index]->re_type=tableVar[hash_funct("type-specifier")]->type;
			int count_param=0;
			count_param=tableFunct[hash_funct("params")]->size_param;
			for(int i=0;i<count_param;i++)
			{
				tableFunct[index]->param_type.push_back(tableFunct[hash_funct("params")]->param_type[i]);
			}
		}
	}
    //查找compound-stmt
    int index2=hash_funct("compound-stmt"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="compound-stmt")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		perror("无法查找到compound-stmt!");  
	//比较返回类型是否一样
	//cout<<temp1->re_type <<" "<<tableFunct[index]->re_type;
    if(temp1->re_type!=tableFunct[index]->re_type) 
        p_error("返回值的类型与函数定义返回类型不一致");
}
/*12params param-list*/
void paramlist()
{
	//查找
	int index2=hash_funct("param-list"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="param-list")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到param-list!"); 
	//创建
	bool b=true;
	int index=hash_funct("params");
	table_funct* temptable=tableFunct[index];
	if(temptable!=NULL )
	{
		while(temptable!=NULL)
		{
			if(temptable->name=="params")
			{	
                b=false;
				break;
			}
			else
				temptable=temptable->next;
		}
		
	}
	if(temptable==NULL)
	{
         temptable=new table_funct();  
		 tableFunct[index]=temptable;
    }
   temptable->name ="params";
   temptable->size_param =temp1->size_param;
   for(int i=0;i<temptable->size_param;i++)
   {
        temptable->param_type.push_back(temp1->param_type[i]);
   }
  
}
/*13params void*/
void paramvoid()
{
	int index2=hash_funct("params"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="params")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
	{
		temp1=new table_funct();
		tableFunct[index2]=temp1;
	}
	temp1->param_type.push_back("void");
	temp1->size_param=0;
	
}
/*14param-list param-list , param*/
void paramlist_s()
{
	//查找param-list
    int index3=hash_funct("param-list"); 
    table_funct* temp3=tableFunct[index3];
	while(temp3!=NULL)
	{
		if(temp3->name=="param-list")
			break;
		else
			temp3=temp3->next;
	}
	if(temp3==NULL)
		p_error("无法查找到param-list!"); 
     //查找param 
    int index=hash_funct("param"); 
    table_funct* temp=tableFunct[index];
	while(temp!=NULL)
	{
		if(temp->name=="param")
			break;
		else
			temp=temp->next;
	}
	if(temp==NULL)
		p_error("无法查找到param!"); 
    //将param中的参数类型传递给param-list
    temp3->param_type.push_back(temp->param_type.front());    
}
/*15param-list param*/
void plist_param()
{
	 //查找param
    int index4=hash_funct("param"); 
    table_funct* temp=tableFunct[index4];
	while(temp!=NULL)
	{
		if(temp->name=="param")
			break;
		else
			temp=temp->next;
	}
	if(temp==NULL)
		p_error("无法查找到param!"); 
	//建立param-list
     int index=hash_funct("param-list");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="param-list")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index]=temp2;
	}   
    temp2->name ="param-list";
     //param-list的参数个数加一
     temp2->size_param++;
     //将param的类型添加到param-list的参数类型 表中 
     temp2->param_type.push_back(temp->param_type.front());
	 
}
/*16param type-specifier ID*/
void paramID(string name)
{
	//构建ID
	int index2=hash_funct(name);
	bool existID=false;
	table_var* temptable_var=tableVar[index2];
	if(temptable_var==NULL)
	{
		temptable_var=new table_var();
		temptable_var->name=name;
	}
	else
	{
		while(temptable_var!=NULL)
		{
			if(temptable_var->name==name)
			{
				existID=true;
				break;
			}
			else
				temptable_var=temptable_var->next;
		}
		if(existID==true)
		{
			string s=name+"变量已被定义!";
			p_error(s);//未声明就使用
		}
	}
	if(existID==false)
	{
		temptable_var=new table_var();
		temptable_var->name=name;
		tableVar[index2]=temptable_var;
	}
	
    //构建param
	int index=hash_funct("param");
	bool b=true;
	table_funct* temptable=tableFunct[index];
	if(temptable!=NULL)
	{
		while(temptable!=NULL)
		{
			if(tableFunct[index]->name=="param")
			{//找到的话覆盖原来的
				b=false;
				break;
			}
			else
				temptable=temptable->next;
		}
	}
	if(b==true)
	{
		temptable=new table_funct();
		temptable->name="param";
		tableFunct[index]=temptable;
	}
	
	//查找type-specifier
	bool b1=true;		
	int temp=hash_funct("type-specifier");
	if(tableVar[temp]==NULL)
		p_error("无法找到param");
	else
	{
		table_var* temptable2=tableVar[temp];
		while(temptable2!=NULL)
		{
			if(temptable2->name=="type-specifier")
			{
				b1=false;
				temptable->param_type.push_back(temptable2->type);
				//temptable_var->type=temptable2->type;
				temptable_var->type=stack_type.top();
				stack_type.pop();
				break;
					
			}
			else
				temptable2=temptable2->next;
		}
		if(b1==true)
			p_error("无法找到param");
	}
	
}
/*17param type-specifier ID [ ]*/
void paramArray(string name)
{	
	paramID(name);
}
/*18compound-stmt { local-declarations statement-list }*/
void compound_stmt()
{
 //创建compound-stmt
    int index=hash_funct("compound-stmt");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)
	{
		if(temp2->name=="compound-stmt")
			break;
		else
			temp2=temp2->next;
	}
	if(temp2==NULL)
	{
		temp2=new table_funct();
		tableFunct[index]=temp2;
	}  
	temp2->name="compound-stmt";
    //查找statement-list       
    int index2=hash_funct("statement-list"); 
    table_funct* temp1=tableFunct[index2];
	while(temp1!=NULL)
	{
		if(temp1->name=="statement-list")
			break;
		else
			temp1=temp1->next;
	}
	if(temp1==NULL)
		p_error("无法查找到statement-list");
    //将返回的类型传给statment-list   
    temp2->re_type=temp1->re_type;     
}
/*19local-declarations local-declarations var-declaration*/

/*20local-declarations #*/

/*21statement-list statement-list statement*/
void statement_list()
{
    //创建statement-list
    int index=hash_funct("statement-list");
	table_funct* temp2=tableFunct[index];
	while(temp2!=NULL)

⌨️ 快捷键说明

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