安徽大學(xué)編譯原理實(shí)驗(yàn)三正規(guī)文法轉(zhuǎn)正規(guī)式10頁_第1頁
安徽大學(xué)編譯原理實(shí)驗(yàn)三正規(guī)文法轉(zhuǎn)正規(guī)式10頁_第2頁
安徽大學(xué)編譯原理實(shí)驗(yàn)三正規(guī)文法轉(zhuǎn)正規(guī)式10頁_第3頁
安徽大學(xué)編譯原理實(shí)驗(yàn)三正規(guī)文法轉(zhuǎn)正規(guī)式10頁_第4頁
安徽大學(xué)編譯原理實(shí)驗(yàn)三正規(guī)文法轉(zhuǎn)正規(guī)式10頁_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、實(shí)驗(yàn)三:正規(guī)文法轉(zhuǎn)正規(guī)式實(shí)驗(yàn)日期:2012.4.27 教師簽字: 成績: 實(shí)驗(yàn)名稱: 由正規(guī)文法構(gòu)造正規(guī)式實(shí)驗(yàn)原理:對于任意一個正規(guī)文法GS,都存在一個正規(guī)式與其等價(jià),故可以使用RG構(gòu)造RE運(yùn)行結(jié)果:/* *版權(quán) (c) 2012 Part of Regular Grammar to Regular Expression *文件名zgwftozgs.cpp*Sid: E10914012 *AuthorYangxiagnyu *Description 2012/04/25 */ #include<iostream>#include<cstdio>#include<m

2、ap>#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> MMAPE;ty

3、pedef 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;void add

4、Symbol(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,string(

5、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.substr(0

6、,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->s

7、econd)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=127;s

9、ymbnum=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()expression=rem

10、oveLoop(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&g

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論