編譯原理C語言詞法分析器_第1頁
編譯原理C語言詞法分析器_第2頁
編譯原理C語言詞法分析器_第3頁
編譯原理C語言詞法分析器_第4頁
編譯原理C語言詞法分析器_第5頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、編譯原理 C語言詞法分析器一、實驗題目編制并調試C詞法分析程序。a. txt源代碼: main() int sum=0 ,it=1;/* Variable declaration*/ if (sum=1) it+; else it=it+2; 設計其詞法分析程序,能識別出所有的關鍵字、標識符、常數(shù)、運算符(包括復合運算符,如+)、界符;能過濾掉源程序中的注釋、空格、制表符、換行符;并且能夠對一些詞法規(guī)則的錯誤進行必要的處理,如:標識符只能由字母、數(shù)字和下劃線組成,且第一個字符必須為字母或下劃線。實驗要求:要給出所分析語言的詞法說明,相應的狀態(tài)轉換圖,單詞的種別編碼方案,詞法分析程序的主要算法思

2、想等。 二、實驗目的 1、理解詞法分析在編譯程序中的作用; 2、掌握詞法分析程序的實現(xiàn)方法和技術; 3、加深對有窮自動機模型的理解。 三、主要函數(shù)void load ( ) void char_search (char *word)void main ( ) void intb_search(char *word)void scan ( ) void c_search(char *word)void inta_search(char *word) void cc_search(char *word)四、設計1.主函數(shù) void main ( )繪制程序界面調用初始化函數(shù):void load (

3、 ) 實現(xiàn)文件的建立調用主掃描函數(shù):void scan ( ) 實現(xiàn)文件的掃描分析完成后指引用戶查看相關文件,直到用戶輸入退出命令函數(shù)結束2. 初始化函數(shù) void load ( )通過文件指針建立相關文件函數(shù)結束3. 保留字及標識符判斷函數(shù) void char_search(char *word)接收數(shù)據(jù)是否為保留字否:查標識符表是:寫入輸出文件沒找到:寫入表文件找到:寫入輸出文件寫入輸出文件4. 整數(shù)類型判斷函數(shù) void inta_search(char *word)接收數(shù)據(jù)找到:寫入輸出文件寫入輸出文件查表沒找到:寫入整數(shù)常量表文件5. 浮點類型判斷函數(shù) void intb_searc

4、h(char *word)沒找到:寫入整數(shù)常量表文件找到:寫入輸出文件查表接收數(shù)據(jù)寫入輸出文件6. 字符串常量判斷函數(shù) void cc_search(char *word)7. 字符常量判斷函數(shù) void c_search(char *word) 同4、5函數(shù)圖8.主掃描函數(shù) void scan ( )函數(shù)開始讀入源文件中的一個單詞判斷宏定義忽略無效字符忽略調用保留字及標識符函數(shù)void char_search進行處理字符串字符常量調用字符常量判斷函數(shù)void c_search進行處理調用字符串常量判斷函數(shù)void cc_search進行處理字符串常量整數(shù)常量調用整數(shù)類型判斷函數(shù)void in

5、ta_search進行處理調用浮點類型判斷函數(shù)void intb_search進行處理浮點數(shù)常量注釋寫入注釋文件查運算符、分隔符表并寫入輸出文件限制符五、關鍵代碼#include #include #include char *key0= ,auto,break,case,char,const,continue,default,do,double,else,enum,extern,float,for,goto,if,int,long,register,return,short,signed,sizeof,static,struct,switch,typedef,_Complex,_Imagin

6、ary,union,unsigned,void,volatile,while;/*保留字表*/char *key1= ,(,),;,;/*分隔符表*/char *key2= ,+,-,*,/,%,=,=,=,!=,!,&,|,|,&,=,?:,-,+,-,.,+=,-=,*=,/=;/*運算符表*/int xx035,xx110,xx231;int temp_key3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;/* 初始化函數(shù) */void load()int mm;for (mm=0;mm=34;mm+)xx0mm=0;for (mm=0;m

7、m=9;mm+)xx1mm=0;for (mm=0;mm=30;mm+)xx2mm=0;FILE *floading;if (floading=fopen(key0.txt,w)=NULL)printf(Error! Cant create file : key0.txt);return;fclose (floading);/*建立保留字表文件:key0.txt*/if (floading=fopen(key1.txt,w)=NULL)printf(Error! Cant create file : key1.txt);return;/*建立分隔符表文件:key1.txt*/if (fload

8、ing=fopen(key2.txt,w)=NULL)printf(Error! Cant create file : key2.txt);return;fclose(floading);/*建立運算符表文件:key2.txt*/if (floading=fopen(key3.txt,w)=NULL)printf(Error! Cant create file : key3.txt);return;fclose (floading);/*建立標識符表文件:key3.txt*/if (floading=fopen(c40.txt,w)=NULL)printf(Error! Cant create

9、 file : c40.txt);return;fclose (floading);/*建立整數(shù)類型常量表文件:c40.txt*/if (floading=fopen(c41.txt,w)=NULL)printf(Error! Cant create file : c41.txt);return;fclose (floading);/*建立浮點類型常量表文件:c41.txt*/if (floading=fopen(c42.txt,w)=NULL)printf(Error! Cant create file : c42.txt);return;fclose (floading);/*建立字符類型

10、常量表文件:c42.txt*/if (floading=fopen(c43.txt,w)=NULL)printf(Error! Cant create file : c43.txt);return;fclose (floading);/*建立字符串類型常量表文件:c43.txt*/ if (floading=fopen(defination.txt,w)=NULL)printf(Error! Cant create file : defination.txt);return;fclose (floading);/*建立注釋文件:defination.txt*/if (floading=fope

11、n(output.txt,w)=NULL)printf(Error! Cant create file : output.txt);return;fclose (floading);/*建立內部碼文件:output.txt*/if (floading=fopen(temp_key1,w)=NULL)printf(Error! Cant create file : temp_key1);return;fclose (floading);/*建立保留字臨時表文件:temp_key1*/if (floading=fopen(temp_key3,w)=NULL)printf(Error! Cant c

12、reate file : temp_key3);return;fclose (floading);/*建立標識符臨時文件:temp_key3*/if (floading=fopen(temp_c40,w)=NULL)printf(Error! Cant create file : temp_c40);return;fclose (floading);/*建立整數(shù)類型常量臨時文件:temp_c40*/if (floading=fopen(temp_c41,w)=NULL)printf(Error! Cant create file : temp_c41);return;fclose (fload

13、ing);/*建立浮點類型常量臨時文件:temp_c41*/if (floading=fopen(temp_c42,w)=NULL)printf(Error! Cant create file : temp_c42);return;fclose (floading);/*建立字符類型常量臨時文件:temp_c42*/if (floading=fopen(temp_c43,w)=NULL)printf(Error! Cant create file : temp_c43);return;fclose (floading);/*建立字符串類型常量臨時文件:temp_c43*/* 保留字及標識符判斷

14、函數(shù) */void char_search(char *word)int m,line=0,csi=0;int value=0;int value2=0;char c,cs100;FILE *foutput,*finput;for (m=1;m)chin=fgetc(fin);/*chin=fgetc(fin);*/*對空格符、水平制表符進行處理*/else if (chin= )|(chin=t);/*對回車符進行處理*/else if (chin=n);/*對單引號內的字符常量進行處理*/else if (chin=target0)if (xx19=0)fout=fopen(key1.tx

15、t,a);fprintf(fout,1t9tt%cn,target0);fclose(fout);xx19=1;temp0=chin;chin=fgetc(fin);temp1=chin;chin=fgetc(fin);if (chin!=target0)temp2=chin;chin=fgetc(fin);temp3=chin;temp4=0;elsetemp2=chin;temp3=0;c_search(temp);/*對雙引號內的字符串常量進行處理*/else if (chin=)int i=0;tempi+=;chin=fgetc(fin);while (chin!=)tempi+=c

16、hin;chin=fgetc(fin);tempi=;tempi+1=0;cc_search(temp);/*對保留字、標識符進行處理*/else if (chin=A)&(chin=a)&(chin=A)&(chin=a)&(chin=0)&(chin=0)&(chin=0)&(chin=9)|(chin=.)if (chin=.)dotcount+;if (dotcount=2)break;tempi+=chin;chin=fgetc(fin);tempi=0;if (dotcount=1)intb_search(temp);elseinta_search(temp);if (chin!=

17、EOF)fseek (fin,-1L,SEEK_CUR);/*對注釋進行處理*/else if (chin=/)chin=fgetc(fin);if (chin=)fout=fopen(output.txt,a);fprintf(fout,2t30tt/=n);fclose(fout);else if (chin!=*)fout=fopen(output.txt,a);fprintf(fout,2t4tt/n);fclose(fout);fseek(fin,-1L,SEEK_CUR);else if (chin=*)count=0;chin=fgetc(fin);fout=fopen(defi

18、nation.txt,a);fprintf(fout,/*);while (count!=2)count=0;while (chin!=*)fprintf(fout,%c,chin);chin=fgetc(fin);count+;fprintf(fout,%c,chin);chin=fgetc(fin);if (chin=/)count+;fprintf(fout,%cn,chin);elsefprintf(fout,%c,chin);chin=fgetc(fin);/*對運算符、分隔符進行處理*/elseint time=0;int firstblood=0;temp0=chin;chin=

19、fgetc(fin);if (chin!=EOF)temp1=chin;temp2=0;for (time=1;time=30;time+)if (strcmp(temp,key2time)=0)firstblood=1;if (xx2time=0)fout=fopen(key2.txt,a);fprintf(fout,2t%dtt%sn,time,temp);fclose(fout);xx2time=1;fout=fopen(output.txt,a);fprintf(fout,2t%dtt%sn,time,temp);fclose(fout);break;if (firstblood!=1

20、)fseek(fin,-1L,SEEK_CUR);temp1=0;for (time=1;time=9;time+)if (strcmp(temp,key1time)=0)if (xx1time=0)fout=fopen(key1.txt,a);fprintf(fout,1t%dtt%sn,time,temp);fclose(fout);xx1time=1;fout=fopen(output.txt,a);fprintf(fout,1t%dtt%sn,time,temp);fclose(fout);break;for (time=1;time 查看保留字表文件 *n);printf(* 1 -

21、 查看分隔符表文件 *n);printf(* 2 - 查看運算符表文件 *n);printf(* 3 - 查看標識符表文件 *n);printf(* 4 - 查看整數(shù)類型常量表 *n);printf(* 5 - 查看浮點類型常量表 *n);printf(* 6 - 查看字符類型常量表 *n);printf(* 7 - 查看字符串類型常量表 *n);printf(* 8 - 查看注釋文件 *n);printf(* 9 - 查看內部碼文件 *n);printf(* - *n);printf(* Q - 退出 *n);printf(*n);printf(n);load();scan();printf

22、(n);printf(分析完成!n);getchar();printf(n);printf(請輸入命令:);command=getchar();while (command!=Q)&(command!=q)switch (command)case 0:printf(*n);printf(n);fread=fopen(key0.txt,r);charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fread);printf(n);printf(*n);printf(n);printf(請輸入命令:);break;case 1:printf(*n);printf(n);fread=fopen(key1.txt,r);charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fread);printf(n);printf(*n);printf(n);printf(請輸入命令:);break;case 2:printf(*n);printf(n);fread=fopen(key2.txt,r);charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fr

溫馨提示

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

最新文檔

評論

0/150

提交評論