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

📄 词法分析器.txt

📁 C语言编写的词法分析器
💻 TXT
字号:
// cifa0.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"iostream"
#include"windows.h"
#define MAX 255	
FILE *infile ,*outfile;
char strToken[MAX];//存放构成单词符号的字符串;
char ch;//存放最新读进的源程序字符;
int pos = 0;//strToken数组的指针;
char getChar()//将下一输入字符读入ch,搜索指示器自动迁移一字符;
{		
	ch = fgetc(infile);
	if(ch == EOF)
	{
		fprintf(outfile,"------End------");
	}
	//++pos;
		return (ch);
}
char GetBC()//检查ch中的字符是否为空白,若是,调用GetChar(),直至读入非空字符;
{
	while(ch == ' '||ch =='\n'||ch =='\t'||ch =='\b')
	{
		getChar();
	}
	return (ch);
}
void Concat(char ch)//将ch中的字符连接到strToken之后;
{
	strToken[pos++] = ch; 
} 
bool IsLetter(char ch)
{
	if((ch >='a'&& ch <='z')||(ch >='A'&&ch <='Z'))
		return true;
	else return false;
}
bool IsDigit(char ch)
{
	if(ch >='0'&&ch <='9')
		return true;
	else return false;
}
void Retract()//处理*
{
	--pos;
	ch = ' ';
}
int keyList(char strToken[])
{	//标识符和32个关键字
	if(strcmp(strToken,"auto")==0)
		return 1;
	else if(strcmp(strToken,"double")==0)
		return 2;
	else if(strcmp(strToken,"int")==0)
		return 3;
	else if(strcmp(strToken,"struct")==0)
		return 4;
	else if(strcmp(strToken,"break")==0)
		return 5;
	else if(strcmp(strToken,"else")==0)
		return 6;
	else if(strcmp(strToken,"long")==0)
		return 7;
	else if(strcmp(strToken,"switch")==0)
		return 8;	
	else if(strcmp(strToken,"case")==0)
		return 9;
	else if(strcmp(strToken,"enum")==0)
		return 10;
	else if(strcmp(strToken,"register")==0)
		return 11;
	else if(strcmp(strToken,"typedef")==0)
		return 12;
	else if(strcmp(strToken,"char")==0)
		return 13;
	else if(strcmp(strToken,"extern")==0)
		return 14;
	else if(strcmp(strToken,"return")==0)
		return 15;
	else if(strcmp(strToken,"const")==0)
		return 17;
	else if(strcmp(strToken,"flaot")==0)
		return 18;
	else if(strcmp(strToken,"short")==0)
		return 19;
	else if(strcmp(strToken,"unsigned")==0)
		return 20;
	else if(strcmp(strToken,"continue")==0)
		return 21;
	else if(strcmp(strToken,"for")==0)
		return 22;
	else if(strcmp(strToken,"signed")==0)
		return 23;
	else if(strcmp(strToken,"void")==0)
		return 24;
	else if(strcmp(strToken,"default")==0)
		return 25;
	else if(strcmp(strToken,"goto")==0)
		return 26;
	else if(strcmp(strToken,"do")==0)
		return 29;
	else if(strcmp(strToken,"if")==0)
		return 30;
	else if(strcmp(strToken,"static")==0)
		return 31;
	else if(strcmp(strToken,"while")==0)
		return 32;
	else //标识符
		return 33;										
}
int keyList2(char strToken[])
{//运算符
	if(strcmp(strToken,"+")==0)
		return 34;
	if(strcmp(strToken,"++")==0)
		return 35;
	if(strcmp(strToken,"+=")==0)
		return 36;	
	else if(strcmp(strToken,"-")==0)
		return 37;
	if(strcmp(strToken,"--")==0)
		return 38;
	if(strcmp(strToken,"-=")==0)
		return 39;
	else if(strcmp(strToken,"*")==0)
		return 40;
	else if(strcmp(strToken,"**")==0)
		return 41;
	else if(strcmp(strToken,"*=")==0)
		return 42;
	else if(strcmp(strToken,"/")==0)
		return 43;
	else if(strcmp(strToken,"/=")==0)
		return 44;
	else if(strcmp(strToken,"=")==0)
		return 45;
	else if(strcmp(strToken,"==")==0)
		return 46;
	else if(strcmp(strToken,"!")==0)
		return 47;
	else if(strcmp(strToken,"!=")==0)
		return 48;
	else if(strcmp(strToken,"{")==0)
		return 49;
	else if(strcmp(strToken,"}")==0)
		return 50;
	else if(strcmp(strToken,"(")==0)
		return 51;
	else if(strcmp(strToken,")")==0)
		return 52;
	else if(strcmp(strToken,"[")==0)
		return 53;
	else if(strcmp(strToken,"]")==0)
		return 54;
	else if(strcmp(strToken,">")==0)
		return 55;
	else if(strcmp(strToken,">=")==0)
		return 56;
	else if(strcmp(strToken,"<")==0)
		return 57;
	else if(strcmp(strToken,"<=")==0)
		return 58;
	else if(strcmp(strToken,"&")==0)
		return 59;
	else if(strcmp(strToken,"%")==0)
		return 60;
	else if(strcmp(strToken,"#")==0)
		return 61;
	else if(strcmp(strToken,",")==0)
		return 62;
	else if(strcmp(strToken,";")==0)
		return 63;
	else if(strcmp(strToken,":")==0)
		return 64;
	else if(strcmp(strToken,"::")==0)
		return 65;
	else if(strcmp(strToken,"\"")==0) //" " "
		return 66;
	else if(strcmp(strToken,".")==0) 
		return 67;
	else if(strcmp(strToken,"->")==0) 
		return 68;
	else if(strcmp(strToken,"~")==0) 
		return 69;
	else if(strcmp(strToken,">>")==0) 
		return 70;
	else if(strcmp(strToken,"<<")==0) 
		return 71;
	else if(strcmp(strToken,"^")==0) 
		return 72;
	else if(strcmp(strToken,"|")==0) 
		return 73;
	else if(strcmp(strToken,"||")==0) 
		return 74;
	else 
		return 84;
}
int main(int argc, char* argv[])
{	
	printf("Initializing System...\n");
	Sleep(500);
	printf("Analying The Souce Code...\n ");
	for(int i =0;i<3;i++)
	{
		printf("--");
		Sleep(1000);
	}
	printf("\n");
	printf("Analy Completed.The Result is in .\\outfile.txt\n");
	if((infile=fopen("infile.txt","r"))==NULL)//以读方式打开输入文件;
	{
		printf("Open infile error!\n");
		exit(0);
	}
	if((outfile=fopen("outfile.txt","w"))==NULL)//以写方式打开输出文件;
	{
		printf("open outfile error!\n");
		exit(1);
	}
	getChar();
	GetBC();
	while(ch !=EOF)
	{
//------------------------识别常数---------------------------------------
	
		if(IsDigit(ch))
		{	pos = 0;
			
			while(IsDigit(ch))
			{
				Concat(ch);
				getChar();		
			}
			if(ch =='.')
			{
				Concat(ch);
				getChar();
			}
				
		while(IsDigit(ch))
		{
			Concat(ch);
			getChar();
		}
		if(ch=='E'|| ch=='D')
		{
			Concat(ch);
			getChar();
		if(ch =='+'||ch =='-')
		{
			Concat(ch);
			getChar();
		if(IsDigit(ch))
			{
				Concat(ch);
				getChar();
			}
			
		}
		if(IsDigit(ch))
			{
				Concat(ch);
				getChar();
			}
		while(IsDigit(ch))
			{
				Concat(ch);
				getChar();
			}
		strToken[pos++] ='\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",0,'"',strToken,'"',")");
		}
		else
		{
			strToken[pos++] ='\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",0,'"',strToken,'"',")");
		}	
				GetBC();	
	} 
//-----------------------------end----------------------------------------
//------------------------识别标识符和关键字---------------------------------------	
	if(IsLetter(ch)||ch=='_')
	{	pos = 0;
		
		while(IsLetter(ch)||IsDigit(ch)||ch=='_')
		{
			Concat(ch);
			getChar();
		}
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList(strToken),'"',strToken,'"',")");
		GetBC();
	}	
//----------------------------------end-------------------------------------------
//----------------------------运算符 ------------------------------------
	if(ch=='+')//+,++,+=
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='+')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else if(ch=='=')
		{
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
	if(ch=='-')//-,--,-=,->
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='-')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else if(ch=='=')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else if(ch=='>')//->
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
		
	if(ch=='*')//*,**,*=
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='*')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else if(ch=='=')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
	if(ch=='=')//=,==
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='=')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
	
	if(ch=='!')//!,!=
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='=')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();

	if(ch=='{')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
		getChar();
	}
	GetBC();
	if(ch=='}')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
		getChar();
	}
	GetBC();
	if(ch=='(')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
		getChar();
	}
	GetBC();
	if(ch==')')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
		getChar();
	}
	GetBC();
	if(ch=='[')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
		getChar();
	}
	GetBC();
	if(ch==']')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
		getChar();
	}
	GetBC();
	if(ch=='>')//>,>=,>>
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='=')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else if(ch=='>')//>>
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
	if(ch=='&')//<,<=,<<
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='=')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else if(ch=='&')//<<
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
	if(ch=='%')//!,!=
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='=')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
	if(ch=='#')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
	getChar();
	}
	GetBC();
	if(ch==',')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
	getChar();
	}
	GetBC();
	if(ch==';')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
	getChar();
	}
	GetBC();
	if(ch==':')//:,::
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch==':')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
	if(ch=='"')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
	getChar();
	}
	GetBC();
	if(ch=='.')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
	getChar();
	}
	GetBC();
	if(ch=='~')
	{	pos=0;
		Concat(ch);
		strToken[pos++] = '\0';
		fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
	getChar();
	}
	GetBC();
	if(ch=='|')//!,!=
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='|')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else if(ch=='=')//<<
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");	
		}
	}
	GetBC();
	if(ch=='?')//?=
	{
		pos=0;
		Concat(ch);
		getChar();
		if(ch=='=')
		{	
			Concat(ch);
			strToken[pos++] = '\0';
			fprintf(outfile,"%s%d,%c%s%c%s\n","(",keyList2(strToken),'"',strToken,'"',")");
			getChar();
		}
		else 
		{
			strToken[pos++] = '\0';
			fprintf(outfile,"%s,%c%c\n","ERROR",":",ch);	
		}
	}
	GetBC();
//-----------------------------------end-----------------------------------------
}
	fclose(infile);
	fclose(outfile);
	return 0;
}

⌨️ 快捷键说明

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