河北工業(yè)大學(xué)編譯原理實(shí)驗(yàn)報(bào)告_第1頁(yè)
河北工業(yè)大學(xué)編譯原理實(shí)驗(yàn)報(bào)告_第2頁(yè)
河北工業(yè)大學(xué)編譯原理實(shí)驗(yàn)報(bào)告_第3頁(yè)
河北工業(yè)大學(xué)編譯原理實(shí)驗(yàn)報(bào)告_第4頁(yè)
河北工業(yè)大學(xué)編譯原理實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩10頁(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、編譯原理實(shí)驗(yàn)報(bào)告組員: 韋廷廷()、熊敏()、馬昊()任課老師:吳 清一、 任務(wù)概述 本次實(shí)驗(yàn)我們要完成的任務(wù)主要是實(shí)現(xiàn)一個(gè)簡(jiǎn)單語(yǔ)言集的詞法、語(yǔ)法和語(yǔ)義分析程序,驗(yàn)證實(shí)際編譯系統(tǒng)的實(shí)現(xiàn)方法。我們組的分工并不是詞法分析、語(yǔ)法分析、語(yǔ)義分析每人負(fù)責(zé)一個(gè),而是先確定每種分析按照哪一種方法實(shí)現(xiàn),然后每個(gè)人寫出自己的想法,按照自己的想法實(shí)驗(yàn),最后總結(jié)到一起,有問題大家一起討論,共同商量解決的辦法。二、系統(tǒng)設(shè)計(jì)實(shí)驗(yàn)采用的實(shí)現(xiàn)方法和依據(jù): 語(yǔ)言中的各類單詞符號(hào)及其分類碼表單詞符號(hào)類別編碼類別碼的助記符單詞值begin1BEGINend2ENDif3IFthen4THENelse5ELSE標(biāo)識(shí)符6ID字母打頭

2、的字母數(shù)字串無(wú)符號(hào)常數(shù)7UCON機(jī)內(nèi)二進(jìn)制表示8LT=9LE=10EQ11NE12GT=13GE:=14IS+15PL-16MI*17MU/18DI 實(shí)驗(yàn)采用的實(shí)現(xiàn)方法和依據(jù):文法:E T | E+T | E-T T F | T*F | T/F F i | (E)SLR(1)分析表狀態(tài)ACTIONGOTO()+-*/i#ETF0S4S51231S6S7Acc2R3R3R3S8S9R33R6R6R6R6R6R64S4S510235R8R8R8R8R8R86S4S51137S4S51238S4S5139S4S51410S15S6S711R1R1R1S8S9R112R2R2R2S8S9R213R4R

3、4R4R4R4R414R5R5R5R5R5R515R7R7R7R7R7R7三、系統(tǒng)實(shí)現(xiàn)(包括必要的框圖,各.h和.c文件說(shuō)明,所有函數(shù)功能的說(shuō)明,數(shù)據(jù)結(jié)構(gòu)、各種表格、變量等的說(shuō)明,以及函數(shù)調(diào)用關(guān)系圖等)(1)各.h和.c文件說(shuō)明Cifa.cpp的功能:字符串掃描識(shí)別。Table.cpp的功能:存放SLR分析法需要用到的ACTION和GOTO表。Yufa.cpp的功能:引用Cifa.cpp、Table.cpp兩個(gè)文件進(jìn)行語(yǔ)法、語(yǔ)義的分析。(2)函數(shù)功能說(shuō)明詞法分析部分函數(shù)說(shuō)明:int lookup (char *token) /比較是否是關(guān)鍵字 int GetChar(char ch) /每調(diào)用

4、一次,就把掃描指示器當(dāng)前所指示的源程序字符送入字符變量ch,然后把掃描指示器前推一個(gè)字符位置。 int HandleError (void)/報(bào)錯(cuò)函數(shù) int EXCUTE (int state, int symbol)/狀態(tài)轉(zhuǎn)換 int lookup (char *token) /比較是否是關(guān)鍵字void out(int a,char *token)/輸出函數(shù) void scanner_example (FILE *fp)/詞法分析語(yǔ)法、語(yǔ)義部分函數(shù)功能說(shuō)明:void REPORT_ERROR()/報(bào)錯(cuò)函數(shù)void Accept()/語(yǔ)法成功接受int INDEX(char a)/獲取當(dāng)前字

5、符串對(duì)應(yīng)的索引void yuyi(int n)/語(yǔ)義子程序void INPUT(int a)/語(yǔ)法分析程序(2) 數(shù)據(jù)結(jié)構(gòu)、各種表格、變量等的說(shuō)明Cifa.cpp中:char *KeyWordTableMAX_KEY_NUMBER=begin,end, if, then, else, KEY_WORD_END;/數(shù)組指針關(guān)鍵字int w,n,p,e,d;/w尾數(shù)累加器,n小數(shù)位數(shù)計(jì)數(shù)器,p指數(shù)累加器,e指數(shù)符號(hào)標(biāo)記,int Class;/標(biāo)識(shí)單詞類型Table.cpp#define MAXROW 16 /行數(shù)#define MAXCOL 11 /列數(shù)int TableMAXROWMAXCOLY

6、ufa.cpp中#define NUMBER 9/表達(dá)式的個(gè)數(shù)int StateMAXState;/狀態(tài)棧int CURRENTSTATE=0;/標(biāo)識(shí)當(dāng)前狀態(tài)int LENGTHNUMBER=1,3,3,1,3,3,1,3,1;/表達(dá)式右邊的長(zhǎng)度int Yes=0;/判斷是否結(jié)束int tag=0;/判斷是否需要調(diào)用詞法程序四、系統(tǒng)工作過程及運(yùn)行說(shuō)明(使用操作指南)程序使用:在工程里創(chuàng)建一個(gè)b.txt文件以識(shí)別算數(shù)運(yùn)算表達(dá)式五、源程序清單(要求有詳細(xì)注釋)和實(shí)例程序運(yùn)行結(jié)果源程序清單:Cifa.cpp#include#include#include#include#include#define

7、 DIGIT 1#define POINT 2#define OTHER 3#define POWER 4# define ID 6# define UCON 7# define LT 8# define LE 9# define EQ 10# define NE 11# define GT 12# define GE 13# define IS 14# define PL 15 /+# define MI 16 /-# define MU 17# define DI 18#define zuokuohao 19#define youkuohao 20#define jin 21#define

8、 ClassOther 200#define EndState -1#define MAX_KEY_NUMBER 20 /*關(guān)鍵字的數(shù)量*/#define KEY_WORD_END END /*關(guān)鍵字結(jié)束標(biāo)記*/char *KeyWordTableMAX_KEY_NUMBER=begin,end, if, then, else, KEY_WORD_END;/數(shù)組指針char TOKEN20;char ch;int w,n,p,e,d;/w尾數(shù)累加器,n小數(shù)位數(shù)計(jì)數(shù)器,p指數(shù)累加器,e指數(shù)符號(hào)標(biāo)記,int Class;/標(biāo)識(shí)單詞類型int ICON;double FCON;static int

9、CurrentState=0;int result;int start=0;/指示程序的開始int end=0;/指示程序的結(jié)束int GetChar (void);int EXCUTE (int,int);int HandleOtherWord (void) return ClassOther; int HandleError (void) printf (Error!n); return 0;int lookup (char *token) /比較是否是關(guān)鍵字 int n=0; while (strcmp(KeyWordTablen, KEY_WORD_END) /*strcmp比較兩串是

10、否相同,若相同返回0*/ if (!strcmp(KeyWordTablen, token) /*比較token所指向的關(guān)鍵字和保留字表中哪個(gè)關(guān)鍵字相符*/ return n+1; /*根據(jù)單詞分類碼表I,設(shè)置正確的關(guān)鍵字類別碼,并返回此類別碼的值*/ break; n+; return 0; /*單詞不是關(guān)鍵字,而是標(biāo)識(shí)符*/int GetChar (char a) char c=a; if(isdigit(c) d=c-0; /字符c與字符0的ascii碼差值,返回類型為一個(gè)整數(shù) return DIGIT; if (c=.) return POINT; if (c=E|c=e) retur

11、n POWER; if (c=+) return PL; if (c=-) return MU; return OTHER;void report_error( ) printf(錯(cuò)誤n);void out1(int a,char *token)/輸出函數(shù) switch (a) case 1:printf(BEGIN, )n);break; case 2:printf(END, )n);break; case 3:printf(IF, )n);break; case 4:printf(THEN, )n);break; case 5:printf(ELSE, )n);break; case 6:

12、 printf(ID,%s)n,token);break; case 8:printf(LT,%s)n,token);break; case 9:printf(LE,%s)n,token);break; case 10:printf(EQ,%s)n,token);break; case 11:printf(NE,%s)n,token);break; case 12:printf(GT,%s)n,token);break; case 13:printf(GE,%s)n,token);break; case 14:printf(IS,%s)n,token);break; case 15:print

13、f(PL,%s)n,token);break; case 16:printf(MI,%s)n,token);break; case 17:printf(MU,%s)n,token);break; case 18:printf(DI,%s)n,token);break; case 19:printf(, )n);break; case 20:printf(), )n);break; default: report_error( );break; int out(int a)switch(a)case 7:return 6;break;/常量case 15:return 2;break;/+cas

14、e 16:return 3;break;case 17:return 4;break;case 18:return 5;break;case 19:return 0;break;case 20:return 1;break;case 21:return 7;break; case 22:return 100;break;/判斷是否是空格或換行case 26: return 26;break;/標(biāo)識(shí)符default:return 001;report_error();break;void scanner_example (FILE *fp)/文件掃描器 int i, c; ch=fgetc(fp

15、);if(ch= |ch=n) scanner_example (fp); else if (isalpha (ch) /判斷是否是英文字母 TOKEN0=ch; ch=fgetc(fp); i=1; while (isalnum (ch) TOKENi=ch; i+; ch=fgetc (fp); TOKENi=0; fseek(fp,-1,1); /* retract*/ c=lookup (TOKEN); if (c=0) /printf(算術(shù)表達(dá)式不需要n); result=out(26); /標(biāo)識(shí)符 else if(c=1) start=1; out1 (c, ); if (c=2)

16、end=1;out1(c, ); else if (isdigit(ch)|ch=.) /判斷是否是數(shù)字或“.” i=0; /TOKENi=ch; if(isdigit(ch) d=ch-0; /字符c與字符0的ascii碼差值,返回類型為一個(gè)整數(shù) EXCUTE(CurrentState,DIGIT); if (ch=.) EXCUTE(CurrentState,POINT); if (ch=E|ch=e) EXCUTE(CurrentState,POWER); if (ch=+)EXCUTE(CurrentState,PL) ; if (ch=-) EXCUTE(CurrentState,M

17、U); while(CurrentState!=EndState) TOKENi=ch; i+;ch=fgetc(fp);int c=GetChar(ch); EXCUTE(CurrentState,c); TOKENi=0; fseek(fp,-1,1); / printf(UCON,%g)n,FCON); result=out(UCON);else switch(ch) /關(guān)系運(yùn)算符 case ) out1 (NE, ); else fseek (fp,-1,1); printf(算術(shù)表達(dá)式不需要n); / out (UCON); break; case =: printf(算術(shù)表達(dá)式不需

18、要n); /out(EQ, ); break; case : ch=fgetc(fp); if(ch=) printf(算術(shù)表達(dá)式不需要n); /out(GE, ); else fseek(fp,-1,1); printf(算術(shù)表達(dá)式不需要n); /out(GT, ); break;case :ch=fgetc(fp); if(ch=) printf(算術(shù)表達(dá)式不需要n); /out(IS, ); break;case +:result=out(PL);break;case -:result=out(MI);break;case *:result=out(MU);break;case /:re

19、sult=out(DI);break;case (:result=out(zuokuohao);break;case ):result=out(youkuohao);break;case ;:result=out(jin);break;case EOF:break;default:report_error();break;return;int EXCUTE (int state, int symbol) switch (state) case 0:switch (symbol) case DIGIT: n=0;p=0;e=1;w=d;CurrentState=1;Class=UCON;brea

20、k; case POINT: w=0;n=0;p=0;e=1;CurrentState=3;Class=UCON;break; default: HandleOtherWord( );Class=ClassOther; CurrentState=EndState; break; case 1:switch (symbol) case DIGIT: w=w*10+d;break; /CurrentState=1 case POINT: CurrentState=2;break; case POWER: CurrentState=4;break; default: FCON=w;CurrentSt

21、ate=EndState; break; case 2:switch (symbol) case DIGIT: n+;w=w*10+d;break; case POWER: CurrentState=4;break; default: FCON=w*pow(10,e*p-n);CurrentState=EndState; break; case 3:switch (symbol) case DIGIT: n+;w=w*10+d;CurrentState=2;break; default: HandleError( );CurrentState=EndState; break; case 4:s

22、witch (symbol) case DIGIT: p=p*10+d;CurrentState=6;break; case MU: e=-1;CurrentState=5;break; case PL: e=1;CurrentState=5;break; default: HandleError( );CurrentState=EndState; break; case 5:switch (symbol) case DIGIT: p=p*10+d;CurrentState=6;break; default: HandleError( );CurrentState=EndState; brea

23、k; case 6:switch (symbol) case DIGIT: p=p*10+d;break; default: FCON=w*pow(10,e*p-n);CurrentState=EndState; break; return CurrentState; int cifa(FILE *fp) CurrentState=0;/初始0狀態(tài) scanner_example (fp); return(result);Table.cpp#include#include#include#include#include#define MAXROW 16 /行數(shù)#define MAXCOL 11

24、 /列數(shù)#define S1 1#define S2 2#define S3 3#define S4 4#define S5 5#define S6 6#define S7 7#define S8 8#define S9 9#define S10 10#define S11 11#define S12 12#define S13 13#define S14 14#define S15 15#define R1 21#define R2 22#define R3 23#define R4 24#define R5 25#define R6 26#define R7 27#define R8 28

25、#define acc 100/SLR(1)分析表,2130表示規(guī)約,120表示移近,0報(bào)錯(cuò)int TableMAXROWMAXCOL=S4,0,0,0,0,0,S5,0,S1,S2,S3,0,0,S6,S7,0,0,0,acc,0,0,0,0,23,23,23,8,9,0,23,0,0,0,0,26,26,26,26,26,0,26,0,0,0,4,0,0,0,0,0,5,0,10,2,3,0,28,28,28,28,28,0,28,0,0,0,4,0,0,0,0,0,5,0,0,11,3,4,0,0,0,0,0,5,0,0,12,3,4,0,0,0,0,0,5,0,0,0,13,4,0,0

26、,0,0,0,5,0,0,0,14,0,15,6,7,0,0,0,0,0,0,0,0,21,21,21,8,9,0,21,0,0,0,0,22,22,22,8,9,0,22,0,0,0,0,24,24,24,24,24,0,24,0,0,0,0,25,25,25,25,25,0,25,0,0,0,0,27,27,27,27,27,0,27,0,0,0;Yufa.cpp#include#include#include#include#include#includecifa.cpp#includetable.cpp # define PL 15 /+# define MI 16 /-# defin

27、e MU 17# define DI 18#define acc 100#define MAXState 20#define NUMBER 9/表達(dá)式的個(gè)數(shù)#define MAXTEMP 10/最多臨時(shí)變量的個(gè)數(shù)int StateMAXState;/狀態(tài)棧int i=0;int CURRENTSTATE=0;/標(biāo)識(shí)當(dāng)前狀態(tài)int LENGTHNUMBER=1,3,3,1,3,3,1,3,1;/表達(dá)式右邊的長(zhǎng)度int length;int index;/索引int Yes=0;/判斷是否結(jié)束int tag=0;/判斷是否需要調(diào)用詞法程序/extern char * cifa();double e

28、1,t4,f7;/整形char e1220,t4220,f7220;/字符型int TEMPMAXTEMP;int temp=0;int biaoshifu=0;FILE * fp;void REPORT_ERROR()Yes=1;long f1=ftell(fp);char ch=fgetc(fp);fseek(fp,-1,1);printf(語(yǔ)法錯(cuò)誤,第%d個(gè)字符%C處n,f1,ch);void Accept()Yes=1;/printf(語(yǔ)法分析成功n);int INDEX(char a)/獲取當(dāng)前字符串對(duì)應(yīng)的索引int i;switch (a)case E:i=8;return(i);

29、break;case T:i=9;return(i);break;case F:i=10;return(i);break;default: REPORT_ERROR();return(0); break;void Gen(int a,double i1,double i2,int t)/輸出四元式switch(a)case PL:printf(+,%g,%g,%d)n,i1,i2,t);break;case MI:printf(-,%g,%g,%d)n,i1,i2,t);break;case MU:printf(*,%g,%g,%d)n,i1,i2,t);break;case DI:print

30、f(/,%g,%g,%d)n,i1,i2,t);break;default:break;void yuyi(int n)/語(yǔ)義子程序switch(n)case 1: temp+;/產(chǎn)生臨時(shí)變量,Gen(PL,e1,t4,temp);e1=temp;break;case 2:temp+;/產(chǎn)生臨時(shí)變量,TEMPtempGen(MI,e1,t4,temp);e1=temp;break;case 3:e1=t4;break;case 4:temp+;Gen(MU,t4,f7,temp);t4=temp;break;case 5:temp+;Gen(DI,t4,f7,temp);t4=temp;break;case 6:t4=f7;break;case 7:f7=e1;break;case 8:f7=FCON;break;default :printf(errorn);break;void Gen2(int a,char *i1,char *i2,int t)/輸出四元式switch(a)case PL:printf(+,%s,%s,%d)n,&i1,&i2,t);break;case MI:printf(-,%s,%s,%d)n,&i1,&i2,t);break;case MU:printf(*,%s,

溫馨提示

  • 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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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)論