




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)二語(yǔ)法分析程序的設(shè)計(jì)姓名:_ 學(xué)號(hào):_ 專業(yè)班級(jí)一、實(shí)驗(yàn)?zāi)康耐ㄟ^(guò)設(shè)計(jì)、編制、調(diào)試一個(gè)典型的語(yǔ)法分析程序,實(shí)現(xiàn)對(duì)詞法分析程序所提供的單詞序列進(jìn)行語(yǔ)法檢查和結(jié)構(gòu)分析,進(jìn)一步掌握常用的語(yǔ)法分析中預(yù)測(cè)分析方法。二、實(shí)驗(yàn)內(nèi)容設(shè)計(jì)一個(gè)文法的預(yù)測(cè)分析程序,判斷特定表達(dá)式的正確性。三、實(shí)驗(yàn)要求1、 給出文法如下:GEE-T|E+T;T-F|T*F;F-i|(E);2、 根據(jù)該文法構(gòu)造相應(yīng)的LL(1)文法及LL(1)分析表,并為該文法設(shè)計(jì)預(yù)測(cè)分析程序, 利用C語(yǔ)言或C+語(yǔ)言或Java語(yǔ)言實(shí)現(xiàn);3、 利用預(yù)測(cè)分析程序完成下列功能:1) 手工將測(cè)試的表達(dá)式寫入文本文件,每個(gè)表達(dá)式寫一行,用“;”表示結(jié)束;2)
2、讀入文本文件中的表達(dá)式;3) 調(diào)用實(shí)驗(yàn)一中的詞法分析程序搜索單詞;4) 把單詞送入預(yù)測(cè)分析程序,判斷表達(dá)式是否正確(是否是給出文法的語(yǔ)言),若錯(cuò)誤,應(yīng)給出錯(cuò)誤信息;5) 完成上述功能,有余力的同學(xué)可以進(jìn)一步完成通過(guò)程序?qū)崿F(xiàn)對(duì)非LL(1)文法到LL(1)文法的自動(dòng)轉(zhuǎn)換(見(jiàn)實(shí)驗(yàn)二附加資料1)。四、實(shí)驗(yàn)環(huán)境PC微機(jī)DOS操作系統(tǒng)或Windows操作系統(tǒng)Turbo C程序集成環(huán)境或Visual C+程序集成環(huán)境五、實(shí)驗(yàn)步驟1、 分析文法,將給出的文法轉(zhuǎn)化為L(zhǎng)L(1)文法;2、 學(xué)習(xí)預(yù)測(cè)分析程序的結(jié)構(gòu),設(shè)計(jì)合理的預(yù)測(cè)分析程序;3、 編寫測(cè)試程序,包括表達(dá)式的讀入和結(jié)果的輸出;4、 測(cè)試程序運(yùn)行效果,測(cè)試
3、數(shù)據(jù)可以參考下列給出的數(shù)據(jù)。六、測(cè)試數(shù)據(jù)輸入數(shù)據(jù):編輯一個(gè)文本文文件expressi on. txt,在文件中輸入如下內(nèi)容:10;1+2;(1+2)*3+(5+6*7);(1+2)*3+4;1+2+3+(*4+5);(a+b)*(c+d);(ab3+de4)*5)+1;正確結(jié)果:(1)10;輸出: 正確(2)1+2;輸出: 正確(3)(1+2)*3+(5+6*7);輸出:正確(4)(1+2)*3+4輸出: 錯(cuò)誤(5)1+2+3+(*4+5)輸出: 錯(cuò)誤(6)(a+b)*(c+d)輸出: 正確(7)(ab3+de4)*5)+1輸出: 錯(cuò)誤七、源代碼import java.util.*; impo
4、rt java.io.*;public class test2 staticStri ngkey_word = ma in, if,the n, while, do,int,else ;static Stri ng cal_word = +, -, *, /, , , (,), , , =, !=, !, =,=,T|E+T; T-F|T*F; F-i|(E);*/static String gram = E-TA, A-+TA, A-, T-FB, B-*FB, B-,F-P, F-(E) ;staticString staticString staticString staticStrin
5、g staticString staticString staticString staticString staticString staticString followE = ), # ; followEA = ), # ; followT = +, ), # ; followTB = +, ), # ; followF = *, +, ), # ; firstE = i, ( ; firstEA = +, ; firstT = i, ( ; firstTB = *, ; firstF = i, ( ;static String list = , i, +, *, (, ), # , E,
6、 TA, null, null, TA, null, null , A, null, +TA, null, null, , , T, FB, null, null, FB, null, null , B, null, , *FB, null, , , F, i, null, null, (E), null, null ;public static void scan(String infile,String outfile, Stack word,Stack expression)throws Exception java.io.File file = newjava.io.File(infi
7、le);Scanner input = new Scanner(file);java.io.PrintWriter output = new PrintWriter(outfile);int count = 0;wordcount.push(#);while (input.hasNext() String tmp = input.next();int i = 0;while (i tmp.length() if (tmp.charAt(i) = 1) /檢查十進(jìn)制數(shù)字String num = ;while (tmp.charAt(i) = 0)num += tmp.charAt(i);i+;i
8、f (i = tmp.length()break;output.println();wordcount.push(i);expressioncount.push(num);if (i + 2 tmp.length()/檢查十六進(jìn)制數(shù)字if (tmp.charAt(i) = 0 &tmp.charAt(i + 1) = x)i += 2;String num = ;while (tmp.charAt(i) = 0) |(tmp.charAt(i) = a) num += tmp.charAt(i);i+;if (i = tmp.length()break;output.println()
9、;wordcount.push(i);expressioncount.push(num);if (i + 1 tmp.length()/檢查八進(jìn)制數(shù)字if (tmp.charAt(i) = 0) i+;String num = ; while (tmp.charAt(i) = 7&tmp.charAt(i) 0) num += tmp.charAt(i);i+; if (i = tmp.length() break;output.println();wordcount.push(i);expressioncount.push(num);/檢查關(guān)鍵字和變量if (i tmp.length
10、() if (i = a& tmp.charAt(i) = a &tmp.charAt(i) z) tmp_word += tmp.charAt(i); i+;if (i = tmp.length() break;boolean is_keyword = false; for (int j = 0; j =if (tmp_word.equals(key_wordj) output.println( + key_wordj + ,wordcount.push(key_wordj); expressioncount.push(key_wordj);is_keyword = true
11、;break;if (!is_keyword) output.println( + 0 + , + tmp_word +wordcount.push(i); expressioncount.push(tmp_word);/檢查運(yùn)算符以及;if (i tmp.length() if (i + 1 tmp.length() if (tmp.charAt(i + 1) = =) for (int j = 0; j cal_word.length; j+) if(cal_wordj.equals(+ tmp.charAt(i + 1) output.println( + cal_wordj +word
12、count.push(cal_wordj);expressioncount.push(-); if(wordcount.peek() = ;) wordcount.pop();wordcount.push(#); count+;wordcount.push(#);i += 2;break;tmp.charAt(i)for (int j = 0; j cal_word.length; j+) if (cal_wordj.equals( + tmp.charAt(i) output.println( + cal_wordj + , +Iwordcount.push(cal_wordj);expre
13、ssioncount.push(cal_wordj); if(wordcount.peek() = ;) wordcount.pop();wordcount.push(#);count+; wordcount.push(#);i+;break;input.close(); output.close();public static void main(String args) throws Exception String infile =D:/expression.txt;String outfile = D:/result2.txt;Stack tmpword = new Stack20;S
14、tack expression = new Stack20; for(int i = 0; i tmpword.length; i+) tmpwordi =new Stack(); expressioni = newStack();test1.scan(infile, outfile, tmpword, expression);public static void printArray(String s) for (int i = 0; i s.length;i+) System.out.print(si);System.out.println();public static void isL
15、L1(Stack tmpword)String input = tmpword.toArray(new String0);int i =0;while(tmpwordi.isLL1(tmpwordi);i+;int inputCount = 0;Stack status = new Stack();status.push(inputinputCount+); status.push(E);boolean flag = true;boolean result = true;while (flag) if (isVt(status.peek()if(status.peek().equals(inp
16、utinputCount) status.pop();inputCount+;public static boolean isVt(String s) /elseflag = false; result = false; result = false;else if (status.peek().equals(#)if (status.peek().equals(inputinputCount)flag = false;elseflag = false; result = false;elseif(listindexInList(status.peek(),inputinputCount)0i
17、ndexInList(status.peek(),inputinputCount)1 != null)int a = indexInList(status.peek(), inputinputCount);if (lista0a1.endsWith()status.pop();elsestatus.pop();for (int i = lista0a1.length() - 1; i = 0;i-)else flag =false; result =false;status.push( + lista0a1.charAt(i);if (result)System.out.println(elseSystem.out.println(IIII正確);錯(cuò)誤);判斷是否為Vtfor (int i = 1; i list0.length - 1; i+) if(s.equals(list0i) return true;ret
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度退股及員工持股計(jì)劃合同
- 個(gè)人股權(quán)轉(zhuǎn)讓合同2025年度含業(yè)績(jī)對(duì)賭條款
- 二零二五年度臨時(shí)項(xiàng)目經(jīng)理職務(wù)聘用與成果轉(zhuǎn)化合同
- 二零二五年度旅游團(tuán)隊(duì)保險(xiǎn)責(zé)任免除聲明
- 電線電纜購(gòu)銷合同
- 管理層勞動(dòng)合同工資
- 個(gè)人數(shù)字資產(chǎn)管理協(xié)議
- 全新池塘出租協(xié)議
- 月餅產(chǎn)品代銷合同
- 鄉(xiāng)村旅游發(fā)展策略與實(shí)施方案
- 體育競(jìng)賽的組織與編排
- 動(dòng)靜脈內(nèi)瘺的圍手術(shù)期護(hù)理-課件
- reaxys使用介紹課件
- 工程建設(shè)項(xiàng)目管理培訓(xùn)教材課件
- 11-化學(xué)動(dòng)力學(xué)基礎(chǔ)-2-考研試題資料系列
- 《簡(jiǎn)愛(ài)》課本劇劇本
- 社區(qū)獲得性肺炎臨床路徑
- 產(chǎn)品品質(zhì)檢驗(yàn)流程標(biāo)準(zhǔn)規(guī)范模板()
- GB∕T 7260.40-2020 不間斷電源系統(tǒng) UPS 第4部分:環(huán)境 要求及報(bào)告
- 安全文明施工管理(EHS)方案(24頁(yè))
- 水廠項(xiàng)目基于BIM技術(shù)全生命周期解決方案-城市智慧水務(wù)講座課件
評(píng)論
0/150
提交評(píng)論