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

📄 test3.cpp

📁 小型对delphi编译器包括语法语义及词法
💻 CPP
字号:
#include"Word.h"

struct IdType
{
	char name[10];
	int Type,Kind,Addr,lev;							//Kind
}IdTab[288];

struct temp
{
	int kind;										//表明临时变量
	int type;										//表明类型
	int lev;										//用于定义优先级
	char name[10];
}temp1[100],temp2[100];

struct List
{
	int num;										//临时标量常数
	char name[10];
};
struct Quad											//用于表示四元式
{
	int Op;
	List Arg1;
	List Arg2;
	List Result;
}Qtype[100];
/*char bi[23][8]={" ","In","call","ret",":=I",":=R","par","valueI","valueR","addr","j","j<","j<="
,"j>","j>=","j=","j<>","+I","+R","*I","*R"};*/

int k=0;											//用于标志二元式
int id;
int lev;
int t; 
int cong;											//标示临时标量
int cong1;
int four;											//标示四元式
int flag;											//用于寻找真出口
int flag1=3;	
int flag2;	
int flag3;
int flag4;									//
//int 
//int tu;							//biao
void Expression();
bool  sentence();
void BoolExpression();
void Appearbefore();
void Procedding();
void longsentence();


void  design()
{
	for(int t=0;t<=flag2;t++)
	{
		if(Qtype[t].Result.num==0&&Qtype[t].Op==11)
			Qtype[t].Result.num=four+1;
	}
//	Qtype[flag2].Result.num=four+1;
/*	for(int i=0;i<cong1;i++)
	 {
		 temp2[i].kind=temp2[i].lev=temp2[i].type=0;
		 for(int j=0;j<10;j++)
		 temp2[i].name[j]='0';
	 }cong1=0;*/
}

void bool_gen()
{		
	int t=0;int t1=four;
	while(t<cong1)
	{
		int bian=1;
		strcpy(Qtype[four].Arg1.name,temp2[t].name);
	 	if(temp2[t+1].lev==25)
		Qtype[four].Op=14;//temp2[t+1].lev;
		else if(temp2[t+1].lev==23)
			Qtype[four].Op=12;
		else if(temp2[t+1].lev==24)
			Qtype[four].Op=13;
		else if(temp2[t+1].lev==26)
			Qtype[four].Op=15;
		else if(temp2[t+1].lev==27)
			Qtype[four].Op=16;
		else
			Qtype[four].Op=17;
	//	Qtype[four].Op=12;	
		strcpy(Qtype[four].Arg2.name,temp2[t+2].name);
		if(temp2[t+3].lev==21)
		{
			Qtype[four].Result.num=four+3;
			++four;t=t+4;
			strcpy(Qtype[four].Arg1.name,"0");
			Qtype[four].Op=11;
			strcpy(Qtype[four].Arg2.name,"0");
			for(int i=t;i<cong1;i++)
			if(temp2[i].lev==21||temp2[i].lev==22)
			{ 
				++bian;
				if(temp2[i].lev==22)
				Qtype[four].Result.num=four+bian*2;
			}
		}
		else 
		{	
			t=t+4;
			for(int i=t;i<cong;i++)
			if(temp2[i].lev==21||temp2[i].lev==22)
			{ 
				++bian;
				if(temp2[i].lev==21)
				{
					bian=bian*2+four;
					Qtype[four].Result.num=bian+1;
				}
			}
			++four;
				strcpy(Qtype[four].Arg1.name,"0");
			Qtype[four].Op=11;
			strcpy(Qtype[four].Arg2.name,"0");
			Qtype[four].Result.num=four+2;
		}++four;
	
	}
//	if(temp2[cong1-4].lev==21)
		Qtype[four-1].Result.num=0;
	for(;t1<four;t1++)
	{
		if(Qtype[t1].Result.num==0&&Qtype[t1].Op!=11)
			Qtype[t1].Result.num=four+1;
	}
	flag1=0;
	flag2=four-1;										//用以
	for(int i=0;i<cong1;i++)
	 {
		 temp2[i].kind=temp2[i].lev=temp2[i].type=0;
		 for(int j=0;j<10;j++)
		 temp2[i].name[j]='0';
	 }cong1=0;
//	flag3=0;
//	--four;

}

void progen()
{	
	int y=0;
    int t; static int ci;static int temp=0;
	for(int temp2=1;temp2<cong;temp2++)				//变量类型不匹配
		if(temp1[0].type==1&&temp1[temp2].type!=temp1[0].type)
		{cout<<"warning"<<":变量类型不匹配"<<endl;break;}
	for(int n=1;n<cong-1;n++)
	{
		t=1;y++;
		for(int i=1; i<cong;i++)
		{
			//int Max;
		if(temp1[i].lev>temp1[t].lev)
			t=i;
		}

		if(temp1[t].lev%2==0)
		{
			if(temp1[0].type==1)
				Qtype[four].Op=19;
			else
				Qtype[four].Op=20;
		}
		else 
		{
			if(temp1[0].type==1)
				Qtype[four].Op=21;
			else
				Qtype[four].Op=22;
		}
			temp1[t].lev=-1;
	strcpy(Qtype[four].Arg1.name,temp1[t].name);
	Qtype[four].Arg1.num=temp1[t].kind;
		while(temp1[t+1].lev<0)
		++t;
	strcpy(	Qtype[four].Arg2.name,temp1[t+1].name);
	Qtype[four].Arg2.num=temp1[t+1].kind;
		ci=ci+1;
		temp1[t+1].kind=ci;
		strcpy(temp1[t+1].name,"T");
	strcpy(Qtype[four].Result.name,temp1[t+1].name);
	Qtype[four].Result.num=temp1[t+1].kind;
	++four;	
	Qtype[four].Arg2.num=temp1[t+1].kind;
	strcpy(	Qtype[four].Arg2.name,temp1[t+1].name);
	}
	if(temp1[0].type==1)
	Qtype[four].Op=5;
	else
	Qtype[four].Op=6;
	strcpy(Qtype[four].Arg1.name,temp1[0].name);
	if(y==0)
	{strcpy(Qtype[four].Arg2.name,temp1[1].name);
		Qtype[four].Result.num=0;}
	else
	{
		strcpy(	Qtype[four].Arg2.name,Qtype[four-1].Result.name);
		Qtype[four].Arg2.num=Qtype[four-1].Result.num;
	}
	strcpy(Qtype[four].Result.name," ");
	++four;	
	if(flag3==1)									//if 语句用于跳出
	{
		Qtype[four].Op=11;
		strcpy(Qtype[four].Arg1.name,"0");
		strcpy(Qtype[four].Arg2.name,"0");
		Qtype[four].Result.num=-1;
		++four;
	}
	if(flag3==2)									//while 语句jmp
	{
		Qtype[four].Op=11;
		strcpy(Qtype[four].Arg1.name,"0");
		strcpy(Qtype[four].Arg2.name,"0");
		Qtype[four].Result.num=-2;
		++four;
	}
}


int lookup()
{
	for(int i=0;i<id;i++)
	{
		if(!(strcmp(an[k].word,IdTab[i].name)))
		return IdTab[i].Type;
	}
	cout<<an[k].row<<": need define variable";
		exit(0);
}

void Factor()											//19.<因子>
{
	if(an[k].kind==33)
	{	
		++k; lev=lev+2;									// an[k].lev=+2;
		Expression();
		if(an[k].kind==34)
		{
			++k;lev=lev-2;
			temp1[cong-1].lev=temp1[cong-1].lev-2;
		//	tu=0;//an[k-1].lev=lev;
		}
		else {cout<<an[k].row<<": Need ')'"; }
	}
	else if(an[k].kind==1||an[k].kind==2)
	{
		if(an[k].kind==1)
		temp1[cong].type=lookup();
		else
		temp1[cong].type=1;
		temp1[cong].lev=lev;
		strcpy(temp1[cong].name,an[k].word);	
		++cong; ++k;
	//	an[k].lev=lev;
			//if(an[k].num==1)
			//{	cout<<an[k].row<<": error(variable need defination)"; exit(0);}	
	

	}
}
void relation()
{
	if(an[k].kind>=23&&an[k].kind<=28)
	{
		
		++k;
	}
//	else if(an[k].kind
}
void Temptype()
{
	int t=k-3;
	strcpy(temp2[cong1].name,an[t].word);
	temp2[cong1].lev=3;
	++cong1;++t;
	strcpy(temp2[cong1].name,an[t].word);	
	temp2[cong1].lev=an[t].kind;						
	++cong1;++t;
	strcpy(temp2[cong1].name,an[t].word);
	temp2[cong1].lev=3;
	++cong1;
}


void RelationExpression()
{
	int t=k;
    Expression();
	if(t==k)
	{
		cout<<an[k].row<<":  need Expression";
		exit(0);
	}
	relation();
	//if(an[k]
	Expression();
	Temptype();
	if(an[k].kind==21||an[k].kind==22)
	{
		strcpy(temp2[cong1].name,an[k].word);			
			temp2[cong1].lev=an[k].kind;				//为|的情况
			++cong1;
			++k;
			BoolExpression();
	}
	
	
}

void BoolExpression()
{
	if(an[k].kind==33)
	{
		++k;
		if(an[k].kind==20)
		{++k;BoolExpression();}
		else
			RelationExpression();
		if(an[k].kind==34)
			++k;
		else
		{
			cout<<an[k].row<<"error need ')'";
			exit(0);
		}
	}
	else
	{
		if(an[k].kind==20)
		{++k;BoolExpression();}
	//	else if(an[k].kind==1||an[k].kind==2)
			RelationExpression();
	}
   	
}
void Item()											//18.<项>
{
 	Factor();
	  while(an[k].kind==16||an[k].kind==18)
	  {
		  if(an[k].kind==18)
			  temp1[cong-1].lev=temp1[cong-1].lev+1;
		 //	strcpy(temp1[cong].name,an[k].word);		
		 // temp1[cong].lev=-1;
		  ++k;  Factor();
	  }
	  //else if(an[k].kind==
}

void Expression()									
{
	Item();
}

void TermSentence()									 //13.<条件语句>
{
    
	  // ++k;
	   BoolExpression();
	   	bool_gen();
	   if(an[k].kind==9)
	   {
		   ++k;sentence();
		 //design();
		   //flag3=1;
		 //  if(an[k].kind==10)
		//	 { ++k;sentence();}
		   
	   }
	   else
	   {
		   cout<<an[k].row<<": error need 'then'";
		   exit(0);
	   }
	   if(an[k].kind==10)
	   {
		   ++k;//int t=four;//++flag;
		  sentence();//flag=1;
	   }
}
void CycleSentence()
{
	BoolExpression();
	bool_gen();
	if(an[k].kind==12)
	{++k;sentence();}

}
void EvaluateSentence()									//12.<赋值语句>
{
	if(an[k].kind==1)
		{	
			 int a=lookup();
			strcpy(temp1[cong].name,an[k].word);
			temp1[0].type=a;
				++cong;	k++;
			if(an[k].kind==29)
			{ ++k;Expression();}						//17.<表达式>
			else 
			{
				cout<<an[k].row<<"error need':='";
				exit(0);
			}
	
		}
}
void ComplexSentence()
{
	if(flag3==1||flag3==2)
		flag3=0;
		 longsentence();
		 Qtype[four].Op=11;
		strcpy(Qtype[four].Arg1.name,"0");
		strcpy(Qtype[four].Arg2.name,"0");
		if(flag3==1)
		Qtype[four].Result.num=-1;
		else
		Qtype[four].Result.num=-2;
		++four;
	 if(an[k].kind==7)
		 ++k;
	 else
	 {
		 cout<<an[k].row<<":  error need 'end'";
		 exit(0);
	 }
}

	

bool  sentence()										//10.<语句>
{
		++flag1;									
		if(flag1==2&&flag4!=1)
		design();										//回填,

	if(an[k].kind==1)
	{
	/*	++flag1;									
		if(flag1==2)
		design();*/
		for(int i=0;i<cong;i++)
		 {
			 temp1[i].kind=temp1[i].lev=temp1[i].type=0;
			 for(int j=0;j<10;j++)
			temp1[i].name[j]='0';
		 }
		 cong=0;
		EvaluateSentence();
		 progen(); 
		return true;
	}
	else if(an[k].kind==8)
	{  
		++k;flag3=1;								//用于jmp跳出循环
		TermSentence();
	//	if(flag3==0)
		//flag4=four;
		for(int a=0;a<four;a++)
			if(Qtype[a].Result.num==-1)
				Qtype[a].Result.num=four+1;
		flag3=0;//flag4=0;
		
		return true;
	}
	else if(an[k].kind==11)
	{
		flag3=2;flag4=1;
		++k; int t=four;
		CycleSentence();
		for(int a=0;a<four;a++)
		if(Qtype[a].Result.num==-2)
			Qtype[a].Result.num=t+1;
		design();
		flag3=0;flag4=0;
		return true;
	}
	else if(an[k].kind==6)
	{
		++k;
		ComplexSentence();
		return true;
	}

	else if(an[k].kind==30)
		return true;
		return false;
}
/*	else if(an[k].kind==6)
		ComplexSentence();
	else if(an[k].kind==8)*/

//}
void longsentence()							    	//9.<语句串>
{
	while(sentence())
	{
		if(an[k].kind==30)
			++k;
		else 
		{
			cout<<an[k].kind<<": error(need';'";
			exit(0);
		}
	}
}

void fparametrer()										//8.<形参>
{
	if(an[k].kind==4)
	{
		++k;
 L1:	if(an[k].kind==1)
			++k;
		if(an[k].kind==32)
			{++k; goto L1;}
		else
		{
			if(an[k].kind==35)
			{
				++k;
				if(an[k].kind==14||an[k].kind==15)
					++k;
				else
				{cout<<an[k].row<<": error Id need 'Type'";exit(0);}
			}
			else
			{cout<<an[k].row<<":error need':'"; exit(0);}
		}
	}
}


void Formalparameter()									//7.<形参部分>
{
	if(an[k].kind==33)
	{
			++k;
		L3:	fparametrer();
		if(an[k].kind==30)
			goto L3;
		else if(an[k].kind==34)
			++k;
		else
		{
			cout<<an[k].row<<": error no ')'";
			exit(0);
		}
	}

}
void ProcessExplain()								// 6.<过程声明>
{
	
	if(an[k].kind==5)
	{
		++k;
		if(an[k].kind==1)
		{
			strcpy(IdTab[id].name,an[k].word);
			IdTab[id].Kind=an[k].kind;
			IdTab[id].Type=3;
			IdTab[id].Addr=k;++k;++id;
		}
		else 
		{
			cout<<an[k].row<<"error need 'Id'";
			exit(0);
		}
		Formalparameter();
		if(an[k].kind==30)
			++k;
		else
		{
			cout<<an[k].row<<": error no ';'";
			exit(0);
		}
		Procedding();

	}
	
}

void Appearbefore()
{
	for(int i=0;i<id;i++)
		if (!(strcmp(IdTab[i].name,an[k].word)))
		{
			cout<<an[k].row<<": the variable has been defined before";
			exit(0);
		}

}

void SimpleExplain()								// 5.<简体说明>
{
	int n=0; //int r;//r=k;
Ll:	if(an[k].kind==1)
	{
		++n;
		Appearbefore();
			//r=k
		strcpy(IdTab[id].name,an[k].word);
		IdTab[id].Kind=1;
		++k;
		IdTab[id].Addr=k;
		++id;
	if(an[k].kind==32)
		{ ++k;	goto Ll;}
	if(an[k].kind==35)
		++k;
	else
		{cout<<an[k-1].row<<": error(need':')";exit(0);}
	if(an[k].kind==14||an[k].kind==15)
	{
		for(int i=0;i<n;i++)
		{
			if(an[k].kind==14)
				IdTab[id-i-1].Type=1;
			else 
				IdTab[id-i-1].Type=2;			
		} ++k;
	}
	else 
		{cout<<an[k-1].row<<"no type"; exit(0);}
	if(an[k].kind==30)
		++k;
	}
	else
		cout<<an[k-1].row<<": error(no variable)";
	if(an[k].kind==1)
		SimpleExplain();

}
	
void Explain()									//	4.<说明>
{
	if(an[k].kind==4)
	{++k;SimpleExplain();}
	else 
	ProcessExplain();
}

void  Explication()								//3.<说明串>
{
L4:   Explain();
	if(an[k].kind==30)
	{	++k;goto L4;}
	

}

void Procedding()								//2.<程序体>
{
	Explication();
	if(an[k].kind==6)
	{
		++k;
	    longsentence();						   	//9.<语句串>
		if(an[k].kind==7)
			++k;
		else 
		{
			cout<<an[k].row<<": need' end'";
			exit(0);
		}
	/*	if(an[k].kind!=31)
		{
			cout<<an[k].row<<": need '.'";
			exit(0);
		}*/

	}
	else
	{
		cout<<an[k].row<<": need 'begin'";
		exit(0);
	}
	
	//cout<<"ok";
}

void program()								    	// 1.<程序>
{
	if(an[k].kind==3)
	{
	   ++k;
	   if(an[k].kind==1)
	   {	
		   strcpy(IdTab[id].name,an[k].word);
			IdTab[id].Kind=an[k].kind;
			IdTab[id].Type=3;++k;
			IdTab[id].Addr=k;//层次
		    ++id;
		   if(an[k].kind==30)
		   {++k; Procedding();}						//	2.<程序体>
		   else
			   cout<<an[k].row<<": error(need ';')";
		   if(an[k].kind==31)
		   {++k; cout<<"oK"<<endl;}
		   else
		   {
				cout<<an[k].row<<":need '.'";
				 exit(0);
		   }
		   if(an[k].kind!=0)
		   {
			   cout<<an[k].row<<"error: 多余的字符";
			   exit(0);
		   }
		   //if(an[k]

	   }
	   else
		   cout<<an[k].row<<":error(no variable)";
	}
	else
		cout<<an[k].row<<":error(need program)";

}

void print()
{

	for(int i=0;i<four;i++)
	{  
	//	if(Qtype[i].Result.num==i+2&&Qtype[i].Op==11)
		//	++i;
		cout<<i+1<<": ";
		switch(Qtype[i].Op)
		{

		case 1:
		case 2:
		case 3:
		case 5:
			cout<<"(:=I,";break;
		case 6:
			cout<<"(:=R,";break;
		case 7:
		case 11:
			cout<<"(j, ";break;
		case 12:
			cout<<"(j<,";break;
		case 13:
			cout<<"(j<=,";break;
		case 14:
			cout<<"(j>," ;break;
		case 19:
			cout<<"(+I,";break;
		case 20:
			cout<<"(+R,";break;
		case 21:
			cout<<"(*I,";break;
		case 22:
			cout<<"(*R,";break;
		}

		cout<<Qtype[i].Arg1.name;
		if(Qtype[i].Arg1.num>0)
			cout<<Qtype[i].Arg1.num;
		cout<<","<<Qtype[i].Arg2.name;
		if(Qtype[i].Arg2.num>0)
			cout<<Qtype[i].Arg2.num;
		cout<<","<<Qtype[i].Result.name<<Qtype[i].Result.num<<")"<<endl;
	}
}
void main()
{
	Word();
	program();
	print();
}

⌨️ 快捷键说明

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