編譯原理實驗報告(一)----詞法分析程序(共11頁)_第1頁
編譯原理實驗報告(一)----詞法分析程序(共11頁)_第2頁
編譯原理實驗報告(一)----詞法分析程序(共11頁)_第3頁
編譯原理實驗報告(一)----詞法分析程序(共11頁)_第4頁
編譯原理實驗報告(一)----詞法分析程序(共11頁)_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上 編譯原理實驗報告(一) 詞法分析程序【目的要求】 通過設計編制調(diào)試一個具體的詞法分析程序,加深對詞法分析原理的理解。并掌握在對程序設計語言源程序進行掃描過程中將其分解為各類單詞的詞法分析方法。 【題目分析】本實驗以用戶指定的想編譯的以C語言編寫的文件作為詞法分析程序的輸入數(shù)據(jù)。在進行詞法分析中,先自文件頭開始以行為單位掃描程序,將該行的字符讀入預先設定的一個數(shù)組緩沖區(qū)中,然后對該數(shù)組的字符逐詞分割,進行詞法分析,將每個詞分割成關(guān)鍵字、標識符、常量和運算符四種詞種,最終產(chǎn)生四個相對應的表,即關(guān)鍵字表、標識符表、常量表和運算符表,它們以文件的形式進行存儲。除此之外,還產(chǎn)

2、生一個編譯后的文件,它指定了每個詞在四個表中的位置?!緦嶒炦^程】下面就詞法分析程序中的文件和主要變量進行說明: 文件: cpile.c 主程序文件 key.txt 關(guān)鍵字文件 operation.txt 運算符文件 id.txt 標識符文件 const.txt 常量文件 after_com.txt 編譯后產(chǎn)生的文件 主要變量: FILE *sfp,*nfp,*ifp,*kfp,*cfp,*pfp; char ib5020 標識符表(動態(tài)生成)char cb5010 常量表(動態(tài)生成) char kb4410 關(guān)鍵字表(預先定義好的)char pb365 運算符表(預先定義好的) 主要的子函數(shù)名

3、: int number(char s,int i); 數(shù)字處理函數(shù) int letter(char s,int i); 字符處理函數(shù) int operation(char s,int i); 運算符處理函數(shù) void seti (char s); 標識符建立函數(shù)void setc (char s); 常量建立函數(shù) void cfile(char s1, char s2,int m); 將詞和詞所在表中位置寫入編譯后文件 void error1(char s); 字符處理出錯報告 void error2(char s); 標識符處理出錯報告 void error3(char s); 運算符處理

4、出錯報告void openall(); 打開所有文件void writeall(); 將四個表寫入文件void closeall(); 關(guān)閉所有文件下面簡要分析一下詞法分析程序的運行流程:能否打開所要編譯的C語言文件 否 報錯 能判斷當前字符是否是文件結(jié)束符是結(jié)束從源程序中讀入一行到數(shù)組緩沖區(qū)否判斷當前字符是否是n是否判斷當前字符是否是字母是轉(zhuǎn)關(guān)鍵字和標識符處理否判斷當前字符是否是數(shù)字是轉(zhuǎn)數(shù)字處理否判斷當前字符是否是運算符是轉(zhuǎn)運算符處理否【程序調(diào)試】現(xiàn)有源程序a.c清單如下: #include <stdio.h>int main(int argc, char *argv)char

5、ch; int i; ch='a' ch=ch+32 ; i=ch; printf("%d id %cn",i,ch);/*打印*/ return 0; 運行詞法分析程序后,顯示如下結(jié)果:after_com.txt文件:#p-2includei-0<p-14stdio.hi-1>p-16intk-2maini-2(p-7intk-2argci-3,p-6chark-0*p-9argvi-4p-18p-21)p-8p-23chark-0chi-5;p-13intk-2ii-6;p-13chi-5=p-15'p-19ai-7'p-19

6、;p-13chi-5=p-15chi-5+p-1032c-0;p-13ii-6=p-15chi-5;p-13printfk-33(p-7"p-1%p-4di-8idi-9%p-4ci-10p-20ni-11"p-1,p-6ii-6,p-6chi-5)p-8;p-13returnk-280c-1;p-13p-25key.txt 關(guān)鍵字文件:0 char1 short2 int3 unsigned4 long5 float6 double7 struct8 union9 void10 enum11 signed12 const13 volatile14 typedef15 au

7、to16 register17 static18 extem19 break20 case21 continue22 default23 do24 else25 for26 goto27 if28 return29 switch30 while31 sizeof32 txt33 printf34 FILE35 fopen36 NULL37 fclose38 exit39 r40 read41 close42 w43 fprintf id.txt 標識符文件:0 include1 stdio.h2 main3 argc4 argv5 ch6 i7 a8 d9 id10 c11 noperatio

8、n.txt 運算符文件:0 !1 "2 #3 $4 %5 &6 ,7 (8 )9 *10 +11 -12 :13 ;14 <15 =16 >17 ?18 19 '20 21 22 .23 24 |25 26 !=27 >=28 <=29 =30 +31 -32 &&33 /*34 */ const.txt 常量文件:0 321 0結(jié)果完全正確。詞法分析程序如下:#include <stdio.h>#include <string.h>#include <stdlib.h>#include &l

9、t;ctype.h>#define SIZE 256#define null 0int line=0,error=0,mark1=0,mark2=0; char *sname;FILE *sfp,*nfp,*ifp,*kfp,*cfp,*pfp;/*-*/ char ib5020; char cb5010; char kb4410="char","short","int","unsigned","long","float","double",&quo

10、t;struct","union","void", "enum","signed","const","volatile","typedef","auto","register","static","extem", "break","case","continue","default",&q

11、uot;do","else","for","goto", "if","return","switch","while","sizeof","txt","printf","FILE","fopen","NULL", "fclose","exit","r","re

12、ad","close","w","fprintf" char pb365="!",""","#","$","%","&",",","(",")","*","+","-",":","","<","=

13、",">","?","","'", "","",".","","|","","!=",">=","<=","=","+","-","&&","/*","*/"/*-定義

14、了四個二元數(shù)組存放四個表-*/int main(int argc, char *argv) char sbuffSIZE; char nbuffSIZE; int i;/*-*/ int number(char s,int i); int letter(char s,int i); int operation(char s,int i); void seti (char s); void setc (char s); void cfile(char s1, char s2,int m); void error2(char s); void error1(char s);void error3(

15、char s);void openall();void writeall();void closeall();/*-*/ if(argc!=2) printf("Please input the file you want to compile:n"); exit(1); else sname=argv1; sfp=fopen(argv1,"r"); openall(); i=0; while(i<50 ) strcpy(ibi," "); strcpy(cbi," "); i+; /*-*/ while(f

16、gets(sbuff,SIZE,sfp) i=0;line+; /*以行的方式讀取源代碼進行編譯*/while(sbuffi!='n') if(isalpha(sbuffi) /* 處理關(guān)鍵字和標識符 */ i=letter(sbuff,i); else if(isdigit(sbuffi) i=number(sbuff,i); /* 處理數(shù)字常量 */ else i=operation(sbuff,i); /* 處理運算符 */fputc('n',nfp); /*-*/ if(error)printf("!There are %d errors.&q

17、uot;,error); /* 最后錯誤個數(shù)報告 */elseprintf("Success to compile the file."); writeall();closeall(); return 0;/*$*/void openall() /* 打開所有必需的文件 */ kfp=fopen("key.txt","w+"); /* 打開關(guān)鍵字表 */ pfp=fopen("operation.txt","w+"); /* 打開運算符表 */ cfp=fopen("const.txt

18、","w+"); /* 打開常量文件 */ ifp=fopen("id.txt","w+"); /* 打開標識符文件 */ nfp=fopen("after_com.txt","w+"); /* 打開編譯后會產(chǎn)生的文件 */ if(kfp=NULL | cfp=NULL | ifp=NULL | pfp=NULL | nfp=NULL) printf("Cannot open these files"); /* 打開文件有錯,退出程序 */ exit(0); /*-

19、*/void writeall() int m=0; while (m<36&&pbm!=" ") fprintf(pfp,"%d%s%sn", m," ",pbm);m+; m=0; while (m<44&&kbm!=" ") fprintf(kfp,"%d%s%sn", m," ",kbm);m+; m=0; while (m<mark2&&cbm!=" ") fprintf(cfp,

20、"%d%s%sn", m," ",cbm);m+; m=0; while (m<mark1&&ibm!=" ") fprintf(ifp,"%d%s%sn", m," ",ibm);m+; /*-*/void closeall() /* 關(guān)閉所有文件 */ fclose(kfp); fclose(pfp); fclose(cfp); fclose(ifp); fclose(nfp);/*$*/int letter(char sbuff,int i)char str10; v

21、oid cfile(char s1, char s2,int m); void seti (char s); void setc (char s); int m=0,n=0; strn=sbuffi; while(isalnum(sbuffi)|sbuffi='.') strn+=sbuffi+; strn='0' while (m<44&&(strcmp(str,kbm)!=0) m+; if (m<44) cfile(str,"k",m); else m=0; while (m<50&&(

22、strcmp(str,ibm)!=0) m+; if (m<50) cfile (str,"i",m); else seti(str); return i;/*-*/int number(char sbuff,int i)char s2,str10; void cfile(char s1, char s2,int m);void seti (char s);void setc (char s); void error2(char s); void error1(char s);int m=0,n=0;strn=sbuffi;i+;n+; if(sbuffi='

23、.') strn=sbuffi; if (sbuffi='E' | sbuffi='e') && (sbuffi+1='+' | sbuffi+1='-' |isdigit(sbuffi+1) strn=sbuffi;strn+=sbuffi+; while (isdigit(sbuffi) strn+=sbuffi+; /*對可能出現(xiàn)的十進制數(shù)的處理*/ strn+='0' while (m<50&&(strcmp(str,cbm)!=0) m+; if (isalp

24、ha(sbuffi) error2(str); else if (m<50) cfile(str,"c",m); else setc(str);return i;/*-*/int operation(char sbuff,int i) void error3(char s); void cfile(char s1, char s2,int m); void setc (char s); int k=0,m=0;char s4; if (sbuffi='/'&&sbuffi+1='*') i=i+2; while (!(sbuffi='*'&&sbuffi+1='/') i+; i=i+2; /*若為注釋則跳過*/ else if(sbuffi=' ') while(sbuffi=' ') i+; else sk=sbuffi; while (k<3) sk+=sbuffi+; while (k>=0) sk='0' m=0; while (m<36&&(strcmp(s,pbm)!=0) m+; if (m<36) cf

溫馨提示

  • 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

提交評論