实验二求文法中每个非终结符的FIRSTVT集.docx
《实验二求文法中每个非终结符的FIRSTVT集.docx》由会员分享,可在线阅读,更多相关《实验二求文法中每个非终结符的FIRSTVT集.docx(8页珍藏版)》请在冰豆网上搜索。
实验二求文法中每个非终结符的FIRSTVT集
实验二算符优先文法及FIRSTVT集
1.实验目的:
掌握算符优先文法FIRSTVT集的求法
2.实验内容:
求以下文法所有非终结符的FIRSTVT集
LE
EE+T
ET
TT*F
TF
F(E)
Fdigit
3.实验要求:
a)文法可以在程序中指定,也可以在程序运行时从键盘输入;
b)digit表示终结符;
c)在屏幕上输出最终结果;
编码:
#include
#include
usingnamespacestd;
#defineMAX100
structSTACK
{
intleft;
intright;
};
intsearch(chart,char*str,intnum)
{
inti;
for(i=0;i{
if(t==str[i])
returni;
}
return-1;
}
intmain()
{
charwenfa[MAX][MAX];//00个长度为100的产生式
charstr[MAX];
charzhongjiefu[MAX];
charfeizhongjiefu[MAX];
chartemp[MAX];
inti,j,k;
intflag1=0,flag2=0,flag3=0;
boolF[MAX][MAX];
STACKS[MAX];
inttop=-1;
memset(F,false,sizeof(F));
cout<<"请输入产生式,最后以end结束"<while(cin>>str&&strcmp(str,"end"))//只要str不等end,返回值就不为0;strcmp(constchar*s1,constchar*s2);说明:
当s1s2时,返回值>0
{
strcpy(wenfa[flag1++],str);
}
//------------求出不重复的非终结符集和终结符集------------for(i=0;i{
if(i==0)
{
feizhongjiefu[flag2++]=wenfa[i][0];//存法产生式的非终结符
}
else
{
for(j=0;j{
if(wenfa[i][0]==feizhongjiefu[j])
{
break;
}
}
if(j==flag2)
{
feizhongjiefu[flag2++]=wenfa[i][0];
}
}
}
for(i=0;i{
intcount=0;
for(j=3;j{
temp[count++]=wenfa[i][j];
}
temp[count]='\0';
if(strcmp(temp,"digit")==0)
{
if(i==0)
{
zhongjiefu[flag3++]='';
}
else
{
for(j=0;j{
if(zhongjiefu[j]=='')
{
break;
}
}
if(j==flag3)
{
zhongjiefu[flag3++]='';
}
}
}
else
{
for(j=3;j{
if(j==3&&(wenfa[i][j]>'Z'||wenfa[i][j]<'A')&&flag3==0)
{
zhongjiefu[flag3++]=wenfa[i][j];
}
else
{
for(k=0;k{
if(wenfa[i][j]==zhongjiefu[k])
{
break;
}
}
if(k==flag3&&(wenfa[i][j]>'Z'||wenfa[i][j]<'A'))
{
zhongjiefu[flag3++]=wenfa[i][j];
}
}
}
}
}
//开始
inta,b;
for(i=0;i{
if(wenfa[i][3]>'Z'||wenfa[i][3]<'A')
{
intcount=0;
for(j=3;j{
temp[count++]=wenfa[i][j];
}
temp[count]='\0';
if(strcmp(temp,"digit")==0)
{
a=search(wenfa[i][0],feizhongjiefu,flag2);
b=search('',zhongjiefu,flag3);
F[a][b]=true;
top++;
S[top].left=a;
S[top].right=b;
}
else
{
a=search(wenfa[i][0],feizhongjiefu,flag2);
b=search(wenfa[i][3],zhongjiefu,flag3);
F[a][b]=true;
top++;
S[top].left=a;
S[top].right=b;
}
}
else
{
intcount=0;
for(j=4;j{
temp[count++]=wenfa[i][j];
}
temp[count]='\0';
if(strcmp(temp,""))//避免后续所指单词没有内容。
{
if(strcmp(temp,"digit")==0)
{
a=search(wenfa[i][0],feizhongjiefu,flag2);
b=search('',zhongjiefu,flag3);
F[a][b]=true;
top++;
S[top].left=a;
S[top].right=b;
}
else
if(wenfa[i][4]>'Z'||wenfa[i][4]<'A')
{
a=search(wenfa[i][0],feizhongjiefu,flag2);
b=search(wenfa[i][4],zhongjiefu,flag3);
F[a][b]=true;
top++;
S[top].left=a;
S[top].right=b;
}
}
}
}
while(top!
=-1)
{
charB,a;
intt1;
B=feizhongjiefu[S[top].left];
a=zhongjiefu[S[top].right];
t1=S[top].right;
for(i=0;i{
intt;
t=search(wenfa[i][0],feizhongjiefu,flag2);
if(wenfa[i][3]==B&&B!
=wenfa[i][0]&&F[t][t1]==false)
{
F[t][t1]=true;
S[top].left=t;
S[top].right=t1;
top++;
}
}
top--;
}
for(i=0;i{
cout<<"FIRSTVT("<for(j=0;j{
if(F[i][j]==true)
{
if(zhongjiefu[j]=='')
{
cout<<"digit,";
}
else
{
cout<}
}
}
if(zhongjiefu[j]=='')
cout<<"digit";
else
cout<cout<<"}"<}
return0;
}