版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、計算機與信息學(xué)院編譯原理 實驗專業(yè)班級計算機科學(xué)與技術(shù) 13-3 班學(xué)生及學(xué)號2013211694課程教學(xué)班號 0002任課教師實驗指導(dǎo)教師 實驗地點第六、二機房2014 2015學(xué)年第 二 學(xué)期實驗一 詞法分析設(shè)計能描述:1、對輸入的 txt 文件內(nèi)的內(nèi)容進(jìn)行詞法分析;2、由文件流輸入 test.txt 中的內(nèi)容;3、刪除空格類字符,包括回車、制表符空格,對文件中的各類字符進(jìn)行詞法分析;4、統(tǒng)計行數(shù)和列數(shù)用于錯誤單詞的定位;5、如果發(fā)現(xiàn)錯誤則出錯,打印出分析后的結(jié)果。二、程序結(jié)構(gòu)描述:1、key 定義一個“關(guān)鍵字”表(用數(shù)組k 表示,數(shù)組里有八個元素 ); operator 定義一個“運算符
2、”表(用數(shù)組o 表示,數(shù)組里有四個元素) separator 定義一個“分界符”表(用數(shù)組p 表示,數(shù)組里有六個元素 );字符變量 ch ,存放讀進(jìn)的源程序字符 ;2、判斷掃描的字符是否為關(guān)鍵字:回 1,否則返回 0;判斷掃描的字符是否為字母:則返回 0;判斷掃描的字符是否為常數(shù):則返回 0;判斷掃描的字符是否為運算符:否則返回 0;判斷掃描的字符是否為分界符:回 1,否則返回 0;把整數(shù)轉(zhuǎn)化為字符串:string詞法分析函數(shù):void CMyDlg:IsKey(string stinput);若是關(guān)鍵字返IsLetter(char chinput);若是返回 1,否IsDigit(char
3、chinput);若是返回 1,否IsOptr(string stinput);若是返回 1,IsSeparator(string stinput);若是返2string(n)yse(ifstream &in)三、詳細(xì)的算法描述:(詳細(xì)代碼見附錄)IsKey(string stinput) /判斷掃描的字符是否為關(guān)鍵字if(!strcmp(ki.c_str(),stinp return 0; _str() return 1;IsLetter(char chinput)/判斷掃描的字符是否為字母if(chinput=a)&(chinput=A)&(chinput=0&chinput ;caseT
4、G,G-+TG|-TG,G-,T-FS,S-*FS|/FS,S-,F-(E),F-i;/文法3、控制部分:從鍵盤輸入一個表達(dá)式符號串;public void changedUpdate(Event e)str=area_in.getText();/讀 area_in 里輸入的句子str+=#;/自動加上“#”public void removeUpdate ( changedUpdate(e);public void insertUpdate ( changedUpdate(e);Event e)Event e)4、利用 LL(1)分析算法進(jìn)行表達(dá)式處理:根據(jù) LL(1)分析表對表達(dá)式符號串進(jìn)
5、行堆棧(或其他)操作,輸出分析結(jié)果,如果遇到錯誤則顯示錯誤信息。三、詳細(xì)的算法描述:(詳細(xì)代碼見附錄)void one_grammar_(String s,j)/求某個產(chǎn)生式的集if(return_vt_add(s.charAt(j)!=-1)/產(chǎn)生式右邊第一個為終結(jié)符把 s.charAt(j)加入 s.charAt(0)的集中else if(!return_vn_add(s.charAt(j)Vt.length)/產(chǎn)生式右邊第一個為非終結(jié)符 調(diào)用后面的“求非終結(jié)符V 關(guān)于該文法的集”函數(shù),把集中 s.charAt(j)的集中不為的加入 s.charAt(0)的if(!return_vn_ad
6、d(s.charAt(j)Vt.length)/如果屬于當(dāng)前s.charAt(j)的集 s.charAt(j)的集沒有求,先求其集,把 s.charAt(j)的集中不為的加入 s.charAt(0)的集中if(s.charAt(j+1)!=|)s.charAt(j+1)不是|,就將 s.charAt(j+1)的集加入 s.charAt(0)的集Elses.charAt(j+1)是|等價于此式子結(jié)束,就將加入 s.charAt(0)的集 void void voidone_vn_ all_(char v)/求非終結(jié)符 V 關(guān)于該文法的集()/求所有非終結(jié)符的集j)/求關(guān)于某個產(chǎn)生式的 follo
7、w 集one_grammar_follow(String s,if(j=s.length()-1|(js.length()-1&s.charAt(j+1)=|)/字符位于產(chǎn)生式末尾時將產(chǎn)生式左邊的那個字符的 follow 集加到集中字符的 followif(!followreturn_vn_add(s.charAt(0)Vt.length)/產(chǎn)生式左邊的 follow 集未求時,先求產(chǎn)生式左邊的 follow 集 for(i=0;iVt.length;i+)/產(chǎn)生式左邊的非終結(jié)符的 follow 集加到字符的 follow 集中if(js.length()-1&s.charAt(j+1)!=|
8、)/將字符右邊的第一個字符的集加到字符的 follow 集中if(return_vt_add(s.charAt(j+1)!=-1)/如果是終結(jié)符 if(Vtreturn_vt_add(s.charAt(j+1)!=)/該字符不為,將該字符加入到字符的 follow 集中 else /該字符不為終結(jié)符該字符的集中除了的終結(jié)符加入字符的 follow集中if(return_vt_add()!=-1)當(dāng)集中有,將產(chǎn)生式左邊的那個字符的follow 集加到字符右邊的字符串的字符集的follow集中if(s.charAt(0)=s.charAt(j) return;/產(chǎn)生式左邊的非終結(jié)符相同時,返回字符
9、與void void voidone_vn_follow(char v)/求非終結(jié)符 v 的 follow 集 all_follow()/求所有非終結(jié)符的 follow 集in_table(j,i,String s,m)/將某一個產(chǎn)生式填入分析表中/ 構(gòu)造分析表void build_table()i,j,m; String s;for(i=0;iGrammar.length;i+) j=3;while(jGrammari.length() m=return_right_l(j,Grammari); if(m=-1)m=Grammari.length();s=Grammari.substring
10、(j, m);/將 j 到m-1 賦值到s in_table(j,i,s,m);j=m+1;System.out.prln(分析表:);for(i=0;iVn.length;i+) for(j=0;jE+T,E-T,T-T*F,T-F,F-(E),F-i;/LR 文法stackS; /狀態(tài)棧stackX;/符號棧 char input10;/輸入字符num(string s) /判斷字符串中的數(shù)字void pr(i,char*c) /剩余輸入串的輸出 same(char step=1,poa) /用于查找終結(jié)符=0,se=0; /步驟、指針、狀態(tài)m,n,l; /用于判斷終結(jié)符,分析表,表達(dá)式右
11、部的長度三、詳細(xì)的算法描述:(詳細(xì)代碼見附錄)/*分析程序*voidyse()string str2=#; string str3=0; X.push(#); S.push(0);/在符號棧里先放入#/在狀態(tài)棧里先放入 0/#壓符號棧/0 壓狀態(tài)棧coutstep+tstr3tstr2t;pr(po,input);/*初始化/*進(jìn)入循環(huán) while(true)se=S.top();/狀態(tài)棧的棧頂?shù)臓顟B(tài)/分析輸入的最左邊的字符/終結(jié)符是 vt 中第幾個ch1=inputpo m=same(ch1); str1=actions;em;/查找 action 表找到匹配的/*移進(jìn)動作 if(str10
12、=s)S.push(n);X.push(ch1);/狀態(tài)棧壓棧,加入一個數(shù)字/符號棧壓棧,加入輸入的字符str3=str3+ch2;/狀態(tài)字符型將數(shù)字直接加在后面po pr+;(po/指針后移,input);/剩余輸入串的輸出/*/若是移進(jìn),即 s/1、輸入串少了一個/2、符號加上一個(str2)/3、(str3)狀態(tài):由當(dāng)前狀態(tài)和已加入 str2 的符號查 action 表,由表決定轉(zhuǎn)到哪一個狀態(tài),/ 將相應(yīng)的序號加上/*/*歸約動作else if(str10=r)l=LRn-1.length()-3;/表達(dá)式右邊的長度for(i=1;iE+T:用 E 替換 E+T)se=S.top();/
13、取狀態(tài)棧棧頂if(LRn-10=E)/第一列S.push(num(gose0);/查 goto 表后給狀態(tài)棧加一個 else if(LRn-10=T)/第二列S.push(num(gose1);/查 goto 表后給狀態(tài)棧加一個 else if(LRn-10=F)/第三列 S.push(num(gose2);/查 goto 表后給狀態(tài)棧加一個 /*/若是規(guī)約,即 r/1、利用產(chǎn)生式將符號(str2)改 n 個/2、狀態(tài)相應(yīng)出棧 n 個(str3)/3 、由已出后的狀態(tài)和已改后的符號查 goto 表, 給狀態(tài)加一個(str3.str2)/4、輸入串不變/*/*出錯else if(str1=err
14、or)coutERROR程序錯誤,分析結(jié)束!endl; flag=false;/*分析成功else if(str1=acc)cout分析成功,終止程序endl; flag=false;四、的測試方法和:首先要輸入需要規(guī)約的字符串,注意要以#號結(jié)尾,不然最終的分析結(jié)果沒有完全顯示,#號是為了讓它知道結(jié)果匹配,分析成功五、實驗總結(jié) (設(shè)計的特點、收獲與體會):1、期初想像實驗二一樣,讓它自動構(gòu)造分析表,然后完成 LR(1)分析,后來發(fā)現(xiàn)它涉及到終結(jié)符和非終結(jié)符,還有 action 動作以及 goto 動作,不一樣的動作下面還有“s”移進(jìn)和“r”規(guī)約,情況較多較復(fù)雜,最終沒有實現(xiàn),采取了自己構(gòu)造分析
15、表完成分析過程。2、num(string s) string str=;/判斷字符串中的數(shù)字for(j=0;j=0&sj=9) str=str+sj;i=atoi(str.c_str(); return i;/將字符串轉(zhuǎn)化成整數(shù)當(dāng)讀到 s 或者 r 時,意味著要進(jìn)行移進(jìn)或者規(guī)約,我采用了 num 函數(shù),獲取 r以及s 后面的數(shù)字,來決定最終,移進(jìn)后轉(zhuǎn)到哪一個狀態(tài),或者用哪一個產(chǎn)生式進(jìn)行規(guī)約,由于使用了判斷數(shù)字是否在 09 之間,所以沒有辦法讀到第 12 步驟中狀態(tài)棧里的兩位數(shù) 10(見上面截圖,10 的地方采用的是冒號:)3、由于實驗二花了太多的時間,所以實驗三還沒有來得及做圖形化界面,后期可
16、以將代碼該進(jìn)為可以自動杜秋雨文件中的要分析的字符串,然后進(jìn)行 LR(1)分析,也可以在后期作出一個圖形化界面。還有就是上一條所提到的冒號問題,可以該進(jìn)為用“”將兩位數(shù)的數(shù)字括起來,這樣用兩段代碼標(biāo)識數(shù)字方面后期的檢查和閱讀,判碼執(zhí)行是否正確。附錄實驗一源代碼:/ 詞法分析器 Dlg.cpp : implemen/ion file#include stdafx.h #include 詞法分析器.h #include 詞法分析器 Dlg.h #include #include #include #include#ifdef _DEBUG#define new DEBUG_NEW #undef TH
17、IS_FILEsic char THIS_FILE = FILE ;#endifusing namespatd;string k8=do,end,for,if,prf,scanf,then,while;/key定義一個“關(guān)鍵字”表(用數(shù)組表示,數(shù)組里有八個元素 )string o4=+,-,*,/; /operator 定義一個“運算符”表(用數(shù)組表示,數(shù)組里有四個元素)string p6=,;,(,),;/separator 定義一個“分界符”表(用數(shù)組表示,數(shù)組里有六個元素 )char ch;/字符變量 ch ,存放step=0; string temp;讀進(jìn)的源程序字符/ CMyDlg
18、dialogCMyDlg:CMyDlg(CWnd* pParent /*=NULL*/): CDialog(CMyDlg:IDD, pParent)/AFX_DATA_INIT(CMyDlg)/ NOTE: the ClassWizard will add member initialization here/AFX_DATA_INIT/ Notet LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CMyDlg:DoData
19、Exchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CMyDlg)DDX_Control(pDX, IDC_LIST1, m_ctllist); DDX_Control(pDX, IDC_BUTTON1, m_button);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMyDlg, CDialog)/AFX_MSG_MAP(CMyDlg)ON_WM_PA()ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1)/AFX_MS
20、G_MAPEND_MESSAGE_MAP()/ CMyDlg message handlersBOOL CMyDlg:OnInitDialog()CDialog:OnInitDialog();/ Set the icon for this dialog.The framework does this automatically/when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);SetIcon(m_hIcon, FALSE);/ Set big icon/ Set small icon/ TODO: A
21、dd extra initialization hereDWORD dwStyle=m_ctllist.GetExtendedStyle();/添加網(wǎng)格線 dwStyle |=LVS_EX_FULLROWSELECT;dwStyle |=LVS_EX_GRIDLINES;m_ctllist.SetExtendedStyle(dwStyle);m_ctllist.InsertColumn(0,單詞,LVCFMT_LEFT,70); m_ctllist.InsertColumn(1,二元序列,LVCFMT_LEFT,70); m_ctllist.InsertColumn(2,類型,LVCFMT_L
22、EFT,90); m_ctllist.InsertColumn(3,位置(行,列),LVCFMT_LEFT,90);return TRUE;/ return TRUEunless you set the focus to a control/ If you add a minimize button to your dialog, you will need the code below/to draw the icon.For MFC applications using the/this is automatically done for you by the framework./vie
23、w m,void CMyDlg:OnPaif (IsIconic()()CPaDC dc(this); / device context for paingSendMessage(WM_ICONERASEBKGND, (RAM) dc.GetSafeHdc(), 0);/ Center icon in cnt rectanglecxIcon = GetSystemMetrics(SM_CXICON); cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetCntRect(&rect);x = (rect.Width() - cxIcon + 1)
24、 / 2;y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPa();/ The system calls this to obta/the minimized window.he cursor to display while the user dragsHCURSOR CMyDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;/判斷掃描的字符是否為關(guān)鍵字 IsKey(string stinput)i; for(i=0
25、;i=a)&(chinput=A)&(chinput=0&chinput=9)return 1;/0,1,2,.9 return 0;/判斷掃描的字符是否為運算符 IsOptr(string stinput)i; for(i=0;i4;i+)if(!strcmp(oi.c_str(),stinp_str()return 1;/strin1;/string b=stinput;/const char*c=o1.c_str();/將 string 對象轉(zhuǎn)化為char*對象/將 string 對象轉(zhuǎn)化為 char*對象/const char*csecond=stinp/strcmp(c,csecon
26、d) ; return 0;表示程序異常結(jié)束_str();/用來比較 c和 csecond 字符串/返回 0 表示程序正常結(jié)束,返回-1/判斷掃描的字符是否為分界符 IsSeparator(string stinput)i; for(i=0;i6;i+)if(!strcmp(pi.c_str(),stinp/ if(pi=stinput) return 1;_str()return 1;/string a=p1;/string b=stinput;/const char*c=p1.c_str();/將string 對象轉(zhuǎn)化為char*對象/將 string 對象轉(zhuǎn)化為 char*對象/cons
27、t char*csecond=stinp/strcmp(c,csecond) ; return 0;表示程序異常結(jié)束_str();/用來比較 c和 csecond 字符串/返回0表示程序正常結(jié)束,返回-1/把整數(shù)轉(zhuǎn)化為字符串string2string(n)stringstream ss; sss;return s;void CMyDlg:yse(ifstream &in)/詞法分析函數(shù)string strtoken=;/存放單詞符號的字符串,置為空串char ch;line=1,row=0;列數(shù)while(in.get(ch)strtoken=;if(ch= )|(ch=t)/存放讀進(jìn)的源程序
28、字符/設(shè)置行數(shù)列數(shù),line 為行數(shù),row 為/開始掃描/置 strToken 為空串/如果是空格或者tab 健 ,不處理/如果是換行符,則行數(shù)加一else if(ch=n) line+;row=0;/關(guān)鍵字、標(biāo)識符的處理 else if(IsLetter(ch)row+; while(IsLetter(ch)|IsDigit(ch)strtoken+=ch; in.get(ch); if(in.eof() break;后時 break/如果掃描的字符是字母/繼續(xù)向后掃描/掃描的字符是字母或常數(shù)/讀入/繼續(xù)掃描/end of file 當(dāng)讀一個文件讀到文件最if(!in.eof() in.p
29、utback(ch); /如果沒有讀到文件尾,文件指針(光標(biāo))后退一個字節(jié),即回調(diào)一個字符位置if(IsKey(strtoken)關(guān)鍵字表,如果是關(guān)鍵字直接輸出/判斷是否為關(guān)鍵字,調(diào)用函數(shù)查詢CString cs1(strtoken.c_str();m_ctllist.InsertItem(step,cs1);string temp; temp=(1,+strtoken+); CString cs2(temp.c_str();m_ctllist.SetItemText(step,1,cs2);m_ctllist.SetItemText(step,2,TEXT(關(guān)鍵字);temp=(+2stri
30、ng(line)+,+2string(row)+); CString cs4(temp.c_str();m_ctllist.SetItemText(step,3,cs4);step+;/coutstrtoken(1,strtoken)t tt(line,row)endl;關(guān)鍵字else /如果不是關(guān)鍵字,則為標(biāo)識符 CString cs1(strtoken.c_str();m_ctllist.InsertItem(step,cs1);string temp; temp=(6,+strtoken+); CString cs2(temp.c_str();m_ctllist.SetItemText(
31、step,1,cs2);m_ctllist.SetItemText(step,2,TEXT(標(biāo)識符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str(); m_ctllist.SetItemText(step,3,cs4);step+;/coutstrtokent(6,strtoken)tt tt(line,row)endl;標(biāo)識符/無符號整數(shù)處理 else if(IsDigit(ch)row+; while(IsDigit(ch)strtoken+=ch; ch=in.get(); if(in.eof() break;
32、后時 break/如果掃描的首字符是常數(shù)/列數(shù)加 1,即繼續(xù)向后掃描/讀入/繼續(xù)掃描/end of file 當(dāng)讀一個文件讀到文件最if(IsLetter(ch)/如果數(shù)字后面是字母while(IsLetter(ch)|IsDigit(ch)/后面是字母或者常數(shù) strtoken+=ch;ch=in.get();if(in.eof() break;/end of file 當(dāng)讀一個文件讀到文件最后時 breakCString cs1(strtoken.c_str();m_ctllist.InsertItem(step,cs1);m_ctllist.SetItemText(step,1,TEXT
33、(Error);m_ctllist.SetItemText(step,2,TEXT(Error);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str();m_ctllist.SetItemText(step,3,cs4);step+;/coutstrtokentErrorttErrortt(line,row)endl; /出錯else if(ch= |in.eof()/數(shù)字后面是空格或者已經(jīng)到達(dá)文件末尾CString cs1(strtoken.c_str();m_ctllist.InsertItem(step,cs1);st
34、ring temp; temp=(5,+strtoken+); CString cs2(temp.c_str();m_ctllist.SetItemText(step,1,cs2);m_ctllist.SetItemText(step,2,TEXT(常數(shù));temp=(+2string(line)+,+2string(row)+); CString cs4(temp.c_str();m_ctllist.SetItemText(step,3,cs4);step+;/coutstrtokent(5,strtoken)tttt(line,row)endl;/是常數(shù)常數(shù)in.putback(ch);/
35、文件指針(光標(biāo))后退一個字節(jié),即回調(diào)一個字符位置elsestrtoken=; strtoken+=ch; if(IsOptr(strtoken)row+;/讀入/繼續(xù)掃描/掃描的字符是運算符/列數(shù)加 1,即繼續(xù)向后掃描if(ch=+)ch=in.get();if(ch=+)m_ctllist.InsertItem(step,TEXT(+);m_ctllist.SetItemText(step,1,TEXT(3,+);m_ctllist.SetItemText(step,2,TEXT(算術(shù)運算符);temp=(+2string(line)+,+2string(row)+);CString cs4
36、(temp.c_str(); m_ctllist.SetItemText(step,3,cs4);step+;/cout+t(3,+)tt 算 術(shù) 運 算 符t(line,row)endl;elsem_ctllist.InsertItem(step,TEXT(+);m_ctllist.SetItemText(step,1,TEXT(3,+);m_ctllist.SetItemText(step,2,TEXT(算術(shù)運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str(); m_ctllist.SetItemText(s
37、tep,3,cs4);step+;/cout+t(3,+)tt 算 術(shù) 運 算 符t(line,row)endl;in.putback(ch);if(ch=-)ch=in.get();if(ch=-)m_ctllist.InsertItem(step,TEXT(-);m_ctllist.SetItemText(step,1,TEXT(3,-);m_ctllist.SetItemText(step,2,TEXT(算術(shù)運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str(); m_ctllist.SetItemText(
38、step,3,cs4);step+;/cout-t(3,-)tt 算 術(shù) 運 算 符t(line,row)endl;elsem_ctllist.InsertItem(step,TEXT(-);m_ctllist.SetItemText(step,1,TEXT(3,-);m_ctllist.SetItemText(step,2,TEXT(算術(shù)運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str(); m_ctllist.SetItemText(step,3,cs4);step+;/cout-t(3,-)tt算術(shù)運算符t
39、(line,row)endl;in.putback(ch);if(ch=*)ch=in.get();m_ctllist.InsertItem(step,TEXT(*);m_ctllist.SetItemText(step,1,TEXT(3,*);m_ctllist.SetItemText(step,2,TEXT(算術(shù)運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str(); m_ctllist.SetItemText(step,3,cs4);step+;/cout*t(3,*)tt算術(shù)運算符t(line,row)en
40、dl;in.putback(ch); if(ch=/)ch=in.get();m_ctllist.InsertItem(step,TEXT(/);m_ctllist.SetItemText(step,1,TEXT(3,/);m_ctllist.SetItemText(step,2,TEXT(算術(shù)運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str(); m_ctllist.SetItemText(step,3,cs4);step+;/cout/t(3,/)tt算術(shù)運算符t(line,row)endl;in.putba
41、ck(ch);else if(IsSeparator(strtoken)row+;/掃描的字符是分界符/列數(shù)加 1,即繼續(xù)向后掃描CString cs1(strtoken.c_str();m_ctllist.InsertItem(step,cs1);string temp; temp=(2,+strtoken+); CString cs2(temp.c_str();m_ctllist.SetItemText(step,1,cs2);m_ctllist.SetItemText(step,2,TEXT(分界符);temp=(+2string(line)+,+2string(row)+);CStri
42、ng cs4(temp.c_str(); m_ctllist.SetItemText(step,3,cs4);step+;/coutstrtokent(2,strtoken)tt tt(line,row)endl;elseswitch(ch)分界符row+;case= : row+;/列數(shù)加 1,即繼續(xù)向后掃描m_ctllist.InsertItem(step,TEXT(=);m_ctllist.SetItemText(step,1,TEXT(4,=);m_ctllist.SetItemText(step,2,TEXT(關(guān)系運算符);temp=(+2string(line)+,+2string
43、(row)+);CString cs4(temp.c_str();m_ctllist.SetItemText(step,3,cs4);step+;/cout=t(4,=)tt 關(guān)系運算符t(line,row) :row+;ch=in.get();if(ch=)CString cs1(strtoken.c_str(); m_ctllist.InsertItem(step,cs1);string temp;temp=(4,+strtoken+);CString cs2(temp.c_str();m_ctllist.SetItemText(step,1,cs2);m_ctllist.SetItemT
44、ext(step,2,TEXT(關(guān)系運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str();m_ctllist.SetItemText(step,3,cs4);step+;/cout=t(4,=)tt關(guān)系運算符t(line,row);m_ctllist.SetItemText(step,1,TEXT(4,);m_ctllist.SetItemText(step,2,TEXT(關(guān)系運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str();m
45、_ctllist.SetItemText(step,3,cs4);step+;/coutt(4,)tt 關(guān)系運算符t(line,row)endl;in.seekg(-1,ios:cur);為-1,即文件指針(光標(biāo))后退一個字節(jié)break;case :row+;ch=in.get(); if(ch=)/址為當(dāng)前位置,偏移量m_ctllist.InsertItem(step,TEXT(=);m_ctllist.SetItemText(step,1,TEXT(4,=);m_ctllist.SetItemText(step,2,TEXT(關(guān)系運算符);temp=(+2string(line)+,+2s
46、tring(row)+);CString cs4(temp.c_str();m_ctllist.SetItemText(step,3,cs4);step+;/cout=t(4,=)tt 關(guān)系運算符t(line,row) m_ctllist.InsertItem(step,TEXT();m_ctllist.SetItemText(step,1,TEXT(4,);m_ctllist.SetItemText(step,2,TEXT(關(guān)系運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str();m_ctllist.SetIt
47、emText(step,3,cs4);step+;/coutt(4,)tt關(guān)系運算符t(line,row)endl;elsem_ctllist.InsertItem(step,TEXT();m_ctllist.SetItemText(step,1,TEXT(4,);m_ctllist.SetItemText(step,2,TEXT(關(guān)系運算符);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str();m_ctllist.SetItemText(step,3,cs4);step+;/coutt(4,)tt 關(guān)系運算符t(lin
48、e,row)endl;in.seekg(-1,ios:cur);量為-1,即文件指針(光標(biāo))后退一個字節(jié)break; default :row+;/址為當(dāng)前位置,偏移CString cs1(strtoken.c_str();m_ctllist.InsertItem(step,cs1);m_ctllist.SetItemText(step,1,TEXT(Error);m_ctllist.SetItemText(step,2,TEXT(Error);temp=(+2string(line)+,+2string(row)+);CString cs4(temp.c_str(); m_ctllist.S
49、etItemText(step,3,cs4);step+;/coutchtErrorttErrortt(line, row)TG,G-+TG|-TG,G-,T-FS,S-*FS|/FS,S-,F-(E),F-i;/文法public spublic sic String str;/待分析串ic String stack= ; /分析棧LL1()all_();/求集all_follow();build_table();/求/求 follow 集分析表init();/構(gòu)建 Window 內(nèi)的組件setVisible(true);/顯示此 Window setDefaultCloseOperation
50、(JFrame.EXIT_ON_CLOSE);/退出應(yīng)用程序后的默認(rèn)窗口關(guān)閉操作void init()fir=new JButton(集);fol=new JButton(follow 集);pre=new JButton(分析表);y=new JButton(y);/JButton(String text):創(chuàng)建一個帶文本的按鈕fi=new JTextArea(10,10);fJTextArea(10,10);pr=new JTextArea(10,75);area_in=new JTextArea(10,10); area_out=new JTextArea(35,35);/JTextAr
51、ea(TextArearows,columns): 構(gòu)造具有指定行數(shù)和列數(shù)的新的空setLocation(10,10); /窗setSize(600,600);/窗體大小置setResizable(false);/設(shè)置窗體不可由用戶調(diào)整大小 fi.setBackground(Color.LIGHT_GRAY); fo.setBackground(Color.LIGHT_GRAY); pr.setBackground(Color.LIGHT_GRAY); area_in.setBackground(Color.WHITE); area_out.setBackground(Color.LIGHT_
52、GRAY);/setBackground(Color bg):設(shè)置此組件的背景色 fir.setForeground(Color.black); fol.setForeground(Color.black); pre.setForeground(Color.black);y.setForeground(Color.black);/setForeground(Color fg):設(shè)置此組件的前景色/BorderLayout.NORTH 設(shè)置Panel North = nenel();集Nor Nor Nor NorNordd(this.fir); /dd(this.fol); /follow 集
53、dd(this.pre); /分析表ydd(this.y);/dd(new Label(請先在最右側(cè)輸入要分析的句子再點擊y);this.add(North,BorderLayout.NORTH);/BorderLayout.WEST 設(shè)置Panel West = nenel();West.add(this.fi); /集West.add(this.fo); /follow 集this.add(West,BorderLayout.WEST);/BorderLayoENTER 設(shè)置Panel Center = nenel();Center.add(this.area_out);/分析結(jié)果this
54、.add(Center,BorderLayoENTER);/BorderLayout.EAST 設(shè)置Panel East = nenel();East.add(this.area_in);/輸入要分析的句子this.add(East,BorderLayout.EAST);/BorderLayout.SOUTH 設(shè)置Panel South = nenel();Soudd(this.pr);/分析表this.add(South,BorderLayout.SOUTH);pack();/自動調(diào)整窗口大小,使之正好能容納放置的所有組件listenFir=new ListenFir();listenFol
55、=new ListenFol(); listenPre=new ListenPre();listeny=new Listeny();fir.addActionListener(listenFir); fol.addActionListener(listenFol); pre.addActionListener(listenPre);y.addActionListener(listeny);listenFir.setFi(fi); listenFol.setFo(fo);listenPre.setPr(pr);listeny.setAn(area_out);(area_in.get().addL
56、istener(this);/事件public void changedUpdate(Event e)str=area_in.getText();/讀 area_in 里輸入的句子str+=#;/自動加上“#”public void removeUpdate ( changedUpdate(e);public void insertUpdate ( changedUpdate(e);Event e)Event e)/* 返回最近的在其右邊的一個|位置*/return_right_l(i,String s)for(i=i+1;is.length();i+) if(s.charAt(i)=|) /
57、charAt(return i;/存在,就返回位置index):返回指定索引處的char值return -1;/返回-1,表示沒有|在其右邊/* 返回c 在非終結(jié)符表中的位置*/public sici;return_vn_add(char c)for(i=0;iVn.length;i+) if(c=Vni) return i;return -1;/* 返回c 在終結(jié)符表中的位置*/public sici;return_vt_add(char c)for(i=0;iVt.length;i+) if(c=Vti) return i;return -1;/求某個產(chǎn)生式的void one_gramma
58、r_集(String s,j)/v 為產(chǎn)生式左邊的非終結(jié)符的序號v=return_vn_add(s.charAt(0); i;/產(chǎn)生式右邊第一個為終結(jié)符 if(return_vt_add(s.charAt(j)!=-1)/把 s.charAt(j)加入 s.charAt(0)的集中vreturn_vt_add(s.charAt(j)=true;/產(chǎn)生式右邊第一個為非終結(jié)符else/如果 s.charAt(j)的集沒有求,先求其集if(!return_vn_add(s.charAt(j)Vt.length)one_vn_(s.charAt(j);/調(diào)用后面的“求非終結(jié)符 V 關(guān)于該文法的集”函數(shù)
59、/把 s.charAt(j)的集中不為的加入 s.charAt(0)的集中for(i=0;iVt.length;i+)if(return_vn_add(s.charAt(j)i&Vti!=)vi=true;/如果屬于當(dāng)前 s.charAt(j)的集if(return_vn_add(s.charAt(j)return_vt_add()/j=s.length-1,將加入 s.charAt(0)的 if(j=s.length()-1)vreturn_vt_add()=true;/s.charAt(j+1)不是|,就將s.charAt(j+1)的集集加入s.charAt(0)的集if(s.charAt
60、(j+1)!=|) j+;one_grammar_(s,j);/s.charAt(j+1)是|等價于此式子結(jié)束,就將加入 s.charAt(0)的 elsevreturn_vt_add()=true;集/求非終結(jié)符 V 關(guān)于該文法的集void one_vn_i,j; String s;(char v)for(i=0;iTGif(s.charAt(0)=v)/產(chǎn)生式左邊是要求的非終結(jié)符j=3;/E-TG(0:E;1:-;2:;3:T)one_grammar_(s,j);/求關(guān)于此產(chǎn)生式的集(s:E-TG;j:T)/void_of_each_grammar(String s,j):求某個產(chǎn)生式的集
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 未來十年移動支付的科技發(fā)展趨勢預(yù)測
- 標(biāo)準(zhǔn)化管理在生產(chǎn)現(xiàn)場的挑戰(zhàn)與對策
- 現(xiàn)代音樂文化的全球化傳播路徑
- 13人物描寫一組(說課稿)2023-2024學(xué)年統(tǒng)編版語文五年級下冊
- Unit 1 Playtime Lesson 3(說課稿)-2023-2024學(xué)年人教新起點版英語二年級下冊001
- 25 少年閏土 第二課時 說課稿-2024-2025學(xué)年語文六年級上冊 統(tǒng)編版
- Unit1 London is a big city(說課稿)2023-2024學(xué)年外研版(三起)四年級下冊
- 2024-2025學(xué)年高中生物 第七章 現(xiàn)代生物進(jìn)化理論 第1節(jié) 現(xiàn)代生物進(jìn)化理論的由來說課稿3 新人教版必修2
- Unit 2 Being a good language learner Exploring and Using 說課稿-2024-2025學(xué)年高中英語重大版(2019)必修第一冊
- 2025挖掘機勞動合同范文
- 北師大版五年級上冊四則混合運算100道及答案
- 專項債券在燃?xì)饣A(chǔ)設(shè)施建設(shè)中的融資作用
- 人教部編版道德與法治八年級下冊:6.3 《國家行政機關(guān)》說課稿1
- GE-LM2500+G4航改燃?xì)廨啓C在艦船和工業(yè)上的應(yīng)用
- 2024山東能源集團(tuán)中級人才庫選拔(高頻重點提升專題訓(xùn)練)共500題附帶答案詳解
- 鋼鐵是怎樣煉成的讀后感作文700字
- 武漢市江夏區(qū)2022-2023學(xué)年七年級上學(xué)期期末數(shù)學(xué)試卷【帶答案】-109
- 學(xué)校物業(yè)服務(wù)合同范本專業(yè)版
- SL 288-2014 水利工程施工監(jiān)理規(guī)范
- 部編版八年級語文上冊期末考試卷
- 2024年02月中央軍委后勤保障部2024年公開招考專業(yè)技能崗位文職人員筆試參考題庫附帶答案詳解
評論
0/150
提交評論