if(pipei(chuan,fenxi,yc,b))
return1;
else
return0;
}
else
return0;
}
else
{
if(ch=='#')
{
if(ch==a)
{
cout<<"分析成功"<return1;
}
else
return0;
}
else
if(ch=='@')
{
fenxi.erase(fenxi.length()-1,1);
if(pipei(chuan,fenxi,yc,b))
return1;
else
return0;
}
else
{
i=NODE.find(ch);
if(a=='#')
{
x=ENODE.find('@');
if(x-1)
j=ENODE.length()-1;
else
j=ENODE.length();
}
else
j=ENODE.find(a);
if(yc[i][j].length())
{
cout<"<fenxi.erase(fenxi.length()-1,1);
for(k=yc[i][j].length()-1;k>-1;k--)
if(yc[i][j][k]!
='@')
fenxi+=yc[i][j][k];
if(pipei(chuan,fenxi,yc,b))
return1;
else
return0;
}
else
return0;
}
}
}
voidmain()
{
edge*n;
stringstr,(*yc)[50];
inti,j,k;
boolflag=0;
cout<<"请输入上下文无关文法的总规则数:
"<cin>>SUM;
cout<<"请输入具体规则(格式:
左部右部,@为空):
"<n=newedge[SUM];
for(i=0;ifor(j=0;j{
str=n[i].getrg();
if(NODE.find(str[j])>NODE.length()&&ENODE.find(str[j])>ENODE.length())
ENODE+=str[j];
}
//计算first集合
for(i=0;i{
first(n[i],n,i);
}
//outfu(10,"~*~");cout<for(i=0;iif(n[i].getfirst().find("@"){
if(NODE.find(n[i].getro()){
for(k=1;k{
if(NODE.find(n[i].getrg()[k]){
for(j=0;j{
if(n[i].getrg()[k]==n[j].getlf()[0])
{
n[i].newfirst(n[j].getfirst());
break;
}
}
if(n[j].getfirst().find("@")>n[j].getfirst().length())
{
n[i].delfirst();
break;
}
}
}
}
}
//计算follow集合
for(k=0;k{
for(i=0;i{
if(n[i].getlf()==n[0].getlf())
n[i].newfollow("#");
follow(n[i],n,i);
}
for(i=0;i{
for(j=0;jif(n[j].getrg().find(n[i].getlf())==n[j].getrlen()-1)
n[i].newfollow(n[j].getfollow());
}
}
//计算select集合
for(i=0;i{
select(n[i],n);
}
for(i=0;i{
str.erase();
for(j=0;jif(n[j].getlf()[0]==NODE[i])
{
if(!
str.length())
str=n[j].getselect();
else
{
for(k=0;kif(str.find(n[j].getselect()[k]){
flag=1;
break;
}
}
}
}
//输出
cout<outfu(SUM,"");
cout<<"First";
outfu(SUM,"");
cout<<"Follow"<outfu(5+SUM,"-*-");
cout<for(i=0;i{
for(j=0;jif(NODE[i]==n[j].getlf()[0])
{
outfu(3,"");
cout<outfu(SUM+4,"");
out(n[j].getfirst());
outfu(SUM+4-2*n[j].getfirst().length(),"");
out(n[j].getfollow());
cout<break;
}
}
outfu(5+SUM,"-*-");
cout<";
if(flag)
{
cout<<"该文法不是LL
(1)文法!
"<return;
}
else
{
cout<<"该文法是LL
(1)文法!
"<}
//输出预测分析表
cout<"<yc=newstring[NODE.length()][50];
outgraph(n,yc);
stringchuan,fenxi,fchuan;
cout<";
cin>>chuan;
fchuan=chuan;
fenxi="#";
fenxi+=NODE[0];
i=0;
cout<"<cout<<"步骤";
outfu(7,"");
cout<<"分析栈";
outfu(10,"");
cout<<"剩余输入串";
outfu(8,"");
cout<<"推导所用产生式或匹配";
if(pipei(chuan,fenxi,yc,i))
cout<"<else
cout<"<}
截屏如下:
三.实验中的问题及心得
这次实验让我更加熟悉了LL
(1)的工作流程以及LL
(1)分析表的构造方法。
以前课堂上搞不懂的算法流程通过实验都能得到进一步的了解。