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

📄 do-while循环语句的翻译程序设计(lr方法、输出三地址表示).cpp

📁 通过设计、编制、调试一个DO-WHILE循环语句的语法及语义分析程序
💻 CPP
字号:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include <iomanip.h>
#include "iostream.h"
#include <fstream.h>
#define N 50

char* WEN[]={"6","Zd{E}w(T)","EAE","EA","AiPA","Ai;","TiTi"};
char shuru[100];//char shuru[]={"d{s=s+i;m=m-b*i%c;}w(i<9)#"};

struct FENXI{
    char act_0[11];
    int act_1[11];
    int go[4];
}action[19];
//struct chars{
//	char a[10];
//};
//chars *shuru[]={"do","{","i","=","i","+","i",";","}","while","(","i","<","i",")"}

void Init(){
    /*---------------       ACTION      -----------------------*/
    action[0].act_0[0]='s';       action[0].act_1[0]=2;
    action[1].act_0[10]='A';      
    action[2].act_0[1]='s';       action[2].act_1[1]=3;
    action[3].act_0[6]='s';       action[3].act_1[6]=6;
    action[4].act_0[2]='s';       action[4].act_1[2]=7;
    action[5].act_0[2]='r';       action[5].act_1[2]=3;
    action[5].act_0[6]='s';       action[5].act_1[6]=6;
    action[5].act_0[9]='r';       action[5].act_1[9]=3;
    action[6].act_0[7]='s';       action[6].act_1[7]=9;
    action[6].act_0[9]='s';       action[6].act_1[9]=10;
    action[7].act_0[3]='s';       action[7].act_1[3]=11;
    action[8].act_0[2]='r';       action[8].act_1[2]=2;
    action[8].act_0[10]='s';      action[8].act_1[10]=2;
    action[9].act_0[6]='s';       action[9].act_1[6]=6;
	action[10].act_0[2]='r';      action[10].act_1[2]=5;
    action[10].act_0[6]='r';      action[10].act_1[6]=5;
    action[10].act_0[10]='r';     action[10].act_1[10]=5;
    action[11].act_0[4]='s';      action[11].act_1[4]=13;
	action[12].act_0[2]='r';      action[12].act_1[2]=4;
    action[12].act_0[6]='r';      action[12].act_1[6]=4;
	action[12].act_0[10]='r';     action[12].act_1[10]=4;
    action[13].act_0[6]='s';      action[13].act_1[6]=15;
    action[14].act_0[5]='s';      action[14].act_1[5]=16;
    action[15].act_0[8]='s';      action[15].act_1[8]=17;
	action[16].act_0[10]='r';     action[16].act_1[10]=1;
    action[17].act_0[6]='s';      action[17].act_1[6]=18;
    action[18].act_0[5]='r';      action[18].act_1[5]=6;
    
    /*--------------------     GOTO      -------------------------*/
    action[0].go[0]=1;
    action[3].go[1]=4;
    action[3].go[3]=5;
    action[5].go[1]=8;
    action[5].go[3]=5;
    action[9].go[3]=12;
    action[12].go[2]=14;
    action[13].go[2]=14;
}

void Print_LR(){//cout<<"hello"<<endl;
    int i,j;
	cout<<"                         ACTION                                 GOTO"<<endl;
	cout<<"-----------------------------------------------------------------------------"<<endl;
    cout<<"状态   do   {    }  while  (    )    i   op   op1   ;    #   Z   E   T   A"<<endl; 
	cout<<"------------------------------------------------------------------------------"<<endl;
    for(i=0;i<19;i++){
	cout<<setw(3)<<i;
	for(j=0;j<11;j++)
	    cout<<setw(3)<<action[i].act_0[j]<<setw(2)<<action[i].act_1[j];
	for(j=0;j<4;j++)
	    cout<<setw(4)<<action[i].go[j];
	cout<<endl;
    }
    cout<<"-------------------------------------------------------------------------------"<<endl;
}

void LR(){
    int Tai[50],len=1;
    int i,j,k,l,m,tmp_n=0,num=0;
    int X;
    char temp;
	//char tmp[5];
	//for(int n=0;n<5;n++)
	//	tmp[n]='\0';
    char fuhao[N]={"#"};
    num=strlen(shuru);
    Tai[0]=0;
	cout<<"状态                 符号                    输入串    "<<endl;
    for(i=0;i<num;){
	j=Tai[len-1];
	temp=shuru[i];
	switch(temp){
		case '+':
		case '-':
		case '*':
		case '/':
		case '=':
		case '%':   temp='P';  break;
		case '<':
		case '>':   temp='T';  break;
	}
	if(temp!='P'&&temp!='T'&&temp!='d'&&temp!='w')
		if(temp>='0'&&temp<='9'||temp>='a'&&temp<='z'||temp>='A'&&temp<='Z')
			temp='i';

	switch(temp){
	    case 'd':    k=0;	 break;
	    case '{':    k=1;    break;
	    case '}':    k=2; 	 break;
        case 'w':    k=3; 	 break;
	    case '(':    k=4;    break;
	    case ')':    k=5;    break;
		case 'i':    k=6; 	 break;
		case 'P':    k=7; 	 break;
		case 'T':    k=8; 	 break;
		case ';':    k=9; 	 break;
	    case '#':    k=10;   break;
	}

        if(action[j].act_0[k]=='s'){
	    Tai[len++]=action[j].act_1[k];
	    tmp_n=strlen(fuhao)-1;
	    fuhao[++tmp_n]=shuru[i++];
	    for(l=0;l<len;l++)
		cout<<Tai[l];
	    cout<<setw(20)<<fuhao;
	    cout<<setw(3)<<" ";
		
	    for(l=i;l<num-1;l++)
		cout<<shuru[l];
	    cout<<endl;
        
	}
	else if(action[j].act_0[k]=='r'){
	     X=strlen(WEN[action[j].act_1[k]])-1;
	     len-=X;
	     for(m=len;m<len+X;m++)
		  fuhao[m]='\0';
	     fuhao[len]=WEN[action[j].act_1[k]][0];

	     switch(WEN[action[j].act_1[k]][0]){
		 case 'Z':
		      l=0;
		      break;
		 case 'E':
		      l=1;
		      break;
		 case 'T':
		      l=2;
		      break;
         case 'A':
		      l=3;
		      break;
	     }
	     len+=1;

	     Tai[len-1]=action[Tai[len-2]].go[l];
	     for(l=0;l<len;l++){
		 printf("%d",Tai[l]);//cout<<Tai[l];
	     }
	     cout<<setw(20)<<fuhao;
	     cout<<"    ";
		 
	     for(l=i;l<num-1;l++)
		 cout<<shuru[l];
	     cout<<endl;
         
	}
	else if(action[j].act_0[k]=='A'){
	    cout<<"                  正确接受!!\n";
	    cout<<"-------------------------------------------------------------------\n";
	    cout<<"           输出三地址代码序列:!!!\n";
		cout<<"-------------------------------------------------------------------\n";
	    return;//exit(0);
	}
	else{
	    cout<<"             出错!!"<<endl; 
		cout<<"-------------------------------------------------------------------\n";
	    cout<<"             按键退出!!"<<endl;
		cout<<"-------------------------------------------------------------------\n";
	    getch();
	    exit(0);
	}
    }
}

 bool   IsLetter(char &ch)//判断ch中的字符是否为字符
	{
		if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
              return   (1);

		else
			return(0);

	}

  bool   IsDigit(char &ch)//判断ch中的字符是否为数字
	{
//k--;
		if(((ch)<='9')&&((ch)>='0'))
			return   (1);
		else
			return   (0);
	 }



char sr[100],ch[50],tmp[50] ;
char a[5];
int i4=0,q=0;
void shuchu()
{
	int j1=0,j2=0;
	cout<<"loop:"<<endl;
	while(tmp[j1+1]!='\0')
		{
		   if(tmp[j1+1]==';')
		   { // 
		    	cout<<"   "<<a[j2]<<a[j2+1]<<tmp[j1]<<"\n\n";
			     j2+=2;j1+=2;
		   }
	        else
				{
				  cout<<"   "<<"T="<<tmp[j1]<<tmp[j1+1]<<tmp[j1+2]<<endl;
				     tmp[j1+2]='T';j1+=2;
				}	
		}
	cout<<"if T>0  loop"<<endl;
	
}

void main(){
	cout<<"产生式集合:"<<endl;
	cout<<"Z->do{E}while(T)\n";
	cout<<"E->AE|A\n";
	cout<<"A->b op A| b;\n";
	cout<<"T->b op1 b \n";
	Init();
    Print_LR();
    
	//cout<<"shuru(#)"<<endl;
	//cin>>sr;



	char filename[20];

		cout<<"请输入文件名:"<<endl;
		cin>>filename;
		ifstream dic(filename,ios::in);
        
		//form[0]=dic.get();
		while(!dic.eof())    
		{      // cout<<"hello";
			    char m;
				m=dic.get();
			    if((m==32)||(m==10))
				{}
				else
				{sr[q]=m;
				if(sr[q]=='#')
					{
						cout<<"你输入的程序段为\t\n";
						cout.write(sr,q);
						 break; 

					}
				q++;}
			}
		cout<<endl;
	//cout<<sr<<endl;
	cout<<"                 按键运行 "<<endl;//
    getch();
    cout<<endl;
	cout<<"-------------------------------------------------------------------------\n";
	shuru[0]=sr[0];
	for(int k1=1,k2=1;sr[k1]!='\0';k1++)
	{
		if(IsLetter(sr[k1])==0&&IsDigit(sr[k1])==0)
		{ shuru[k2]=sr[k1];k2++;}
		switch(sr[k1-1])
		{ case '+':
		  case '-':
		  case '*':
          case '/':
          case '=':
          case '%':		  
          case '(':
          case ')':
          case '{':
          case '}':
          case '<':
          case '>': shuru[k2]=sr[k1];k2++;break;
		  case ';': if(sr[k1]!='}') 
					{shuru[k2]=sr[k1];k2++;break;}
		} 
	}
	//cout<<shuru<<endl;
	for(int i1=2,i2=0;shuru[i1]!='#';i1++)
	{   if(shuru[i1]!='}'&&shuru[i1]!=')')
		{
	       ch[i2]=shuru[i1];
		    i2++;
		}
	    if(shuru[i1]=='}')
			i1+=2;
	 }
	 for(int i3=0,i5=0;ch[i3]!='#';i3++)
	 {  
	   
		if(ch[i3]=='='||ch[i3+1]=='=')
			{
			  a[i4]=ch[i3];
		      i4++;
			}
		    
		else if(ch[i3]!=';')
			{
			  tmp[i5]=ch[i3];
		       i5++;
			}
		else
			{
			   tmp[i5]=ch[i3];
		     	i5++;		     
			}
	 } 
	  // cout<<shuru<<endl;// cout<<a;
	//	cout<<tmp<<endl;

    LR();
	shuchu();
	
	}

⌨️ 快捷键说明

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