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

📄 cp.cpp

📁 编译器之词法分析
💻 CPP
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct symbolrecord				/*单词编码表*/
{
	char symbol[10];
	int id;
}symbolrecord[100];

struct entrytype				/*符号表*/
{
	char idname[10];
	int address;
	char type[10];
}entrytype[100];

struct tokentype				/*字*/
{
	int id;
	int entry;
}tokentype[100];

struct idname
{
	int id;
	char name[10];
}idname[100];

///////////////////////////////////////////////////////////////////////////
int strlenth;
char str[1000];
char word[10];//识别标识符
int n_word=0;//为标识符计数
int n_idname=0;//为变量计数
int token=0;
int num_symbolrecord=0;
int num_entrytype=0;
int num_tokentype=0;

void scanner();
void sort(char ch);
void recogid(char ch,int token);//lookup char
void recogdig(char ch,int token);//lookup num
void handlecom(char ch,int token);
void recogdel(char ch,int token);
void initsymb();//读入单词编码表
void inittoken();//读入token表
void writeid();//将变量写到文件中
void readme();
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
	readme();
	scanner();
	writeid();
}

void scanner()
{
	char a;
	int n=0;
	initsymb();//读入单词编码表
	inittoken();//读入token表
	

	FILE* pfile;
	pfile=freopen("MY.txt","r",stdin);
	if(pfile==NULL)
		printf("open file failed!\n");
	a=getchar();
	if((int)a==10) a=getchar();
	while(a!=EOF)
	{
		str[n]=a;
		sort(a);
		n++;
		a=getchar();
		if((int)a==10) a=getchar();
	}
	printf("\nread resource file complete!\n");
	fclose(pfile);	
}
//////////////////////////////////////////////////////////////////////////////
void initsymb()
{
	FILE *pfile;

	int i=0;
	char c;
	char strsymb[100]={""};
	char *p=&c;

	pfile=freopen("symbolrecord.txt","r",stdin);
	if(pfile==NULL) printf("failed open token file!\n");
	c=getchar();

	while(c!=EOF)
	{
		if((int)c!=10&&(int)c!=32&&((int)c<48||(int)c>57)&&(int)c>=0)
		{
			strsymb[i]=c;	
			strsymb[i+1]='\0';
			i++;
			c=getchar();
			if((int)c==32)
			{
				strcpy(symbolrecord[num_symbolrecord].symbol,strsymb);
				printf("%s   ",strsymb);
				for(int a=0;a<i;a++)
					strsymb[a]='\0';
				i=0;
			}
		}
		else if((int)c>=48&&(int)c<=57)//number
		{
			strsymb[i]=c;			
			strsymb[i+1]='\0';
			i++;
			c=getchar();
			if((int)c==32||(int)c==10||(int)c==-1)
			{
				int temp=0;
				sscanf(strsymb,"%d",&temp);
				printf("%d\n",temp);
				symbolrecord[num_symbolrecord].id=temp;
				num_symbolrecord++;
				for(int a=0;a<i;a++)
					strsymb[a]='\0';
				i=0;
			}
		}

		else c=getchar();
	
	}
	printf("symbolrecord file read OK!\n");
	fclose(pfile);
}


void inittoken()
{
	FILE* pfile;
	char cc;
	char readnum[100]={""};
	pfile=freopen("token.txt","r",stdin);
	if(pfile==NULL) printf("open token file failed!\n");
	cc=getchar();
	int temp=0;
      int s=0;
	while(cc!=EOF)
	{

  
		if((int)cc!=32&&(int)cc!=10)
		{
			readnum[s]=cc;
			s++;
			cc=getchar();

		}
		else if((int)cc==32)
		{
			sscanf(readnum,"%d",&temp);
			for(int f=0;f<=10;f++)
			{
				readnum[f]='\0';
			}
			tokentype[num_tokentype].id=temp;
			num_tokentype++;
			printf("%d  ",temp);
			s=0;
			cc=getchar();
		}
		else if((int)cc==10)
		{
			sscanf(readnum,"%d",&temp);
			for(int f=0;f<=10;f++)
			{
				readnum[f]='\0';
			}
			tokentype[num_tokentype].entry=temp;
			num_tokentype++;
			printf("%d\n",temp);
			s=0;
			cc=getchar();
		}
		else printf("error!\n");
	
	}
	printf("************open token file OK!************\n");
	printf("****************token字说明****************\n");
	printf("token  功能\n");
	printf("   1   关键字\n");
	printf("   2   变量\n");
	printf("   3   数字\n");
	printf("   4   界符\n");
	printf("   5   运算符\n");
	printf("********************************************\n");
	printf("程序中仅包含对<=号的处理,对于>=和==号处理方式与此相同\n");
	printf("为了简单,程序中没有对界符进行处理\n");
	printf("文档说明:\n");
	printf("MY.txt   源程序文件\n");
	printf("symbolrecord.txt  单词编码表文件\n");
	printf("token.txt  token字文件\n");
	printf("id.txt   变量存储文件\n");
	
	printf("*******************************************\n");


}


void sort(char ch)
{
	if(((int)ch>=65&&(int)ch<=90)||((int)ch>=97&&(int)ch<=122)) /*字母 1*/
	{
		token=1;
		recogid(ch,token); 
	}
	else if((int)ch>=48&&(int)ch<=57)							/*数字 3*/
	{
		token=3;
		recogdig(ch,token);
	}
	else if(ch=='/')											/*除法或注释 4*/
	{
		token=4;
		handlecom(ch,token);
	}
	else if((int)ch<65||(int)ch>90&&(int)ch<97||(int)ch>122)
	{
		token=5;
		recogdel(ch,token);									/*界符 5*/		
	}
}


void recogid(char ch,int token)
{
	char ch2;
	int flag=0;
	char temp[10]={""};
	word[n_word]=ch;
	n_word++;
	ch2=getchar();
	while((int)ch2!=32&&(int)ch2!=10&&ch2!=';'&&((((int)ch2>=65&&(int)ch2<=90)||((int)ch2>=97&&(int)ch2<=122))||((int)ch2>=48&&(int)ch2<=57)))//字母或数字
	{
		word[n_word]=ch2;
		ch2=getchar();
		n_word++;
	}
	for(int i=0;i<=11;i++)
	{
		strcpy(temp,symbolrecord[i].symbol);	//识别关键字
		flag=(strcmp(word,temp));
		if(flag==0)
		{
			printf("%s  %d\n",word,token);	
			break;
		}
	}

	if(flag!=0)
	{
		int flag2=0;
		token=2;
		printf("%s  %d\n",word,token);	//字符变量
		strcpy(temp,word);
		for(int j=0;j<=n_idname;j++)
		{
			if(int flag3=(strcmp(idname[j].name,word))==0) //查找已存在的变量
			{
				//printf("flag3=%d\n",flag3);
				flag2=1;
				break;
			}
		}
		if(flag2==0)
		{ 
			strcpy(idname[n_idname].name,word);//如变量表中没有该变量,存入表中
			idname[n_idname].id=n_idname;
		//	printf("%d  %s\n",idname[n_idname].id,idname[n_idname].name);
			flag=0;
			n_idname++;
		}
	}

	for(i=0;i<=n_word;i++)
	{
		word[i]='\0';
	}

	n_word=0;

	if(ch2==EOF) return;
	else if(ch2==' '||(int)ch2==10)
		return;
	else if(ch2=='/')
		handlecom(ch2,4);
	else recogdel(ch2,5);

	token=0;

}


void recogdig(char ch,int token)
{
		printf("%c  %d\n",ch,token);
		token=0;
}

void handlecom(char ch,int token)
{
		printf("%c  %d\n",ch,token);
			token=0;
}


void recogdel(char ch,int token)
{
	char ch2;
	ch2=getchar();
		printf("%c  %d  ",ch,token);
		switch(ch)
		{	
			case '=':
				{
					printf("15\n");
					if(ch2>=97&&ch2<=122||ch2>=65&&ch2<=90)
					{
						recogid(ch2,1);
					}
					else if(ch2>=48&&ch2<=57)
					{
						recogdig(ch2,3);
					}

					break;
				}
			case '!':{printf("16\n");break;}
			case '&':{printf("17\n");break;}
			case '|':{printf("18\n");break;}
			case '<':
					{
						if(ch2=='=')
						{
							printf("%c   21    (<=)\n",ch2);
							//printf("21\n");
							break;
						}
						else 
						{
							printf("19\n");
							if((int)ch2>=97&&(int)ch2<=122||(int)ch2>=65&&(int)ch2<=90)
								recogid(ch2,1);
							else if((int)ch2>=48&&(int)ch2<=57)
								recogdig(ch2,3);
							else if(ch2=='/')
								handlecom(ch2,4);
							else recogdel(ch2,5);
							break;
						}
					}

			case '>':{printf("20\n");break;}
			case '<=':{printf("21\n");break;}
			case '>=':{printf("22\n");break;}
			case '<>':{printf("23\n");break;}
			case '==':{printf("24\n");break;}
			case '+':
				{
					printf("25\n");
					if(ch2>=97&&ch2<=122||ch2>=65&&ch2<=90)
					{
						recogid(ch2,1);
					}
					break;
				}
			case '*':{printf("26\n");break;}
			case '(':{printf("27\n");break;}
			case ')':{printf("28\n");break;}
			default: {printf("NO such a letter!");}
		}
			token=0;
}

void writeid()
{
	FILE* pfile=freopen("id.txt","w",stdout);

	for(int i=0;i<n_idname;i++)
	{
		printf("%d  %s\n",idname[i].id,idname[i].name);
	}
	if(pfile==NULL)
		printf("failed open id file!\n");
	else
	{
		fclose(pfile);
		printf("Succeed write IDs to files!\n");
	}
}

void readme()
{
	printf("			******************\n");
	printf("			*    词法分析    *\n");
	printf("			*实验学院:马智超*\n");
	printf("			*学号:6030310104*\n");
	printf("			*   2006/03/22   *\n");
	printf("			******************\n");
}

⌨️ 快捷键说明

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