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

📄 compile.cpp

📁 自己写的一个编译程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//自己写的一个编译程序,可以从二进制文件中读取一段c程序,进行表达式的识别和一些简单的c语句,最后生成三元式输出 
#include"stdio.h"
 #include"conio.h"
 #include"string.h"
 #include"math.h"
 # define max 300
 #define big 50
typedef struct
	{int row[big][3];
	 int length;
	}stack;

typedef struct
	{
	 int no;
         char value[8];
	}twocell;       //store twocell//
typedef struct
	{ twocell row[max];
          int length;
	}two;         //list//

typedef  struct
	{ char name[8];
	  int value;
	  int type;
	  
	}varcell;    //yufa
typedef struct
	{varcell row[2*big];
         int length;
	}var;
typedef struct
	{int op;
	 int opn1;
	 int opn2;
         int addr;
	}fourcell;
typedef struct
	{fourcell row[2*big];
         int length;
	}four;                 //list
int count,len,wrong;char mid[max],word[8],b[8],c[2*max];two mem;FILE *fp; var variable;int expact[11][8],expgo[11][8];
int formula[7];stack l;four fourlist;

void error(int wr)
	{
	 switch(wr)
		{case 1:printf("(1)number define wrongly.\n");break;
		 case 2:printf("(2)mark define wrongly.\n");break;
		 case 3:printf("(3)extra sign.\n");break;
		 case 4:printf("(4)complex define wrongly.\n");break;
		 case 5:printf("(5)codition define wrongly.\n");break;
		 case 6:printf("(6)cycle define wrongly.\n");break;
		 case 7:printf("(7)define define wrongly.\n");break;
		 case 8:printf("(8)first word  error.\n");break;
		 case 9:printf("(9)relationexp error.\n");break;
		 case 10:printf("(10)';'expected.\n");break;
		 case 11:printf("(11)expaction error.\n");break;
		 case 12:printf("(12)opn2 should be '0' in define.\n");break;
		 case 13:printf("(13)pop error.\n");break;
		 case 14:printf("(14)action error.\n");break;
		 case 15:printf("(15)backpath error.\n");break;
		 case 16:printf("(16)printf fourlist error.\n");break;
		 case 17:printf("(17)'.'expected.\n");break;
		 case 18:printf("(18)'do' expected\n");break;
		 case 19:printf("(19)'if' can't behind 'then'\n");break;
		 case 20:printf("(20)'then' expect.\n");break;
		 case 21:printf("(21)':=' expected\n");break;
		// case :printf("(2)\n");break;
		// case :printf("(2)\n");break;
		// case :printf("(2)\n");break;
		// case :printf("(2)\n");break;
		}
	  wrong=1;
	}
int newtemp()
	{char c[8];int i,j,k;j=0;
	strcpy(c,"A");k=0;
	if(variable.length>=0)
		{do{    i=0;k=0;
			for( i;i<=variable.length;i++)
			if(!strcmp(c,variable.row[i].name))k=1;

			if(!k)

		       { ++variable.length;
		       strcpy(variable.row[variable.length].name,c);
			       //	printf("varc=%s,k=%d ",variable.row[variable.length].name,k);
			}
			else {
			     c[j]=c[j]+1;
			     c[j+1]='\0';//printf("c=%s",c);
			     if(c[j]>'Y')j++;

			     }

		   }while(k);

		return variable.length;
		}
	  else {++variable.length;strcpy(variable.row[variable.length].name,c);return variable.length; }
	}
int gen(int op,int opn1,int opn2,int addr)
	{switch(op)
		{case 0:variable.row[addr].value=variable.row[opn1].value+variable.row[opn2].value;break;
		 case 1:variable.row[addr].value=variable.row[opn1].value*variable.row[opn2].value;break;
		 case 2:if(opn2!=0){error(12);//printf("opn2 should be 0 in :=.");wrong=1;
		                   }
			  variable.row[addr].value=variable.row[opn1].value;break;// :=
		
		 default :
break;
		}
	//printf("(%d,%d,%d,%d)\n",op,opn1,opn2,addr);
	fourlist.row[fourlist.length].op=op;
	fourlist.row[fourlist.length].opn1=opn1;
	fourlist.row[fourlist.length].opn2=opn2;
	fourlist.row[fourlist.length].addr=addr;
	fourlist.length++;
	return fourlist.length;
	}
void entry(int j)
       {int p=0;
	if(mem.row[count-1].no==1)
		{for(int i=0;i<strlen(mem.row[count-1].value);i++)
		p=p*10+(mem.row[count-1].value[i]-'0');
		variable.row[j].value=p;
                
		//printf("s=%s;variable[%d]=%d\n",mem.row[count-1].value,j,p);
		}
	//else printf("the no!=1;no=(%d,%s)\n",mem.row[count-1].no,mem.row[count-1].value);
	}
void pushc(stack &l,int k,int j)
	{// l.length++;
	l.row[l.length][0]=k;   //name
	l.row[l.length][2]=j;     //position
	  
	}
void pushs(stack &l,int k)
	{ l.length++;
	l.row[l.length][1]=k;
	  
	}                 //state
void pop(stack &l,int k)
	{if(l.length-k>=0)
	 l.length=l.length-k;
	 else error(13);// printf("pop error");
	// printf("l.length=%d\n",l.length);
	}
int lookup(char *c)
	{for(int i=0;i<=variable.length;i++)
	 if(!strcmp(c,variable.row[i].name)){ return i;}
         if (i>variable.length)return newtemp();
	}
int getstate()
	{int i=0;
	 if(l.length>=0)
          i=l.row[l.length][1];
	 return i;
	}
void expactioninit()
	{int i,j;
	for( i=0;i<=10;i++)
	 for( j=0;j<=7;j++)
	 { expact[i][j]=-1;}
	 expact[0][2]=0;expact[0][4]=0;expact[0][7]=0;
	 expact[1][0]=0;expact[1][1]=0;expact[1][6]='a';expact[1][7]='a';
	 expact[2][2]=0;expact[2][4]=0;expact[2][7]=0;
	 expact[3][0]=0;expact[3][1]=0;expact[3][3]=0;
	 expact[4][0]=4;expact[4][1]=4;expact[4][6]=4;
	 expact[5][0]=5;expact[5][1]=5;expact[5][3]=5;expact[5][6]=5;expact[5][7]=5;
	 expact[6][2]=0;expact[6][4]=0;expact[6][7]=0;
	 expact[7][0]=2;expact[7][1]=2;expact[7][3]=2;expact[7][6]=2;
	 expact[8][4]=0;expact[8][7]=0;
	 expact[9][0]=3;expact[9][1]=3;expact[9][3]=3;expact[9][6]=3;
	 expact[10][0]=6;expact[10][1]=6;expact[10][3]=6;expact[10][6]=6;
	 
	 formula[0]=0;formula[1]=1;formula[2]=3;formula[3]=3;formula[4]=3;formula[5]=1;formula[6]=1;

	}
void expgotoinit()
	{int i,j;
	 for( i=0;i<=10;i++)
	 for( j=0;j<=7;j++)
	 expgo[i][j]=-1;
	 expgo[0][2]=2;expgo[0][4]=5;expgo[0][5]=1;expgo[0][7]=10;
	 expgo[1][0]=6;expgo[1][1]=8;
	 expgo[2][2]=2;expgo[2][4]=5;expgo[2][5]=3; expgo[2][7]=10;
	 expgo[3][0]=6;expgo[3][1]=8;expgo[3][3]=4;
	 expgo[6][2]=2;expgo[6][4]=5;expgo[6][5]=7;expgo[6][7]=10;
	 
	 expgo[8][4]=5;expgo[8][5]=9;expgo[8][7]=10;


	 

	}
int strtono()
	{int i=-1;
	switch(mem.row[count].no)
		{case 1:i=4; break;
		 case 2:i=7;break;
		 case 21:i=2;break;
		 case 22:i=3;break;
		 case 17:i=0;break;
		 case 19:i=1;break;
		 case 9:
		 case 12:
		 case 14:
		 case 15:
		 case 16:
		 case 23:
		 case 27:
		 case 28:
		 case 29:
                 case 30:
		 case 31:i=6;break;
		 defaul :printf("expresion extra sign\n");wrong=1;break;
		}
         return i;
	}
void expgoto(stack &l,int i,int j)
	{//printf("goto[%d][%d]=%d\n",i,j,expgo[i][j]);
         pushs(l,expgo[i][j]);
	 }
int expaction(stack &l,int i,int j)
	{//printf("expection:=%d,i=%d,j=%d\n",expact[i][j],i,j);
	 switch(expact[i][j])
		{case -1:error(14);printf("action errori=%d,j=%d\n",i,j);wrong=1;
			return 0;
		 case 0:expgoto(l,i,j);pushc(l,strtono(),0);count++;break;
		 case 1:pop(l,formula[1]);expgoto(l,getstate(),5);pushc(l,5,0);break;
		 case 2:gen(0,l.row[l.length][2],l.row[l.length-2][2],newtemp());pop(l,formula[2]);expgoto(l,getstate(),5);pushc(l,5,variable.length);break;
		 case 3:gen(1,l.row[l.length][2],l.row[l.length-2][2],newtemp());pop(l,formula[3]);expgoto(l,getstate(),5);pushc(l,5,variable.length);break;
		 case 4:pop(l,formula[4]);expgoto(l,getstate(),5);pushc(l,5,l.row[l.length+1][2]);break;
		 case 5:pop(l,formula[5]);expgoto(l,getstate(),5);pushc(l,5,newtemp());variable.row[variable.length].type=1;entry(variable.length);break;
		 case 6:char c[big];strcpy(c,mem.row[count-1].value);
		        int x;x=lookup(c);//printf("mark=%s\n",c);
			pop(l,formula[6]);expgoto(l,getstate(),5);
			pushc(l,5,x);strcpy(variable.row[x].name,c);entry(x);break;
		 case 'a':pop(l,1); return 1;
		 default:error(11);//printf("error:expact[%d][%d]=%d\n",i,j,expact[i][j]);
		         break;
		}
	return 0;
	}

void advance(){count++;}
int match(int i)
	{//if(count<mem.length)
		if(mem.row[count].no==i)return 1;
		 else return 0;
	// else return 1;
	}
void rem(two &mem,int i)
	{
	strcpy(mem.row[mem.length].value,word);mem.row[mem.length].no=i;mem.length++;
	}
int key()
	{int i=0;
	 if(!strcmp(word,"program"))   {i=1;rem(mem,3);}
	 if(!strcmp(word,"var"))       {i=1;rem(mem,4);}
	 if(!strcmp(word,"procedure")) {i=1;rem(mem,5);}
	 if(!strcmp(word,"begin")) {i=1;rem(mem,6);}
	 if(!strcmp(word,"end"))   {i=1;rem(mem,7);}
	 if(!strcmp(word,"if"))    {i=1;rem(mem,8);}
	 if(!strcmp(word,"then"))  {i=1;rem(mem,9);}
	 if(!strcmp(word,"else"))  {i=1;rem(mem,10);}
	 if(!strcmp(word,"while")) {i=1;rem(mem,11);}
	 if(!strcmp(word,"do"))    {i=1;rem(mem,12);}
	 if(!strcmp(word,"call"))  {i=1;rem(mem,13);}
	 return i;
	}
void clear()
	{ for(int i=0;i<8;i++)word[i]='\0';
	}
void num(char &m)
{// printf("dd%c",m);
if('0'<=mid[count]&&mid[count]<='9')
	{word[len]=mid[count];
	len++;//printf("%d,%d,%s",len,count,mid[count]);
	advance();

	m=mid[count]; num(m);
	 }

else {
	switch(mid[count])
	  {case '\0':
	   case ' ':
	   case '>':
	   case '<':
	   case '=':
	   case '+':
	   case '-':

⌨️ 快捷键说明

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