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

📄 词法分析器.cpp

📁 词法分析器 编译原理 C++ VC6.0
💻 CPP
字号:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

FILE *fp,*fp1;

char word[33][20]={"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"};     //C语言所有关键字

char limit[22]={'(',')','[',']','.','!',',','&',
                   '*','/','%','+','-','<','>','=',';','{','}','#','_','\''}; 
bool IsDigit(char ch){
	if(ch>='0'&&ch<='9')
		return true;
	return false;
}

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

char Getchar(FILE *fp){
	char ch;
	ch=fgetc(fp);
	return ch;
}

bool getBC(char ch){
	if(ch==' ')
		return false;
	return true;
	
}

void print(char *limit,char ch){
	int i,code=-1;
	for(i=0;i<22;i++)
		if(limit[i]==ch){
			code=i;
			break;
			}
	printf("%c\t%d\n",ch,code);
}

int Reserve(char str[],FILE *fp){
	char ch;
	char temp[20];
	int i=0;
	int number=0;
	ch=fgetc(fp);
	while(!feof(fp)){
		while(ch!='\n')
		{
			temp[i]=ch;
			i++;
			ch=fgetc(fp);
		}
		temp[i]='\0';
		number++;
		if(strcmp(temp,str)==0)
			return number;						//返回关键字在文件中的位置
	}
	return 0;
}

void manage(FILE *fp){
	int code=-1,i;
	int number=0;
	char strToken[20];
	char ch,temp;
	printf("符号\t位置\n");
	while(!feof(fp)){
		for(i=0;i<20;i++)
			strToken[i]='\0';
		ch=Getchar(fp);
		if(getBC(ch)){
			if(IsLetter(ch)){
				number=0;
				while(IsLetter(ch)||IsDigit(ch)){
					strToken[number]=ch;
					number++;
					ch=Getchar(fp);
				}
				fseek(fp,-1L,1);
				strToken[number]='\0';
				for(int i=0;i<32;i++){
					if(strcmp(word[i],strToken)==0){
						code=i;
						break;
					}
				}
				if(code==-1){
					for(i=0;i<number;i++)
						printf("%c",strToken[i]);
					printf("\n");
					for(int i=0;i<20;i++)
						strToken[i]='\0';
				}
				else{
					for(i=0;i<number;i++)
						printf("%c",strToken[i]);
					printf("\t%d\n",code);
					code=-1;
					for(i=0;i<20;i++)
						strToken[i]='\0';
				}
			}
			else if(IsDigit(ch)){
				number=0;
				while(IsDigit(ch)){
					strToken[number]=ch;
					number++;
					ch=Getchar(fp);
				}
				fseek(fp,-1L,1);
				strToken[number]='\0';
				for(int j=0;j<number;j++)
						printf("%c",strToken[j]);
				printf("\n");
				for(int i=0;i<20;i++)
						strToken[i]='\0';
			}
			else if(ch=='<'){
				temp=ch;
				ch=Getchar(fp);
				if(ch=='=')
					printf("<=\n");
				else{
					fseek(fp,-1L,1);
					ch=Getchar(fp);
					print(limit,temp);
				}
			}
			else if(ch=='>'){
				temp=ch;
				ch=Getchar(fp);
				if(ch=='=')
					printf(">=\n");
				else{
					fseek(fp,-1L,1);
					print(limit,temp);
				}
			}
			
			else if(ch=='+'){
				temp=ch;
				ch=Getchar(fp);
				if(ch=='+')
					printf("++\n");
				else{
					fseek(fp,-1L,1);
					print(limit,temp);
				}
			}
			else if(ch=='+'){
				temp=ch;
				ch=Getchar(fp);
				if(ch=='+')
					printf("++\n");
				else{
					fseek(fp,-1L,1);
					print(limit,temp);
				}
			}
			else if(ch=='-'){
				temp=ch;
				ch=Getchar(fp);
				if(ch=='-')
					printf("--\n");
				else{
					fseek(fp,-1L,1);
					print(limit,temp);
				}
			}
			
			else if(ch=='!'){
				temp=ch;
				ch=Getchar(fp);
				if(ch=='=')
					printf("!=\n");
				else{
					fseek(fp,-1L,1);
					print(limit,temp);
				}
			}
			else{
				code=-1;
				for(i=0;i<22;i++)
					if(limit[i]==ch){
						code=i;
						break;
					}
				if(code==-1)
					printf("%c\n",ch);
				else{
					printf("%c\t%d\n",ch,code);
					code=-1;
				}
			}
		}
		else{}
	}
}
				


void input(){
	char ch;
	if((fp=fopen("code1.txt","w"))==NULL)
	{
		printf("文件打开错误!\n");
		exit(0);
	}
	flushall();
	printf("请输入程序:(以'.'号结束)\n");
	do{
		ch=getchar();
		fputc(ch,fp);
	}while(ch!='.');
	fclose(fp);
	if((fp=fopen("code1.txt","r"))==NULL)
	{
		printf("文件打开错误!\n");
		exit(0);
	}
	manage(fp);
}

void fileinput(){
	char filename[20];
	flushall();
	printf("请输入文件名:");
	scanf("%s",filename);
	if((fp=fopen(filename,"r"))==NULL)
	{
		printf("文件打开错误!\n");
		exit(0);
	}
	manage(fp);
}

void choose(){
	printf("1.从键盘输入\t2.从文件读入\n");
	int choice;
	printf("请输入选择:");
	scanf("%d",&choice);
	switch(choice){
	case 1:input();break;
	case 2:fileinput();break;
	default:printf("输入选择错误!\n请重新输入:\n");choose();
	}
}

int main(){
	choose();
	fclose(fp);
	return 0;
}

⌨️ 快捷键说明

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