《編譯原理(實驗部分)》實驗2_PL0詞法分析.doc_第1頁
《編譯原理(實驗部分)》實驗2_PL0詞法分析.doc_第2頁
《編譯原理(實驗部分)》實驗2_PL0詞法分析.doc_第3頁
《編譯原理(實驗部分)》實驗2_PL0詞法分析.doc_第4頁
《編譯原理(實驗部分)》實驗2_PL0詞法分析.doc_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

編譯原理(實驗部分)實驗2_PL0詞法分析一、實驗?zāi)康募由詈挽柟虒τ谠~法分析的了解和掌握;初步認識PL/0語言的基礎(chǔ)和簡單的程序編寫;通過本實驗?zāi)軌虺醪降牧私夂驼莆粘绦蛟~法分析的整個過程;提高自己上機和編程過程中處理具體問題的能力。二、實驗設(shè)備1、PC 兼容機一臺;操作系統(tǒng)為WindowsWindowsXP。2、Visual C+ 6.0 或以上版本, Windows 2000 或以上版本,匯編工具(在Software 子目錄下)。三、實驗原理PL/O語言的編譯程序,是用高級語言PASCAL語言書寫的。整個編譯過程是由一些嵌套及并列的過程或函數(shù)完成。詞法分析程序是獨立的過程GETSYM完成,供語法分析讀單詞時使用。四、實驗步驟閱讀所給出的詞法分析程序(pl0_lexical.c),搞懂程序中每一個變量的含義,以及每一個過程的作用,并在該過程中進行中文注釋;閱讀完程序后,畫出各過程的流程圖;給出的程序包含兩處輸入錯誤,利用所給的pl/0源程序(test.pl0)對程序進行調(diào)試,使其能正確對所給文件進行分析并能夠解釋運行;在閱讀懂所給出的詞法分析程序后,將你對詞法分析的理解寫在實驗報告上。實驗代碼#includemain() printf (my name is 08061118 yuchaofeng );主程序:#include #include #include #include #include #define NULL 0FILE *fp;char cbuffer;char *key8=DO,BEGIN,ELSE,END,IF,THEN,VAR,WHILE;char *border6=,;,:=,.,(,);char *arithmetic4=+,-,*,/;char *relation6=,=,;char *consts20;char *label20;int constnum=0,labelnum=0;int search(char searchchar,int wordtype) int i=0; switch (wordtype) case 1:for (i=0;i=7;i+) if (strcmp(keyi,searchchar)=0) return(i+1); ; case 2:for (i=0;i=5;i+) if (strcmp(borderi,searchchar)=0) return(i+1); ; return(0); case 3:for (i=0;i=3;i+) if (strcmp(arithmetici,searchchar)=0) return(i+1); ; ; return(0); ; case 4:for (i=0;i=5;i+) if (strcmp(relationi,searchchar)=0) return(i+1); ; ; return(0); ; case 5:for (i=0;i=constnum;i+) if (strcmp(constsi,searchchar)=0) return(i+1); ; constsi-1=(char *)malloc(sizeof(searchchar); strcpy(constsi-1,searchchar); constnum+; return(i); ; case 6:for (i=0;i=labelnum;i+) if (strcmp(labeli,searchchar)=0) return(i+1); ; labeli-1=(char *)malloc(sizeof(searchchar); strcpy(labeli-1,searchchar); labelnum+; return(i); ; char alphaprocess(char buffer) int atype; int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer) alphatp+i=buffer; buffer=fgetc(fp); ; alphatpi+1=0; if (atype=search(alphatp,1) printf(%s (1,%d)n,alphatp,atype-1); else atype=search(alphatp,6); printf(%s (6,%d)n,alphatp,atype-1); ; return(buffer); char digitprocess(char buffer) int i=-1; char digittp20; int dtype; while (isdigit(buffer) digittp+i=buffer; buffer=fgetc(fp); digittpi+1=0; dtype=search(digittp,5); printf(%s (5,%d)n,digittp,dtype-1); return(buffer); char otherprocess(char buffer) int i=-1; char othertp20; int otype,otypetp; othertp0=buffer; othertp1=0; if (otype=search(othertp,3) printf(%s (3,%d)n,othertp,otype-1); buffer=fgetc(fp); goto out; ; if (otype=search(othertp,4) buffer=fgetc(fp); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,4) printf(%s (4,%d)n,othertp,otypetp-1); goto out; else othertp1=0; printf(%s (4,%d)n,othertp,otype-1); goto out; ; if (buffer=:) buffer=fgetc(fp); if (buffer=) printf(:= (2,2)n); buffer=fgetc(fp); goto out; else if (otype=search(othertp,2) printf(%s (2,%d)n,othertp,otype-1); buffer=fgetc(fp); goto out; ; if (buffer!=n)&(buffer!= ) printf(%c error,not a wordn,buffer); buffer=fgetc(fp);out: return(buffer); void main() int i; for (i=0;i=20;i+) labeli=NULL; constsi=NULL; ; if (fp=fopen(skh.c,r)=NULL) printf(error); else cbuffer = fgetc(fp); while (cbuffer!=EOF) if (isa

溫馨提示

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

評論

0/150

提交評論