編譯原理課程設(shè)計LR分析器總控程序的實現(xiàn)_第1頁
編譯原理課程設(shè)計LR分析器總控程序的實現(xiàn)_第2頁
編譯原理課程設(shè)計LR分析器總控程序的實現(xiàn)_第3頁
編譯原理課程設(shè)計LR分析器總控程序的實現(xiàn)_第4頁
編譯原理課程設(shè)計LR分析器總控程序的實現(xiàn)_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、合肥工業(yè)大學(xué)計算機與信息學(xué)院課程設(shè)計課 程:編譯原理專業(yè)班級:計算機科學(xué)與技術(shù)09-2班學(xué) 號:姓 名: 一:設(shè)計題目 題目: lr分析器總控程序的實現(xiàn)設(shè)計內(nèi)容及要求:對p.101中的文法,按圖5.5lr分析表構(gòu)造lr分析器。要求程序按p.102例5.7那樣,對于輸入串i*i+i,輸出lr分析器的工作過程。改進:可以自行輸入輸入串,能按重新按鈕重新開始,外加了一個計時器。二:設(shè)計目的 進一步了解了lr分析器的整個工作過程,將lr分析器以圖形界面的形式展現(xiàn)了出來,有利加深了對lr分析過程的掌握。三:實驗原理本程序是用windows編的,大體的思想是這樣的:通過建立一個符號棧,狀態(tài)棧,輸入棧(結(jié)構(gòu)

2、體類型定義的)分別用來存放符號,狀態(tài),輸入串,將lr分析表構(gòu)造成為一個二維數(shù)組table139,其中0-11表示狀態(tài)結(jié)點,2126表示規(guī)約標(biāo)號,-1表示error(出錯),12表示acc(接受),在acation函數(shù)里面通過i等于多少來判斷到底是規(guī)約還是移進。在main_oncommand()函數(shù)中通過switch.case.來判斷每次及下一步所要執(zhí)行的操作。運行的時候,首先要輸入一個以#結(jié)尾的輸入串,然后單擊開始下一步.,如果規(guī)約成功,則彈出一個規(guī)約成功的對話框,否則彈出一個規(guī)約失敗的對話框。當(dāng)然在運行的過程中如果出現(xiàn)什么差錯,都可以單擊重新開始按鈕重新輸入輸入串重新開始。四:實驗代碼#in

3、clude stdafx.h#include #include #include resource.h#include maindlg.hchar *str26=e-e+t,e-t,t-t*f,t-f,f-(e),f-i;int flag=0;#define max 20typedef structint stack1max;int top1;status;typedef structchar stack2max;int top2;symbol_instr;char index_char9=i,+,*,(,),#,e,t,f;/為二維數(shù)數(shù)組的縱坐標(biāo)/lr分析表/0-11表示狀態(tài)結(jié)點,2126表

4、示規(guī)約標(biāo)號,/-1表示error(出錯),12表示acc(接受)int table139 = 5,-1,-1, 4,-1,-1, 1, 2, 3,-1, 6,-1,-1,-1,12,-1,-1,-1,-1,22, 7,-1,22,22,-1,-1,-1,-1,24,24,-1,24,24,-1,-1,-1, 5,-1,-1, 4,-1,-1, 8, 2, 3,-1,26,26,-1,26,26,-1,-1,-1, 5,-1,-1, 4,-1,-1,-1, 9, 3, 5,-1,-1, 4,-1,-1,-1,-1,10,-1, 6,-1,-1,11,-1,-1,-1,-1,-1,21, 7,-1

5、,21,21,-1,-1,-1,-1,23,23,-1,23,23,-1,-1,-1,-1,25,25,-1,25,25,-1,-1,-1;/規(guī)約規(guī)則struct rulechar x;int y;r6=e,3,e,1,t,3,t,1,f,3,f,1; /后面的代表a中的長度bool winapi main_proc(hwnd hwnd, uint umsg, wparam wparam, lparam lparam) switch(umsg) handle_msg(hwnd, wm_initdialog, main_oninitdialog); handle_msg(hwnd, wm_com

6、mand, main_oncommand);handle_msg(hwnd,wm_close, main_onclose); return false;void init_stack1(hwnd hwnd,status *p)if( !p)messagebox(hwnd,text(出錯),text(警告),mb_ok|mb_iconhand);p-top1 = -1;void push1(hwnd hwnd,status *&p,int x)if(p-top1 top1+;p-stack1p-top1 = x;else messagebox(hwnd,text(出錯),text(警告),mb_

7、ok|mb_iconhand);int pop1(hwnd hwnd,status *p)int x;if(p-top1 != 0)x = p-stack1p-top1;p-top1-;return x;else messagebox(hwnd,text(狀態(tài)棧為空),text(警告),mb_ok|mb_iconhand);/printf(n狀態(tài)棧1空!n);return 0;void out_stack(hwnd hwnd,status *p)if(p-top1 0)messagebox(null,text(狀態(tài)棧為空),text(警告),mb_ok|mb_iconhand);/printf

8、(n狀態(tài)棧3空!n);int i;tchar str256;for(i=0;itop1;i+)wsprintf(&stri,%d,p-stack1i);setdlgitemtext(hwnd,idc_edit1,str);int get_top1(hwnd hwnd,status *p)int x;if(p-top1 != -1)x = p-stack1p-top1;return x;else messagebox(hwnd,text(狀態(tài)棧為空),text(警告),mb_ok|mb_iconhand);/printf(n狀態(tài)棧2空!n);return 0;void init_stack2(h

9、wnd hwnd,symbol_instr *p)if( !p)messagebox(hwnd,text(出錯),text(警告),mb_ok|mb_iconhand);p-top2= -1;void push2(hwnd hwnd,symbol_instr *p,char x)if(p-top2 top2+;p-stack2p-top2 = x;p-stack2p-top2+1=0;else messagebox(hwnd,text(出錯),text(警告),mb_ok|mb_iconhand);char pop2(hwnd hwnd,symbol_instr *p)char x;if(p-

10、top2 != -1)x = p-stack2p-top2;p-top2-;p-stack2p-top2+1=0;return x;else messagebox(hwnd,text(符號棧為空),text(警告),mb_ok|mb_iconhand);/printf(n符號棧1空!n);return 0;void out_stack1(hwnd hwnd,symbol_instr *p)if(p-top2 stack2);void out_stack2(hwnd hwnd,symbol_instr *p)if(p-top2 stack2);char get_top2(hwnd hwnd,sy

11、mbol_instr *p)char x;if(p-top2 != -1)x = p-stack2p-top2;return x;else messagebox(hwnd,text(符號棧為空),text(警告),mb_ok|mb_iconhand);/printf(n符號棧2空!n);return 0;void print(hwnd hwnd,status *status_p,symbol_instr *symbol_p,symbol_instr *instr_p)out_stack(hwnd,status_p); /輸出狀態(tài)棧的內(nèi)容out_stack1(hwnd,symbol_p); /輸

12、出符號棧的內(nèi)容out_stack2(hwnd,instr_p); /輸出輸入串int get_index_char(char i)for(int j=0;j=0 & i=21 & i=26)x = ri-21.y;for(j=0;jx;j+)pop1(hwnd,status_p);pop2(hwnd,symbol_p);push2(hwnd,instr_p,ri-21.x);setdlgitemtext(hwnd,idc_edit4,str2i-21);void callback timerproc(hwnd hwnd,uint message,uint itimerid,dword dwti

13、me)systemtime stlocal;tchar buf256;getlocaltime(&stlocal);wsprintf(buf,%d年%d月%d日 %d:%d:%d,stlocal.wyear,stlocal.wmonth,stlocal.wday,stlocal.whour,stlocal.wminute,stlocal.wsecond);setdlgitemtext(hwnd,idc_edit5,buf);bool main_oninitdialog(hwnd hwnd, hwnd hwndfocus, lparam lparam)settimer(hwnd,0,1000,t

14、imerproc);return true;status *status_p=new status;symbol_instr *symbol_p=new symbol_instr;symbol_instr *instr_p=new symbol_instr ; void main_oncommand(hwnd hwnd, int id, hwnd hwndctl, uint codenotify)switch(id) case idc_button1:init_stack1(hwnd,status_p); /初始化各棧init_stack2(hwnd,symbol_p);init_stack2

15、(hwnd,instr_p);/壓進棧初始元素push1(hwnd,status_p,0);push2(hwnd,symbol_p,#);char x;tchar msg256;getdlgitemtext(hwnd,idc_edit3,msg,sizeof(msg);unsigned int i;for(i=0;i top2 != 0)x = pop2(hwnd,symbol_p);push2(hwnd,instr_p,x);print(hwnd,status_p,symbol_p,instr_p);/打印初始分析表*/break;case idc_button2: action(hwnd,status_p,symbol_p,instr_p);break;case idc_button3:setdlgitemtext(hwnd,idc_edit1,text();

溫馨提示

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

評論

0/150

提交評論