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

📄 define.cpp

📁 define 编译原理词法分析器 八、 实验一:设计SAMPLE语言的词法分析器 检查要求: a) 启动程序后
💻 CPP
字号:
//单词编码定义
#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>


using namespace std;
string map[62]={
				"", "and", "array", "begin", "bool", "call", "case", "char", "constant" ,
				"dim", "do", "else", "end", "false", "for", "if", "input", "integer", "not",
				"of", "or", "output", "procedure", "program", "read", "real", "repeat", "set",
				"stop", "then", "to", "true", "until", "var", "while", "write","","","", "(",
				")", "*", "*/", "+", ",", "-", ".", "..", "/", "/*", ":", ":=", ";", "<", "<=",
				"<>", "=", ">", ">=", "[","]"
				};

typedef struct ID
{
	string cs1;
	 ID   *next;
	 int	count;
	 int att;
}id;
void main()
{
	cout<<"作者:孙立超;计算机科学与技术2班;200530472370;"<<endl;
	char word[20], ch ;
	FILE *fp;
	string cs;
	static	int sign=1;
	static	int i2=1;
	ID *log=new ID;

	ID *p;			//字符常数的链表	
	p=new ID;				
	p->next=NULL;
	
	ID	*q;			//整数的链表
	q=new ID;	
	q->next=NULL;
	
	ID *r;			//标记符的链表
	r=new ID;
	r->next=NULL;

	fp=fopen("测试文件.txt","r");
	if(fp==NULL)
	{
		cout<<"file can not be open\n";
		exit(0);
	}
	while(!feof(fp))
	{
		for(int i=0;i<20;i++)			//打开文件,以空格为分界,将字符串赋值给string类型的cs
		{
			ch=fgetc(fp);
			if(ch!=' '&&ch!='\n')
				word[i]=ch;
			else
			{
			cs.assign(word,i);
				break;
			}	

		}
	//	cout<<"af"<<cs<<"fd"<<endl;

	if(cs.compare("")==0)
		continue;

	for(int i1=1;i1<61;i1++)		//遍历比较map表,
		{	if(cs.compare(map[i1])==0)			//找出关键字和特殊符号的编码,注意对注释内容的处理
			{
				log->cs1=cs;
				log->att=i1;
				log->count=0;
			}
		}

static int count7 =0;							//count7哨兵
if(cs.compare("/*")==0)
{
	count7=1;		
}
if(count7&&cs.compare("*/")!=0)
{
		continue;
}
if(cs.compare("*/")==0)
{	count7=0;}

//查找字符常数,然后放入p链表
	if(cs.find("'")==0)				
	{
		cs=cs.substr(1,cs.length()-2);
		ID *temporary=new ID;
		temporary->cs1.assign(cs);
		temporary->att=38;
		temporary->next=NULL;
		int	count1=1;
		ID *q1, *q3, *q2;
			q1=q;	q3=q;
		while(q1!=NULL)
		{
			if((temporary->cs1).compare(q1->cs1)==0)
			{
				count1=0;
				log->count=q1->count;
				log->cs1=q1->cs1;
				delete(temporary);
				break;
			}
			else
			{
				q2=q1;
				q1=q1->next;
			}
		}
		if(count1)
		{
			temporary->count=sign;
			q2->next=temporary;	
			log->cs1=temporary->cs1;
			log->att=temporary->att;
			log->count=sign;
			sign++;
		}
			//	cout<<log->cs1<<log->count<<log->att<<endl;
			//	q3=q3->next;
			//	while(q3!=NULL)
			//		{	cout<<q3->cs1<<q3->count<<q3->att<<endl;q3=q3->next ;}
	}
	else
	{
		//查找标识符,然后标记.
	int count4=1;									//count4哨兵
	for(int ic=0;ic<cs.length();ic++)	
	{
		if(65>cs[ic]||cs[ic]>122||(90<cs[ic]&&cs[ic]<96))
		{			
			count4=0;break;
		}
	}
		if(count4)
		{	
			int count5=1;
			for(int ib=1;ib<=35;ib++)
				if(cs.compare(map[ib])==0)
					{
						count5=0;break;				//count5哨兵
					}
				if(count5)
				{	
					ID *temporary2=new ID;
					temporary2->cs1.assign(cs);
					temporary2->att=36;
					temporary2->next=NULL;
					int	count6=1;				//count6哨兵
					ID *r1, *r3, *r2;
					r1=r;	r3=r;			
					while(r1!=NULL)
					{
						if((temporary2->cs1).compare(r1->cs1)==0)
						{
							count6=0;
							log->count=r1->count;
							log->cs1=r1->cs1;
							delete(temporary2);
							break;
						}
						else
						{
							r2=r1;
							r1=r1->next;
						}
					}
					if(count6)
					{
						temporary2->count=sign;
						r2->next=temporary2;	
						log->cs1=temporary2->cs1;
						log->att=temporary2->att;
						log->count=sign;
						sign++;
					}	
						//这个位置的cs为所要求的字符串,其他也是如此
				//	cout<<"标识符"<<log->cs1<<log->count<<log->att<<endl;
				//	r3=r3->next;
				//	while(r3!=NULL)
				//	{cout<<r3->cs1<<r3->count<<r3->att<<endl;r3=r3->next ;}
			}
		}

//查找整数,然后标记
int count2=1;
	for(int ia=0;ia<cs.length();ia++)	
		if(47>=cs[ia]||cs[ia]>=58)
			{count2=0;break;}
		
		if(count2)						//筛选出整数的cs
		{
			ID *temporary1=new ID;
			temporary1->cs1.assign(cs);
			temporary1->att=37;
			temporary1->next=NULL;
			int	count3=1;
			ID *p1, *p3, *p2;
				p1=p;	p3=p;	
			while(p1!=NULL)
			{
				if((temporary1->cs1).compare(p1->cs1)==0)
				{
					count3=0;
					log->count=p1->count;
						log->cs1=p1->cs1;
					delete(temporary1);
					break;
				}
				else
				{
					p2=p1;
					p1=p1->next;
				}
			}
			if(count3)
			{
				temporary1->count=sign;
				p2->next=temporary1;	
				log->cs1=temporary1->cs1;
				log->att=temporary1->att;
				log->count=sign;
				sign++;
			}
		//	cout<<log->cs1<<log->count<<log->att<<endl;
		//		p3=p3->next;
		//			while(p3!=NULL)
		//			{	cout<<"整数"<<p3->cs1<<p3->count<<p3->att<<endl;p3=p3->next; }
		}


}
if(log->cs1.compare("")==0)
	continue;
//cout<<"df"<<log->cs1<<"gh"<<endl;
//			cout<<"("<<log->att<<","<<log->count<<")\t";

		if(log->count)
		{
		//	cout<<log->cs1<<endl;
				cout<<"("<<log->att<<","<<log->count<<")\t";
				if(i2%5==0)
					cout<<endl;
				i2++;
		}
		else
		{ 
			if(log->cs1.compare("]")==0)
				cout<<log->cs1;
			cout<<"("<<log->att<<",-)\t";
				if(i2%5==0)
					cout<<endl;
				i2++;
		}

}
cout<<endl;
	
	fclose(fp);
}

⌨️ 快捷键说明

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