編譯原理詞法語(yǔ)法語(yǔ)義分析器設(shè)計(jì)打印版_第1頁(yè)
編譯原理詞法語(yǔ)法語(yǔ)義分析器設(shè)計(jì)打印版_第2頁(yè)
編譯原理詞法語(yǔ)法語(yǔ)義分析器設(shè)計(jì)打印版_第3頁(yè)
編譯原理詞法語(yǔ)法語(yǔ)義分析器設(shè)計(jì)打印版_第4頁(yè)
編譯原理詞法語(yǔ)法語(yǔ)義分析器設(shè)計(jì)打印版_第5頁(yè)
已閱讀5頁(yè),還剩28頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、編譯原理詞法語(yǔ)法語(yǔ)義分析器設(shè)計(jì)打印版編譯原理課程設(shè)計(jì)報(bào)告小組成員 :2010年 06月 24日一. 任務(wù)及要求課程設(shè)計(jì)要實(shí)現(xiàn)的內(nèi)容 :) 設(shè)計(jì)符號(hào)表 (1確定符號(hào)表的組織方式,一般應(yīng)包括名字欄和信息欄,其中名字欄作為關(guān)鍵 字。要考慮能夠存儲(chǔ)有關(guān)名字的信息,并可以高效地完成如下操作 :a. 查找: 根據(jù)給定的名字,在符號(hào)表中查找其信息。如果該名字在符號(hào)表中不存在,則將其加入到符號(hào)表中,否則返回指向該名字的指針 ;b. 刪除: 從符號(hào)表中刪除給定名字的表項(xiàng)。(2) 設(shè)計(jì)詞法分析器設(shè)計(jì)各單詞的狀態(tài)轉(zhuǎn)換圖,并為不同的單詞設(shè)計(jì)種別碼。將詞法分析器設(shè)計(jì)成供語(yǔ)法分析器調(diào)用的子程序。功能包括 :a. 能夠拼出

2、語(yǔ)言中的各個(gè)單詞 ;b. 將拼出的標(biāo)識(shí)符填入符號(hào)表 ;c. 返回( 種別碼, 屬性值) 。(3) 語(yǔ)法分析要求用遞歸下降分析法、預(yù)測(cè)分析法或 SLR分析法,實(shí)現(xiàn)對(duì)表達(dá)式、各種說(shuō)明語(yǔ)句、控制語(yǔ)句進(jìn)行語(yǔ)法分析。若語(yǔ)法正確,則輸出一棵語(yǔ)法樹若語(yǔ)法錯(cuò)誤,要求指出出錯(cuò)性質(zhì)和出錯(cuò)位置 (行號(hào))。出錯(cuò)處理應(yīng)設(shè)計(jì)成一個(gè)出 錯(cuò)處理子程序。1(詞法分析器產(chǎn)生下述小語(yǔ)言的單詞序列這個(gè)小語(yǔ)言的所有的單詞符號(hào),以及它們的種別編碼和內(nèi)部值如下表:?jiǎn)卧~符號(hào) 種別編碼 助記符 內(nèi)碼值DIM 1 $DIM - IF 2 $IF - DO 3 $D0 -STOP 4 $STOP- END 5 $END -標(biāo)識(shí)符6 $ID - 常

3、數(shù)(整)7 $INT 內(nèi)部字符串= 8 $ASSIGN 標(biāo)準(zhǔn)二進(jìn)形式 + 9 $PLUS - * 10 $STAR - * 11 $POWER -, 12$COMMA - ( 13 $LPAR - )14 $RPAR -2010022220朱恒恒2010022226劉穗清2( 語(yǔ)法分析器能識(shí)別由大于 >小于<加+ 減- 乘* 除/ 括號(hào)() 賦值=操作數(shù)所 組成的算術(shù)表達(dá)式,其文法如下 :使用的算法可以是:預(yù)測(cè)分析法;遞歸下降分析法;算符優(yōu)先分析法;LR分析法 等。(1) A->EB(2) B- >>EB|vEB|&(3) E->TGG->+T

4、G|- TG|£(5) T->FS(6) S- >*FS|/FS| &(7) F->(E)|i |i=E2010022211吳曉凱3 具體的種別編碼和內(nèi)部值 :?jiǎn)卧~符號(hào)種別編碼單詞值int 1char 2float 3if 4else 5do 6while 7printf 8main 9標(biāo)識(shí)符 100 內(nèi)部字符串 常數(shù)(整) 200 二進(jìn)制數(shù)值表示 = = 401= 402>= 403> 404<= 405< 406!= 407! 408409 +=410 +411 +412 -=413 - -414 -415 *=416 *417

5、/=418 /419 A501 ;( 502) 503 504 505 506 507508 :“ 509 %= 510% 511 , 512# 513 514 空格 515$ 04. 流程圖 主流程圖幵?shī)叾x關(guān)褪宇表卅4請(qǐng)用戶輸入字符串T陜?nèi)胗脩糨斎氲淖址?1調(diào)用掃描子程序:T輸出單詞二元組掃描程序流程圖:(a) ,標(biāo)識(shí)符詞法分析流程圖(b) ,數(shù)字(整)詞法分析流程圖(c) ,其他字符流程圖吏眞初始優(yōu)讀取下一牛字符J W拼成標(biāo)識(shí)符,.*FLAG為對(duì)應(yīng)關(guān)饉宇的單 詞種別丙,itaifs -返回*論(a)始化是否需垂繼續(xù)讀入(b) (c)主流程圖賦値血申鬥網(wǎng)為對(duì)應(yīng)羌犍字的單 詞種別碼謹(jǐn)取下

6、一個(gè)宇符,FLAG=2D0讀取下一個(gè)字符-拼成數(shù)字開如返回*劉穗清 20100222265. First/FollowFirst(A)=( ,i Follow(A)=# First(B)=>,<,£ Follow(B)=#First(E)=(,i Follow(E)=>,<,),# First(F)=(,i Follow(F)=*,(,+,-First(G)=+,-, £ Follow(G)=>,<,),# First(T)=c,i Follow(T)=+,-,>,<,),# First(S)=*,(,& Follow

7、(S)=+, -,>,<,),#吳曉凱 20100222116.源程序/#include "stdafx.h"#in clude <iostream>#include<string>using namespace std;#include<stdio.h>#include<stdlib.h>#include<sstream>int i,j,k,flag,number,status; /*status which is use to judge the string is keywords or not!

8、*/char ch;char words10 = " "char program500;int flags500; /存儲(chǔ)輸入句子string cnt500;/標(biāo)識(shí)符int temp=0; / 數(shù)組下標(biāo)int is_right; /判斷輸出信息int Scan(char program)char *keywords13 = "void","main","if","then","break","int","char","floa

9、t","include","for", "while","printf","scanf" / 關(guān)鍵字number=0;status=0;j=0;ch=programi+; / 遍歷if (ch >= 'a') && (ch <= 'z' ) /while (ch >= 'a') && (ch <= 'z' )wordsj+=ch; ch=programi+;i-;wo

10、rdsj+ = '0'for (k = 0; k < 13; k+)if (strcmp (words,keywordsk) = 0) / switch(k)case 0: flag = 1;status = 1; break;case 1: flag = 2;status = 1; break;字母判斷是否為關(guān)鍵字case 2: flag = 3;status = 1; break;case 3: flag = 4; status = 1; break;case 4: flag = 5; status = 1; break;case 5: flag = 6; statu

11、s = 1; break;case 6: flag = 7; status = 1; break;case 7: flag = 8; status = 1; break;case 8: flag = 9; status = 1; break;case 9: flag = 10; status = 1; break;case 10: flag = 11; status = 1; break;case 11: flag = 12; status = 1; break;case 12: flag = 13; status = 1; break;if (status = 0)flag = 100; /

12、標(biāo)識(shí)符 ()數(shù)字()else if (ch >= '0') && (ch <= '9') /number = 0;while (ch >= '0' ) && (ch <= '9' ) number = number*10+(ch-'0');ch = programi+;flag = 200;i-;else switch (ch) / 運(yùn)算符和標(biāo)點(diǎn)符號(hào) case '=': if (ch = '=') wordsj+ = ch; w

13、ordsj = '0' ch = programi+; if (ch = '=') wordsj+ = ch; wordsj = '0' flag = 401; lse ei-; flag = 402; break; case'>': if (ch = '>') wordsj+ = ch; wordsj = '0' ch = programi+;if (ch = '=') wordsj+ = ch; wordsj = '0' flag = 403; els

14、ei-; flag = 404; break; case'<': if (ch = '<') wordsj+ = ch; wordsj = '0'ch = programi+; if (ch = '=')wordsj+ = ch; wordsj = '0' flag = 405; elsei-; flag = 406; break;case'!': if (ch = '!') wordsj+ = ch; wordsj = '0'ch = programi+

15、; if (ch = '=') wordsj+ = ch; wordsj = '0'flag = 407;elsei-; flag = 408; break;case'+': if (ch = '+') wordsj+ = ch;wordsj = '0' ch = programi+; if (ch = '=')wordsj+ = ch; wordsj = '0'flag = 409;else if (ch = '+')wordsj+ = ch; wordsj = &

16、#39;0'flag = 410;elsei-;flag = 411;break;case'-':if (ch = '-') wordsj+ = ch;wordsj = '0' ch = programi+; if (ch = '=')wordsj+ = ch; wordsj = '0'flag = 412;else if( ch = '-')wordsj+ = ch; wordsj = '0'flag = 413;elsei-;flag = 414;break;case&#

17、39;*':if (ch = '*') wordsj+ = ch;wordsj = '0' ch = programi+; if (ch = '=') wordsj+ = ch; wordsj = '0'flag = 415;elsei-;flag = 416; break;case'/': if (ch = '/') wordsj+ = ch; wordsj = '0'ch = programi+; if (ch = '=')wordsj+ = ch; wo

18、rdsj = '0'flag = 417; elsei-;flag = 418; break;case'A':wordsj = ch; wordsj+1 = '0' flag = 419; break;case'':wordsj = ch; wordsj+1 = '0' flag = 501;break;case'(':wordsj = ch; wordsj+1 = '0'flag = 502; break;case')': wordsj = ch; wordsj+1

19、 = '0' flag = 503; break;case'': wordsj = ch; wordsj+1 = '0' flag = 504; break;case'': wordsj = ch; wordsj+1 = '0' flag = 505; break;case'': wordsj = ch; wordsj+1 = '0' flag = 506; break;case'': wordsj = ch; wordsj+1 = '0' flag

20、= 507; break;case':': wordsj = ch; wordsj+1 = '0' flag = 508; break;case'"': wordsj = ch; wordsj+1 = '0' flag = 509; break; case'%': if (ch = '%')wordsj+ = ch; wordsj = '0' ch = programi+; if (ch = '=') wordsj+ = ch; wordsj = '

21、0'flag = 510;elsei-;flag = 511; break;case',': wordsj = ch; wordsj+1 = '0' flag = 512;break; case'#': wordsj = ch; wordsj+1 = '0' flag = 513; break; case'': wordsj = ch; wordsj+1 = '0' flag = 514; break;case' ':/ 空格 wordsj ='_' word

22、sj+1 = '0' flag = 515;break; case'$': wordsj = '#' wordsj+1 = '0' flag = 0;break; default: flag = -1;break;return flag;void e();void e1();void e2();void t();void t1();void t2();void f();void f1();void p();void e()cout<<"E->TE''"<<endl;

23、 t();e2();void e1()if(flagstemp=411) cout<<"E'->+T"<<endl;temp+;t();else if(flagstemp=414) cout<<"E'->-T"<<endl; temp+;t();elseis_right=0;void e2()if(flagstemp=411|flagstemp=414) cout<<"E''->E'E''"<&l

24、t;endl;e1();e2();else if (flagstemp!=0|flagstemp!=503)cout<v"E”->A"v<e ndl;return ;elseis_right=0;void t()cout<<"T->FT''"<<endl;f();t2();void t1()if(flagstemp=416)cout<<"T'->*F"<<endl;temp+;f();else if(flagstemp=418)cou

25、t<<"T'->/F"<<endl;temp+;f();else is_right=0;void t2()if(flagstemp=416|flagstemp=418)cout<<"T''->T'T''"<<endl;t1();t2();else if (flagstemp!=0|flagstemp!=503)cout<v"T”->A"v<e ndl;return ;else is_right=0; void

26、f()cout<<"F->PF'"<<endl;p();f1();void f1()if(flagstemp=419)cout<<"F'->AF"<<e ndl;temp+;f();else if (flagstemp!=0&&flagstemp!=503&&flagstemp!=411 &&flagstemp!=414&&flagstemp!=416&&flagstemp!=418)cout<

27、<"F'->A"<<endl;is_right=1;temp+;void p()if(flagstemp=100|flagstemp=200)cout<<"P->i"<<endl;temp+;elseif(flagstemp=502)cout<<"P->(E)"<<endl;temp+;e();if(flagstemp=503)cout<<"P->(E)"<<endl;temp+;elseis_

28、right=0;else is_right =0; / 語(yǔ)義分析以及中間代碼生成 int ye();int ye1(int a); int ye2(int a); int yt(); int yt1(int a); int yt2(int a); int yf();int yf1(int a); int yp();int v=-1;int num=0;int ww;string strn;int nn;int newTemp()num+;nn+;stringstream stream; stream<<nn;stream>>strn;stream.clear();cnt

29、num-1="temp"cntnum-1.operator+=(strn);/ 把字符串 s 連接到當(dāng)前字符串的結(jié)尾 /cntnum-1=strcat("Temp",strn);/ cntnum-1="temp"return num-1;void siyuan(int a,int b,int c,int d)/輸出四元 cout<<"("<<cnta<<","<<cntb<<","<<cntc<&l

30、t;","<<cntd<<")"<<endl;int ye()int rt,t1;rt=yt();t1=rt;rt=ye2(t1);return rt;int ye1(int a)int rt,t1;t1=a;if(flagstemp=411) / 加法 temp+;int tt=v+1;v+;int t2=yt();int rr=newTemp();siyuan(tt,t1,t2,rr);rt=rr;return rt;減法 else if(flagstemp=414) /temp+;int tt=v+1;v+;in

31、t t2=yt();int rr=newTemp();siyuan(tt,t1,t2,rr);rt=rr;return rt;else return t1; int ye2(int a) int rt,t1;t1=a;if(flagstemp=411|flagstemp=414) rt=ye1(t1);t1=rt;rt=ye2(t1);return rt;else if (flagstemp!=0|flagstemp!=503) return t1;else return t1; int yt()int rt,t1;rt=yf();t1=rt; rt=yt2(t1); return rt;in

32、t yt1(int a) int rt,t1;t1=a;if(flagstemp=416) / 乘法 int tt=v+1;v+;temp+;int t2=yf();int rr=newTemp(); siyuan(tt,t1,t2,rr);rt=rr;return rt;除法else if(flagstemp=418) / temp+;int tt=v+1;v+;int t2=yf();int rr=newTemp(); siyuan(tt,t1,t2,rr);rt=rr;return rt;else return t1; int yt2(int a) int rt,t;t=a;if(flagstemp=416|flagstemp=418) rt=yt1(t);t=rt;rt=yt2(t);return rt;else return t; int yf()int t1,rt;rt=yp();t1=rt;rt=yf1(t1);return rt; int yf1(int a) /乘方int rt,t1;t

溫馨提示

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

評(píng)論

0/150

提交評(píng)論