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

📄 词法分析器.zip.txt

📁 类c语言词法分析器,文件能够识别c语言关键字和单词(1.0版)
💻 TXT
📖 第 1 页 / 共 2 页
字号:
#include "Aow_head.h"
keyword_file kwf;//定义为全局变量
int  keyword_file::Getin()//写文件
{
	int i;
	ofstream outfile;
	outfile.open("preventword.dat",ios::binary);
	for(i = 0;i < 10;i++)
	{
		switch(i)
		{
		case 0:
			strcpy(word,"main");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 1:
			strcpy(word,"int");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 2:
			strcpy(word,"float");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 3:
			strcpy(word,"printf");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 4:
			strcpy(word,"scanf");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 5:
			strcpy(word,"%d");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 6:
			strcpy(word,"%f");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 7:
			strcpy(word,"return");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 8:
			strcpy(word,"for");
			outfile.write((char *)this,sizeof(*this));
			break;
		case 9:
			strcpy(word,"while");
			outfile.write((char *)this,sizeof(*this));
			break;
		}
		
	}
	outfile.close();
	return 0;
		
}

int keyword_file::Find(char singword[])//查找关键字
{
	ifstream infile;
	infile.open("preventword.dat",ios::binary);
	if(!infile)
		cout << "文件不能打开!"
		     << endl;
	else
	{
		infile.read((char *)this,sizeof(*this));
		while(!infile.eof())
		{
			if(!strcmp(word,singword))
				return 1;
			infile.read((char *)this,sizeof(*this));
		}
		return 0;
	}
	return 0;
}


int Analysis::Search_word(char cpp[])//cpp[]保存一行字母
{
	ofstream k_outfile;
	ofstream s_outfile;
	ofstream d_outfile;
	ofstream b_outfile;	
	ofstream o_outfile;
	ifstream s_infile;
	char word[51];
	char ch;
	int i,j;
	float floa; //a,b,c是将数字变为二进制的中间变量
	int intb;
	float floc;
	int len_i,len_j; // len_i 为整数部分二进制的长度,len_j为小数部分二进制长度
	
	i = 0;
	len_i = len_j = 0;
	ch = 'a';
	P_N_sign = PLUS;
	while(cpp[i] != '\0' && cpp[i] != '#')
	{
		ch = cpp[i];
		while(ch == ' ' || ch == 10 || ch == 9)
		{
			i++;
			ch = cpp[i];
		}
		if(ch >= 'a'&&ch <= 'z'||ch >='A'&&ch <= 'Z'||ch >= '0'&& ch <= '9' || ch == '_'|| ch == '%')
		{
			if(ch >= 'a'&&ch <= 'z' || ch == '%')
			{
				j = 0;
				do
				{
					word[j++] = ch;
					i++;
					ch = cpp[i];
				}while(ch >= 'a'&&ch <= 'z'||ch >='A'&&ch <= 'Z'||ch >= '0'&& ch <= '9' || ch == '_');
				word[j] = '\0';
				if(kwf.Find(word))
				{
					k_outfile.open("savek.dat",ios::binary|ios::app);
					if(!strcmp(word,"main")) k->sign = 1;
					else 
						if(!strcmp(word,"int")) k->sign = 2;
						else
							if(!strcmp(word,"float")) k->sign = 3;
								else
									if(!strcmp(word,"printf")) k->sign = 4;
									else
										if(!strcmp(word,"scanf")) k->sign = 5;
										else 
											if(!strcmp(word,"%d")) k->sign = 6;
											else 
												if(!strcmp(word,"%f")) k->sign = 7;
												else if(!strcmp(word,"return")) k->sign = 8;
													else if(!strcmp(word,"for")) k->sign = 9;
														else if(!strcmp(word,"while")) k->sign = 10;
					strcpy(k->kword,word);
					k_outfile.write((char *)k,sizeof(*k));
					k_outfile.close();
				}
				else
				{
					int writed = 0;
					s_infile.open("saves.dat",ios::binary);
					s_infile.seekg(0,ios::beg);//以下为判空
					int len_a = s_infile.tellg();
					s_infile.seekg(0,ios::end);
					int len_b = s_infile.tellg();
					if(len_a == len_b)
						{	
							s_infile.close();
							s_outfile.open("saves.dat",ios::binary|ios::app);
							s->sign = 11;
							strcpy(s->sword,word);
							s->sloc += 1;
							s_outfile.write((char *)s,sizeof(*s));
							s_outfile.close();
						}
					else
					{
						s_infile.seekg(0,ios::beg);
						s_infile.read((char *)s,sizeof(*s));
						while(!s_infile.eof())
						{
							if(!strcmp(word,s->sword)) { writed = 1;break; }
							s_infile.read((char *)s,sizeof(*s));
						}
						s_infile.close();
						if(!writed)
						{
							s_outfile.open("saves.dat",ios::binary|ios::app);
							s->sign = 11;
							strcpy(s->sword,word);
							s->sloc += 1;
							s_outfile.write((char *)s,sizeof(*s));
							s_outfile.close();
						}
					}
				}//end of else
			}//end of if(ch >= 'a'&&ch <= 'z')
			else
			{
				if(ch >='A'&&ch <= 'Z' || ch == '_')
				{
					j = 0;
					do
					{
						word[j++] = ch;
						i++;
						ch = cpp[i];
					}while(ch >= 'a'&&ch <= 'z'||ch >='A'&&ch <= 'Z'||ch >= '0'&& ch <= '9' || ch == '_');
					word[j] = '\0';
					int writed = 0;
					s_infile.open("saves.dat",ios::binary);
					s_infile.read((char *)s,sizeof(*s));
					while(!s_infile.eof())
						{
							if(!strcmp(word,s->sword)) { writed = 1;break; }
							s_infile.read((char *)s,sizeof(*s));
						}
						s_infile.close();
						if(!writed)
						{
							s_outfile.open("saves.dat",ios::binary|ios::app);
							s->sign = 11;
							strcpy(s->sword,word);
							s->sloc += 1;
							s_outfile.write((char *)s,sizeof(*s));
							s_outfile.close();
						}//end if(!writed)
					}//end of if(ch >='A'&&ch <= 'Z' || ch == '_')
				else//数字
					{
Digital:				 j = 0;
						do
						{
							word[j++] = ch;
							i++;
							ch = cpp[i];
						}while(ch >= '0'&&ch <= '9' || ch == '.');
						word[j] = '\0';
						d->data = P_N_sign * Change_to_float(word);
						d->sign = 12;
						floa = d->data;	//将数字变为二进制
						if(floa < 0) {d->flag = 1; floa *= -1;}
						else d->flag = 0;
						intb = (int)floa;
						floc = floa - intb;
						if(intb == 0 || intb == 1)
							d->int_Binary[len_i++] = intb;
						else
						{
							while(intb != 0 && intb != 1)
							{
								d->int_Binary[len_i++] = (int)(intb%2);
								intb = intb/2;
								if(len_i > 16) {cout << "数字益处!"; exit(0);}
							}
							if(intb == 1) d->int_Binary[len_i++] = 1;
						}	//end else
						len_i--;
						d->len_int = len_i;
						while(len_j < 16 && floc != 1)	//将小数部分变为二进制
						 {
							 floc *= 2;
							 d->flo_Binary[len_j++] = (int)floc;
							 floc = floc - d->flo_Binary[len_j-1];
						 }
						d->len_flo = len_j;
						d_outfile.open("saved.dat",ios::binary|ios::app);
						d_outfile.write((char *)d,sizeof(*d));
						d_outfile.close();
					}//end of else
				}//end of else
		}//end of if(ch >= 'a'&&ch <= 'z'||ch >='A'&&ch <= 'Z'||ch >= '0'&& ch <= '9' || ch == '_')
		else//运算符与界符
		{
			if(ch == '+'||ch == '-' ||ch == '*' ||ch == '/' ||ch == '>' || ch == '<' ||
				ch == '=')
			{
				j = 0;
				do
				{
					word[j++] = ch;
					i++;
					ch = cpp[i];
				}while(ch == '=');
				word[j] = '\0';
				if(!strcmp(word,"+")) 
				{
					int plus_loc = i-1;//negative location 判断‘+’是不是正号
					plus_loc -= 1;
					while(cpp[plus_loc] == ' ')
						plus_loc--;
						if(cpp[plus_loc] == '=' || cpp[plus_loc] == '(')
						{
							P_N_sign = PLUS;
							ch = cpp[i];
							while(ch == ' '||ch == 9|| ch == 10)
							{
								i++;
								ch = cpp[i];
							}
							goto Digital;//假定'+'号后面就是数字(待改进)
						}
						else
							o->sign = 13;
				}
					
				else
					if(!strcmp(word,"-"))
					{
						int neg_loc = i-1;//negative location 判断‘-’是不是负号
						neg_loc -= 1;
						while(cpp[neg_loc] == ' ')
							neg_loc--;
						if(cpp[neg_loc] == '=' || cpp[neg_loc] == '(')
						{
							P_N_sign = NEGATIVE;
							ch = cpp[i];
							while(ch == ' '||ch == 9|| ch == 10)
							{
								i++;
								ch = cpp[i];
							}
							goto Digital;//假定'-'号后面就是数字(待改进)
						}
						else
							o->sign = 14;
					}
					else if(!strcmp(word,"*")) o->sign = 15;
						else if(!strcmp(word,"/")) o->sign = 16;
							else if(!strcmp(word,">")) o->sign = 17;
								else if(!strcmp(word,"<")) o->sign = 18;
									else if(!strcmp(word,">=")) o->sign = 19;
										else if(!strcmp(word,"<=")) o->sign = 20;
											else if(!strcmp(word,"=")) o->sign = 21;
												else if(!strcmp(word,"==")) o->sign = 22;
				strcpy(o->oword,word);
				o_outfile.open("saveo.dat",ios::binary|ios::app);
				o_outfile.write((char *)o,sizeof(*o));
				o_outfile.close();
			}//end of if
			else 
			{
				switch(ch)
				{
				case ',':
					b->bword = ch;
					b->sign = 27;
					b_outfile.open("saveb.dat",ios::binary|ios::app);
					b_outfile.write((char *)b,sizeof(*b));
					b_outfile.close();
					i++;
					break;
				case ';':
					b->bword = ch;
					b->sign = 28;
					b_outfile.open("saveb.dat",ios::binary|ios::app);
					b_outfile.write((char *)b,sizeof(*b));
					b_outfile.close();
					i++;
					break;
				case '{':

⌨️ 快捷键说明

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