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

📄 word.h

📁 实现编译原理中LL1文法分析功能 编译原理课程中
💻 H
字号:
class Buf{
	public:
		char buffer[1000];
		int pr;//指针
		int lenth;
		Buf(){
			pr=0;
			lenth=0;
		}
};

char * readcf(char *name,char *buf){//对待分析程序段进行预处理
	FILE *program=fopen(name,"r");//打开待分析的程序代码文件
	int i=0;
	char ch;
	ch=fgetc(program);
	while(ch!=EOF){//将回车、制表、连续空格替换为单个空格
		if(isspace(ch)){
			buf[i++]=' ';
			while(isspace(ch))
				ch=fgetc(program);
		}//if
		buf[i++]=ch;
		ch=fgetc(program);
	}//while
	buf[i]='\0';
	fclose(program);
	return buf;
}

bool Ispunc(char ch){//判断是否为界符或运算符
	char punc[15]="+*-/><=(); ";
	for(int i=0;i<11;i++){
		if(ch==punc[i])
			return(1);
	}
	return(0);
}

bool Notlegal(char ch){//判断字符是否非法
	return !(isalnum(ch)||Ispunc(ch));
}

int nextword(Buf &buf,char *temp){//读取下一个单词,返回f的值,temp为缓存空间
	char ch;
	int f;//0字母串,1十进制,2八进制,3十六进制,4界符,9非法字符
	int i=0;
	ch=buf.buffer[buf.pr++];//读下一个待分析字符
	if(Notlegal(ch))//非法字符返回9
		f=9;
	else if(isalpha(ch)){//如果字符为字母,将连续将其后的字母或数字都读入缓存,作为字母串
		while(isalnum(ch)){
			temp[i++]=ch;
			ch=buf.buffer[buf.pr++];
		}//while
		temp[i]='\0';//在尾加上结束符
		buf.pr--;
		f=0;//并返回0
	}//if
	else if(isdigit(ch)){//以下判断首字符为数字的情况
			if(ch!='0'){//十进制
				while(isdigit(ch)){
					temp[i++]=ch;
					ch=buf.buffer[buf.pr++];
				}
			temp[i]='\0';
			buf.pr--;
			f=1;//返回1
			}
			else{
				ch=buf.buffer[buf.pr++];//首字符为0
				if(isdigit(ch)&&ch!='0'){//八进制
					while(isdigit(ch)){
					if(ch=='8'||ch=='9') continue;//如果八进制数中出现8、9则跳过
					temp[i++]=ch;
					ch=buf.buffer[buf.pr++];
					}
					temp[i]='\0';
					buf.pr--;
					f=2;//返回2
				}
				else if(ch=='x'){//十六进制
					ch=buf.buffer[buf.pr++];
					while(isxdigit(ch)){
					temp[i++]=ch;
					ch=buf.buffer[buf.pr++];
					}
					temp[i]='\0';
					buf.pr--;
					f=3;//返回3				
				}
				else {//为0
					temp[i++]='0';
					temp[i]='\0';
					f=1;
					buf.pr--;
				}//返回1
			} 				
	}//结束对数字的判断
	else if(Ispunc(ch)){
			temp[i++]=ch;
			temp[i]='\0';
			f=4;
		}
	return f;
}

void word(char *expname,char exp[N][N]){
	Buf buf;
	int f;//接收单词种类
	int i=0,j=0;//作exp的下标
	char temp[N]={'\0'};
	readcf(expname,buf.buffer);
	buf.lenth=strlen(buf.buffer);//文件的长度
	while(buf.pr<buf.lenth){//当不文件为结束时循环扫描
		f=nextword(buf,temp);
		if(f>=0&&f<=3)
			exp[i][j++]='i';
		else if(f=4){
			if(*temp!=' '&&*temp!=';')
				exp[i][j++]=*temp;				
			if(*temp==';'){
				exp[i][j++]='#';
				i++;
				j=0;
			}
		}//if		
	}//while		
}

⌨️ 快捷键说明

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