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

📄 lex.cpp

📁 编译词法分析程序
💻 CPP
字号:
/************************************************************
实现文件lex.cpp
作者:张勇
日期:2003.6.28
*************************************************************/

#include "global.h"

void init()//初始化符号表
{
	
	struct  entry* p  = symtable;

	int j=0;
	for( j =0; j<6; j++)
	{	
		
		p->lexptr = str[j].c_str();
		
		p->token = j+3;
		p++;
	}


}
/******************************************************************
功能:判断一个字符是否为数字
*******************************************************************/
bool IsDigit(char ch)
{
	return (ch >='0' && ch <= '9');
}
/******************************************************************
功能:判断一个字符是否确为字母
*******************************************************************/
bool IsAlpha(char ch)
{
	return (ch >='a' && ch <= 'z'|| ch>'A '&& ch<'Z');
}

/******************************************************************
功能:在关键字表中查找与arr相同的关键字
找到就返回类别编码,否则返回0
*******************************************************************/
int  FindOK(char* arr)
{
	
	for(int i=0; i<sizeof(symtable)/sizeof(symtable[0]); ++i)
	{
	
	
		if(!strcmp(symtable[i].lexptr ,arr))
		{
			
			return  symtable[i].token;
		}
	}
 return 0;
}
/********************************************************************
以下为主分析函数
从输入文件里面读,把分析结果写到输出文件中
参数:fpin :输入文件指针  fpout: 输出文件指针
********************************************************************/
void parse(FILE* fpin,FILE* fpout)
{
	char arr[MAXBUF];
	int i=0;
	int j=0;
     

	while(1)
	{
	fscanf(fpin,"%c",&ch);

		if( ch==' '|| ch =='\t')
			;
		else if( ch=='\n')
			lineno++;
		else if( IsDigit(ch))
		{
			while(IsDigit(ch))
			{
				 
				arr[j] = ch;
				j++;
				fscanf(fpin,"%c",&ch);
				
			}
			
		    fseek(fpin,-1L,SEEK_CUR);//文件指针后退一个字节

			char* temp1 =(char*)malloc(j+1);
			memcpy(temp1,arr,j);
			temp1[j] ='\0';//把数组里面的内容拷贝到连外一个数组里面,因为我定义的
			//arr为255个字节,实际上写不到那么多,所以只拷贝实际上有数据的

			j=0;//恢复初始状态,以备下次使用
			fprintf(fpout,"%s\t\t%d\n",temp1,2);//常数
		
			free(temp1);//释放内存
		    
			
		}

		else if(IsAlpha(ch))//是字母开头的
		{
		
			while(IsAlpha(ch) || IsDigit(ch))
			{
				arr[i] =ch;
				i++;
				fscanf(fpin,"%c",&ch);
				
			}
			fseek(fpin,-1L,SEEK_CUR);
			
			char* temp = (char*)malloc(i+1) ;
			memcpy(temp,arr,i);
			temp[i] ='\0';

			i=0;
			/*基本思想同处理数字的*/
		
          if(FindOK(temp))//查表
		  {
			 
			 fprintf(fpout,"%s\t\t%d \n",temp,FindOK(temp));
			 
		  }
		  else
		  {
			  fprintf(fpout,"%s\t\t%d\n",temp,1);//标示符号
			  
		  }
		  free(temp);
		}
	
		//以下为2字节的运算符号
		else if( ch==':')
		{
			fscanf(fpin,"%c",&ch);
			if(ch=='=') 
				fprintf(fpout,"%s\t\t%d\n",":=",20);
			
		else 	
			fprintf(fpout,"error in compileing %d lines unknown character %c \n",lineno,ch);//出错了
		}

		else if(ch=='>')
		{  
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			fprintf(fpout,"%s\t\t%d\n",">=",16);
			else
			fprintf(fpout,">\t\t15\n");
		}
		else if( ch=='<')
		{
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			{fprintf(fpout,"<=\t\t18\n");}
			else if( ch=='>')
			{fprintf(fpout,"<>\t\t19");}
			else 
			{fprintf(fpout,"<\t\t19\n");}
		}
	

		else {
	   	//以下为一个字节的运算符号
		if(ch=='-') {fprintf(fpout,"-\t\t10\n");continue;}
		if(ch==';') {fprintf(fpout,";\t\t21\n");continue;}
		if(ch=='+') {fprintf(fpout,"+\t\t9\n");continue;}
		if(ch=='*') {fprintf(fpout,"*\t\t11\n");continue;}
		if(ch=='/') {fprintf(fpout,"/ \t\t12\n");continue;}
		if(ch=='(') {fprintf(fpout,"(\t\t13\n");continue;}
		if(ch==')') {fprintf(fpout,")\t\t14\n");continue;}
		if(ch=='.') {fprintf(fpout,".\t\t22\n");continue;}
		if(ch==',') {fprintf(fpout,",\t\t23\n");continue;}
		if(ch=='#') break;//分析结束
		else fprintf(fpout,"error in compileing %d lines unknown character %c \n",lineno,ch);//出错了
		}
		
	}
	

}



/***************************************************************************
主函数
****************************************************************************/
int main()
{
	char filenamein[14],filenameout[14];
	printf("please input the filename of your source code(the name length can not exceed 25: \n");
	printf("(eg.the filename can be c:\\test.txt)\n");
	scanf("%s",filenamein);
	printf("please input the filename of your output file(the name length can not exceed 25: \n");
	printf("(eg. c:\\output.txt)\n");
	scanf("%s",filenameout);

	FILE* fpin = fopen(filenamein,"r");
	FILE* fpout =fopen(filenameout,"w");

	init();
	parse(fpin,fpout);

    fclose(fpin);
	fclose(fpout);
	printf("press any key to exit..........\n");
	getchar();
	return 0;
	

}

⌨️ 快捷键说明

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