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

📄 lexical.cpp

📁 一个简单的词法分析器,可完成对C语言代码的分析
💻 CPP
字号:
#include<cstdio>
#include<cstdlib>
#include<cstring>

#include<iostream>
using namespace std;

const int MAX_BUFFER = 20;		//定义保留字的最大字符数
const int MAX_RESERVE = 50;		//定义保留字的最大数目
/*保留字数组*/
char* reserve[]={"int","double","char","do", "if", "while","for","printf",
				"else","switch","case","break","return","float",
};

struct keyWord		//关键字
{
	char lexeme[MAX_BUFFER];
};
struct keyWord symbolTable [MAX_RESERVE];


/*判断输入的字符是否是数字:0-9,是则返回 true ,否则返回 flase*/
bool isDigit(char digit);

/*判断输入的字符是否是字母:a-z 或A-Z 是则返回 true ,否则返回 flase*/
bool isLetter(char ch);

/*初始化符号表,以便于查找关键字*/
void initial();	

/*将输入的字符与字符表中预定义的关键字进匹配,是则返回 true ,否则返回 flase*/
bool search(char *temp);

/*分析函数,从文件名为"in"的源文件中读入程序,然后再将分析的结果送"out"*/
void lexicalAnalyse(FILE *in,FILE *out);



//主函数

void main()
{
    FILE *in,*out;		//定义文件指针

	initial();
	//打开文件
	if((in = fopen("in.txt","r")) == NULL)
	{
		printf("Cannot open file \n");
		exit(0);
	}
	//向文件中写入数据
	if((out = fopen("out.txt","w")) == NULL)
	{
		printf("Cannot open file \n");
		exit(0);
	}
	//分析函数
	lexicalAnalyse(in,out);
    //关闭文件
	fclose(in);
	fclose(out);
	
	
}


bool isDigit(char digit)
{
     if(digit >= '0' && digit <= '9')
        return true;
     else 
		return false;
}

bool isLetter(char ch)
{
	if((ch >= 'a' && ch <= 'z') || ch >= 'A' && ch <='Z')
		return true;
	else
		return false;
}


void initial()								//对符号表进行初始化
{
	for( int j = 0; j < 14; j++)
		strcpy(symbolTable[j].lexeme,reserve[j]);
}


bool search(char *temp)
{
	for(int i = 0; i < 14;i++)
		if(!strcmp(symbolTable[i].lexeme,temp))
			return true;
	return false;
}    

void lexicalAnalyse(FILE *in,FILE *out)
{
	char letterArray[MAX_BUFFER];		
	char ch;		
	int j=0;				
 
	while((ch = fgetc(in)) != EOF)			//读入字符,判断空格、字母、数字、界符
	{
		if(ch == ' '|| ch == '\t' || ch == '\n'){ }		//如果读入的字符是空格或制表符,则跳过
		
		//识别数字
		else if(isDigit(ch))				//如果是数字
		{ 
			while(isDigit(ch) || ch == '.')				//判断和读取数字
			{ 
				letterArray[j++] = ch;		//将数字存入数组 letterArray 中
				ch = fgetc(in);
			} 
			letterArray[j] = '\0';			//加入字符串结束符
			j = 0;							//恢复 j 的值
			fseek(in,-1L,1);				//从文件当前位置回退一个字符
			
			//将信息输出到文件 out 中去,格式为(3,  "10"   )
			fprintf(out,"(%d,  \"%s\"\t)\n",3,letterArray);
			
		}
		
		//识别保留字或标识符
		else if(isLetter(ch))				//如果是字母
		{
			while(isLetter(ch) || isDigit(ch))		//如果是字母或是数字,检查保留字或标识符
			{
				letterArray[j++] = ch;
				ch = fgetc(in);
			}
			letterArray[j] = '\0';
			j = 0;
			fseek(in,-1L,1);
			
			if(search(letterArray))			//如果是关键字
				fprintf(out,"(%d,  \"%s\"\t)\n",1,letterArray);
			else							//普通标识符
				fprintf(out,"(%d,  \"%s\"\t)\n",2,letterArray); 
		}
		
		//识别算术运算符
		else if(ch == '+')
			fprintf(out,"(%d,  \"%s\"\t)\n",4,"+");
		else if(ch == '-')
			fprintf(out,"(%d,  \"%s\"\t)\n",4,"-");
		else if(ch == '*')
			fprintf(out,"(%d,  \"%s\"\t)\n",4,"*");
		else if(ch == '/')
			fprintf(out,"(%d,  \"%s\"\t)\n",4,"/");
		
		
		//识别关系运算符
		else if(ch == '=')		//识别 == 或 =
		{
			ch = fgetc(in);
			if(ch == '=')		//如果是 ==
				fprintf(out,"(%d,  \"%s\"\t)\n",4,"==");
			else
			{
				fprintf(out,"(%d,  \"%s\"\t)\n",4,"=");		//如果是 =
				fseek(in,-1L,1);		//文件的当前指针回退
			}
		}
		else if(ch == '<')				//识别 <= 或 <> 或 <
		{
			ch = fgetc(in);
			if(ch == '=')				//如果是 <=
				fprintf(out,"(%d,  \"%s\"\t)\n",4,"<=");
			else if(ch == '>')			//如果是 <>
				fprintf(out,"(%d,  \"%s\"\t)\n",4,"<=");
			else						//如果是 <
			{
				fprintf(out,"(%d,  \"%s\"\t)\n",4,"<");		
				fseek(in,-1L,1);		//文件的当前指针回退
			}
		}
		else if(ch == '>')				//识别 >= 或 >
		{
			ch = fgetc(in);
			if(ch == '=')				//如果是 >=
				fprintf(out,"(%d,  \"%s\"\t)\n",4,">=");
			else						//如果是 >
			{
				fprintf(out,"(%d,  \"%s\"\t)\n",4,">");		
				fseek(in,-1L,1);		//文件的当前指针回退
			}
		}
		
		else if(ch == '!')				//识别 !=
		{
			ch = fgetc(in);
			if(ch == '=')
				fprintf(out,"(%d,  \"%s\"\t)\n",4,"!=");
			else
				fseek(in,-1L,1);		//不是 "!",文件的当前指针回退
		}
		
		//识别分隔符
		else if(ch==',')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,",");
		else if(ch==';')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,";");
		else if(ch == '(')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,"(");
		else if(ch == ')')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,")");
		else if(ch == '{')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,"{");
		else if(ch == '}')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,"}");
		else if(ch == '[')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,"[");
		else if(ch == ']')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,"]");
		else if(ch == '.')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,".");
		else if(ch == '\"')
			fprintf(out,"(%d,  \"%s\"\t)\n",5," \" ");
		else if(ch == '#')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,"#");
		else if(ch == '-')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,"-");
		else if(ch == '\\')
			fprintf(out,"(%d,  \"%s\"\t)\n",5,"\\");
		else fprintf(out,"\n无法识别的字符 %c\n",ch);
	} 
}

⌨️ 快捷键说明

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