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

📄 publicfunction.h

📁 利用字符二维数组对文法、First集、Follow集、终结符集、非终结符集等数据进行存储
💻 H
字号:
bool inset(char ch,char *a)
{
	int i=0;
	while(a[i]!='\0')
	{
		if(a[i]==ch)
			return true;
		i++;
	}
	return false;
}
bool add(char X,char A,char first[100][100])
{
	bool change=false;
	int i=0;
	while(first[i][0]!=A)//找到A的位置
		i++;
	if(X<65||X>90&&X!='@')//x为终结符,把该终结符加到A的first集合中
	{
		int j=0;
		while(first[i][j]!=X&&first[i][j]!='\0')
			j++;
		if(first[i][j]=='\0')
		{first[i][j]=X;first[i][j+1]='\0';return true;}
	}
	else if(X>=65&&X<=90)//x为非终结符,把x的first集合加到A的first集合中
	{
		int ps=0;
		while(first[ps][0]!=X)//找到x的位置
			ps++;
		int pa=0;
		while(first[i][pa]!='\0')
			pa++;
		int j=1;
		while(first[ps][j]!='\0')
		{
			if(inset(first[ps][j],first[i])==false&&first[ps][j]!='@')//x中的符号不在A中且不为@
			{
				first[i][pa]=first[ps][j];
				pa++;
                change=true;
			}
			j++;
		}
	}
	return change;
}
bool addfirstfollow(char fir,char fol,char first[100][100],char follow[100][100])
{
	bool change=false;
	int pfi=0;
	while(first[pfi][0]!=fir)
		pfi++;
	int pfo=0;
	while(follow[pfo][0]!=fol)
		pfo++;
	int j=0;
	while(follow[pfo][j]!='\0')
		j++;
	int i=1;
	while(first[pfi][i]!='\0')
	{
		if(inset(first[pfi][i],follow[pfo])==false)
		{
			follow[pfo][j]=first[pfi][i];
			j++;
			change=true;
		}
		i++;
	}
	return change;
}


void closure(int x,int y,int clo[100],int Gnum,char G[100][100])
{
	int i=0;
	clo[i++]=x;
	clo[i++]=y;
	clo[i]=-1;
	if(G[x][y]>=65&&G[x][y]<=90)
	{
		for(int j=0;j<Gnum;j++)
		{
			if(G[j][0]==G[x][y])
			{
				clo[i++]=j;
				clo[i++]=1;
				clo[i]=-1;
			}
		}
	}
}
int  checkconflict(int N,int DA[100][100],char follow[100][100],char G[100][100])
{
	               int next=0;
					while(DA[N][next]!=-1)//检查每一条规则
					{
						int i1=DA[N][next];
						int i2=DA[N][next+1];
						if(G[i1][i2]=='@'||G[i1][i2]=='\0')//是归约项
						{
							int fol=0;
							while(follow[fol][0]!=G[i1][0])//找到G[i1][0]的follow集合
								fol++;
							char followset[100];
							int g1=1;int g2=0;
							while(follow[fol][g1]!='\0')//把follow集合放到一个新的数组
							{
								followset[g2]=follow[fol][g1];
								g1++;
								g2++;
							}
							followset[g2]='\0';
							
							int j1=0;
							while(DA[N][j1]!=-1)//检查每一项是否和规约项冲突
							{
								if(DA[N][j1]==i1&&DA[N][j1+1]==i2){j1=j1+2;continue;}
								int q1=DA[N][j1];
								int q2=DA[N][j1+1];
								if(G[q1][q2]!='@'&&G[q1][q2]!='\0')//移进项和归约项是否冲突
								{
									if(inset(G[q1][q2],followset)==true)
										//cout<<"状态"<<N<<"产生移进归约冲突"<<endl;
                                                                                                                                                                   return 1;
								}
								else if(G[q1][q2]!='@'||G[q1][q2]!='\0')//归约项是否和归约项产生冲突
								{
									int fol2=0;
									while(follow[fol2][0]!=G[q1][0])//找到G[q1][0]的follow集合
									     fol2++;
									int p1=1;
									while(follow[fol2][p1]!='\0')
									{
										if(inset(follow[fol2][p1],followset)==true)
											//cout<<"状态"<<N<<"产生归约归约冲突"<<endl;
                                                                                                                                                                                  return 2;
										p1++;
									}
								}
								j1=j1+2;
							}//while(DA[N][j1]!=-1)
						}//if是归约项
						next=next+2;
					}//while检查每一条规则
return 0;
}


		

⌨️ 快捷键说明

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