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

📄 expression.cpp

📁 按递归下降方式设计其编译程序
💻 CPP
字号:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>
//packet java.lang.Object;
//#include <Afx.h> 
//#include <afx.h>
//#include <CString.h>
//#include <afxwin.h>         // MFC core and standard components
//#include <afxext.h>         // MFC extensions
//#include <afxdisp.h>        // MFC Automation classes
//#include <afxdtctl.h>		// MFC support for Internet Explorer 4 Common Controls
//#ifndef _AFX_NO_AFXCMN_SUPPORT
//#include <afxcmn.h>	
#define cxmax 200
#define nmax 10000
#define kk 4
int timesoffetch;
enum stackop{lit,opr,sto,lod,jmp,INT};
class Table
{
public:
	char name;
	int value;
	int adr;
	Table(){adr=0;value=0;}
};
class Code
{
public :
	enum stackop fct;
	int l;
	int a;
};
enum operator1{ident,number,Lparn,Rparn,plus,minus,times,slash,becomes};
char id;
char ch;
char a[5];
Table table[60];
Code code[200];
int tx;
int cx;
int dx;
int i;
int num;
int line;
bool compliane_pass=true;
enum operator1 sym;
int t=0;
		int p=0;
		int s[30];
		
class LineOfExpression
{
public :
	int  position(char id);
	void block();
	void expression();
	void factor();
	void term();
	void getch1(char &ch);
	void getsym();
	void enter(char id);
	void gen(enum stackop d1,int l,int a);
	bool ch_ina_z(char ch);
	void error(int n);
	void interpret();
	void listcode();
	bool ch_in_a_z_9(char ch);
	bool ch_in_0_9(char ch);
	int position_with_cx(int cx0);
	int test_term();
	int test_factor();
	int test_expression();
};
fstream file("Expression.txt",ios::in|ios::out);

/////////////////////////////////////////////////////////////////////////////////////////////
    int  LineOfExpression::position(char id)
	{
	
		table[0].name=id;
		int i=tx;
		while(table[i].name!=id)
			i=i-1;
		return i;
	}
	//end of position
/////////////////////////////////////////////////////////////////////////////////////////////

	void LineOfExpression::block()
	{
		dx=3;
		int cx1=cx;
		gen(jmp,0,0);
         int cx2=cx;
		gen(INT,0,tx);
		getsym();
		this->expression();
		     	code[cx1].a=1;
			code[cx2].a=tx;//返回后回填分配空间
         
	}
	//end of block
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::expression()
	{
		if(!this->test_expression())
		{
			error(0);
		}
		enum operator1 addop;
		if(sym==plus||sym==minus)
		{
			addop=sym;
			getsym();
			term();
		
			if(addop==minus)
			{
				gen(opr,0,1);
			
			//cout<<"s[t]="<<s[t]<<endl;
			}
		
	        else 
				term();
		}	
	if(sym!=plus&&sym!=minus)
			term();
	
		while(sym==plus||sym==minus)
		{
			addop=sym;
			getsym();
			term();
			if(addop==plus)
			{
				gen(opr,0,2);				
			}
			if(addop==minus)
			{
				gen(opr,0,3);
			}
		}
		
	}
	//end of expression
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::term()
	{
	   
		enum operator1 mulop;
		factor();		
		while(sym==times||sym==slash)
		{
			mulop=sym;
			getsym();
			if(ch==EOF)
			{
				error(4);//4号错误,乘号后面无数字
			   exit(1);
			}
			factor();
			if(mulop==times)
			{    
				gen(opr,0,4);
			}
			else if(mulop==slash)
				gen(opr,0,5);
			

		}

	}
	//end of term	
/////////////////////////////////////////////////////////////////////////////////////////////
   void LineOfExpression::factor()
	{
	
		
		int ii=0;
		char id0;
		
		 if(sym==ident)
		{
			 
			 
			ii=position(a[0]);
			if(ii==0)
			{
			
				error(3);//3号错误,变量表中不存在应用的变量
				//getsym();
				//factor();
				//exit(1);
			}
			else
			{
				gen(lod,1,table[ii].adr);
				getsym();
			}
		
		}
		else if(sym==Lparn)
		{   
			getsym();
			if(sym==number)
			{
				expression();
				if(sym==Rparn)
					getsym();
			}
			else if(sym==ident)
			{
				id0=a[0];
				getsym();
				if(sym==becomes)
				{
					enter(id0);
					getsym();
					expression();
					if(sym==Rparn)
					{
					ii=position(id0);
						getsym();
					gen(sto,1,table[ii].adr);
					}
				else
					
				{
					//getsym();
					//factor();
					error(2); //2 号错误,括号后面字符非法
					
				}
				}
			}
			
		}
	else if(sym==number)
		{
		
			gen(lit,0,num);
			getsym();
		
	
		}
	else if(ch!=EOF)

	error(1); 
	else
		;//1 号错误,表示不是因子的处理范围
 //只有返回0时才是正确的
	}
	//end of factor
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::getch1(char &ch)
	{
	
	
       while(true)
	   {
             file.seekg(i,ios::beg);
		     file.get(ch);
			   i++;
		   if(ch==' '||ch==32||ch=='\0')
		   {
			   continue;
		   }
		   else 
		   {
			  break;
		   }
		   if(ch==EOF)
		   {
			   
			  file.close();
			 // ch=';';
		   }
	   }
	   
	}
	//end of getch1
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::getsym()
	{
		int k=0;
		getch1(ch);
		
		 if(ch=='=')
		 {	sym=becomes;}
		else if(ch=='(')
		{	sym=Lparn;}
		else if(ch==')')
		{	sym=Rparn;}
		else if(ch=='+')
		{	sym=plus;}
		else if(ch=='-')
		{	sym=minus;}
		else if(ch=='*')
		{sym=times;}
		else if(ch=='/')
			sym=slash;
	    else if(this->ch_ina_z(ch))//what you fetch is letter
		{
			while(true)
			{
				a[k++]=ch;
				getch1(ch);
				if(ch==';')
				exit(0);
				if(!this->ch_in_a_z_9(ch))
				{
					i=i-1;
					break;
				}
			}
			if(k>=kk)
			{
				k=kk;
				a[k]='\0';
				
			}
			else
				for(int j=k;j<kk;j++)
					a[j]='\0';
				sym=ident;
				return;
		}
		else if(ch_in_0_9(ch))
		{
		   num=0;
		   
			int counter_num=0;
			while(true)
			{
				num=10*num+(ch-'0');
				counter_num++;
				getch1(ch);
				sym=number;
				if(!ch_in_0_9(ch))
				{
					i=i-1;
					break;
				}
			}
			
			if(counter_num>nmax)
			{
				error(1);//数长超过了上限
			}
		
			
		}
		else if(ch==EOF)
		{;}
		else
		{
			cout<<"error:illegal input of::"<<ch<<ch<<endl;
		
		}

			
	}
	//end of getsym
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::enter(char id)
	{
		tx=tx+1;
		table[tx].name=id;
		table[tx].adr=dx;
		dx=dx+1;
	}
	//end of enter
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::gen(enum stackop d1,int l,int a)
	{
		if(cx>cxmax)
		{
			error(2);
			exit(0);
		}

		code[cx].fct=d1;
		
		code[cx].l=l;
	//	if(l==1)
	//	code[cx].a=a+20;
	//	else
			code[cx].a=a;
		cx=cx+1;
	}
	//end of gen
/////////////////////////////////////////////////////////////////////////////////////////////
	bool LineOfExpression::ch_ina_z(char ch)
	{
		for(int m=0;m<26;m++)
		{
			if(ch=='a'+m)
				return true;
		
		}
		return false;
	}
	//end of ch_ina_z
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::error(int n)
	{   compliane_pass=false;
		switch (n)
		{
		case 0:
			cout<<"Input: ' "<<ch<<" ' at the '"<<i<<"'th letter is not a member of an expression!"<<endl;
			break;
		case 1:
			cout<<"Input:  ' "<<ch<<" ' at the  '"<<i<<" 'is not a member of an term!"<<endl;
			break;
		case 2:
			cout<<" '("<<a[0]<<"'at the "<<i<<"'th is illegal"<<endl;
			break;
		case 3:
			cout<<"Not exist of:  '"<<a[0]<<"'   variable!"<<endl;
			break;
	    case 4:
			cout<<"  Unexcept end of expression!"<<endl;
			break;
		case 5:
			break;
		};
	}
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::interpret()
	{
		cout<<endl<<endl<<"------------------start pl/0--------------------------------      "<<endl;
		int end_cx=0;
	
		for(int m=1;m<=3;m++)
			s[m]=0;
		t=3;
		int addr=0;
		while(end_cx<cx)
		{
			switch(code[p].fct)
			{
			case lit:
				t=t+1;
				s[t]=code[p].a;
				cout<<"t="<<t<<" s[t]="<<s[t]<<"                  lit"<<endl;
				break;
			case opr:
				switch(code[p].a)
				{
				case 0:
					t=0;
					p=s[t+3];
					break;
				case 1:
					s[t]=-s[t];
					cout<<"t="<<t<<"      s[t]="<<s[t]<<"        s[t-1]="<<s[t-1]<<"取负"<<endl;
					
					break;
				case 2:
					cout<<"t="<<t<<"      s[t]="<<s[t]<<"        s[t-1]="<<s[t-1]<<"加"<<endl;
					
					s[t-1]=s[t-1]+s[t];
                     t=t-1;
					break;
				case 3:
					cout<<"t="<<t<<"      s[t]="<<s[t]<<"        s[t-1]="<<s[t-1]<<"减"<<endl;
					
					s[t-1]=s[t-1]-s[t];
					t=t-1;
					break;
				case 4:
					cout<<"t="<<t<<"      s[t]="<<s[t]<<"        s[t-1]="<<s[t-1]<<"乘"<<endl;
				
					s[t-1]=s[t-1]*s[t];
					t=t-1;
					break;
 			case 5:
				cout<<"t="<<t<<"      s[t]="<<s[t]<<"        s[t-1]="<<s[t-1]<<"除"<<endl;
				
					s[t-1]=s[t-1]/s[t];
					t=t-1;
					break;
			
				};
				break;
				case lod:
					
					t=t+1;
					s[t]=s[code[p].a];
					cout<<"t="<<t<<"      s[t]="<<s[t]<<"        s[t-1]="<<s[t-1]<<" lod"<<endl;
					break;
				case sto:
					s[code[p].a]=s[t];
					//t=t-1;	
					break;
				case jmp:
					p=code[p].a;
					continue;
					//break;
				case INT:
					t=t+code[p].a;
					
					break;
			};
			p=p+1;
			end_cx++;	
			
		}                

	 
	}
/////////////////////////////////////////////////////////////////////////////////////////////
	void LineOfExpression::listcode()
	{
	fstream file;
	file.open("result.txt",ios::in|ios::out);
	line=1;
		for(int m=0;m<cx;m++)
		{
			
			switch(code[m].fct)
			{
			case opr:cout<<line++<<"  :opr"<<"---"<<code[m].l<<"---"<<code[m].a<<endl;
				file.write("opr",4);
				break;
			case lit:cout<<line++<<"  :lit"<<"---"<<code[m].l<<"---"<<code[m].a<<endl;
				file.write("lit",4);
				break;
			case lod:cout<<line++<<"  :lod"<<"---"<<code[m].l<<"---"<<code[m].a<<endl;
				file.write("lod",4);
				break;
			case sto:cout<<line++<<"  :sto"<<"---"<<code[m].l<<"---"<<code[m].a<<endl;
				file.write("sto",4);
				break;
			case jmp:cout<<line++<<"  :jmp"<<"---"<<code[m].l<<"---"<<code[m].a<<endl;
				file.write("jmp",4);
				break;
			case INT:cout<<line++<<"  :INT"<<"---"<<code[m].l<<"---"<<code[m].a<<endl;

			};
		}
		file.close();
		for( m=1;m<tx+1;m++)
				cout<<table[m].name<<"*******"<<table[m].value<<"*****"<<table[m].adr<<endl;
	}
/////////////////////////////////////////////////////////////////////////////////////////////
	bool LineOfExpression::ch_in_a_z_9(char ch)
	{
		for(int m=0;m<10;m++)
		{
			if(ch=='0'+m)
				return true;
		}
		return (ch_ina_z(ch));
	}
	//end of ch_ina_z_9
/////////////////////////////////////////////////////////////////////////////////////////////
	bool LineOfExpression::ch_in_0_9(char ch)
	{
		for(int m=0;m<10;m++)
		{
			if(ch==('0'+m))
			{
			return true;
			}
		}
		return false;
	}
	//end of ch_in_0_9
////////////////////////////////////////////////////////////////////////////////////////////
	int LineOfExpression::position_with_cx(int cx1)
	{int m;
		for(m=1;m<tx;m++)
			
		{
			if(cx1==table[m].value)
				break;
		}
			return m;
	}//end of position_with_cx
////////////////////////////////////////////////////////////////////////////////////////////////
	int LineOfExpression::test_term()
	{

		return 0;
	}//end of test_term
///////////////////////////////////////////////////////////////////////////////////////////
	int LineOfExpression::test_expression()
	{
      if(sym==slash||sym==times)
		  return 0;
	  return 1;
	}//end of test_expression
////////////////////////////////////////////////////////////////////////////////////////////////
	int LineOfExpression::test_factor()
	{
     return 0;
	}//end of test_factor
///////////////////////////////////////////////////////////////////////////////////////////////
	class toString
	{
	public :
		char cha[6];
		toString()  {    }
	  int getLength(char *str)
	  {
		  for(int m=0;;m++)
		  {
			  if(str[m]==' '||str[m]=='\0'||str[m]==32)
				  break;
			  
		  }
		  return m;
	  }
	  void change_int_char(int value)
	  {    int i=10000;
	        int v;
			int w=0;
			int block=0;
		  while(true)
		  { 
			  if(i==0)
			     break;
            if(v==0&&block==0)
			{
				
				;
			}
			else
			{   
				block=1;
				cha[w++]=v+'0';
			
			}
				i/=10;
				v=value/i;
		  }
		  cha[w]=' ';
		  cout<<"cha____str======"<<cha<<endl;
	  }


	};
	void main()
	{	
	
	    int op=1;
		int return_block;
		while(op)
		{
				LineOfExpression exp;
cout<<"编译(1)--中间代码(2)--解释执行(3)--运行结果(4)--退出(5)"<<endl;
         cin>>op;
        switch(op)
		{
		case 1: exp.block();break;
		case 2: exp.listcode();break;
		case 3:
			if(::compliane_pass)
				exp.interpret();
			else
				cout<<"not pass compliane yet!"<<endl;
			break;
		case 4:cout<<"result=大"<<s[t-1]<<endl;break;
		case 5:op=0;break;
		};
	
	   
	
		
	//	
		}
	//fstream file1;
		//file1.open("Expr.txt",ios::in|ios::out);
	//	CString str="kkkkkkk";

	//	str.Format("结果=%s[t]",s[t]);
       // file1.write(str,str.GetLength());
        //file1.close();
	 /*	toString obj;
	 obj.change_int_char(s[t]);
	 char str[6];
	 for(int l=0;l<6;l++)
		 str[l]=obj.cha[l];
	 cout<<str<<"5555555555"<<endl;
	
	 ;*/
		
	}

⌨️ 快捷键说明

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