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

📄 词法编译器.cpp

📁 计算机科学与技术专业课程编译原理的课程实验代码
💻 CPP
字号:

#include<iostream.h>
#include<iostream.h>
#include<string.h>
#include<ostream>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>


#define  MAX 18   //分析表的最大容量
#define  MAXBUF 255
char  ch =' ';   // 存放读入当前的输入字符
int lineno=0;

struct reserve   //关键字
{
 char lexptr[MAXBUF];
 int token;
};

struct reserve symtable[MAX];
char * str[]={"program","const","var","integer","long","procedure","if","then","while","do","read","write","begin","end","odd"};




void Inital()     //对符号表进行初始化
{ 
	for( int j=0; j<15; j++) 
	{  
		strcpy(symtable[j].lexptr,str[j]); 
		symtable[j].token=j+1; 
	}
}
 

int Search(char *temp)
{
 
	for(int i=0; i <sizeof(symtable)/sizeof(symtable[0]); i++) 
	{  
		if(!strcmp(symtable[i].lexptr ,temp))  
		{   
			return  symtable[i].token;  
		} 
	} 
	return 0;
}     

 

void Syntax(FILE *fpin,FILE *fpout)    //分析程序
{
 
	char Temp[MAXBUF]; 
	int i=0; 
	int j=0; 
	fprintf(fpout,"%s\t","  符号  ") ;
    fprintf(fpout,"%s\t","识别号") ;
	fprintf(fpout,"%s\n","类别") ;
	while((ch=fgetc(fpin))!=EOF)    //读入字符判断,空格、字母、数字、界符 
	{  
		if(ch==' '||ch=='\t')  
		{   

		}
		else if(ch=='\n')    //如果是换行符,则行号加1  
		{  
   			lineno++;  
		}  
		else if(isdigit(ch))   //如果是数字  
		{    
			while(isdigit(ch))   //判断和读取数字   
			{     
				Temp[j]=ch;    
				j++;    
				ch=fgetc(fpin);   
			}   
			Temp[j]='\0';   
			j=0; 			
			fprintf(fpout,"%s\t%d\t",Temp,2) ; 
			fprintf(fpout,"%s\n","数字") ;
		}  
		else if (isalpha(ch))  //如果是字母  
		{   
			while(isalpha(ch)||isdigit(ch))   
			{    
				Temp[j]=ch;    
				j++;    
				ch=fgetc(fpin);  
			}  			  
			Temp[j]='\0';   
			j=0;  
			if (Search(Temp)) //如果是关键字  
			{    
				fprintf(fpout,"%s\t%d\t",Temp,Search(Temp));
				fprintf(fpout,"%s\n","关键字") ;
   			}   
			else    
			{
				fprintf(fpout,"%s\t%d\t",Temp,34); //普通标志符
				fprintf(fpout,"%s\n","标志符") ;

			}
 
		}  
		else if(ch==':')    
		{  
			ch=fgetc(fpin);  
			if(ch=='=')    
			{    
				fprintf(fpout,"%s\t%d\t",":=",30);    //如果是 := 
				fprintf(fpout,"%s\n","复合运算符") ;
			}   
			else  
			{    
				fprintf(fpout,"%s\t%d\t",":",29);   //如果是 :
				fprintf(fpout,"%s\n","类别说明符") ;    		
			}  
		}  
		else if (ch=='>')  
		{   
			ch=fgetc(fpin);   
			if(ch=='=')    //如果是 >=   
	
			{    
				fprintf(fpout,"%s\t%d\t",">=",25);
				fprintf(fpout,"%s\n","复合运算符") ;
			}   
			else  
			{    
				fprintf(fpout,"%s\t%d\t",">",24);  //如果是 >    				
				fseek(fpin,-1L,SEEK_CUR);   
			}
  		}  
		else if(ch=='<')  
		{    
			ch=fgetc(fpin);   
			if(ch=='>')   
			{    
				fprintf(fpout,"%s\t%d\t","<>",21);  // 如果是 <>  
				fprintf(fpout,"%s\n","复杂运算符") ;
			}   
			else if(ch=='=')
   			{    
				fprintf(fpout,"%s\t%d\t","<=",23);   //如果是 <= 
				fprintf(fpout,"%s\n","复合运算符") ;
			}   
			else    	
			{   
				fprintf(fpout,"%s\t%d\t","<",22);   //如果是 <   
				fprintf(fpout,"%s\n","简单运算符") ;		
			}  
		}  
		else if(ch=='/')  
		{    
			ch=fgetc(fpin);   
			if(ch=='*')
   			{
   				ch=fgetc(fpin);
s:   
				while(ch!='*')   
				{     
					ch=fgetc(fpin);    
				}    
				while(ch=='*')    
				{     
					ch=fgetc(fpin);     
					while(ch!='/')     
					{      
						goto s;   //如果是注释 /*  */     
					}    
				}   
			}   
			else if(ch=='/')   
			{    
				ch=fgetc(fpin);
   				while(ch!='\n')    
				{    
					ch=fgetc(fpin);   //如果是注释 //    
				}
   			}   
			else    
			{      
				fprintf(fpout,"%s\t%d\t","/",24); 
				fprintf(fpout,"%s\n","简单运算符") ;				  
			}  
		}  
		else if(ch=='+')  
		{   
			fprintf(fpout,"%s\t%d\t","+",16);
			fprintf(fpout,"%s\n","简单运算符") ;
		}  
		else if(ch=='-')  
		{   
			fprintf(fpout,"%s\t%d\t","-",17); 
			fprintf(fpout,"%s\n","简单运算符") ;
		}  
		else if(ch=='*')  
		{   
			fprintf(fpout,"%s\t%d\t","*",18); 
			fprintf(fpout,"%s\n","简单运算符") ;
		}  
		else if(ch=='(') 
		{   
			fprintf(fpout,"%s\t%d\t","(",31);  
			fprintf(fpout,"%s\n","简单运算符") ;
		}  
		else if(ch==')')  
		{   
			fprintf(fpout,"%s\t%d\t",")",32);
			fprintf(fpout,"%s\n","简单运算符") ;
  
		}  
		else if(ch=='#')  
		{   
			fprintf(fpout,"%s\t%d\t","#",35);
			fprintf(fpout,"%s\n","简单运算符") ;
		} 	
		else if(ch=='.')  
		{   
			fprintf(fpout,"%s\t%d\t",".",26); 
			fprintf(fpout,"%s\n","程序结束符") ;
		}  
		else if(ch==';')  
		{   
			fprintf(fpout,"%s\t%d\t",";",28);
			fprintf(fpout,"%s\n","语句结束符") ;
		}  
		else if(ch=='=')  
		{   
			fprintf(fpout,"%s\t%d\t","=",20);
			fprintf(fpout,"%s\n","简单运算符") ;
		}  
		else if(ch==',')  
		{   
			fprintf(fpout,"%s\t%d\t",",",27);
			fprintf(fpout,"%s\n","简单运算符") ;
		}  
		else fprintf(fpout,"无法识别的字符 %c\n",ch)  ;
}

} 


void main()
{
 
	cout<<"**********************************"<<endl;
	cout<<"**      词   法   分   析       **"<<endl;
	cout<<"**                              **"<<endl;
	cout<<"**      开发者: 唐志贤         **"<<endl;
	cout<<"**      学  号:20032337        **"<<endl;
	cout<<"**                              **"<<endl;		
	cout<<"**********************************"<<endl;
	char filenamein[10]; 
	char filenameout[10];  
	cout<<"请输入源程序的文件路径和扩展名!"<<endl;
	scanf("%s",filenamein); 
	cout<<"请输入词法分析结果输出的文件路径和扩展名!"<<endl;
	scanf("%s",filenameout); 
	FILE* fpin=fopen(filenamein,"r"); 
	FILE* fpout=fopen(filenameout,"w"); 
	Inital(); 
	Syntax(fpin,fpout); 
	fclose(fpin); 
	fclose(fpout); 
}



 

⌨️ 快捷键说明

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