正规文法正规式Word文档格式.docx
- 文档编号:13896365
- 上传时间:2022-10-14
- 格式:DOCX
- 页数:11
- 大小:21.10KB
正规文法正规式Word文档格式.docx
《正规文法正规式Word文档格式.docx》由会员分享,可在线阅读,更多相关《正规文法正规式Word文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
#includeviostream>
usingnamespacestd;
structRule
{
stringleft;
〃规则左部,因为输入的为2型文法,
stringright;
//规则右部
};
structRuleData
vectorvstring>
right;
classGrammar
private:
vector<
Rule>
grammar;
//文法
Rulerule;
〃规则
Dleft;
RuleDataruledata;
public:
Grammar(){}
~Grammar(){}
voidChangeinput(stringinput);
〃输入分析
voidShow();
//
voidDataChange(intC);
//存储结构转换
RuleData>
grammardata;
voidGrammar:
Changelnput(stringinput)〃扫描字符串,遇到'
-'
停止,
{//并跳两格
inthelp1=0;
rule.left・erase();
rule.right.erase();
for(inti=0;
i<
int(input.size());
i++)
if(input[i]=='
)
help1=i;
break;
}
rule.left+=input[i];
if(helpl!
=1)
coutvv"
不符合要求!
!
"
;
exit(O);
helpl=helpl+2;
for(intj=helpl;
jvint(input.size());
j++){
rule.right+=input[j];
grammar.push_back(rule);
DataChange(intC)
inti,j;
if(C==0)//简单->
复杂
intl=0;
grammardata・clear();
ruledata.left.erase();
ruledata.right・clear();
ruledata.left=grammar[0].left;
ruledata.right.push_back(grammar[0].right);
grammardata.push_back(ruledata);
for(i=1;
int(grammar.size());
i++)//
存储转换
for(j=0;
j<
int(grammardata.size());
if(grammar[i].left==grammardata[j].left){
grammardata[j].right.push_back(grammar®
.right);
l=1;
break;
if(l==0)
ruledata.1eft・erase();
ruledata.right・clear();
ruledata.left=grammar[i].left;
ruledata.right.push_back
(grammar[i].right);
l=0;
if(C==1)//复杂->
简单
grammar.clear();
for(i=0;
int(grammardata.size());
rule.left.erase();
<
int
rule.left=grammardata[i].left;
for(j=0;
j
(grammardata[i].right.size());
j++)rule.right=grammardata[i].right.at(j);
grammar.push_back(rule);
}voidGrammar:
Show()
输入的文法的正规式为:
vvendl;
for(inti=0;
ivint(grammar.size());
coutvvgrammar[i].leftvv"
="
vvgrammar[i].rightvvendl;
}classGenerateGtoE:
publicGrammar//正规文
法转正规式
{private:
GenerateGtoE(){}~GenerateGtoE(){}voidGenerating();
};
voidGenerateGtoE:
Generating()
DataChange(0);
//STEP1
〃将文法G的所有非终结符形如a1A|a2A|...的候选式
〃归并为(a1|a2|...)A的侯选式,其中a€Vt,A€Vn
stringZ1="
|"
;
stringZ2="
("
stringZ3="
)"
stringZ4="
*"
stringhelpl,help2;
int(grammardata・size());
i++)for(intj=0;
int
(grammardata[i].right・size());
j++)
for(intk=j+1;
k<
k++)
help1.erase();
help2.erase();
intcj=grammardata[i].right・at(j)・length()-1;
intck=grammardata[i].right.at(k)・length()-1;
stringAj=
grammardata[i]・right.at(j)・substr(cj);
stringAk=
grammardata[i]・right.at(k).substr(ck);
if(Aj==Ak&
&
Aj>
="
A"
&
Aj<
Z"
helpl=Z1+
grammardata[i]・right.at(k)・substr(O,ck);
help2=Z2+
grammardata[i].right・at(j)・substr(O,cj);
grammardata[i].right・at(j)=help2+helpl+Z3+Aj;
else
grammardata[i]・right.at(k)・substr(0,ck);
help2=
grammardata[i].right・at(j)・substr(O,cj-1);
for(intt=k;
t<
(grammardata[i].right.size()-1);
t++)
grammardata[i].right・at(t)=grammardata[i].right・at(t+1);
grammardata[i].right・pop_back();
k--;
//DataChange
(1);
//STEP2
//先将规则变A->
(b1|b2...)*(a1|a2|...),再用其替换掉其他规则中的A
//由下而上,逐步替换for(i=grammardata.size()-1;
i>
=0;
i--)
stringhelp;
help.erase();
//A的右部的最后的符号为A
for(intj=0;
intcj=grammardata[i]・right.at(j)・length()-1;
//在A的右部集中含有最后符号为A的右部
if(grammardata[i].right・at(j).find
(grammardata[i].left)
==cj)
//将A->
yA变为A->
y*,help1="
y"
+"
if(help1.length()==0)
help1=grammardata[i].right.at(j).substr
(0,cj);
help1+=Z1+
grammardata[i].right.at(j).substr(0,cj);
}else
if(help2.empty())else
help2+=Z1+grammardata[i].right・at(j);
if(help2.find亿2)==string:
npos&
help2・find(Z3)==string:
help2.find(Z1)!
=string:
npos)
help2=Z2+help2+Z3;
grammardata[i].right・clear();
if(helpl.empty()==false)
help=help1+Z4;
help+=help2;
grammardata[i].right.push_back(help);
for(j=i-1;
j>
j--)
for(intk=0;
(grammardata[j].right.size());
intck=grammardata[j].right.at(k).length()
-1;
==ck)
help=grammardata[j].right.at
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 正规 文法