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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

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

3、數(shù)(整)7 $INT 內部字符串= 8 $ASSIGN 標準二進形式 + 9 $PLUS - * 10 $STAR - * 11 $POWER -, 12$COMMA - ( 13 $LPAR - )14 $RPAR -2010022220朱恒恒2010022226劉穗清2( 語法分析器能識別由大于 >小于<加+ 減- 乘* 除/ 括號() 賦值=操作數(shù)所 組成的算術表達式,其文法如下 :使用的算法可以是:預測分析法;遞歸下降分析法;算符優(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 具體的種別編碼和內部值 :單詞符號種別編碼單詞值int 1char 2float 3if 4else 5do 6while 7printf 8main 9標識符 100 內部字符串 常數(shù)(整) 200 二進制數(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. 流程圖 主流程圖幵妳定義關褪宇表卅4請用戶輸入字符串T陜入用戶輸入的字符串, 1調用掃描子程序:T輸出單詞二元組掃描程序流程圖:(a) ,標識符詞法分析流程圖(b) ,數(shù)字(整)詞法分析流程圖(c) ,其他字符流程圖吏眞初始優(yōu)讀取下一牛字符J W拼成標識符,.*FLAG為對應關饉宇的單 詞種別丙,itaifs -返回*論(a)始化是否需垂繼續(xù)讀入(b) (c)主流程圖賦値血申鬥網為對應羌犍字的單 詞種別碼謹取下

6、一個宇符,FLAG=2D0讀取下一個字符-拼成數(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; /存儲輸入句子string cnt500;/標識符int temp=0; / 數(shù)組下標int is_right; /判斷輸出信息int Scan(char program)char *keywords13 = "void","main","if","then","break","int","char","floa

9、t","include","for", "while","printf","scanf" / 關鍵字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;字母判斷是否為關鍵字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、標識符 ()數(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) / 運算符和標點符號 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; / 語義分析以及中間代碼生成 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 連接到當前字符串的結尾 /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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論