




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)通達學院專業(yè)課程設計 II題 目: 詞法分析程序的構造 專 業(yè) 計 算 機 通 信 學 生 姓 名 班 級 學 號 28 班 指 導 教 師 徐 佳 指 導 單 位 計算機學院計算機科學與技術系日 期 2012.11.12-2012.11.23 精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)教教師師評評語語同學出勤率(滿勤、較高、一般,較低),學習態(tài)度(端正、較端正、一般、較差),程序設計基礎(好、較好、一般、較差) ,演示程序(已經、沒有)達到了基本要求,算法設計(好、較好、一般) ,界面友好程度(好、較好、一般) ,答辯過程中回答問題(準確、較準確、錯
2、誤率較高) ,撰寫報告格式(規(guī)范、一般)、內容(豐滿、簡單) 、表述(清晰、一般、不清楚), (圓滿、較好、基本)完成了課題任務。 教師簽名:教師簽名: 年年 月月 日日成成 績績 評評 定定備備 注注精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè) 詞法分析程序的構造詞法分析程序的構造一、一、 課題內容和要求課題內容和要求通過狀態(tài)轉換圖構造 C 或者 PASCAL 語言子集的詞法分析程序。原理解析:選取語言,例如選取了 C 語言,選取其中一個子集,例如包含了部分關鍵字 main、float、if、for 等等,特殊符號( 、=、+等等,特殊定義的標識符變量以及部分常量等,采用編譯原理詞法分析中有窮
3、自動機的思想構建出該語言子集的狀態(tài)轉換圖,并編碼實現(xiàn)?;疽螅海?)將選取的語言子集編寫一個簡單程序,放在一個文本文件中;(2)要將一個個單詞區(qū)分清楚并歸類(例如 for 屬于關鍵字)。二、需求和思路分析二、需求和思路分析本課題是用 C+語言設計,選取的是 C 語言子集。編寫對簡單語言進行詞法分析的詞法分析程序。1、識別子集中的關鍵字、標識符、常數、運算符和分界符等。2、對子集中的字符類型進行歸類三、概要設計三、概要設計(1)狀態(tài)轉換圖: 精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)(2)核心代碼: 1)定義:char cbuffer;char*keyword14=if,else,for,wh
4、ile,do,float,return,break,continue,int,void,main,const,printf; /關鍵字char *border8= , , ; , , , ( , ) ,:=,.; /分隔符char *arithmetic6=+ , - , * , / , + , -; /運算符char *relation7= , , = , = ,!=; /關系運算符 char *lableconst80; /標識符2)函數調用:search(char searchchar,int wordtype)/查找類型alphaprocess(char buffer) /字符處理過程
5、digitprocess(char buffer) /數字處理過程otherprocess(char buffer) /分隔符、運算符、邏輯運算符等main()/主函數3) 狀態(tài)類型:狀態(tài)轉換圖的形式: 每個狀態(tài)對應一個帶標號的 case 語句 轉向邊對應 goto 語句switch (wordtype) case 1: for (i=0;i=13;i+) /關鍵字 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)質文檔-傾情為你奉上專心-專注-專業(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+) /關系運算符 if (strcmp(relationi,searchchar)=0) return(i+1); return(0); case 5: for (t=40;t=constnum;t+) /常數 if (strcmp(searchchar,labl
7、econstt)=0)/判斷該常數是否已出現(xiàn)過 return(t+1); lableconstt-1=(char *)malloc(sizeof(searchchar); /為新的元素分配內存空間 strcpy(lableconstt-1,searchchar); /為數組賦值 lableconst指針數組名 constnum+; /常數個數自加 return(t); case 6: 精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(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+; /標識符個數自加 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; 四、詳細設計四、詳細設計實驗環(huán)境:visual C+6.0 win7 系統(tǒng)源程序代碼:#include #include #include #include #include 精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(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; /關鍵字ch
10、ar *border8= , , ; , , , ( , ) ,:=,.; /分隔符char *arithmetic6=+ , - , * , / , + , -; /運算符char *relation7= , , = , = ,!=; /關系運算符 char *lableconst80; /標識符int constnum=40;int lableconstnum=0; /統(tǒng)計常數和標識符數量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+) /關鍵字 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)質文檔-傾情為你奉上專心-專注-專業(yè) case 4: for (i=0;i=6;i+) /關系
12、運算符 if (strcmp(relationi,searchchar)=0) return(i+1); return(0); case 5: for (t=40;t=constnum;t+) /常數 if (strcmp(searchchar,lableconstt)=0)/判斷該常數是否已出現(xiàn)過 return(t+1); lableconstt-1=(char *)malloc(sizeof(searchchar); /為新的元素分配內存空間 strcpy(lableconstt-1,searchchar); /為數組賦值 lableconst 指針數組名 constnum+; /常數個數
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+; /標識符個數自加 return(i); default:cout錯誤!; char alphaprocess(char buffer) /字符處理過程 int atype;
14、精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè) int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer) /這兩個函數分別是判字符和判數字函數位于 ctype.h 中 alphatp+i=buffer; fp.get(buffer); alphatpi+1=0;/在末尾添加字符串結束標志 if (atype=search(alphatp,1) coutrow: row String= alphatpttt關鍵字endl; else atype=search(alphatp,6); /標識符 coutrow: row Stri
15、ng= alphatpttt標識符endl; return(buffer);char digitprocess(char buffer) /數字處理過程 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數字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)質文檔-傾情為你奉上專心-專注-專業(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) /關系運算符 fp.get(buffer); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,4) /判斷該關系運算符是否是由連續(xù)的兩個字符組成的 coutrow: row String= othertpttt關系運算符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)質文檔-傾情為你奉上專心-專注-專業(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)質文檔-傾情為你奉上專心-專注-專業(yè) cbuffer=digitprocess(cbuffer); else cbuffer=otherprocess(cbuffer); cout標識符個數是:lableconstnum分別是endl; i=0; while(ilableconstnum) coutlableconsti+ ; coutendl; cout詞法分析結束!n; getchar(); (3)程序流程圖:精選優(yōu)質文檔-傾情為你奉上專心-專注-專業(yè)五、測試數據及其結果分析五、測試數據及其結果分析 若源程序中沒有.txt 文檔,則會出
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 倉儲智能化系統(tǒng)運維與數據安全保障協(xié)議
- 圖錄資料補充及維護服務協(xié)議
- 全渠道加盟商合作共贏協(xié)議
- 智能家居環(huán)境控制系統(tǒng)安裝與定期檢修合同
- 智能家居系統(tǒng)研發(fā)、生產、安裝及培訓合同
- 直播帶貨渠道補充服務合同
- 影視后期制作與技術支持與維護合同
- 銷售管理人員分析
- 森林防火安全教育
- 剖腹產產期護理
- 車輛超速考試試題及答案
- 成人患者營養(yǎng)不良診斷與應用指南(2025版)解讀課件
- 2025年一級注冊建筑師歷年真題答案
- 十五五時期經濟社會發(fā)展座談會十五五如何謀篇布局
- 初中電與磁試題及答案
- 浙江開放大學2025年《行政復議法》形考作業(yè)1答案
- 國家開放大學《西方經濟學(本)》章節(jié)測試參考答案
- 湖南省炎德英才名校聯(lián)合體2025屆高考考前仿真聯(lián)考二英語+答案
- 陽光雨棚制作安裝合同范本
- 燃氣經營安全重大隱患判定標準課件
- MOOC 大學公共體育-華南理工大學 中國大學慕課答案
評論
0/150
提交評論