实验二---求文法中每个非终结符的FIRSTVT集

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验二算符优先文法及FIRSTVT集1.实验目的:掌握算符优先文法FIRSTVT集的求法2.实验内容:求以下文法所有非终结符的FIRSTVT集LEEE+TETTT*FTFF(E)Fdigit3.实验要求:a)文法可以在程序中指定,也可以在程序运行时从键盘输入;b)digit表示终结符;c)在屏幕上输出最终结果;编码:#includeiostream#includestring.husingnamespacestd;#defineMAX100structSTACK{intleft;intright;};intsearch(chart,char*str,intnum){inti;for(i=0;inum;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结束endl;while(cinstr&&strcmp(str,end))//只要str不等end,返回值就不为0;strcmp(constchar*s1,constchar*s2);说明:当s1s2时,返回值0当s1=s2时,返回值=0当s1s2时,返回值0{strcpy(wenfa[flag1++],str);}//------------求出不重复的非终结符集和终结符集------------for(i=0;iflag1;i++)//取非终结符;{if(i==0){feizhongjiefu[flag2++]=wenfa[i][0];//存法产生式的非终结符}else{for(j=0;jflag2;j++){if(wenfa[i][0]==feizhongjiefu[j]){break;}}if(j==flag2){feizhongjiefu[flag2++]=wenfa[i][0];}}}for(i=0;iflag1;i++)//取终结符{intcount=0;for(j=3;jstrlen(wenfa[i]);j++){temp[count++]=wenfa[i][j];}temp[count]='\0';if(strcmp(temp,digit)==0){if(i==0){zhongjiefu[flag3++]='';}else{for(j=0;jflag3;j++){if(zhongjiefu[j]==''){break;}}if(j==flag3){zhongjiefu[flag3++]='';}}}else{for(j=3;jstrlen(wenfa[i]);j++){if(j==3&&(wenfa[i][j]'Z'||wenfa[i][j]'A')&&flag3==0){zhongjiefu[flag3++]=wenfa[i][j];}else{for(k=0;kflag3;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;iflag1;i++){if(wenfa[i][3]'Z'||wenfa[i][3]'A'){intcount=0;for(j=3;jstrlen(wenfa[i]);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;jstrlen(wenfa[i]);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;}elseif(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;iflag1;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;iflag2;i++){coutFIRSTVT(feizhongjiefu[i])={;for(j=0;jflag3-1;j++){if(F[i][j]==true){if(zhongjiefu[j]==''){coutdigit,;}else{coutzhongjiefu[j],;}}}if(zhongjiefu[j]=='')coutdigit;elsecoutzhongjiefu[j];cout}endlendlendl;}return0;}

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功