![實驗二語法分析程序的設計_第1頁](http://file1.renrendoc.com/fileroot_temp2/2020-11/25/302c3197-a80d-45df-9bfb-cb3b58075709/302c3197-a80d-45df-9bfb-cb3b580757091.gif)
![實驗二語法分析程序的設計_第2頁](http://file1.renrendoc.com/fileroot_temp2/2020-11/25/302c3197-a80d-45df-9bfb-cb3b58075709/302c3197-a80d-45df-9bfb-cb3b580757092.gif)
![實驗二語法分析程序的設計_第3頁](http://file1.renrendoc.com/fileroot_temp2/2020-11/25/302c3197-a80d-45df-9bfb-cb3b58075709/302c3197-a80d-45df-9bfb-cb3b580757093.gif)
![實驗二語法分析程序的設計_第4頁](http://file1.renrendoc.com/fileroot_temp2/2020-11/25/302c3197-a80d-45df-9bfb-cb3b58075709/302c3197-a80d-45df-9bfb-cb3b580757094.gif)
![實驗二語法分析程序的設計_第5頁](http://file1.renrendoc.com/fileroot_temp2/2020-11/25/302c3197-a80d-45df-9bfb-cb3b58075709/302c3197-a80d-45df-9bfb-cb3b580757095.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、實驗二 語法分析程序的設計姓名: 學號: 專業(yè)班級一、實驗目的通過設計、編制、調(diào)試一個典型的語法分析程序,實現(xiàn)對詞法分析程序所提供的單詞序列進行語法檢查和結構分析,進一步掌握常用的語法分析中預測分析方法。二、實驗內(nèi)容設計一個文法的預測分析程序,判斷特定表達式的正確性。三、實驗要求1、 給出文法如下:GE E-T|E+T; T-F|T*F; F-i|(E);2、 根據(jù)該文法構造相應的LL(1)文法及LL(1)分析表,并為該文法設計預測分析程序,利用C語言或C+語言或Java語言實現(xiàn);3、 利用預測分析程序完成下列功能:1) 手工將測試的表達式寫入文本文件,每個表達式寫一行,用“;”表示結束;2)
2、 讀入文本文件中的表達式;3) 調(diào)用實驗一中的詞法分析程序搜索單詞;4) 把單詞送入預測分析程序,判斷表達式是否正確(是否是給出文法的語言),若錯誤,應給出錯誤信息;5) 完成上述功能,有余力的同學可以進一步完成通過程序?qū)崿F(xiàn)對非LL(1)文法到LL(1)文法的自動轉換(見實驗二附加資料1)。四、實驗環(huán)境PC微機DOS操作系統(tǒng)或 Windows 操作系統(tǒng)Turbo C 程序集成環(huán)境或 Visual C+ 程序集成環(huán)境五、實驗步驟1、 分析文法,將給出的文法轉化為LL(1)文法;2、 學習預測分析程序的結構,設計合理的預測分析程序;3、 編寫測試程序,包括表達式的讀入和結果的輸出;4、 測試程序運
3、行效果,測試數(shù)據(jù)可以參考下列給出的數(shù)據(jù)。六、測試數(shù)據(jù) 輸入數(shù)據(jù):編輯一個文本文文件expression.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;正確結果:(1)10;輸出:正確(2)1+2;輸出:正確(3)(1+2)*3+(5+6*7);輸出:正確(4)(1+2)*3+4輸出:錯誤(5)1+2+3+(*4+5)輸出:錯誤(6)(a+b)*(c+d)輸出:正確(7)(ab3+de4)*5)+1輸出:錯誤七、源代碼import java.util.*;import
4、 java.io.*;public class test2 static String key_word = main, if, then, while, do, int,else ;static String 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) ;static String followE = ), # ;static String fol
5、lowEA = ), # ;static String followT = +, ), # ;static String followTB = +, ), # ;static String followF = *, +, ), # ;static String firstE = i, ( ;static String firstEA = +, ;static String firstT = i, ( ;static String firstTB = *, ;static String firstF = i, ( ;static String list = , i, +, *, (, ), #
6、, E, 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 = new java.io.Fil
7、e(infile);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) /檢查十進制數(shù)字String num = ;while (tmp.charAt(i) = 0) num += tmp.charAt(
8、i);i+;if (i = tmp.length()break;output.println();wordcount.push(i);expressioncount.push(num);if (i + 2 tmp.length()/ 檢查十六進制數(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.pr
9、intln();wordcount.push(i);expressioncount.push(num);if (i + 1 tmp.length()/ 檢查八進制數(shù)字if (tmp.charAt(i) = 0) i+;String num = ;while (tmp.charAt(i) = 0) num += tmp.charAt(i);i+;if (i = tmp.length()break;output.println();wordcount.push(i);expressioncount.push(num);/ 檢查關鍵字和變量if (i tmp.length() if (i = a&
10、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 key_word.length; j+) if (tmp_word.equals(key_wordj) output.println();wordcount.push(key_wordj);expressioncount.push(key_wordj);is_keyword = true;break;if (!is_ke
11、yword) output.println();wordcount.push(i);expressioncount.push(tmp_word);/ 檢查運算符以及;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)+ tmp.charAt(i + 1) output.println();wordcount.push(cal_wordj);expressio
12、ncount.push(-);if (wordcount.peek() = ;) wordcount.pop();wordcount.push(#);count+;wordcount.push(#);i += 2;break;for (int j = 0; j cal_word.length; j+) if (cal_wordj.equals( + tmp.charAt(i) output.println();wordcount.push(cal_wordj);expressioncount.push(cal_wordj);if (wordcount.peek() = ;) wordcount
13、.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;Stack expression = new Stack20;for (int i = 0; i tmpword.length;
14、 i+) tmpwordi = new Stack();expressioni = new Stack();test1.scan(infile, outfile, tmpword, expression); int i = 0;while (tmpwordi.size() 2)String tmp = expressioni.toArray(new String0);printArray(tmp);isLL1(tmpwordi);i+;public static void printArray(String s)for (int i = 0; i s.length; i+)System.out
15、.print(si);System.out.println();public static void isLL1(Stack tmpword)String input = tmpword.toArray(new String0);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()
16、.equals(inputinputCount)status.pop();inputCount+;elseflag = false;result = false;else if (status.peek().equals(#)if (status.peek().equals(inputinputCount)flag = false;elseflag = false;result = false;else if (listindexInList(status.peek(), inputinputCount)0indexInList(status.peek(), inputinputCount)1
17、 != null)int a = indexInList(status.peek(), inputinputCount);if (lista0a1.endsWith()status.pop();elsestatus.pop();for (int i = lista0a1.length() - 1; i = 0; i-)status.push( + lista0a1.charAt(i);elseflag = false;result = false;if (result)System.out.println(正確);elseSystem.out.println(錯誤);public static boolean isVt(String s) /判斷
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學三年級數(shù)學五千以內(nèi)加減混合兩步運算同步練習訓練題帶答案
- 小學三年級數(shù)學三位數(shù)乘以一位數(shù)水平作業(yè)題
- 藝術校園新風尚
- 春分節(jié)氣講解
- 醫(yī)學知識全景
- 山西省運城市2024-2025學年九年級上學期期末語文試題(解析版)
- 士兵困難補助申請書
- 學校申請書模板
- 高項繼續(xù)教育試題及答案
- 團總之申請書
- 生物-遼寧省大連市2024-2025學年高三上學期期末雙基測試卷及答案
- Unit 4 A glimpse of the future 說課稿-2023-2024學年高二下學期英語外研版(2019)選擇性必修第三冊001
- 加氣站安全課件
- 《民營企業(yè)清廉建設評價規(guī)范》
- 智能RPA財務機器人開發(fā)教程-基于來也UiBot 課件 第2章-常用機器人流程自動化
- 公務車輛定點加油服務投標文件(技術方案)
- 安徽工程大學《回歸分析》2023-2024學年第一學期期末試卷
- 讀書分享《給教師的建議》課件
- 《中小學校園食品安全和膳食經(jīng)費管理工作指引》專題講座
- 廣東省茂名市2023-2024學年高一上學期物理期末試卷(含答案)
- 江蘇省蘇州市昆山、太倉、常熟、張家港四市2024-2025學年八年級上學期期中陽光測評生物學試卷(含答案)
評論
0/150
提交評論