www.pudn.com > MEIPING.rar > MEIPING.C


  /*cadmidcd.w */ 
  # include  
  # include  
  # define M 93 
  typedef struct 
    { char name[10]; 
      char type[10]; 
    } symboltype; 
    typedef struct 
     { char name[10]; 
       char kind[4]; 
       int val; 
       int adr; 
       int size; 
     }elemtype; 
     typedef struct 
      { char oper[10]; 
	char arg1[10]; 
	char arg2[10]; 
	char result[10]; 
      }midcodetype; 
     main() 
      { char ch,type[10],outs[40],variable[10][10]; 
	char oper[10][4],format[10][10],op[10],pr[10]; 
	char wd[10],assi[10],mklen[4],comoper[10]; 
	FILE *fptr, *fptw, *fptm; 
	int i,j,k,t,len,lenmc,lenmk,post,postif,postwh,postfor; 
	int value,ifin,elsein,falsep,postnextst,post2,nextst; 
	int whilein,whileentry,whilecom,forin,forentry; 
	symboltype symbol[4]; 
	elemtype table; 
	midcodetype midcode,foradd; 
	clrscr(); 
	if ((fptr=fopen("c:\\zyctc\\zyctable.c","rb"))==NULL) 
	  { printf("Can't opened file zyctable.c ! "); 
	    getchar(); 
	    exit(1); 
	  } 
	if ((fptw=fopen("c:\\zyctc\\zycmidcode.c","wb+"))==NULL) 
	  { printf("Can't opened file zycmidcode.c ! "); 
	    getchar(); 
	    exit(2); 
	  } 
	if ((fptm=fopen("c:\\zyctc\\zycmarks.c","wb+"))==NULL) 
	  { printf("Can't opened file cadmarks.c ! "); 
	    getchar(); 
	    exit(3); 
	  } 
	ifin=0; 
	forin=0; 
	elsein=0; 
	whilein=0; 
	whilecom=0; 
	len=sizeof(elemtype); 
	lenmc=sizeof(midcodetype); 
	fread(&table,len,1,fptr); 
	while (! feof(fptr)) 
	 { strcpy(wd,table.name); 
	   if (strcmp(wd,"#")==0) 
	     { fread(&table,len,1,fptr); 
	       strcpy(wd,table.name); 
	       if (strcmp(wd,"include")==0) 
		{ strcpy(op,wd); 
		   fread(&table,len,1,fptr); 
		   strcpy(wd,table.name); 
		   if (strcmp(wd,"<")==0 || strcmp(wd,"\"")==0) 
		     { fread(&table,len,1,fptr); 
			strcpy(wd,table.name); 
			if (strcmp(wd,"stdio")==0 || 
			    strcmp(wd,"string")==0) 
			  {  strcpy(pr,wd); 
			     fread(&table,len,1,fptr); 
			     strcpy(wd,table.name); 
			     if (strcmp(wd,".")==0) 
			       { fread(&table,len,1,fptr); 
				 strcpy(wd,table.name); 
				 if (strcmp(wd,"h")==0) 
				 { fread(&table,len,1,fptr); 
				   strcpy(wd,table.name); 
				   if (strcmp(wd,">")!=0 && 
				   strcmp(wd,"\"")!=0) 
				    { strcpy(wd,table.name); 
				      printf("Missing '>' or ' ' ! \n"); 
				    } 
				   else 
				   printf("Missing 'h' ! \n"); 
				 } 
			     else 
			     printf("Missing '.' ! \n"); 
			 } 
		      else 
		      printf("Missing 'stdio' or 'string' ! \n"); 
		     } 
		 else 
		 printf("Missing '<' or ' '! \n"); 
		} 
	     else 
	     printf("Missing 'include' ! \n"); 
	     strcpy(midcode.oper,op); 
	     strcpy(midcode.arg1,"_"); 
	     strcpy(midcode.arg2,"_"); 
	     strcpy(midcode.result,pr); 
	     fwrite(&midcode,lenmc,1,fptw); 
	   } 
	 if(strcmp(wd,"main")==0) 
	   { fread(&table,len,1,fptr); 
	     strcpy(wd,table.name); 
	     if (strcmp(wd,"(")!=0) 
	      { printf("Missing '(' ! \n"); 
		getchar(); 
		continue; 
	      } 
	     strcpy(op,"Call"); 
	     fread(&table,len,1,fptr); 
	     strcpy(wd,table.name); 
	     if (strcmp(wd,")")!=0) 
	     { printf("Missing ')' ! \n"); 
	       getchar(); 
	       continue; 
	     } 
	     fread(&table,len,1,fptr); 
	     strcpy(wd,table.name); 
	     if (strcmp(wd,"{")!=0) 
	     { printf("Missing'{' ! \n"); 
		getchar(); 
		continue; 
	     } 
	     strcpy(pr,"main"); 
	     strcpy(midcode.oper,op); 
	     strcpy(midcode.arg1,"_"); 
	     strcpy(midcode.arg2,"_"); 
	     strcpy(midcode.result,pr); 
	     fwrite(&midcode,lenmc,1,fptw); 
	     fread(&table,len,1,fptr); 
	     strcpy(wd,table.name); 
	   } 
	  if (strcmp(wd,"float")==0 || strcmp(wd,"int")==0 || 
	      strcmp(wd,"char")==0) 
	   { strcpy(type,wd); 
	     i=0; 
	     fread(&table,len,1,fptr); 
	     strcpy(wd,table.name); 
	     if (strcmp(table.kind,"Var")!=0) 
	      { printf("\n Missing Name of varable ! \n"); 
		continue; 
	      } 
	     while (strcmp(table.kind,"Var")==0) 
	      { strcpy(symbol[i].name,wd); 
		strcpy(symbol[i].type,type); 
		i++; 
		fread(&table,len,1,fptr); 
		strcpy(wd,table.name); 
		if (strcmp(wd,";")==0) 
		  break; 
		if (strcmp(wd,",")==0) 
		  { fread(&table,len,1,fptr); 
		    strcpy(wd,table.name); 
		  } 
		else 
		  { printf("\n Missing ',' ! \n"); 
		    continue; 
		  } 
		} 
	      if (strcmp(wd,";")!=0) 
	      printf("\n Missing ';' ! \n"); 
	    } 
	  if (strcmp(wd,"clrscr")==0) 
	   { fread(&table,len,1,fptr); 
	     if (strcmp(table.name,"(")!=0) 
	      { printf("\n Missing '('\n"); 
		continue; 
	      } 
	     fread(&table,len,1,fptr); 
	     if (strcmp(table.name,")")!=0) 
	      { 
		printf("\n Missing ')' ! \n"); 
		continue; 
	      } 
	     strcpy(midcode.oper,"Call"); 
	     strcpy(midcode.arg1,"_"); 
	     strcpy(midcode.arg2,"_"); 
	     strcpy(midcode.result,"clrscr"); 
	     fwrite(&midcode,lenmc,1,fptw); 
	     fread(&table,len,1,fptr); 
	     strcpy(wd,table.name); 
	     if (strcmp(wd,";")!=0) 
	      { printf("\n Missing ';' ! \n"); 
		continue; 
	      } 
	    } 
	   if (strcmp(wd,"printf")==0) 
	    { 
	      i=0; 
	      strcpy(outs," "); 
	      fread(&table,len,1,fptr); 
	      strcpy(wd,table.name); 
	      if (strcmp(table.name,"(")!=0) 
	       { printf("\n Missing '(' ! \n"); 
		 getchar(); 
		 continue; 
	       } 
	      fread(&table,len,1,fptr); 
	      strcpy(wd,table.name); 
	      if (strcmp(table.name,"\"")!=0) 
	       { printf("\n Missing '\"' ! \n"); 
		 getchar(); 
		 continue; 
	       } 
	     fread(&table,len,1,fptr); 
	     strcpy(wd,table.name); 
	     t=0; 
	     while (strcmp(wd,"\"")!=0) 
	      { if (strcmp(wd,"%")!=0 && strcmp(wd,"\\")!=0) 
		{ strcat(outs, " "); 
		  strcat(outs,wd); 
		} 
		if (strcmp(wd,"%")==0) 
		{ fread(&table,len,1,fptr); 
		  strcpy(wd,table.name); 
		  if (strcmp(table.kind,"Con")==0) 
		    { strcpy(op,table.name); 
		      fread(&table,len,1,fptr); 
		      strcpy(wd,table.name); 
		    } 
		  if (strcmp (wd,"c")==0) 
		  strcpy(format[i],"char"); 
		  else 
		  if (strcmp(wd,"d")==0) 
		  strcpy(format[i],"decimal"); 
		  else 
		  if (strcmp(wd,"f")==0) 
		  strcpy(format[i],"float"); 
		  else 
		  if (strcmp(wd,"s")==0) 
		  strcpy(format[i],"string"); 
		  else 
		  { printf("\n Missing c or d or for s ! \n"); 
		    getchar(); 
		    continue; 
		  } 
		 i++; 
		 fread(&table,len,1,fptr); 
		 strcpy(wd,table.name); 
	       } 
	     if (strcmp(wd,"\\")==0) 
	      { fread(&table,len,1,fptr); 
		strcpy(wd,table.name); 
		if (strcmp(wd,"n")==0) 
		  { t++; 
		    if (t==1) 
		    { strcpy(midcode.oper,"printf"); 
		      strcpy(midcode.arg1,"nextline"); 
		      strcpy(midcode.arg2,"_"); 
		      strcpy(midcode.result,"output"); 
		      fwrite(&midcode,lenmc,1,fptw); 
		    } 
		  } 
		} 
	       fread(&table,len,1,fptr); 
	       strcpy(wd,table.name); 
	     } 
	    lenmk=strlen(outs)+1; 
	    if (lenmk>=1) 
	     { lenmk=lenmk % 80; 
	       mklen[0]=lenmk/10+'0'; 
	       mklen[1]=lenmk%10+'0'; 
	       mklen[2]='\0'; 
	       post2=ftell(fptm); 
	       wd[0]=post2/10+'0'; 
	       wd[1]=post2%10+'0'; 
	       wd[2]='\0'; 
	       fwrite(&outs,lenmk,1,fptm); 
	       strcpy(midcode.oper,"printf"); 
	       strcpy(midcode.arg1,wd); 
	       strcpy(midcode.arg2,mklen); 
	       strcpy(midcode.result,"cadmark"); 
	       fwrite(&midcode,lenmc,1,fptw); 
	     } 
	   for (j=1;j=0) 
				       { strcpy(midcode.oper,oper[k]); 
					 strcpy(midcode.arg1,variable[i-1]); 
					 strcpy(midcode.arg2,variable[i]); 
					 strcpy(midcode.result,"result"); 
					 fwrite(&midcode,lenmc,1,fptw); 
					 k--; 
					 i--; 
					 strcpy(variable[i],"result"); 
				       } 
				     postif=ftell(fptw); 
				     strcpy(midcode.oper,"goto"); 
				     strcpy(midcode.arg1,"E_false"); 
				     strcpy(midcode.arg2,"_"); 
				     strcpy(midcode.result,"_"); 
				     fwrite(&midcode,lenmc,1,fptw); 
				     fread(&table,len,1,fptr); 
				     continue; 
				   } 
				 if (strcmp(wd,"else")==0) 
				  { t=0; 
				    elsein++; 
				    strcpy(midcode.oper,"goto"); 
				    strcpy(midcode.arg1,"nextst"); 
				    strcpy(midcode.arg2,"_"); 
				    strcpy(midcode.result,"_"); 
				    postnextst=ftell(fptw); 
				    fwrite(&midcode,lenmc,1,fptw); 
				    fread(&table,len,1,fptr); 
				    continue; 
				  } 
				if (strcmp(wd,"while")==0) 
				 { whilein++; 
				   fread(&table,len,1,fptr); 
				   strcpy(wd,table.name); 
				   if (strcmp(wd,"(")!=0) 
				    { printf("\n Missing '(' ! \n"); 
				      getchar(); 
				    } 
				  fread(&table,len,1,fptr); 
				  strcpy(wd,table.name); 
				  i=0; 
				  j=0; 
				  while (strcmp(wd,")")!=0) 
				    { if (strcmp(table.kind,"Var")==0 || 
					  strcmp(table.kind,"Con")==0) 
					{ strcpy(variable[i],wd); 
					  i++; 
					} 
				     else 
				       { strcpy(comoper,wd); 
					 fread(&table,len,1,fptr); 
					 strcpy(wd,table.name); 
					 if (strcmp(table.kind,"Ope")==0 || 
					     strcmp(table.kind,"Bon")==0) 
					   { strcat(comoper,wd); 
					     strcpy(wd,comoper); 
					     strcpy(oper[j],wd); 
					     j++; 
					  } 
					else 
					 { strcpy(oper[j],comoper); 
					   j++; 
					   continue; 
					 } 
				      } 
				    fread(&table,len,1,fptr); 
				    strcpy(wd,table.name); 
				  } 
				k=j-1; 
				i=i-1; 
				whileentry=ftell(fptw); 
				while (k>=0) 
				 { strcpy(midcode.oper,oper[k]); 
				   strcpy(midcode.arg1,variable[i-1]); 
				   strcpy(midcode.arg2,variable[i]); 
				   strcpy(midcode.result,"result"); 
				   fwrite(&midcode,lenmc,1,fptw); 
				   k--; 
				   i--; 
				   strcpy(variable[i],"result"); 
				 } 
			       postwh=ftell(fptw); 
			       strcpy(midcode.oper,"goto"); 
			       strcpy(midcode.arg1,"E_false"); 
			       strcpy(midcode.arg2,"_"); 
			       strcpy(midcode.result,"_"); 
			       fwrite(&midcode,lenmc,1,fptw); 
			       fread(&table,len,1,fptr); 
			       strcpy(wd,table.name); 
			       if (strcmp(wd,"{")==0) 
				 { whilecom++; 
				   fread(&table,len,1,fptr); 
				   continue; 
				 } 
			      } 
			    if (strcmp(wd,"for")==0) 
			      { forin++; 
				fread(&table,len,1,fptr); 
				strcpy(wd,table.name); 
				if (strcmp(wd,"(")!=0) 
				  { printf("\n Missing '(' ! \n"); 
				   getchar(); 
				 } 
			       fread(&table,len,1,fptr); 
			       strcpy(wd,table.name); 
			       i=0; 
			       j=0; 
			       while (strcmp(wd,")")!=0) 
				 { while (strcmp(wd,";")!=0) 
				   {if (strcmp(table.kind,"Var")==0 || 
					strcmp(table.kind,"Con")==0) 
				      { strcpy(variable[i],wd); 
					i++; 
				      } 
				    else 
				     { strcpy(comoper,wd); 
				       fread(&table,len,1,fptr); 
				       strcpy(wd,table.name); 
				     if (strcmp(table.kind,"Ope")==0 || 
					 strcmp(table.kind,"Bon")==0) 
				       { strcat(comoper,wd); 
					 strcpy(wd,comoper); 
					 strcpy(oper[j],wd); 
					 j++; 
				       } 
				     else 
				       { strcpy(oper[j],comoper); 
					j++; 
					continue; 
				      } 
				   } 
				   fread(&table,len,1,fptr); 
				   strcpy(wd,table.name); 
				   if (strcmp(wd,")")==0) 
				   break; 
				 } 
			       k=j-1; 
			       i=i-1; 
			       while (k>=0) 
				{ if (strcmp(oper[k],"=")==0) 
				   { strcpy(oper[k],":="); 
				     forentry=ftell(fptw)+40; 
				   } 
				 strcpy(midcode.oper,oper[k]); 
				 if (strcmp(oper[k],"++")==0) 
				  { strcpy(foradd.oper,oper[k]); 
				    strcpy(foradd.arg1,variable[i]); 
				    strcpy(foradd.arg2,"1"); 
				    strcpy(foradd.result,variable[i]); 
				  } 
				else 
				 if (strcmp(oper[k],":=")!=0) 
				   { strcpy(midcode.arg1,variable[i-1]); 
				     strcpy(midcode.arg2,variable[i]); 
				     strcpy(midcode.result,"result"); 
				     fwrite(&midcode,lenmc,1,fptw); 
				   } 
				 else 
				   { strcpy(midcode.arg1,variable[i]); 
				     strcpy(midcode.arg2,"_"); 
				     strcpy(midcode.result,variable[i-1]); 
				     fwrite(&midcode,lenmc,1,fptw); 
				   } 
				 k--; 
				 i--; 
				 strcpy(variable[i],"result"); 
			       } 
			     if (strcmp(wd,")")==0) 
			       { postfor=ftell(fptw); 
				 strcpy(midcode.oper,"goto"); 
				 strcpy(midcode.arg1,"E_flase"); 
				 strcpy(midcode.arg2,"_"); 
				 strcpy(midcode.result,"_"); 
				 fwrite(&midcode,lenmc,1,fptw); 
				 break; 
			       } 
			     i=0; 
			     j=0; 
			     fread(&table,len,1,fptr); 
			     strcpy(wd,table.name); 
			   } 
			 fread(&table,len,1,fptr); 
			 continue; 
		       } 
		     if (strcmp(wd,")")==0) 
			{ if (whilecom!=1) 
			  break; 
			if (whilein==1) 
			  { post=postwh; 
			    nextst=ftell(fptw); 
			    value=nextst+40; 
			    post=nextst-post; 
			    fseek(fptw,-post,SEEK_CUR); 
			    fread(&midcode,lenmc,1,fptw); 
			    midcode.result[0]=value/100+'0'; 
			    value=value%100; 
			    midcode.result[1]=value/10+'0'; 
			    value=value%10; 
			    midcode.result[2]=value/10+'0'; 
			    midcode.result[3]='\0'; 
			    fseek(fptw,-lenmc,SEEK_CUR); 
			    fwrite(&midcode,lenmc,1,fptw); 
			    fseek(fptw,post-40,SEEK_CUR); 
			    whilein--; 
			    strcpy(midcode.oper,"goto"); 
			    strcpy(midcode.arg1,"whileentry"); 
			    strcpy(midcode.arg2,"_"); 
			    wd[0]=whileentry/100+'0'; 
			    whileentry=whileentry%100; 
			    wd[1]=whileentry/10+'0'; 
			    whileentry=whileentry%10; 
			    wd[2]=whileentry+'0'; 
			    wd[3]='\0'; 
			    strcpy(midcode.result,wd); 
			    fwrite(&midcode,lenmc,1,fptw); 
			  } 
			fread(&table,len,1,fptr); 
			continue; 
		      } 
		    fread(&table,len,1,fptr); 
		  } 
		fclose(fptr); 
		printf("\n midcode Oper  Arg1  Arg2  Result\n"); 
		rewind(fptw); 
		while (! feof(fptw)) 
		  { printf("\n%13s%10s",midcode.oper,midcode.arg1); 
		    printf("%10s%10s\n",midcode.arg2,midcode.result); 
		    fread(&midcode,lenmc,1,fptw); 
		  } 
		fclose(fptw); 
		printf("\n\n press any key to continue !\n"); 
		getchar(); 
	      } 
	    }