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

📄 morphemeanalysis.cpp

📁 一个词法分析程序的实现。内含详细注释。用于实现一个类C语言的词法分析。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	keyWordTable[6].keywordType.coding=28;
	keyWordTable[6].s="do";

	keyWordTable[7].keywordType.main=1;
	keyWordTable[7].keywordType.declaratorFlag=0;
	keyWordTable[7].keywordType.operatorFlag=0;
	keyWordTable[7].keywordType.operatorPriority=0;
	keyWordTable[7].keywordType.coding=29;
	keyWordTable[7].s="return";
}
void init()
{	//初始化操作
	initSymbolTable();
	initKeyWordTable();
	for(int i=0;i<MaxNum;i++)
	{
		constTable[i].value="";
		constTable[i].number=NULL;
		constTable[i].constType=NULL;

	}
	for(int j=0;j<IdentifierNum;j++)
	{
		identifierTable[i].attribute.attributeType.main=NULL;
		identifierTable[i].attribute.attributeType.declaratorFlag=NULL;
		identifierTable[i].attribute.attributeType.operatorFlag=NULL;
		identifierTable[i].attribute.attributeType.operatorPriority=NULL;
		identifierTable[i].attribute.attributeType.coding=NULL;
		identifierTable[i].identifierValue="";
		identifierTable[i].num=NULL;
	}
	fin=fopen("morphemeAnalysis.txt","w");
	{
		if(!fin)
		{
		printf("cannot open this file!\n");
		exit(0);
		}
	}
}

//判断符号是否为关键字,并返回在关键字表中的位置,查找不成功返回-1
int isKeyWord(string str)
{
	for(int i=0;i<KeyWordNum;i++)
	{
		if(keyWordTable[i].s==str)
			return i;
		else
			continue;
	}
	return -1;
}

//生成关键字的属性字
void makeKeyWordAttribute(KeyWord k)
{
	char p[30];
	strcpy(p,k.s.c_str());
	fprintf(fin,"%d  %d  %d  %d  %d  %s\n",
		k.keywordType.main,k.keywordType.declaratorFlag,
		k.keywordType.operatorFlag,k.keywordType.operatorPriority,k.keywordType.coding,p);
}
//生成标识符的属性字
void makeIdentifierAttribute(string str)
{
	char p[30];
	strcpy(p,str.c_str());
	fprintf(fin,"0      0     1   %s\n",p);
}

//生成常量的属性字
void makeConstAttribute(string str)
{
	char p[30];
	strcpy(p,str.c_str());
	fprintf(fin,"0      0     2   %s\n",p);
}
//查造标识符表,查找不到就将此符号填入标识符表
void searchIdentifierTable(string str)
{
	static int count=0;
	for(int i=0;i<IdentifierNum;i++)
	{
		if(identifierTable[i].identifierValue==str)
		{
			return;
		}
		else
			continue;
	}
	identifierTable[count].identifierValue=str;
	identifierTable[count].attribute.attributeType.main=0;
	identifierTable[count].attribute.attributeType.declaratorFlag=0;
	identifierTable[count].attribute.attributeType.operatorFlag=0;
	identifierTable[count].attribute.attributeType.operatorPriority=0;
	identifierTable[count].attribute.attributeType.coding=1;
	identifierTable[count].num=++count;
	return;
}

//查造常量表
void searchConstTable(string str)
{
	//
}

//查符号机内表示对照表
int searchSymbolTable(char c1,char c2)
{
	for(int i=0;i<SymbolNum;i++)
	{
		if(symbolTable[i].attributeValue[0]==c1&&symbolTable[i].attributeValue[1]==c2)
			return i;
		else
			continue;
	}
	return -1;
}

//生成符号属性字
void makeSymbolAttribute(int i)
{
	fprintf(fin,"%d  %d  %d  %d  %d  %s\n",
		symbolTable[i].attributeType.main,symbolTable[i].attributeType.declaratorFlag,
		symbolTable[i].attributeType.operatorFlag,symbolTable[i].attributeType.operatorPriority,
		symbolTable[i].attributeType.coding,symbolTable[i].attributeValue);
}

//获取一个字符
char getch()
{
	char ch=fgetc(fout);
	if(ch!=EOF)
		return ch;
	else
		exit(0);
}
//词法分析,获取一个符号,并将分析结果写入文件"morphemeAnalysis.txt"
int getSymbol()
{
	string str="";
	char ch;
	int i;
	fout=fopen("test.txt","r");
	if(fout)
		ch=fgetc(fout);
	else
		exit(0);
	while(isspace(ch))
	{
		ch=fgetc(fout);
		if(ch==EOF)
			exit(0);
	}
	while(ch!=EOF)
	{
		if(isalpha(ch))
		{	//是关键字或标识符的情况
			while(isalnum(ch))
			{
				str=str+ch;
				ch=getch();
			}
			fseek(fout,-1,SEEK_CUR);
			if((i=isKeyWord(str))!=-1)
				makeKeyWordAttribute(keyWordTable[i]);		//生成属性字
			else
			{
				searchIdentifierTable(str);
				makeIdentifierAttribute(str);
			}
		}
		else if(isdigit(ch))
		{	//获取的符号为数字的情况
			while(isdigit(ch))
			{
				str=str+ch;
				ch=getch();
			}
			fseek(fout,-1,SEEK_CUR);
			searchConstTable(str);
			makeConstAttribute(str);
		}
		else
		{	//获取符号为机内符号的情况
			char ss[2];
			ss[0]=ch;
			ss[1]=getch();
			/*if((ch=fgetc(fout))!=EOF)
				ss[1]=ch;
			else
			{
				fseek(fout,-1,1);
				ss[1]=NULL;
			}*/
				
			int t=searchSymbolTable(ss[0],ss[1]);
			if(t!=-1)
			{
				makeSymbolAttribute(t);
				exit(0);
			}
			else
			{
				ss[1]=NULL;
				t=searchSymbolTable(ss[0],ss[1]);
				if(t!=-1)
				{
					makeSymbolAttribute(t);
					fseek(fout,-1,SEEK_CUR);
				}
				else
				{
					printf("Source file error!\n");
					fprintf(fin,"Source file error");
					exit(0);
				}
			}

		}
		ch=getch();
		while(isspace(ch))
			ch=getch();
		str="";
	}
	return 0;
}


void main()
{
	//读取源文件并显示在控制台
	if(!(fout=fopen("test.txt","r")))
	{
		printf("cannnot open this file!\n");
		exit(0);
	}
	printf("Source file:\n");
	char ch=fgetc(fout);
	while(ch!=EOF)
	{
		putchar(ch);
		ch=fgetc(fout);
	}
	fclose(fout);
	printf("\n");
	//若源文件末尾没有回车,给源文件末尾自动添加一个回车,否则根据以上去字符函数的定义,读不出文件最后一个符号
	fp=fopen("test.txt","a+");
	fseek(fp,-1,SEEK_END);
	if(fgetc(fp)!='\n')
	{
		fseek(fp,0,SEEK_END);
		fprintf(fp,"\n");
	}
	fclose(fp);

	//程序主体部分
	printf("分析结果已输出到文件morphemeAnalysis.txt!\n");
	init();
	getSymbol();
	fclose(fin);
	//printf("%d\n",isKeyWord("void"));测试时使用的代码
}



//fenhuang8888@sina.com
//标题:(学号)

⌨️ 快捷键说明

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