詞法分析程序的構(gòu)造_第1頁
詞法分析程序的構(gòu)造_第2頁
詞法分析程序的構(gòu)造_第3頁
詞法分析程序的構(gòu)造_第4頁
詞法分析程序的構(gòu)造_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)通達學院專業(yè)課程設(shè)計 II題 目: 詞法分析程序的構(gòu)造 專 業(yè) 計 算 機 通 信 學 生 姓 名 班 級 學 號 28 班 指 導 教 師 徐 佳 指 導 單 位 計算機學院計算機科學與技術(shù)系日 期 2012.11.12-2012.11.23 精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)教教師師評評語語同學出勤率(滿勤、較高、一般,較低),學習態(tài)度(端正、較端正、一般、較差),程序設(shè)計基礎(chǔ)(好、較好、一般、較差) ,演示程序(已經(jīng)、沒有)達到了基本要求,算法設(shè)計(好、較好、一般) ,界面友好程度(好、較好、一般) ,答辯過程中回答問題(準確、較準確、錯

2、誤率較高) ,撰寫報告格式(規(guī)范、一般)、內(nèi)容(豐滿、簡單) 、表述(清晰、一般、不清楚), (圓滿、較好、基本)完成了課題任務。 教師簽名:教師簽名: 年年 月月 日日成成 績績 評評 定定備備 注注精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) 詞法分析程序的構(gòu)造詞法分析程序的構(gòu)造一、一、 課題內(nèi)容和要求課題內(nèi)容和要求通過狀態(tài)轉(zhuǎn)換圖構(gòu)造 C 或者 PASCAL 語言子集的詞法分析程序。原理解析:選取語言,例如選取了 C 語言,選取其中一個子集,例如包含了部分關(guān)鍵字 main、float、if、for 等等,特殊符號( 、=、+等等,特殊定義的標識符變量以及部分常量等,采用編譯原理詞法分析中有窮

3、自動機的思想構(gòu)建出該語言子集的狀態(tài)轉(zhuǎn)換圖,并編碼實現(xiàn)。基本要求:(1)將選取的語言子集編寫一個簡單程序,放在一個文本文件中;(2)要將一個個單詞區(qū)分清楚并歸類(例如 for 屬于關(guān)鍵字)。二、需求和思路分析二、需求和思路分析本課題是用 C+語言設(shè)計,選取的是 C 語言子集。編寫對簡單語言進行詞法分析的詞法分析程序。1、識別子集中的關(guān)鍵字、標識符、常數(shù)、運算符和分界符等。2、對子集中的字符類型進行歸類三、概要設(shè)計三、概要設(shè)計(1)狀態(tài)轉(zhuǎn)換圖: 精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)(2)核心代碼: 1)定義:char cbuffer;char*keyword14=if,else,for,wh

4、ile,do,float,return,break,continue,int,void,main,const,printf; /關(guān)鍵字char *border8= , , ; , , , ( , ) ,:=,.; /分隔符char *arithmetic6=+ , - , * , / , + , -; /運算符char *relation7= , , = , = ,!=; /關(guān)系運算符 char *lableconst80; /標識符2)函數(shù)調(diào)用:search(char searchchar,int wordtype)/查找類型alphaprocess(char buffer) /字符處理過程

5、digitprocess(char buffer) /數(shù)字處理過程otherprocess(char buffer) /分隔符、運算符、邏輯運算符等main()/主函數(shù)3) 狀態(tài)類型:狀態(tài)轉(zhuǎn)換圖的形式: 每個狀態(tài)對應一個帶標號的 case 語句 轉(zhuǎn)向邊對應 goto 語句switch (wordtype) case 1: for (i=0;i=13;i+) /關(guān)鍵字 if (strcmp(keywordi,searchchar)=0) return(i+1); return(0); case 2: for (i=0;i=7;i+) /分隔符 if (strcmp(borderi,searchc

6、har)=0) return(i+1); return(0);精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) case 3: for (i=0;i=5;i+) /運算符 if (strcmp(arithmetici,searchchar)=0) return(i+1); return(0); case 4: for (i=0;i=6;i+) /關(guān)系運算符 if (strcmp(relationi,searchchar)=0) return(i+1); return(0); case 5: for (t=40;t=constnum;t+) /常數(shù) if (strcmp(searchchar,labl

7、econstt)=0)/判斷該常數(shù)是否已出現(xiàn)過 return(t+1); lableconstt-1=(char *)malloc(sizeof(searchchar); /為新的元素分配內(nèi)存空間 strcpy(lableconstt-1,searchchar); /為數(shù)組賦值 lableconst指針數(shù)組名 constnum+; /常數(shù)個數(shù)自加 return(t); case 6: 精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) for (i=0;i=lableconstnum;i+) if (strcmp(searchchar,lableconsti)=0) /判斷標識符是否已出現(xiàn)過 retu

8、rn(i+1); lableconsti-1=(char *)malloc(sizeof(searchchar); strcpy(lableconsti-1,searchchar); lableconstnum+; /標識符個數(shù)自加 return(i); 5) 單字符判斷 if (otypetp=search(othertp,3) /判斷該運算符是否是由連續(xù)的兩個字符組成的 coutrow: row String= othertpttt運算符endl; fp.get(buffer); goto out; else /單字符邏輯運算符 othertp1=0; coutrow: row Strin

9、g= othertpttt邏輯運算符endl; goto out; 四、詳細設(shè)計四、詳細設(shè)計實驗環(huán)境:visual C+6.0 win7 系統(tǒng)源程序代碼:#include #include #include #include #include 精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)using namespace std;ifstream fp(.txt,ios:in);char cbuffer;char *keyword14=if,else,for,while,do,float,return,break,continue,int,void,main,const,printf; /關(guān)鍵字ch

10、ar *border8= , , ; , , , ( , ) ,:=,.; /分隔符char *arithmetic6=+ , - , * , / , + , -; /運算符char *relation7= , , = , = ,!=; /關(guān)系運算符 char *lableconst80; /標識符int constnum=40;int lableconstnum=0; /統(tǒng)計常數(shù)和標識符數(shù)量int row=1; int search(char searchchar,int wordtype) int i=0,t=0; switch (wordtype) case 1: for (i=0;i=

11、13;i+) /關(guān)鍵字 if (strcmp(keywordi,searchchar)=0) return(i+1); return(0); case 2: for (i=0;i=7;i+) /分隔符 if (strcmp(borderi,searchchar)=0) return(i+1); return(0); case 3: for (i=0;i=5;i+) /運算符 if (strcmp(arithmetici,searchchar)=0) return(i+1); return(0);精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) case 4: for (i=0;i=6;i+) /關(guān)系

12、運算符 if (strcmp(relationi,searchchar)=0) return(i+1); return(0); case 5: for (t=40;t=constnum;t+) /常數(shù) if (strcmp(searchchar,lableconstt)=0)/判斷該常數(shù)是否已出現(xiàn)過 return(t+1); lableconstt-1=(char *)malloc(sizeof(searchchar); /為新的元素分配內(nèi)存空間 strcpy(lableconstt-1,searchchar); /為數(shù)組賦值 lableconst 指針數(shù)組名 constnum+; /常數(shù)個數(shù)

13、自加 return(t); case 6: for (i=0;i=lableconstnum;i+) if (strcmp(searchchar,lableconsti)=0) /判斷標識符是否已出現(xiàn)過 return(i+1); lableconsti-1=(char *)malloc(sizeof(searchchar); strcpy(lableconsti-1,searchchar); lableconstnum+; /標識符個數(shù)自加 return(i); default:cout錯誤!; char alphaprocess(char buffer) /字符處理過程 int atype;

14、精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer) /這兩個函數(shù)分別是判字符和判數(shù)字函數(shù)位于 ctype.h 中 alphatp+i=buffer; fp.get(buffer); alphatpi+1=0;/在末尾添加字符串結(jié)束標志 if (atype=search(alphatp,1) coutrow: row String= alphatpttt關(guān)鍵字endl; else atype=search(alphatp,6); /標識符 coutrow: row Stri

15、ng= alphatpttt標識符endl; return(buffer);char digitprocess(char buffer) /數(shù)字處理過程 int i=-1; char digittp20; int dtype; while (isdigit(buffer) digittp+i=buffer; fp.get(buffer); digittpi+1=0; dtype=search(digittp,5); coutrow: row String= digittpttt數(shù)字endl; return(buffer);char otherprocess(char buffer) /分隔符、

16、運算符、邏輯運算符等 int i=-1; char othertp20; int otype,otypetp; othertp0=buffer; othertp1=0; if (otype=search(othertp,3) 精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) fp.get(buffer); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,3) /判斷該運算符是否是由連續(xù)的兩個字符組成的 coutrow: row String= othertpttt運算符endl; fp.get(buffer); goto out; el

17、se /單字符邏輯運算符 othertp1=0; coutrow: row String= othertpttt邏輯運算符endl; goto out; if (otype=search(othertp,4) /關(guān)系運算符 fp.get(buffer); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,4) /判斷該關(guān)系運算符是否是由連續(xù)的兩個字符組成的 coutrow: row String= othertpttt關(guān)系運算符endl; fp.get(buffer); goto out; else /單字符邏輯運算符 othert

18、p1=0; coutrow: row String= othertpttt邏輯運算符endl; goto out; if (buffer=!) /=的判斷 fp.get(buffer); if (buffer=) /cout!= (2,2)n; fp.get(buffer); goto out;精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) else if (otype=search(othertp,2) /分界符 coutrow: row String= othertpttt分隔符endl; fp.get(buffer); goto out; if (buffer!=n)&(buffer

19、!= ) cout錯誤!非法字符為:tttbufferendl; fp.get(buffer); out: return(buffer);void main()printf(=詞法分析器=n); int i; for (i=0;i=50;i+) lableconsti= ;/用于保存標識符 if (!fp) cout源文件無法打開,請檢查!endl; else fp.get (cbuffer); while (!fp.eof() if(cbuffer=n) row+; fp.get(cbuffer); else if (isalpha(cbuffer) cbuffer=alphaprocess

20、(cbuffer); else if (isdigit(cbuffer)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè) cbuffer=digitprocess(cbuffer); else cbuffer=otherprocess(cbuffer); cout標識符個數(shù)是:lableconstnum分別是endl; i=0; while(ilableconstnum) coutlableconsti+ ; coutendl; cout詞法分析結(jié)束!n; getchar(); (3)程序流程圖:精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)五、測試數(shù)據(jù)及其結(jié)果分析五、測試數(shù)據(jù)及其結(jié)果分析 若源程序中沒有.txt 文檔,則會出

溫馨提示

  • 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

提交評論