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

📄 lex.cpp

📁 词法分析 vc++ 适用于简单的c源程序的词法分析
💻 CPP
字号:
/************************************************************
实现文件lex.cpp
作者:
日期:2003.9.12
*************************************************************/
#include<iostream> 
#include<fstream> 
#include<cstdlib> 
#include <stdio.h>
#include <ctype.h>
#include <string>
using namespace std;

# define  MAX 32 //分析表的最大容量
#define   MAXBUF 255
char  ch =' ';// 存放读入当前的输入字符
int lineno;//程序行数
struct entry{
			  const	char *lexptr;
				int token;
			};//关键字表中的结构

struct entry symtable[MAX];//分析表

static string str[32]; 
//static string  str[] = {"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};

/*void parse2(FILE* fstr)
{
	char arr2[25];
    string str[23];
	int i=0;
	int j=0;
	char ch2;
    int n=0;
    while(!feof(fstr))
	{
		fscanf(fstr,"%c",&ch2);	
		while(ch2!=',')
		{						
			arr2[j]=ch2;
			j++;
			fscanf(fstr,"%c",&ch2);	
		}
		fseek(fstr,-1L,SEEK_CUR);//文件指针后退一个字节
		char* temp1 =(char*)malloc(j+1);
		memcpy(temp1,arr2,j);
		temp1[j] ='\0';
		str[n]=temp1;
		j=0;
		free(temp1);
		fseek(fstr,1L,SEEK_CUR);//文件指针后退一个字节
		n++;
	}
}
*/
void init()//初始化符号表
{
	
	struct  entry* p  = symtable;

	int j=0;
	for( j =0; j<32; j++)
	{	
		
		p->lexptr = str[j].c_str();
		
		p->token = j+1;
		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;
    int count=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,35);//常数

			free(temp1);//释放内存
		    
			
		}

		else if(IsAlpha(ch) || ch=='_')                         //标识符是字母或下划线开头的
		{
		  while(IsAlpha(ch) || IsDigit(ch) || 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,34);             //标示符
			  
		  }
		  free(temp);
		}
	
		//以下为2字节的运算符号
	    else if( ch=='+')
		{
			fscanf(fpin,"%c",&ch);
			if(ch=='+') 
				fprintf(fpout,"%s\t\t%d\n","++",55);
			else 	
			    fprintf(fpout,"+\t\t36\n");
		}
		else if( ch=='-')
		{
			fscanf(fpin,"%c",&ch);
			if(ch=='-') 
				fprintf(fpout,"%s\t\t%d\n","--",56);
			else 	
			    fprintf(fpout,"-\t\t37\n");
		}
		else if( ch=='<')
		{
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			fprintf(fpout,"%s\t\t%d\n","<=",42);
			else if( ch=='<')
			fprintf(fpout,"%s\t\t%d\n","<<",57);
			else 
			{fprintf(fpout,"<\t\t40\n");}
		}
		else if(ch=='>')
		{  
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			fprintf(fpout,"%s\t\t%d\n",">=",43);
			else if(ch=='>')
			fprintf(fpout,"%s\t\t%d\n",">>",58);
			else
			fprintf(fpout,">\t\t41\n");
		}
		else if(ch=='=')
		{  
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			fprintf(fpout,"%s\t\t%d\n","==",44);
			else
			fprintf(fpout,"=\t\t54\n");
		}
	    else if(ch=='!')
		{  
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			fprintf(fpout,"%s\t\t%d\n","!=",45);
			else
			fprintf(fpout,"!\t\t51\n");
		}
        else if(ch=='&')
		{  
			fscanf(fpin,"%c",&ch);
			if(ch=='&')
			fprintf(fpout,"%s\t\t%d\n","&&",52);
			else
			fprintf(fpout,"&\t\t47\n");
		}
		else if(ch=='|')
		{  
			fscanf(fpin,"%c",&ch);
			if(ch=='|')
			fprintf(fpout,"%s\t\t%d\n","||",53);
			else
			fprintf(fpout,"|\t\t49\n");
		}
		else {
	 	//以下为一个字节的运算符号
		if(ch=='*') {fprintf(fpout,"*\t\t38\n");continue;}
		if(ch=='/') {fprintf(fpout,"/\t\t39\n");continue;}
        if(ch=='%') {fprintf(fpout,"%c\t\t46\n",37);continue;}
		if(ch=='^') {fprintf(fpout,"^\t\t48\n");continue;}
		if(ch=='~') {fprintf(fpout,"^\t\t50\n");continue;}
		if(ch==',') {fprintf(fpout,",\t\t59\n");continue;}
     	if(ch=='.') {fprintf(fpout,".\t\t60\n");continue;}
        if(ch==':') {fprintf(fpout,":\t\t61\n");continue;}
        if(ch==';') {fprintf(fpout,";\t\t62\n");continue;}
        if(ch=='?') {fprintf(fpout,"?\t\t63\n");continue;}
        if(ch=='#') {fprintf(fpout,"#\t\t64\n");continue;}
        if(ch=='"') {fprintf(fpout,"%c\t\t65\n",34);continue;}
		if(ch=='\\') {fprintf(fpout,"%c\t\t66\n",92);continue;}		
		if(ch=='(') {fprintf(fpout,"(\t\t67\n");continue;}
		if(ch==')') {fprintf(fpout,")\t\t68\n");continue;}
		if(ch=='[') {fprintf(fpout,"[\t\t69\n");continue;}
		if(ch==']') {fprintf(fpout,"]\t\t70\n");continue;}
	//if(ch=='}') {fprintf(fpout,"}\t\t72\n");continue;}
	    if(ch=='{') {fprintf(fpout,"{\t\t71\n");continue;}
		
        if(feof(fpin)) break;
		/*if(ch=='{') {fprintf(fpout,"{\t\t71\n");
		count=count+1;
		continue;
		}
		if(ch=='}') {fprintf(fpout,"}\t\t72\n");
		count=count-1;
		if(count==0) break;
		continue;
		}
		*/
		

		else fprintf(fpout,"error in compileing %d lines unknown character %c \n",lineno,ch);//出错了
		}
		
	}
	

}



/***************************************************************************
主函数
****************************************************************************/
int main()
{


ifstream read; 
ofstream display; 
read.open("str.txt"); 

for (int i=0; i<32; i++) 
read>>str[i]; 
display<<str[13]; 
read.close(); 
display.close(); 
//for (int j=0;j<32;j++)
//cout<<str[j]<<endl;


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:\\*.c)\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");
//FILE* fstr=fopen("str.txt","r");
//parse2(fstr);
//fclose(fstr);
 
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 + -