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

📄 csshi.h

📁 一个LL1分析器,可以输入产生式的条数,以及产生式,开始符号 结束符号 可以输出堆栈的运行情况
💻 H
📖 第 1 页 / 共 2 页
字号:
						{
						csshi::follow[j][3+followchang]=csshi::first[i][2+k];
						csshi::follow[j][2]++;
					//	cout<<"添加一个"<<csshi::follow[i][2+k]<<"给"<<c1<<endl;
						followchang++;
						}
					}
				}
}
void csshi::getfollow()
{
	shuzu Unsz,szR;
//..........................................................
//get the unsame chars's number
	initshuzu(Unsz);
	Unsz=csshi::getunEndch();
	csshi::intunsamech=Unsz.chang;
//..........................................................
	initshuzu(szR);
//把follow集合这个2维数组的每行左段标示对应的元素
	for (int i=1;i<=csshi::intunsamech;i++)
	{
		csshi::follow[i][1]=Unsz.ch[i];
		csshi::follow[i][2]='a';
	}
//主程序算法开始:
	int intunsame;
	int tempf;char tempc;//标记要求谁的follow的集合
	csshi::inputfollow('#',1);
for(int xh=1;xh<=csshi::intunsamech;xh++)
{   intunsame=0;
	while (intunsame<=csshi::intunsamech)
	{
		intunsame++;
		for (int i=1;i<=csshi::intunsamech;i++)
			if (csshi::follow[i][1]==Unsz.ch[intunsame])
			{	//标记要求谁的follow集合
				tempc=csshi::follow[i][1];
				tempf=i;
			}
		for (i=1;i<=csshi::intpro;i++)
			{
				szR=csshi::getrch(i);
				for (int j=2;j<=szR.chang+1;j++)
					if (szR.ch[j]==tempc)//如果找到右部有要求的标记字符
					{
						bool pd=true;
						int followch=j;
						while (pd)
						{
							pd=false;
							if (followch==szR.chang+1)//如果找到的字符是右部最后一个字符
								csshi::copyfollow(csshi::prochar[i][1],tempc);
							else if (szR.ch[followch+1]>'Z' || szR.ch[followch+1]<'A')
								csshi::inputfollow(szR.ch[followch+1],tempf);
							else//后面一个字符是非终结符 
							{
								csshi::firsttofollow(szR.ch[followch+1],tempc);
								if(csshi::infirst(szR.ch[followch+1]))
								{
									pd=true;followch++;
								}
								
							}//else//后面一个字符是非终结符 
						}//while (pd)
					}//if (szR.ch[j]==tempc)//如果找到右部有要求的标记字符
								
			}//for (i=1;i<=csshi::intpro;i++)
	}//while(intunsame<=csshi::intunsamech)
}//for(xh)
}
void csshi::outfollow()
{
	cout<<".............................................................."<<endl;	
	for (int i=1;i<=csshi::intunsamech;i++)
	{
		cout<<csshi::follow[i][1]<<"'s follow:";
		if (csshi::follow[i][2]=='a')
			cout<<"empty";
		else
		{
			bool pd=true;
			char c='a';int j=3;
			while(pd)
			{
				if (c==csshi::follow[i][2])
					pd=false;
				else
				{
				cout<<csshi::follow[i][j];
				j=j+1;
				c++;
				}
			}
		}
   	cout<<endl<<csshi::follow[i][1]<<"'s number:"<<int(csshi::follow[i][2]-'a');
	cout<<endl;
	}
	cout<<".............................................................."<<endl;
}
void csshi::inputselect(char c,int i)
{
int selectchang=int(csshi::select[i][1]-'a');
	bool pd=true;
	for (int j=2;j<=1+selectchang;j++)
		if (csshi::select[i][j]==c)
			pd=false;
	if(pd)
	{
		csshi::select[i][2+selectchang]=c;
		csshi::select[i][1]++;
	//	cout<<"添加一个"<<c<<"给第"<<i<<"条产生式的select集合"<<endl;
	}
}
void csshi::firsttoselect(char cfs,int temps)
{
	int selectchang=int(csshi::select[temps][1]-'a');
	for (int i=1;i<=csshi::intunsamech;i++)
		if (csshi::first[i][1]==cfs)
		{
			int firstchang=int(csshi::first[i][2]-'a');
			for (int k=1;k<=firstchang;k++)
				{
					bool pd=true;
					for (int tempi=2;tempi<=1+selectchang;tempi++)
						if (csshi::select[temps][tempi]==csshi::first[i][2+k])
							pd=false;
					if(pd && csshi::first[i][2+k]!='&')
					{
						csshi::select[temps][2+selectchang]=csshi::first[i][2+k];
						csshi::select[temps][1]++;
					//	cout<<"添加一个"<<csshi::first[i][2+k]<<"给第"<<temps<<"条产生式的select集合"<<endl;
						selectchang++;
					}
				}
		
		}//if (csshi::first[i][1]==cfs)
}
void csshi::followtoselect(char c,int temps)
{
	int selectchang=int(csshi::select[temps][1]-'a');
	for (int i=1;i<=csshi::intunsamech;i++)
		if (csshi::follow[i][1]==c)
		{
			int followchang=int(csshi::follow[i][2]-'a');
			for (int k=1;k<=followchang;k++)
				{
					bool pd=true;
					for (int tempi=2;tempi<=1+selectchang;tempi++)
						if (csshi::select[temps][tempi]==csshi::follow[i][2+k])
							pd=false;
					if(pd)
					{
						csshi::select[temps][2+selectchang]=csshi::follow[i][2+k];
						csshi::select[temps][1]++;
					//	cout<<"添加一个"<<csshi::follow[i][2+k]<<"给"<<c1<<endl;
						selectchang++;
					}//if(pd && csshi::first[i][2+k]!='&')
				}//for (int k=1;k<=firstchang;k++)
		
		}//if (csshi::first[i][1]==cfs)




}
void csshi::getselect()//格式如:E->+F#: a+
/////////////////////////a代表数量,只有一个元素+
{
//初始化select集合的元素个数
	for (int i=1;i<=csshi::intpro;i++)
		csshi::select[i][1]='a';

//主程序开始:
	shuzu szR;
	initshuzu(szR);
	for	(i=1;i<=csshi::intpro;i++)
	{
		szR=csshi::getrch(i);
		bool pd=true;
		int chint=2;
		while(pd)
		{
			pd=false;
			if (szR.ch[chint]=='&')
				csshi::followtoselect(szR.ch[1],i);
			else if (szR.ch[chint]>'Z' || szR.ch[chint]<'A')
				csshi::inputselect(szR.ch[chint],i);
			else
			{
				csshi::firsttoselect(szR.ch[chint],i);
				if (csshi::infirst(szR.ch[chint]))
					if (chint==szR.chang+1)
						csshi::followtoselect(szR.ch[1],i);
					else
					{
						pd=true;chint++;
					}//else
			}//else
		}//while(pd)
	}//for	(i=1;i<=csshi::intpro;i++)
}//end fuction csshi::getselect()
void csshi::outselect()
{
cout<<".............................................................."<<endl;	
	for (int i=1;i<=csshi::intpro;i++)
	{
		cout<<"第"<<i<<"条产生式的select集合是";
		if (csshi::select[i][1]=='a')
			cout<<"empty";
		else
		{
			bool pd=true;
			char c='a';int j=2;
			while(pd)
			{
				if (c==csshi::select[i][1])
					pd=false;
				else
				{
				cout<<csshi::select[i][j];
				j=j+1;
				c++;
				}
			}
		}
   	cout<<endl<<"第"<<i<<"条产生式的select集合元素个数是:"<<int(csshi::select[i][1]-'a');
	cout<<endl;
	}
	cout<<".............................................................."<<endl;
}
void csshi::getchart()
{
//得到非终结字符与终结字符
	int UncharInt,charInt;
	shuzu szU,sz;
	initshuzu(szU);
	initshuzu(sz);
	szU=csshi::getunEndch();
	UncharInt=szU.chang;
	sz=csshi::getEndch();
	charInt=sz.chang;
//...................................................
for (int i=1;i<=UncharInt+1;i++)
	for (int j=1;j<=charInt+2;j++)
		csshi::chart[i][j]=' ';
//初始化行
	for (i=2;i<=UncharInt+1;i++)
		csshi::chart[i][1]=szU.ch[i-1];
//初始化列
	for (int j=2;j<=charInt+1;j++)
		csshi::chart[1][j]=sz.ch[j-1];
	csshi::chart[1][charInt+2]='#';
//...................................................
//主程序开始:
	for (i=1;i<=csshi::intpro;i++)
	{
		int selectchang=int(csshi::select[i][1]-'a');
		for (j=2;j<=selectchang+1;j++)
			for (int k=2;k<=UncharInt+1;k++)
				if (csshi::prochar[i][1]==csshi::chart[k][1])//find the hang
					for (int z=2;z<=charInt+2;z++)
						if (csshi::select[i][j]==csshi::chart[1][z])//find the lie
							csshi::chart[k][z]=char(i+48);
	}

}
void csshi::outchart()
{
//得到非终结字符与终结字符
	int UncharInt,charInt;
	shuzu szU,sz;
	initshuzu(szU);
	initshuzu(sz);
	szU=csshi::getunEndch();
	UncharInt=szU.chang;
	sz=csshi::getEndch();
	charInt=sz.chang;
//..........................................
cout<<"预测分析表:"<<endl;
cout<<"------------------------"<<endl;
for (int i=1;i<=UncharInt+1;i++)
	{	for (int j=1;j<=charInt+2;j++)
			cout<<csshi::chart[i][j]<<"  ";
		cout<<endl;
	}
cout<<"------------------------"<<endl;
}
void csshi::mainfuction()
{
//初始化
	stack st;
	initstack(st);
	push(st,csshi::bgchar);
	cout<<"please input the string,end it by input '#':";
	bool pd=true;int x=1;
	while (pd)
	{
		cin>>csshi::string[x];
		if (string[x]=='#')
			pd=false;
		else
			x++;
	}
	cout<<"序列号"<<"          "<<"字符栈"<<"           "<<"剩余栈"<<"          "<<"动作"<<endl;
//主程序
	int i=1;//标记第几行
	pd=true;//判断是否循环结束
	int otherint=1;//标记去掉几个字符
	int stackint=1;//标示去掉了几个分析栈中的元素
	while (pd)
	{
		if (i<10)
			cout<<"  "<<i<<"             ";
		else
			cout<<" "<<i<<"             ";
		//分析栈
		stackint=st.chang;
		for (int sti=1;sti<=st.chang+1;sti++)
			cout<<st.ch[sti];
		for (int temp=1;temp<=16-st.chang;temp++)
			cout<<" ";
		//字符串栈
		int str=otherint;bool pd1=true;int temp1=0;
		while (pd1)
		{
			cout<<csshi::string[str];
			if (csshi::string[str]=='#')
				pd1=false;
			str++;
			temp1++;
		}
		for(temp=1;temp<=17-temp1;temp++)
			cout<<" ";
//...........................................................
		if(st.ch[st.chang+1]==csshi::string[otherint])
			if (st.ch[st.chang+1]=='#')
			{	cout<<"接受"<<endl;
				pd=false;
			}
			else
			{	
				pop(st);
				otherint++;
				cout<<"匹配"<<endl;
			}
		else
		{
		//从select集合中找对应的产生式
		int UncharInt,charInt;
		shuzu szU,sz;
		initshuzu(szU);
		initshuzu(sz);
		szU=csshi::getunEndch();
		UncharInt=szU.chang;
		sz=csshi::getEndch();
		charInt=sz.chang;
		bool pd2=false;
		char c=st.ch[stackint+1];
		for (int x1=2;x1<=UncharInt+1;x1++)
			for (int x2=2;x2<=charInt+2;x2++)
				if (c==csshi::chart[x1][1] && csshi::string[otherint]==csshi::chart[1][x2])
					{
					pd2=true;
					if (csshi::chart[x1][x2]==' ')
						{
						cout<<"报错"<<endl;
						pd=false;
						}
					else
						{

						cout<<csshi::chart[x1][x2]<<endl;
						pop(st);
						int chanshengshi=int(csshi::chart[x1][x2])-48;
						shuzu szR;
						initshuzu(szR);
						szR=csshi::getrch(chanshengshi);
						for (int pushint=szR.chang+1;pushint>1;pushint--)
							push(st,szR.ch[pushint]);
						}
					}//if
		if (!pd2)
			{
			cout<<"报错"<<endl;
			pd=false;
			}
		}
		i++;
	}
}

⌨️ 快捷键说明

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