




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上实验三:正规文法转正规式实验日期:2012.4.27 教师签字: 成绩: 实验名称: 由正规文法构造正规式实验原理:对于任意一个正规文法GS,都存在一个正规式与其等价,故可以使用RG构造RE运行结果:/* *版权 (c) 2012 Part of Regular Grammar to Regular Expression *文件名zgwftozgs.cpp*Sid: E *AuthorYangxiagnyu *Description 2012/04/25 */ #include<iostream>#include<cstdio>#include
2、<map>#include<string>#include<vector>#include<algorithm>#define _CLR(a) memset(a,0,sizeof(a) #define _lastchar(a) a(a).length()-1using namespace std;class grammartypedef unsigned char uchar;typedef multimap<uchar,string>:iterator MMAPI;typedef pair<int,string> MMA
3、PE;typedef map<int,uchar>:iterator MAPI;typedef pair<MMAPI,MMAPI> MMAPRange;private:multimap<uchar,string> production;/key is the nonterminalmap<uchar,int> symbolidx;map<int,uchar> symboltable;map<uchar,bool> symbolmark;int symbnum;int genint;string expression;voi
4、d addSymbol(uchar _c)if(symbolidx_c>0)return;symbnum+;symbolidx_c=symbnum;symboltablesymbnum=_c;symbolmark_c=false;void addProductionPhase2(uchar keymultimap,string &_tstr)string:iterator s,t;for(s=t=_tstr.begin();s+)if(s=_tstr.end()| *s='|' )production.insert(make_pair(keymultimap,st
5、ring(t,s);if(s=_tstr.end()break;else t=s+1;struct pnodechar t;MMAPI location;pnode()path1000;void killloop(int len,char t)int i;for(i=0;i<=len;i+)if(pathi.t=t)break;string tmp("");for(;i<=len;i+)tmp=tmp.substr(0,tmp.length()-1)+pathi.location->second;tmp="#Loop#"+tmp.sub
6、str(0,tmp.length()-1);production.insert(MMAPE(t,tmp);string removeLoop(int dep,char t)symbolmarkt=true;MMAPRange range=production.equal_range(t);string exp,exp1;for(MMAPI i=range.first;i!=range.second;i+)if(symbolidx_lastchar(i->second)!=0)pathdep.t=t;pathdep.location=i;if(!symbolmark_lastchar(i-
7、>second)exp=exp+i->second.substr(0,i->second.length()-1)+"("+removeLoop(dep+1,_lastchar(i->second)+")|"elsekillloop(dep,_lastchar(i->second);elseif(i->second.find("#Loop#")=0)exp1+=i->second.substr(6,i->second.length()+"|"else exp=exp+i
8、->second+"|"if(!exp.empty()exp=exp.substr(0,exp.length()-1);if(!exp1.empty()exp1=exp1.substr(0,exp1.length()-1);exp1="("+exp1+")*"if(!exp.empty()&&!exp1.empty()exp="|("+exp+")"symbolmarkt=false;exp1+=exp;return exp1;public:grammar()genint=
9、127;symbnum=0;production.clear();symboltable.clear();symbolidx.clear();symbolmark.clear();bool AddProduction(string rawstr)string _tstr(rawstr.begin()+3,rawstr.end();uchar keymultimap=rawstr0;addSymbol(keymultimap);addProductionPhase2(keymultimap,_tstr);return true;void Grammar2Expression()expressio
10、n=removeLoop(0,'S');void AnsOutput()cout<<"S="<<expression<<endl;void ConsoleOutput(int idx)switch(idx)case 0:cout<<"Regular Grammar to Regulat Expression (Author:Yangxiangyu Data:2012-04-25)n"cout<<"Assume all the input are legal.n"break;case 1:cout<<"Number of productionn"break;case 2:cout<<"Productionsn"break;case 3:cout<<"Regular Expression"<<endl;break;int main()ConsoleOutput(0);grammar grm;string str;int nofp;ConsoleOutput(1);cin>>n
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030设施农业行业市场发展分析及前景趋势与投融资发展机会研究报告
- 海洋渔业水产品深加工技术创新创业项目商业计划书
- 无人机维修工作站创新创业项目商业计划书
- 家居清洁服务创新创业项目商业计划书
- 果蔬调味酱蜜饯创新创业项目商业计划书
- 大二下基础护理学题库及答案解析
- 燃气从业人员考试状态及答案解析
- 食品安全案例题库及答案解析
- 期货从业资格考试综合体及答案解析
- 4.1免疫系统的组成和功能教学设计-2023-2024学年高二上学期生物人教版(2019)选择性必修一
- 【2025年】黄淮学院招聘事业编制硕士专职辅导员20名考试笔试试题(含答案)
- 2025年教师职称考试试题及答案
- 餐饮咨询顾问合同范本
- 四级专项模拟考试题库及答案
- 2025-2030中医药大健康产业链整合与投资机会分析报告
- 深圳中考英语听说考试模仿朗读技巧点拨
- 2025年人教版小学五年级数学下册期末考试卷(附参考答案和解析)
- 2025年第九届“学宪法、讲宪法”知识竞赛题库及答案(中小学组)
- 部编人教版小学语文六年级上册【课内外阅读理解专项训练(完整)】含答案
- 2025年高考陕晋宁青卷地理试题解读及答案讲评(课件)
- 3.1生活在新型民主国家 教案 -2025-2026学年统编版道德与法治九年级上册
评论
0/150
提交评论