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

📄 morphology.h

📁 计算机科学与技术专业课程编译原理的课程实验代码
💻 H
字号:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

ofstream T_fout("words.txt");//定义临时输出文件


const int Keynum=16,
		  JFnum=14;
//0 --- Error             错误
//1 --- KEYWord           保 留 字
//2 --- Number            数   字
//3 --- Identity           标示符号
//4 --- Operator            运 算 符
//5 --- Operator(Evaluate)  运算符(参数)
//6 --- Seperator            界符
char *Type[7]={"Error","KeyWord  ","Number   ","Identity ","Operator1","Operator2","Seperator"};
char *KeyWord[Keynum]={"PROGRAM","CONST","VAR","INTEGER","LONG","PROCEDURE","IF","THEN","WHILE","DO","READ","WRITE","BEGIN","END","ODD","CALL"};
char JF[JFnum]={'+' , '-' , '*' , '/' , '=' ,  '<' , '>' , '.' ,  ',' ,';' , ':' ,  '(' , ')' , '#' };

char* Change(char *s)//小写变为大写
{
	char p[1000];
	int len=strlen(s);	
	//p=new char[len+1];
	strcpy(p,s);
	for(int i=0;i<len;i++)
	{
		if(s[i]>='a' && s[i]<'z')
		{
			p[i]=s[i]-32;
		}
		//else p[i]=s[i];
	}
	//p[i]='\0';
	return p;		
}

int CheckType(char* s)//类别判断
{
	int i=0;
	char *Temp=0;
	int len=strlen(s);	
	Temp=Change(s);
	if(s[i]>='0'&&s[i]<='9')
	{
		while(s[i++]&&s[i]) 			
		{
			if(s[i]<'0'||s[i]>'9') return 0;//Error			
		}			
		return 2;//Num
	}
	for(i=0; i<Keynum; i++)
		if(strcmp(Temp,KeyWord[i]) == 0) return 1;//ReWord
	if(s[0]>='A' && s[0]<='Z' || s[0]>='a' && s[0]<'z')
		return 3;//Id
	return 0;//Error
}

int Part(char c)
{
	if(c==' ') return 1;
	for(int i=0; i<JFnum; i++) 
		if(c == JF[i]) return 1;//c为界符 
	return 0;
}
//利用界符分割各标示符 

void WordAna(char *inname, char *outname )
{
	char *s, *p, c;
	ifstream fin(inname);//输入文件的文件名
	ofstream fout(outname);//输出文件的文件名	
	s=new char[80];
	p=new char[80];
	cout<< "词法分析如下..." << endl;
	cout<<"行号  种类     符号"<<endl;
	fout<<"**********************************"<<endl;
	fout<<"**      词   法   分   析       **"<<endl;
	fout<<"**                              **"<<endl;
	fout<<"**      开发者: 唐志贤         **"<<endl;
	fout<<"**      学  号:20032337        **"<<endl;
	fout<<"**                              **"<<endl;		
	fout<<"**********************************"<<endl;
    fout<<"行号  种类     符号"<<endl;	
	for(int i=0; i<80; i++) s[i]='\0';
	int length=0, temp=0, lines=0;
	while(fin.getline(p,80))
	{
        lines++;
        temp=0;
        while(p[temp])
		{
			c=p[temp++];
			if(!Part(c))
			{
				s[length++]=c;
				c='\0';
				continue;
			}
			if(s[0]&&s[0]!=' ')
			{
				int type=CheckType(s);
				cout <<lines<<"(  "<<Type[type]<<" , "<<s<<"  )"<<endl;				
				fout<<lines<<' '<<Type[type]<<' '<<s<<endl;
				T_fout<<lines<<' '<<type<<' '<<s<<endl;

			}
			if(c==':'||c=='<'||c=='>')
			{
			   if(p[temp]=='=')
			   {
				   cout<<lines<<"(  "<<"Operator2, "<<c<<"  )"<<endl;////				  
				   if(c!=':') 
				   {
					   fout<<lines<<' '<<Type[4]<<' '<<c<<'='<<endl;//Operator
					   T_fout<<lines<<' '<<4<<' '<<c<<'='<<endl;
				   }
				   else 
				   {
					   fout<<lines<<' '<<Type[5]<<' '<<c<<'='<<endl;//Operator(Evaluate)
					   T_fout<<lines<<' '<<5<<' '<<c<<'='<<endl;
				   }
				   temp++;
			   }
			   else
			   {
			         cout<<lines<<"(  "<<"Operator1, "<<c<<"  )"<<endl;					
			         fout<<lines<<' '<<Type[4]<<' '<<c<<endl;
					 T_fout<<lines<<' '<<4<<' '<<c<<endl;
			   }
            }
			else 
				if(c!=' ')			
					if(c=='.'||c==','||c==';'||c=='('||c==')')            
					{                
						cout<<lines<<"(  "<<"Seperator , "<<c<<"  )"<<endl;							             
						fout<<lines<<' '<<Type[6]<<' '<<c<<endl;    
						T_fout<<lines<<' '<<6<<' '<<c<<endl;
					}			
					else             
					{                
						cout<<lines<<"(  "<<"Operator1, "<<c<<"  )"<<endl;					            
						fout<<lines<<' '<<Type[4]<<' '<<c<<endl;
						T_fout<<lines<<' '<<4<<' '<<c<<endl;
            
					}            
					for(int i=0; i<length; i++)
						s[i]='\0';				
					length=0;
		
		}
		if(!c)
		{
		    if(s[0]&&s[0]!=' ')
			{
				int type=CheckType(s);
				cout<<lines<<"(  "<<Type[type]<<" , "<<s<<"  )"<<endl;			
				fout<<lines<<' '<<Type[type]<<' '<<s<<endl;
				T_fout<<lines<<' '<<type<<' '<<s<<endl;
			}
			for(int i=0; i<length; i++) s[i]='\0';
            length=0;
		}
	}//end while  
}

⌨️ 快捷键说明

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