




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、編譯原理程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告逆波蘭式生成班級:計(jì)算機(jī)1306班 姓名:張濤 學(xué)號:20133967實(shí)驗(yàn)?zāi)繕?biāo):在對表達(dá)式語法分析的基礎(chǔ)上設(shè)計(jì)實(shí)現(xiàn)逆波蘭式形式的中間代碼生成程序。實(shí)驗(yàn)內(nèi)容:概要設(shè)計(jì):對表文法加入語義動(dòng)作形成屬性文法,這樣在對表達(dá)式語法分析的基礎(chǔ)上設(shè)計(jì)實(shí)現(xiàn)逆波蘭式形式的中間代碼生成程序。數(shù)據(jù)結(jié)構(gòu):int op=0;當(dāng)前判斷進(jìn)度char ch;當(dāng)前字符char nowword10=;當(dāng)前單詞char operate4=+,-,*,/;運(yùn)算符char bound2=(,); 界符struct Tokenint code;char ch10;/Token 定義struct Token toke
2、nlist50; /Token 數(shù)組struct Token tokentemp; 臨時(shí) Token 變量關(guān)鍵函數(shù):int IsLetter(char ch) 判斷 ch 是否為字母int IsDigit(char ch) 判斷ch是否為數(shù)字int Iskey(char *string)判斷是否為關(guān)鍵字int Isbound(char ch)判斷是否為界符int Isboundnum(char ch) 給由界符所在 token 值void T();分析子程序void F();分析子程序void E1();分析子程序void E();分析子程序void T1();分析子程序(包括:概要設(shè)計(jì)、數(shù)據(jù)
3、結(jié)構(gòu)、流程圖、關(guān)鍵函數(shù)等有選擇填寫)源程序代碼:(加入注釋)#include#include#include#includeint op=0;當(dāng)前判斷進(jìn)度char ch;當(dāng)前字符char nowword10=; 當(dāng)前單詞char operate4=+,*,7;char bound2=(,);struct Token(int code;char ch10;);struct Token tokenlist50;void T();void F();void E1();void E();void T1();int IsLetter(char ch) 判斷 ch 是否為字母(int i;for(i=0;
4、i=a&ch=A&ch=Z)return 1;return 0;) int IsDigit(char ch) 判斷ch是否為數(shù)字(int i;for(i=0;i=0&ch=9)return 1;return 0;)int Isbound(char ch)(int i;for(i=0;i2;i+)(if(ch=boundi)(return i+1;)return 0;int Isoperate(char ch)(int i; for(i=0;i4;i+)( if(ch=operatei) ( return i+3; return 0;int main()(FILE *fp;int q=0,m=0
5、;char sour200=”;F:2.txtn);printf(請將源文件置于以下位置并按以下方式命名:if(fp=fopen(F:2.txt,r)=NULL)( printf(文件未找到! n); else while(!feof(fp) if(isspace(ch=fgetc(fp); else sourq=ch;q+; int p=0;printf(輸入句子為:n);for(p;p=q;p+) printf(%c”,sourp); printf(n); int state=0,nowlen=0;BOOLEAN OK=TRUE,ERR=FALSE;int i,flagpoint=0;fo
6、r(i=0;iq;i+)switch(state)(case 0:ch=souri;if(Isbound(ch)(if(ERR)(printf(無法識別 n);ERR=FALSE;OK=TRUE;else if(!OK)( printf( 標(biāo)識符 n”,nowword);(struct Token tokentemp;tokentemp.code=10;int i=0;for(i;i=nowlen;i+)tokentemp.chi=nowwordi;tokenlistm=tokentemp;m+; OK=TRUE;state=4;else if(IsDigit(ch)(if(OK)(memse
7、t(nowword,0,strlen(nowword);nowlen=0;nowwordnowlen=ch;nowlen+;state=3;OK=FALSE;break;else(nowwordnowlen=ch;nowlen+;)else if(IsLetter(ch) (if(OK) (memset(nowword,0,strlen(nowword);nowlen=0;nowwordnowlen=ch;nowlen+;OK=FALSE;if(souri+1=#) (printf( 標(biāo)識符 n,nowword);(struct Token tokentemp;tokentemp.code=1
8、0;int i=0;for(i;i=nowlen;i+)tokentemp.chi=nowwordi;tokenlistm=tokentemp; m+;else (nowwordnowlen=ch;nowlen+;else if(Isoperate(ch)(if(!OK)( printf( 標(biāo)識符 n”,nowword); struct Token tokentemp;tokentemp.code=10;int i=0;for(i;i=nowlen;i+)tokentemp.chi=nowwordi;tokenlistm=tokentemp;m+;OK=TRUE;printf( 運(yùn)算符 n,I
9、soperate(ch),ch);struct Token tokentemp;tokentemp.code=Isoperate(ch);tokentemp.ch0=ch;tokenlistm=tokentemp;m+;break;if(IsLetter(ch)printf(錯(cuò)誤 n);nowwordnowlen=ch;nowlen+;ERR=FALSE;state=0;break;if(IsDigit(ch=souri)nowwordnowlen=ch;nowlen+;else if(souri=.&flagpoint=0)flagpoint=1;nowwordnowlen=ch;nowle
10、n+;elseprintf( 數(shù)字 n”,nowword);i-;state=0;OK=TRUE;struct Token tokentemp;tokentemp.code=20;int i=0;for(i;i=nowlen;i+)tokentemp.chi=nowwordi;tokenlistm=tokentemp;m+;)break;i-;printf( 界符 n,Isbound(ch),ch);struct Token tokentemp;tokentemp.code=Isbound(ch);tokentemp.ch0=ch;tokenlistm=tokentemp;m+;state=0
11、;OK=TRUE;break;)int io=0;for(io;io=m;io+)printf(tokenlist%d.code 值為 %dn,io,tokenlistio.code);printf(tokenlist%d.ch 值為 %sn,io,tokenlistio.ch);)printf(*逆波蘭式為:n);E();printf(n);if(op=m)printf(OK!);elseprintf(WRONG!);return 0;)void E()T();E1();)void E1()if(tokenlistop.code=3|tokenlistop.code=4)char c=tok
12、enlistop.ch0;tokenlistop+;T();E1(); printf(%c,c);)void T1()if(tokenlistop.code=5|tokenlistop.code=6)char c=tokenlistop.ch0;tokenlistop+;F();T1();printf(%c,c);)void T()F();T1();)void F()if(tokenlistop.code=10|tokenlistop.code=20) int i=0;char c10;for(i;itokenlist 6 .code 值為 520 .2|tokenlist6 .ch值力 Btokenlist7.code 1J |tokenlist7.ch值一 tokenlist8.code 值 It o ken list 8 卜值,okenlist9 .code值為6Mkenlist9 .ch值為/kokenlist10.code 值為 20kokenlist10.ch值為 3xokenlistll .code值為2r
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 土地糾紛單方協(xié)議書
- 協(xié)解人員返聘協(xié)議書
- 醫(yī)療安全生產(chǎn)協(xié)議書
- 合伙買賣合同協(xié)議書
- 商場承包轉(zhuǎn)租協(xié)議書
- 商業(yè)房屋轉(zhuǎn)讓協(xié)議書
- 品牌推廣免責(zé)協(xié)議書
- 2025年新型環(huán)保制冷劑生產(chǎn)項(xiàng)目投資回報(bào)期與盈利模式分析報(bào)告
- 單位治安聯(lián)防協(xié)議書
- 合伙退股合同協(xié)議書
- 11471勞動(dòng)爭議處理(第3章)
- 患者跌倒的預(yù)防及管理課件
- 大學(xué)學(xué)院學(xué)生出國(境)訪學(xué)管理辦法(試行)
- 國開作業(yè)《公共部門人力資源管理》形考任務(wù)4:撰寫課程學(xué)習(xí)總結(jié)(第1-9章權(quán)重25%)參考772
- 三年級數(shù)學(xué)-《年月日》整理和復(fù)習(xí)課件
- 兩器車間產(chǎn)能分析
- 機(jī)電廠房金屬壁板隔墻工程施工方案(71頁)
- 2022版義務(wù)教育(數(shù)學(xué))課程標(biāo)準(zhǔn)(含2022年修訂部分)
- 《石鐘山記》教案3
- 報(bào)聯(lián)商——有效溝通PPT課件
- 二年級口算題大全(可直接打印)
評論
0/150
提交評論