實(shí)驗(yàn)二--語法分析程序的設(shè)計(jì)-_第1頁
實(shí)驗(yàn)二--語法分析程序的設(shè)計(jì)-_第2頁
實(shí)驗(yàn)二--語法分析程序的設(shè)計(jì)-_第3頁
實(shí)驗(yàn)二--語法分析程序的設(shè)計(jì)-_第4頁
實(shí)驗(yàn)二--語法分析程序的設(shè)計(jì)-_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、實(shí)驗(yàn)二 語法分析程序的設(shè)計(jì)姓名: 學(xué)號(hào): 專業(yè)班級(jí)一、實(shí)驗(yàn)?zāi)康耐ㄟ^設(shè)計(jì)、編制、調(diào)試一個(gè)典型的語法分析程序,實(shí)現(xiàn)對詞法分析程序所提供的單詞序列進(jìn)行語法檢查和結(jié)構(gòu)分析,進(jìn)一步掌握常用的語法分析中預(yù)測分析方法。 二、實(shí)驗(yàn)內(nèi)容設(shè)計(jì)一個(gè)文法的預(yù)測分析程序,判斷特定表達(dá)式的正確性。三、實(shí)驗(yàn)要求1、 給出文法如下:GE E->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+語言或Java語言實(shí)現(xiàn);3、 利用預(yù)測分析程序完成下列功能:1) 手工將測試的表達(dá)式寫入文本文件,每個(gè)

2、表達(dá)式寫一行,用“;”表示結(jié)束;2) 讀入文本文件中的表達(dá)式;3) 調(diào)用實(shí)驗(yàn)一中的詞法分析程序搜索單詞;4) 把單詞送入預(yù)測分析程序,判斷表達(dá)式是否正確(是否是給出文法的語言),若錯(cuò)誤,應(yīng)給出錯(cuò)誤信息;5) 完成上述功能,有余力的同學(xué)可以進(jìn)一步完成通過程序?qū)崿F(xiàn)對非LL(1)文法到LL(1)文法的自動(dòng)轉(zhuǎ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)化為LL(1)文法;2、 學(xué)習(xí)預(yù)測分析程序的結(jié)構(gòu),設(shè)計(jì)合理的預(yù)測分析程序;3、

3、編寫測試程序,包括表達(dá)式的讀入和結(jié)果的輸出;4、 測試程序運(yùn)行效果,測試數(shù)據(jù)可以參考下列給出的數(shù)據(jù)。 六、測試數(shù)據(jù) 輸入數(shù)據(jù):編輯一個(gè)文本文文件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;正確結(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)+

4、1輸出:錯(cuò)誤七、源代碼import java.util.*;import java.io.*;public class test2 static String key_word = "main", "if", "then", "while", "do", "int","else" ;static String cal_word = "+", "-", "*", "/", &qu

5、ot;<", ">", "", "", "(",")", "", "", "=", "!=", "!", "=", ">=", "<=", "+=", "-=", "*=","/=", "" ;/* * 給

6、定文法GE: E->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 = ")", "#" ;stat

7、ic String followEA = ")", "#" ;static String followT = "+", ")", "#" ;static String followTB = "+", ")", "#" ;static String followF = "*", "+", ")", "#" ;static String firstE = &qu

8、ot;i", "(" ;static String firstEA = "+", "" ;static String firstT = "i", "(" ;static String firstTB = "*", "" ;static String firstF = "i", "(" ;static String list = "", "i", "+&quo

9、t;, "*", "(", ")", "#" , "E", "TA", null, null, "TA", null, null , "A", null, "+TA", null, null, "", "" , "T", "FB", null, null, "FB", null, null , "B"

10、;, null, "", "*FB", null, "", "" , "F", "i", null, null, "(E)", null, null ;public static void scan(String infile,String outfile, Stack<String> word, Stack<String> expression)throws Exception java.io.File file = new ja

11、va.io.File(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) <= '9' && tmp.charAt(i) &g

12、t;= '1') /檢查十進(jìn)制數(shù)字String num = ""while (tmp.charAt(i) <= '9' && tmp.charAt(i) >= '0') num += tmp.charAt(i);i+;if (i = tmp.length()break;output.println("< " + 1 + ", " + num + ">");wordcount.push("i");express

13、ioncount.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) <= '9' && tmp.charAt(i) >= '0') | (tmp.charAt(i) <= 'f' && tmp.cha

14、rAt(i) >= 'a') num += tmp.charAt(i);i+;if (i = tmp.length()break;output.println("< " + 3 + ", " + num + ">");wordcount.push("i");expressioncount.push(num);if (i + 1 < tmp.length()/ 檢查八進(jìn)制數(shù)字if (tmp.charAt(i) = '0') i+;String num = &qu

15、ot;"while (tmp.charAt(i) <= '7' && tmp.charAt(i) >= '0') num += tmp.charAt(i);i+;if (i = tmp.length()break;output.println("< " + 2 + ", " + num + ">");wordcount.push("i");expressioncount.push(num);/ 檢查關(guān)鍵字和變量if (i < t

16、mp.length() if (i < tmp.length() && tmp.charAt(i) >= 'a'&& tmp.charAt(i) <= 'z') String tmp_word = ""while (tmp.charAt(i) >= 'a' && tmp.charAt(i) <= 'z') tmp_word += tmp.charAt(i);i+;if (i = tmp.length()break;boolean is

17、_keyword = false;for (int j = 0; j < key_word.length; j+) if (tmp_word.equals(key_wordj) output.println("< " + key_wordj + ", " + "->");wordcount.push(key_wordj);expressioncount.push(key_wordj);is_keyword = true;break;if (!is_keyword) output.println("<

18、" + 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("&

19、quot; + tmp.charAt(i)+ tmp.charAt(i + 1) output.println("< " + cal_wordj + " ,"+ "->");wordcount.push(cal_wordj);expressioncount.push("-");if (wordcount.peek() = "") wordcount.pop();wordcount.push("#");count+;wordcount.push("#&qu

20、ot;);i += 2;break;for (int j = 0; j < cal_word.length; j+) if (cal_wordj.equals("" + tmp.charAt(i) output.println("< " + cal_wordj + ", " + "->");wordcount.push(cal_wordj);expressioncount.push(cal_wordj);if (wordcount.peek() = "") wordcount.

21、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<String> tmpword = new Stack20;Stack

22、<String> expression = new Stack20;for (int i = 0; i < tmpword.length; i+) tmpwordi = new Stack<String>();expressioni = new Stack<String>();test1.scan(infile, outfile, tmpword, expression); int i = 0;while (tmpwordi.size() > 2)String tmp = expressioni.toArray(new String0);prin

23、tArray(tmp);isLL1(tmpwordi);i+;public static void printArray(String s)for (int i = 0; i < s.length; i+)System.out.print(si);System.out.println();public static void isLL1(Stack<String> tmpword)String input = tmpword.toArray(new String0);int inputCount = 0;Stack<String> status = new Sta

24、ck<String>();status.push(inputinputCount+);status.push("E");boolean flag = true;boolean result = true;while (flag) if (isVt(status.peek()if (status.peek().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(), inputinputC

溫馨提示

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

評(píng)論

0/150

提交評(píng)論