试验二求文法中每个非终结符的FIRSTVT集Word格式文档下载.docx
《试验二求文法中每个非终结符的FIRSTVT集Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《试验二求文法中每个非终结符的FIRSTVT集Word格式文档下载.docx(8页珍藏版)》请在冰豆网上搜索。
![试验二求文法中每个非终结符的FIRSTVT集Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2023-2/1/245646dd-999d-48c6-a131-2b406a89aa94/245646dd-999d-48c6-a131-2b406a89aa941.gif)
usingnamespacestd;
#defineMAX100structSTACK
intIeft;
intright;
};
intsearch(chart,char
iritnum)
inti;
for(i=0;
i<
num;
i++)
{
if(t==str[i])
returni;
}
return-1;
intmain()
char
wenfa[MAX][MAX];
//00
个长度为100的产生式charstr[MAX];
zhongjiefu[MAX];
feizhongjiefu[MAX];
chartemp[MAX];
inti,j,k;
int
flag1=0,flag2=0,fIag3
=0;
boolF[MAX][MAX];
STACKS[MAX];
inttop二T;
memset(F,faIse,sizeof(F));
cout«
"
请输入产生式,最后以end结束H«
endl;
whiIe(cin»
str&
&
strcmp(str,"
end"
))//只要str不等end,返回值就不为0:
strcmp(constchar*s1,constchar*s2);
说明:
当s1<
s2时,返回值<
0当s1=s2时,返回值二0当s1>
s2时,返回值>
strcpy(wenfa[fIag1++],str);
zhongjiefu[j])
if(wenfa[i][0]==fei
//求出
不重复的非终结符集和终结符集
for(i二0;
i<
fIag1;
i++)//取非终结符;
if(i==0)
feizhongjiefu[fIag2++]=wenfa[i][0];
//存法产生式的非终结符
1
eIse
for(j=0;
j<
flag2;
j++
)
break;
if(j==flag2)
feizhongjiefu[fIag2++]二wenfa[i][0];
for(i=0;
flag1;
i++
)//取终结符
intcount二0;
for(j-3;
st门en(wenfa[i]);
j++)
temp[count]二,\(T;
if(strcmp(temp,"
digit”)=0)
zhongjiefu[fIag3++]
rr
flag3;
j++)
if(zhongjiefu[j]=='
if(j二二flag3)
zhongjiefu[fIag3++]
V
strien(wenfa[i]);
if(j==3&
(wenfa[i][
j]>
,Z,||wenfa[i][j]<
'
A'
)&
flag3=0)
[i][j]>
Z‘||wenfa[i][jKA'
))
zhongjiefu[fIag3++]=wenfa[i][j];
for(k=0;
k<
fIag3;
k++
if(wenfa[i][j]=zhongjiefu[k])
zhongjiefu[fIag3++]=wenfa[i][j];
//开始
inta,b;
if(wenfa[i][3]>
Z'
|
}|wenfa[i][3]<
rA'
}{
intcount^O;
if(k=fIag3&
(wenfa
for(j二3;
st门en(wenfa[i]);
j++)
temp[count++]二wenfa
temp[count]二;
a二search(wenfa[i][0],feizhongjiefu,fIag2);
b=search('
zhongjiefu,fIag3);
F[a][b]二true;
top++;
S[top].Ieft二a;
S[top].right=b;
a=search(wenfa[i][0],feizhongjiefu,fIag2);
b=search(wenfa[i][3
],zhongjiefu,fIag3);
F[a][b]二true;
S[top].right=b;
intcount=0;
for(j=4;
st门en(wenfa[i]);
temp[count++]=wenfa
temp[count]二,\0,;
if(strcmp(temp,"
))//避免后续所指单词没有内容。
a=search(wenfa[i][0],feizhongjiefu,fIag2);
b二search('
top++;
if(wenfa[i][4]>
Z'
|wenfa[i][4]<
dig
it”)=0)
a=search(wenfa[i][0
],feizhongjiefu,fIag2);
b二search(wenfa[i][4],zhongjiefu,fIag3);
S[top],left=a;
S[top].right=b;
while(top!
=-1)
charB,a;
int;
op].left];
a二zhongjiefu[S[top]
・right];
11=S[top].right;
i++)
intt;
t二search(wenfa[i][0],feizhongjiefu,fIag2);
if(wenfa[订[3]=B&
B!
=wenfa[i][0]&
F[t][
t1]—false)
F[t][t1]=true;
B二feizhongjiefu[S[t
S[top].Ieft=t;
if(zhongjiefu[j]~
S[top].right=t1;
top__;
fIag2;
,FIRSTVT(,,«
feizhongjiefu[i]«
)={n
flag3-1;
j++)
*)
digit,”;
zhongjiefu[j]
«
n,n;
if(zhongjiefu[j]—'
*)
cout〈〈"
digit”;
if(F[i][j]二二true)
}"
endI«
endI«
endI;
return0;