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

📄 new.cpp

📁 自己的编译原理作业
💻 CPP
字号:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
#include <fstream.h>
#include <ctype.h>


char*  ComWords[65]=
	      {"and",    "array",     "begin",    "bool", "call",
           "case",   "char",      "constant", "dim",  "do",
           "else",   "end",       "false",    "for",  "if",
           "input",  "integer",   "not",      "of",   "or",
		   "output", "procedure", "program",  "read", "real",
		   "repeat", "set",       "stop",     "then", "to",
           "true",   "until",     "var",      "while","write",
		   "alpha",  "digit",     "'char'",    "(",    ")",
		   "*",      "*/",        "+",         ",",   "-",    
		   ".", 	 "..",        "/",         "/*",  ":",
		   ":=",     ";",   	  "<",         "<=",  "<>",
		   "=",      ">",		  ">=",        "[",   "]",
		   0 };

char*  variable[30];        //变量表,序列号代表值
int si=0;                   //变量序列编号:变量二元式的后一项
char*  err[30];             //出错序列表
int    ern[100];            //出错行号
int ei=0;                   //出错序列表编号

int ViewT1(char* tp)        //检查字符串是否在保留字表
{
	for (int t=0;(ComWords[t]!=NULL);t++)
	{
    	if (!strcmp(tp,ComWords[t]))
			return t+1;
	} 
	return 0;
}

int ViewT2(char* tp)        //检查字符串是否在变量表
{
	for (int t=0;(variable[t]!=NULL);t++)
	{
    	if (!strcmp(tp,variable[t]))
		{
			return t+1;
		}
	}
	return 0;
}

void Type(int co,int vi)       //输出二元式,co 代码序号; vi 后缀数字 (0 表示保留字);
{
	if(vi==0)
	{
		cout<<"(";
		if(co<10)
			cout<<" ";
		cout<<co<<", -)    ";
	}
	else
	{
		cout<<"("<<co<<",";
		if(vi<10)
			cout<<" ";
		cout<<vi<<")    ";
	}
}


void Charge(char* tp,int& con,int ln)      //判断函数,分析送入的字串
{
	int cha=0;                     //字符串常量标志,1为常量开始
	char string[20];
	char temp[2];                  //用于接受一个字符的临时串指针
	temp[1]=NULL;
	int i=0;
	int sin=1;                     //注释行
	while(tp[i]!=NULL)
	{
		//如果当前字符是整数
		if(isdigit(tp[i])!=0&&sin==1)      
		{		
			while(isdigit(tp[i])!=0) //读入整个数字常量
			{	    	
				temp[0]=tp[i];
	        	strcat(string,temp);
				i++;
			}
			Type(37,si+1);
			variable[si]=new char [sizeof (string)];
			strcpy(variable[si],string);
			si++;
		    con++;
			if ((con)%5==0)
				cout<<endl;
		}
		for (int j=0;string[j]!=NULL;j++)         //释放string
			string[j]=NULL;

		//如果当前字符是字母
		if(isalpha(tp[i])!=0&&sin==1)      
		{		
			while( (isalpha(tp[i])!=0)||(isdigit(tp[i])!=0) )   //读入整个字串,碰到“符号”结束
			{	    	
				temp[0]=tp[i];
	        	strcat(string,temp);
				i++;
			}
			if(ViewT1(string))       //如果是保留字
			{
				Type(ViewT1(string),0);
		    	con++;
				if ((con)%5==0)
					cout<<endl;
			}
			else                    //是变量
            {
				int bs=ViewT2(string);  //如果已经存在于变量表,返回变量序号
				if(bs)
					Type(36,bs);
				else                    //是新变量,插入变量表
				{
					Type(36,si+1);
					variable[si]=new char [sizeof (string)];
			    	strcpy(variable[si],string);
			    	si++;
				}
				con++;		    	
				if ((con)%5==0)
					cout<<endl;
			}
			    			
		}
		for (j=0;string[j]!=NULL;j++)         //释放string
			string[j]=NULL;
      
		while(tp[i]==32&&sin==1)            //处理空格
			i++;
		
		//如果当前字符是“符号”
		if( (tp[i]!=NULL)&&(tp[i]!=32)&&(isalpha(tp[i])==0)&&(isdigit(tp[i])==0) ) 
		{		
			while( (tp[i]!=NULL)&&(tp[i]!=32)&&(isalpha(tp[i])==0)&&(isdigit(tp[i])==0) ) 
			{
				if(tp[i]=='\'')               //是字符串常量,跳出处理
				{
					cha=1;
					break;
				}
				temp[0]=tp[i];
	        	strcat(string,temp);
				i++;
			}
			if(ViewT1(string))         //如果是保留字
			{
				if( (ViewT1(string)!=49)&&(ViewT1(string)!=42) )
				{
					Type(ViewT1(string),0);
					con++;
					if ((con)%5==0)
						cout<<endl;
				}
				else
				{
					sin*=(-1);
				}
			}

			else if(!cha)                     //无定义符号,报错
			{
				cout<<"#"<<ei+1;
				err[ei]=new char [sizeof ("Sign define error ! in line ")];
				strcpy(err[ei],"Sign define error ! in line ");
				ern[ei]=ln;
				ei++;
			}
			if(cha==1)                         //处理字符串常量
			{
				for (j=0;string[j]!=NULL;j++)         //释放string			
					string[j]=NULL;
				strcat(string,"'");
				i++;
				while(tp[i]!=NULL)
				{	
					if(tp[i]=='\'')           //等到结尾的“ ’”出现
					{
						strcat(string,"'");
				        i++;
				    	cha=0;				
					    break;
					}
			    	temp[0]=tp[i];
	            	strcat(string,temp);
			    	i++;
				}				
				if(cha!=0)                //如果字符串常量没有后标志符“ ’”,报错
				{
				    cout<<"#"<<ei+1;
					err[ei]=new char [sizeof ("String const define error! in line ")];
					strcpy(err[ei],"String const define error! in line ");
					ern[ei]=ln;
					ei++;
				}

				Type(38,si+1);             //插入变量表
				variable[si]=new char [sizeof (string)];
				strcpy(variable[si],string);
				si++;
				con++;
				if ((con)%5==0)
					cout<<endl;
			}
			
		}
		if(sin!=1)                            //处理注释行
			i++;
		for (j=0;string[j]!=NULL;j++)         //释放string
			string[j]=NULL;    	
	}
	if(sin!=1)                                 //注释行出错
	{
		cout<<"#"<<ei+1;
		err[ei]=new char [sizeof ("Remark line error! in line ")];
		strcpy(err[ei],"Remark line error! in line ");
		ern[ei]=ln;
		ei++;
	}
}


void test()
{
	cout<<"  please input text name: ";
	char* path;
	path=new char [20];
	cin>>path;
		int con=0;              //输出二元式的个数

	cout<<"  Open files: "<<path;
	cout<<endl<<"  input command words:"<<endl;
	char inp[20];
	ifstream input1(path);  //用input1接收文本作为输出设备,显示原文
	for (int i=0;;i++)
	{
		input1.getline(inp,128);
		if(input1.fail())
			break;
    	cout<<inp<<endl;
	}
	cout<<endl;
	getch();
	cout<<path;
	cout<<endl<<"  output the command word's ID:"<<endl<<endl;
	ifstream input2(path);  //用input2接收文本作为输出设备,处理字符串
	for (i=0;;i++)
	{
		input2.getline(inp,128);
		if(input2.fail())
			break;
		Charge(inp,con,i+1);
	}
	cout<<endl<<endl;
	for (int j=0;err[j]!=NULL;j++)          //显示出错信息
		cout<<"#"<<j+1<<" "<<err[j]<<ern[j]<<endl;
	getch();

	for (j=0;variable[j]!=NULL;j++)         //清空variable
		delete []variable[j];
	si=0;
	for (j=0;err[j]!=NULL;j++)              //清空err
		delete []err[j];
	ei=0;
}


void main ()
{
	cout<<"***************************************************************"<<endl
	    <<"**                                                           **"<<endl
	    <<"**        'SIMPLE' language analysis 1.0         2002.5.20   **"<<endl
	    <<"**                                                           **"<<endl
	    <<"**            CS99(2)     QiuGuangHua     No.37              **"<<endl
	    <<"**                                                           **"<<endl
	    <<"***************************************************************"<<endl
	    <<endl;
S:
	cout<<endl<<endl<<"   Please choose the test type:"<<endl
	    <<"        t.   test"<<endl
		<<"        q.   quit"<<endl;
	char cho;
	cin>>cho;
	switch(cho)
	{	
	    case 't': test();goto S;
		case 'q': break;
		default: goto S;
	}
	cout<<endl<<"             See you next time!"<<endl<<endl;
	getch();
}

⌨️ 快捷键说明

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