欢迎来到天天文库
浏览记录
ID:55710031
大小:16.50 KB
页数:7页
时间:2020-05-26
《正规文法转换成正规式(C++).doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#include#include#include#includeusingnamespacestd;multimapproduction;setunFinal;setfinal;charS;voidInput(){charch;stringstr;cout<<"非终结符(以0结尾):";while(cin>>ch&&ch!='0')unFinal.insert(ch);cout<<"终结符(以F结尾):";while(cin>>ch&&ch!=
2、'F')final.insert(ch);cout<<"开始符:";cin>>S;cout<<"产生式(以00结尾):"<>ch>>str&&ch!='0')production.insert(make_pair(ch,str));}stringsolve(charch){stringstr("(");stringre("");stringch_1[8];stringstr1[8],str2[8];intlen,i,j,flag=10,strlen,sslen,lslen,num=0;multimap::iteratoriter
3、=production.find(ch);intn=production.count(ch);for(i=0;isecond.size();str1[i].append(iter->second);str2[i]=str1[i];str2[i].replace(1,1,"
4、");ch_1[i]=iter->second.substr(len-1);}for(i=0;i="A"&&ch_1[i]<="Z"){for(j=i+1;j5、r.push_back('(');str.append(str2[i].substr(0));str.append(str2[j].substr(0));for(;j6、');}else{str.append(str1[i].substr(0));if(n>=2)//相同左部产生式个7、数大于等于2个时,才输出"8、"str.push_back('9、');}}sslen=str.size();for(i=0;i10、11、ch_1[i]>"Z")num++;if(num>=2)str.push_back('(');for(i=0;i12、13、ch_1[i]>"Z"){str.append(ch_1[i]);if(i!=n-1)str.push_back('14、');}if(num>=2)str.push_back(')');lslen=str.size();for(inti=0;i15、;i++){if(unFinal.find(str[i])!=unFinal.end())//str[i]是非终结符if(str[i]!=ch){//是其他非终结符re=solve(str[i]);str.replace(i,1,re);i+=re.size()-1;//下标后移Notice!}else{//此非终结符就是chstr.replace(i,1,"*");str.replace(sslen-1,lslen-sslen+1,str.substr(sslen));}}str.push_back(')');returnstr;}intmain(){Input();cout<16、dl<<"输出正规式:";stringstr=solve(S);cout<
5、r.push_back('(');str.append(str2[i].substr(0));str.append(str2[j].substr(0));for(;j6、');}else{str.append(str1[i].substr(0));if(n>=2)//相同左部产生式个7、数大于等于2个时,才输出"8、"str.push_back('9、');}}sslen=str.size();for(i=0;i10、11、ch_1[i]>"Z")num++;if(num>=2)str.push_back('(');for(i=0;i12、13、ch_1[i]>"Z"){str.append(ch_1[i]);if(i!=n-1)str.push_back('14、');}if(num>=2)str.push_back(')');lslen=str.size();for(inti=0;i15、;i++){if(unFinal.find(str[i])!=unFinal.end())//str[i]是非终结符if(str[i]!=ch){//是其他非终结符re=solve(str[i]);str.replace(i,1,re);i+=re.size()-1;//下标后移Notice!}else{//此非终结符就是chstr.replace(i,1,"*");str.replace(sslen-1,lslen-sslen+1,str.substr(sslen));}}str.push_back(')');returnstr;}intmain(){Input();cout<16、dl<<"输出正规式:";stringstr=solve(S);cout<
6、');}else{str.append(str1[i].substr(0));if(n>=2)//相同左部产生式个
7、数大于等于2个时,才输出"
8、"str.push_back('
9、');}}sslen=str.size();for(i=0;i10、11、ch_1[i]>"Z")num++;if(num>=2)str.push_back('(');for(i=0;i12、13、ch_1[i]>"Z"){str.append(ch_1[i]);if(i!=n-1)str.push_back('14、');}if(num>=2)str.push_back(')');lslen=str.size();for(inti=0;i15、;i++){if(unFinal.find(str[i])!=unFinal.end())//str[i]是非终结符if(str[i]!=ch){//是其他非终结符re=solve(str[i]);str.replace(i,1,re);i+=re.size()-1;//下标后移Notice!}else{//此非终结符就是chstr.replace(i,1,"*");str.replace(sslen-1,lslen-sslen+1,str.substr(sslen));}}str.push_back(')');returnstr;}intmain(){Input();cout<16、dl<<"输出正规式:";stringstr=solve(S);cout<
10、
11、ch_1[i]>"Z")num++;if(num>=2)str.push_back('(');for(i=0;i12、13、ch_1[i]>"Z"){str.append(ch_1[i]);if(i!=n-1)str.push_back('14、');}if(num>=2)str.push_back(')');lslen=str.size();for(inti=0;i15、;i++){if(unFinal.find(str[i])!=unFinal.end())//str[i]是非终结符if(str[i]!=ch){//是其他非终结符re=solve(str[i]);str.replace(i,1,re);i+=re.size()-1;//下标后移Notice!}else{//此非终结符就是chstr.replace(i,1,"*");str.replace(sslen-1,lslen-sslen+1,str.substr(sslen));}}str.push_back(')');returnstr;}intmain(){Input();cout<16、dl<<"输出正规式:";stringstr=solve(S);cout<
12、
13、ch_1[i]>"Z"){str.append(ch_1[i]);if(i!=n-1)str.push_back('
14、');}if(num>=2)str.push_back(')');lslen=str.size();for(inti=0;i15、;i++){if(unFinal.find(str[i])!=unFinal.end())//str[i]是非终结符if(str[i]!=ch){//是其他非终结符re=solve(str[i]);str.replace(i,1,re);i+=re.size()-1;//下标后移Notice!}else{//此非终结符就是chstr.replace(i,1,"*");str.replace(sslen-1,lslen-sslen+1,str.substr(sslen));}}str.push_back(')');returnstr;}intmain(){Input();cout<16、dl<<"输出正规式:";stringstr=solve(S);cout<
15、;i++){if(unFinal.find(str[i])!=unFinal.end())//str[i]是非终结符if(str[i]!=ch){//是其他非终结符re=solve(str[i]);str.replace(i,1,re);i+=re.size()-1;//下标后移Notice!}else{//此非终结符就是chstr.replace(i,1,"*");str.replace(sslen-1,lslen-sslen+1,str.substr(sslen));}}str.push_back(')');returnstr;}intmain(){Input();cout<16、dl<<"输出正规式:";stringstr=solve(S);cout<
16、dl<<"输出正规式:";stringstr=solve(S);cout<
此文档下载收益归作者所有