




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、編譯原理-詞法分析器的設計 一 設計說明及設計要求一般來說,編譯程序的整個過程可以劃分為五個階段:詞法分析、語法分析、中間代碼生成、優(yōu)化和目標代碼生成。本課程設計即為詞法分析階段。詞法分析階段是編譯過程的第一個階段。這個階段的任務是從左到右一個字符一個字符地讀入源程序,對構成源程序的字符流進行掃描和分解,從而識別出一個個單詞(也稱單詞符號或符號)。如保留字(關鍵字或基本字)、標志符、常數、算符和界符等等。二 設計中相關關鍵字說明1 基本字:也稱關鍵字,
2、如C語言中的 if , else , while , do ,for,case,break, return 等。2 標志符:用來表示各種名字,如常量名、變量名和過程名等。3 常數:各種類型的常數,如12,6.88,和“ABC”等。4
3、0; 運算符:如 + ,- , * , / ,%, < , > ,<= , >= 等。5 界符,如逗點,冒號,分號,括號,# , , 等。 三 、程序分析 詞法分析是編譯的第一個階段,它的主要任務是從左到右逐個字符地對源程序進行掃描,產生一個個單詞序列,用以語法分析。詞法分析工作可以是獨立的一遍,把字符流的源程序變?yōu)閱卧~序列,輸出在一個中間文件上,這個文件做為語法分析程序的輸入而繼續(xù)編
4、譯過程。然而,更一般的情況,常將詞法分析程序設計成一個子程序,每當語法分析程序需要一個單詞時,則調用該子程序。詞法分析程序每得到一次調用,便從源程序文件中讀入一些字符,直到識別出一個單詞,或說直到下一個單詞的第一個字符為止。四 、模塊設計下面是程序的流程圖五 、 程序介紹在程序當前目錄里建立一個文本文檔,取名為infile.txt,所有需要分析的程序都寫在此文本文檔里,程序的結尾必須以“”標志符結束。程序結果輸出在同一個目錄下,文件名為outfile.txt,此文件為自動生成。本程序所輸出的單詞符號采用以下二元式表示:(單詞種別,單詞自身的值)如程序輸出結果 (57,"#&
5、quot;)(33,"include")(52,"<")(33,"iostream") 等。程序的功能:(1) 能識別C語言中所有關鍵字(共32個)(單詞種別分別為1 32 ,詳情見程序代碼相關部分,下同)(2) 能識別C語言中自定義的標示符 (單詞種別為 33)(3) 能識別C語言中的常數 (單詞種別為0)(4) 能識別C語言中幾乎所有運算符(單詞種別分別為41 54)(5) 能識別C語言中絕大多數界符 (單詞種別分別為
6、 55 66)六 、運行結果輸入文件infile.txt運行結果(輸出文件 outfile.txt)七 、 設計體會八、附錄部分(程序代碼)單詞符號類別編碼單詞符號類別編碼單詞符號類別編碼單詞符號類別編碼if3float21+31#62then4short22-32.63else5unsigned23*33,64while6continue24/34:65do7for25<35>=39begin8signed26>36<=38end9void27=37=41long10default28:=51!=42switch11goto29(52%40cas
7、e12sizeof30)53標識符1enum13volatile43;54常數2register14auto4455typedef15double4556char16int 4657extern17struct4758return18break48<<59union19static49>>60const20“61提示:文件的打開和讀寫函數:FILE *fp,*out; /定義文件指針fp=fopen("infile.txt","r")如果打開文件"infile.txt"失敗,則函數返回NULL,即fp=NULL
8、,第二個參數“r”表示以只讀方式打開,如果為”w”, 則以可寫方式打開調用fgetc(fp)這個函數一次從fp所指向的文件讀取一個字符char ch=fgetc(fp);想文件寫字符的函數為fprintf(FILE * fp,寫進的內容)比如下面的調用fprintf(outfile,"abcdn")是把字符串“abcd”寫到文件outfile的末尾,并且在后面加上了一個換行標志文件讀寫完成后要用函數fclose(fp)關閉。源代碼#include "stdio.h"#include "string.h"#include "c
9、type.h"void analzid(FILE *output,char *p) int i=0; int count=0; if (isalpha(p0)if (strcmp(p,"if")=0) fprintf(output,"(3,if)n");else if(strcmp(p,"then")=0) fprintf(output,"(4,then)n");else if(strcmp(p,"else")=0) fprintf(output,"(5,else)n&quo
10、t;);else if(strcmp(p,"while")=0) fprintf(output,"(6,while)n");else if(strcmp(p,"do")=0) fprintf(output,"(7,do)n");else if(strcmp(p,"begin")=0) fprintf(output,"(8,begin)n");else if(strcmp(p,"end")=0) fprintf(output,"(9,end)n&q
11、uot;);else if(strcmp(p,"long")=0) fprintf(output,"(10,long)n");else if(strcmp(p,"switch")=0) fprintf(output,"(11,switch)n");else if(strcmp(p,"case")=0) fprintf(output,"(12,case)n");else if(strcmp(p,"enum")=0) fprintf(output,"
12、(13,enum)n");else if(strcmp(p,"register")=0) fprintf(output,"(14,register)n");else if(strcmp(p,"typedef")=0) fprintf(output,"(15,typedef)n");else if(strcmp(p,"char")=0) fprintf(output,"(16,char)n");else if(strcmp(p,"extern")=
13、0) fprintf(output,"(17,extern)n");else if(strcmp(p,"return")=0) fprintf(output,"(18,return)n");else if(strcmp(p,"union")=0) fprintf(output,"(19,union)n");else if(strcmp(p,"const")=0) fprintf(output,"(20,const)n");else if(strcmp(p,
14、"float")=0) fprintf(output,"(21,float)n");else if(strcmp(p,"short")=0) fprintf(output,"(22,short)n");else if(strcmp(p,"unsigned")=0) fprintf(output,"(23,unsigned)n");else if(strcmp(p,"continue")=0) fprintf(output,"(24,continu
15、e)n");else if(strcmp(p,"for")=0) fprintf(output,"(25,for)n");else if(strcmp(p,"signed")=0) fprintf(output,"(26,signed)n");else if(strcmp(p,"void")=0) fprintf(output,"(27,void)n");else if(strcmp(p,"default")=0) fprintf(output,
16、"(28,default)n");else if(strcmp(p,"goto")=0) fprintf(output,"(29,goto)n");else if(strcmp(p,"sizeof")=0) fprintf(output,"(30,sizeof)n");else if(strcmp(p,"volatile")=0) fprintf(output,"(43,volatile)n");else if(strcmp(p,"auto&qu
17、ot;)=0) fprintf(output,"(44,auto)n");else if(strcmp(p,"double")=0) fprintf(output,"(45,double)n");else if(strcmp(p,"int")=0) fprintf(output,"(46,int)n");else if(strcmp(p,"struct")=0) fprintf(output,"(47,struct)n");else if(strcmp(p
18、,"break")=0) fprintf(output,"(48,break)n");else if(strcmp(p,"static")=0) fprintf(output,"(49,static)n");else fprintf(output,"(1,%s)n",p); else for(;i<(int)strlen(p);i+) if(isdigit(pi) count+; if (count=(int)strlen(p) fprintf(output,"(2,%s)n&q
19、uot;,p); else if (p0='_'&&(isalpha(p1) fprintf(output,"(1,%s)n",p); else fprintf(output,"%s 未定義n",p); void analzsy(FILE *outfile,char *p) if (strcmp(p,"=")=0) fprintf(outfile,"(37,=)n"); else if(strcmp(p,"+")=0) fprintf(outfile,"
20、(31,+)n"); else if(strcmp(p,"-")=0) fprintf(outfile,"(32,-)n"); else if(strcmp(p,"*")=0) fprintf(outfile,"(33,*)n"); else if(strcmp(p,"/")=0) fprintf(outfile,"(34,/)n"); else if(strcmp(p,"(")=0) fprintf(outfile,"(52,()n&
21、quot;); else if(strcmp(p,")")=0) fprintf(outfile,"(53,)n"); else if(strcmp(p,"")=0) fprintf(outfile,"(55,)n"); else if(strcmp(p,"")=0) fprintf(outfile,"(56,)n"); else if(strcmp(p,"")=0) fprintf(outfile,"(57,)n"); else if
22、(strcmp(p,"")=0) fprintf(outfile,"(58,)n"); else if(strcmp(p,"<<")=0) fprintf(outfile,"(59,<<)n"); else if(strcmp(p,">>")=0) fprintf(outfile,"(60,>>)n"); else if(strcmp(p,"'")=0) fprintf(outfile,"(
23、61,')n"); else if(strcmp(p,"#")=0) fprintf(outfile,"(62,#)n"); else if(strcmp(p,".")=0) fprintf(outfile,"(64,.)n"); else if(strcmp(p,"*")=0) fprintf(outfile,"(33,*)n"); else if(strcmp(p,"/")=0) fprintf(outfile,"(34,/
24、)n"); else if(strcmp(p,"%")=0) fprintf(outfile,"(40,%)n"); else if(strcmp(p,",")=0) fprintf(outfile,"(64,)n"); else if(strcmp(p,":")=0) fprintf(outfile,"(65,:)n"); else if(strcmp(p,"")=0) fprintf(outfile,"(54,;)n");
25、 else if(strcmp(p,">")=0) fprintf(outfile,"(36,>)n"); else if(strcmp(p,"<")=0) fprintf(outfile,"(35,<)n"); else if(strcmp(p,">=")=0) fprintf(outfile,"(39,>=)n"); else if(strcmp(p,"<=")=0) fprintf(outfile,"
26、;(38,<=)n"); else if(strcmp(p,"=")=0) fprintf(outfile,"(41,=)n"); else if(strcmp(p,"!=")=0) fprintf(outfile,"(42,!=)n"); else if(strcmp(p," ")=0) ; else if(strcmp(p,"n")=0) ; else fprintf(outfile,"%s 未定義n",p);void main()FI
27、LE *fp,*out;int i=0,x=0,y=0;int EA=0;char ch,str10000,idstr10,systr2;if(fp=fopen("infile.txt","r")=NULL) printf("Can not open infile!n"); exit(0);if(out=fopen("outfile.txt","w")=NULL) printf("Can not open outfile!n"); exit(0);ch=fgetc(fp);w
28、hile(ch!=EOF)stri=ch;stri+1='0'i+;ch=fgetc(fp);i=0;while(1) if(stri='') break; else if(stri>='a'&&stri<='z')|(stri>='A'&&stri<='Z')| (stri>='0'&&stri<='9')|(stri='_') idstrx=stri;idstrx+1='0'x+;i+;EA=1;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 山東女子學院《田徑Ⅰ》2023-2024學年第二學期期末試卷
- 內蒙古通遼市科爾沁區(qū)第七中學2025年初三下化學試題期中模擬試題含解析
- 張家口市懷來縣2025年數學四年級第二學期期末統(tǒng)考試題含解析
- 濟寧職業(yè)技術學院《文化人類學經典導讀》2023-2024學年第二學期期末試卷
- 上海海事職業(yè)技術學院《俄羅斯國情文化》2023-2024學年第一學期期末試卷
- 山西藝術職業(yè)學院《汽車輕量化技術》2023-2024學年第二學期期末試卷
- 上海外國語大學賢達經濟人文學院《衛(wèi)星導航定位原理與應用》2023-2024學年第二學期期末試卷
- 江西省吉安市遂川中學2025屆高三下學期第一次考試語文試題含解析
- 吉林農業(yè)大學《血液流變學與人體健康》2023-2024學年第一學期期末試卷
- 遼寧職業(yè)學院《農業(yè)企業(yè)管理學》2023-2024學年第二學期期末試卷
- 項目質量管理機構結構框圖
- 一例視神經脊髓炎的護理查房
- 學校“五項管理”問題臺賬
- 眼解剖(簡單版)課件
- 施工進度計劃網絡圖-練習題知識講解
- 廚房隔油池清理記錄
- 常見生物相容性實驗匯總
- 綜合探究三 探尋絲綢之路(課堂運用)
- 企業(yè)重組相關稅收政策培訓教學課件(38張)
- 肝癌的防治(大眾科普版本)-PPT課件
- 職業(yè)危害防治實施管理臺賬
評論
0/150
提交評論