版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 西 安 郵 電 大 學(xué) (計算機(jī)學(xué)院)課內(nèi)實驗報告實驗名稱: 詞法分析器的實現(xiàn) 專業(yè)名稱: 軟件工程班 級: 學(xué)生姓名: 學(xué)號(8位): 指導(dǎo)教師: 王曙燕實驗日期: 2015年4月10日一. 實驗?zāi)康? 深入理解有限自動機(jī)及其應(yīng)用2 掌握根據(jù)語言的詞法規(guī)則構(gòu)造識別其單詞的有限自動機(jī)的方法3基本掌握詞法分析程序的開發(fā)。 二. 實驗內(nèi)容1、 詞法分析器的功能和輸出格式詞法分析器的功能是輸入源程序,輸出單詞符號。詞法分析器的單詞符號常常表示成以下的二元式(單詞種別碼,單詞符號的屬性值)。本實驗中,采用的是一類符號一種別碼的方式。2、 單詞的BNF表示<標(biāo)識符>-> <字母
2、><字母數(shù)字串><字母數(shù)字串>-><字母><字母數(shù)字串>|<數(shù)字><字母數(shù)字串>|<下劃線><字母數(shù)字串>|<無符號整數(shù)>-> <數(shù)字><數(shù)字串><數(shù)字串>-> <數(shù)字><數(shù)字串> |<加法運算符>-> +<減法運算符>-> -<大于關(guān)系運算符>-> > <大于等于關(guān)系運算符>-> >=3、“超前搜索”方法詞法分析時,常常
3、會用到超前搜索方法。如當(dāng)前待分析字符串為“a>+”,當(dāng)前字符為>,此時,分析器倒底是將其分析為大于關(guān)系運算符還是大于等于關(guān)系運算符呢?顯然,只有知道下一個字符是什么才能下結(jié)論。于是分析器讀入下一個字符+,這時可知應(yīng)將>解釋為大于運算符。但此時,超前讀了一個字符+,所以要回退一個字符,詞法分析器才能正常運行。在分析標(biāo)識符,無符號整數(shù)等時也有類似情況。4、三方案設(shè)計1、 詞法形式化描述使用正則文法進(jìn)行描述,則可以得到如下的正規(guī)式:其中ID表示標(biāo)識符,NUM表示整型常量,RES表示保留字,DEL表示界符,OPR表示運算符。 A(ID | NUM | RES | DEL | OPR)
4、 *IDletter(letter | didit)*NUMdigit digit*lettera | | z | A | | Zdigit 0 | | 9RES program | begin | end | var | int | and | or | not | if | then | else | while | doDEL( | ) | . | ; | ,OPR+ | * | := | > | < | = | >= | <= | <> 如果關(guān)鍵字、標(biāo)識符和常數(shù)之間沒有確定的算符或界符作間隔,則至少用一個空格作間隔。空格由空白、制表符和換行符組成。2
5、、 單詞種別定義;A語言中的單詞符號及其對應(yīng)的種別編碼如下表所示:單詞符號種別編碼單詞符號種別編碼program1+16begin2*17end3(18var4)19int5,20and6.21or7:=22not8;23if9>24then10<25else11=26while12>=27do13<=28標(biāo)識符14<>29整型常量15-303、 狀態(tài)轉(zhuǎn)換圖;語言A的詞法分析的狀態(tài)轉(zhuǎn)換圖如下所示:空格符,制表符 或回車符 字母或數(shù)字非字母或數(shù)字2字母01 數(shù)字非數(shù)字3+4數(shù)字587*633()2224非=其他-142513812):>9,.=10111
6、5;/16=28/*2627其他23=1718>19非=<=212025其他四測試數(shù)據(jù)及運行結(jié)果五總結(jié)1 實驗過程中遇到的問題及解決辦法;在實驗中也遇到很多的錯誤,比如剛開時不能讀寫含有數(shù)字和不能識別的單詞的語句,直接程序運行出錯,經(jīng)過很長時間的調(diào)試和分析,最后用單步調(diào)試方法找到了原因所在 2 對設(shè)計及調(diào)試過程的心得體會。 通過本次試驗,我加深了對編譯原理中的詞法分析的理解,同時通過動手,更加鍛煉了自己。本次試驗由于使用的剛剛學(xué)習(xí)的java語言,通過這次機(jī)會加強(qiáng)了自己對java語言的熟悉的使用。六實驗程序的源代碼如下:package Analysis;import java.io.
7、BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;public class A private static char ch;private static String strToken;private static int index = 0;private static int line = 1;private stati
8、c boolean noteTag=false;private Map< Integer,String > keywords;private HashMap<String, Integer> punctuations; private static ArrayList<String> p=new ArrayList<String>(); private static ArrayList<String> q=new ArrayList<String>();/ get and set 函數(shù)public char getCh()
9、 return ch;public void setCh(char ch) A.ch = ch;public String getStrToken() return strToken;public void setStrToken(String strToken) A.strToken = strToken;public void setPunctuations(HashMap<String, Integer> punctuations) this.punctuations = punctuations;public Map<String, Integer> getPu
10、nctuations() return punctuations;public void setKeywords(Map<Integer, String> keywords) this.keywords = keywords;public Map<Integer, String> getKeywords() return keywords;/ 構(gòu)造函數(shù)public A() this.keywords = new HashMap< Integer,String >();keywords.put(1,"Program");keywords.p
11、ut(2,"begin");keywords.put(3,"end");keywords.put(4,"var");keywords.put(5,"int");keywords.put(6,"and");keywords.put(7,"or");keywords.put(8,"not");keywords.put(9,"if");keywords.put(10,"then");keywords.put(11,&quo
12、t;else");keywords.put(12,"while");keywords.put(13,"do");this.punctuations = new HashMap<String, Integer>();punctuations.put("+", 16);punctuations.put("*", 17);punctuations.put("(", 18);punctuations.put(")", 19);punctuations.put(&
13、quot;,", 20);punctuations.put("", 21);punctuations.put(":=", 22);punctuations.put(">", 23);punctuations.put(">=", 24);punctuations.put("<", 25);punctuations.put("<=", 26);punctuations.put(".", 27);punctuations.put
14、("<>", 28);punctuations.put("=", 29);/ 函數(shù)定義(詞法分析函數(shù))public boolean Analyse(char strArray) index = 0;/ 每次分析一行完成后就將index置0char temp1;int rowLength = strArray.length;outer:while (index < rowLength) strToken = ""ch = GetChar(strArray); if (ch = '') System.ou
15、t.println("(21,;) "); else if(ch=':') index+; System.out.println("(22,:=)"); else if(ch='.') System.out.println("(27,.)"); else if(ch='>') if( temp1=this.getNextChar(strArray)='=') System.out.println("(24,>=)"); else index
16、-; System.out.println("(23,>)"); else if(ch='<') if( temp1=this.getNextChar(strArray)='=') System.out.println("(26,<=)"); else if(temp1='>') System.out.println("(28,<>)"); else index-; System.out.println("(25,<)");
17、else if(ch='*'&& noteTag=false) System.out.println("(17,*)"); else if (java.lang.Character.isLetter(ch)&¬eTag=false) strToken = contact(strToken, ch);ch = getNextChar(strArray);while (java.lang.Character.isLetter(ch)| (java.lang.Character.isDigit(ch) strToken = c
18、ontact(strToken, ch);ch = getNextChar(strArray);index-;/ System.err.println("!"+strToken);if (findKeyword(strToken) /System.out.println("(15," + strToken.toString() + ")n");int i=getKeyWordKey(strToken);System.out.println("("+i+",-)"); elseif(!exist(
19、p,strToken) p.add(strToken);int i=getindex(p,strToken);/System.out.println("(14," + strToken.toString() + ")n");System.out.println("(14,"+i+")"); else if (java.lang.Character.isDigit(ch)&¬eTag=false) strToken = this.contact(strToken, ch);ch = this.g
20、etNextChar(strArray);while (java.lang.Character.isDigit(ch) strToken = this.contact(strToken, ch);ch = this.getNextChar(strArray);index-;/System.out.println("(15," + strToken.toString() + ")n"); if(!exist(q,strToken) q.add(strToken); int i=getindex(q,strToken); System.out.println
21、("(15,"+i+")");strToken = "" else if (ch = '/'|noteTag=true) int startj=index; /注釋起始位置標(biāo)記int starti=line;if(noteTag=false) /System.out.println("該部分是注釋注釋,從第"+starti+"行第"+startj+"列開始"); char temp = this.getNextChar(strArray);if (temp =
22、 '*'&¬eTag=false) temp = this.getNextChar(strArray);while(index<rowLength)temp = this.getNextChar(strArray);if(temp='*'&&( temp1=this.getNextChar(strArray)='/')index-;break;if(index>=rowLength)noteTag=true;break outer;else if(noteTag=true&&ch!
23、='*') while(index<rowLength)temp = this.getNextChar(strArray);if(temp='*'&&(temp1=this.getNextChar(strArray)='/')noteTag=false;break; else if(temp='/')while(true)index+;if(index>=rowLength)break outer; else return false; else String key = String.valueOf(
24、ch);if (this.findPunctuation(key) System.out.println("("+this.getPunctuation(key)+","+ key + ")"); else if (key.equals(" ") | key.equals(" ") break; elsereturn false; /System.out.println( "未知符號 " + key + "n"); /strToken = "&q
25、uot;return true;public char GetChar(char array) try while (arrayindex) = ' ') index+;index+;/ 提前指向下一個字符 catch (ArrayIndexOutOfBoundsException e) return ' 'return arrayindex - 1;public char getNextChar(char strChar) index+;return strCharindex - 1;public String contact(String token, ch
26、ar ch) return token + String.valueOf(ch);public boolean findKeyword(String str) for(int i=0;i<13;i+)if(str.equalsIgnoreCase(this.keywords.get(i)return true;return false;public boolean findPunctuation(String str) if (this.punctuations.containsKey(str) return true; elsereturn false;public int getPu
27、nctuation(String str) return this.punctuations.get(str);public boolean Clean() return true;public void callError(int line) System.out.println("出現(xiàn)錯誤,錯誤位置在第" + line + "行,第" + index + "列");public boolean exist(ArrayList<String> p,String strToken)if(p.contains(getStrT
28、oken()return true;elsereturn false;public int getKeyWordKey(String str)for(int i=1;i<=13;i+)if(str.equalsIgnoreCase(this.keywords.get(i)return i;return 10000; public int getindex(ArrayList<String> p,String Str) return p.lastIndexOf(Str)+1; /*int j=0; for(int i=0;i<p.size();i+) if(p.get(i).equ
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度工業(yè)自動化設(shè)備供應(yīng)鏈鋪貨與集成服務(wù)合同
- 2025年度保險合同法律解讀與理賠實務(wù)操作指南
- 二零二四年度「高端旅游項目開發(fā)」合同
- 2025年度數(shù)據(jù)中心建設(shè)與運維管理合同
- 2025年度新能源汽車合同付款補充協(xié)議
- 2025年度物流運輸供應(yīng)商合同
- 2025年度國際能源勘探與開發(fā)服務(wù)合同
- 2025年度企業(yè)知識產(chǎn)權(quán)許可使用合同樣本
- 二零二五年度車輛買賣合同含車輛二手車交易擔(dān)保4篇
- 2025年度婚禮攝影攝像服務(wù)及定制化產(chǎn)品合同
- 蘇教版四年級數(shù)學(xué)下冊第三單元第二課時《常見的數(shù)量關(guān)系》課件
- 浙江省臺州市2021-2022學(xué)年高一上學(xué)期期末質(zhì)量評估政治試題 含解析
- 中國高血壓防治指南(2024年修訂版)解讀課件
- 2024年浙江省中考科學(xué)試卷
- 初三科目綜合模擬卷
- 2024年全國高考新課標(biāo)卷物理真題(含答案)
- ArcGIS軟件入門培訓(xùn)教程演示文稿
- 運動技能學(xué)習(xí)與控制課件第十章動作技能的指導(dǎo)與示范
- 偶函數(shù)講課課件
- 中醫(yī)治療“濕疹”醫(yī)案72例
- 交通工程公司乳化瀝青儲油罐拆除工程安全協(xié)議書
評論
0/150
提交評論