




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、java詞法分析器 使用java開(kāi)發(fā),并且用來(lái)分析java源文件2003年1月12日1. 開(kāi)發(fā)工具:rational rose2002 jedition,borland jbuilder6 professional2. 開(kāi)發(fā)步驟:1) 基于狀態(tài)轉(zhuǎn)換圖的編譯器原理如下: 2)在rose中建立分析器模型框架,根據(jù)分析器的狀態(tài)轉(zhuǎn)換圖算法以及算法構(gòu)造。詞法分析器的框架結(jié)構(gòu)如下圖所示: (分析器軟件包) (詞法分析器的控制器結(jié)構(gòu),包括預(yù)編譯器,掃描程序,保留字表和單詞的類型種別碼表以及詞法分析器的引導(dǎo)程序和控制程序) (詞法分析器的掃描緩沖區(qū)和輸入緩沖區(qū)結(jié)構(gòu)以及獲得緩沖區(qū)的緩沖工廠)3)使用rose正向
2、工程產(chǎn)生java框架代碼,在jbuilder中進(jìn)行編輯實(shí)現(xiàn)功能代碼,生成最終的代碼,進(jìn)行test和debug,最后形成最終的目標(biāo)程序。具體的實(shí)現(xiàn)請(qǐng)參考源代碼。編輯和測(cè)試如下圖所示: (開(kāi)發(fā)環(huán)境) (運(yùn)行結(jié)果,詳細(xì)結(jié)果附在后面)3. 源代碼:/lisence head/*java accidence analyser*author yellowicq*all copyright reserved*version 1.0*/lisence1) 詞法分析器引導(dǎo)文件:main.javapackage jaccidenceanalyse;import javax.xml.parsers.*;import
3、 org.w3c.dom.*;public class main /* * param args * return void * roseuid 3d9bae4702ad */ public static void main(string args) /讀取配置文件,得到系統(tǒng)屬性 string cfgstring = new string4; try cfgstring = main.loadaacfg("d:aacfg.xml"); catch (exception e) e.printstacktrace(system.err); /設(shè)置待讀文件名/ /保留字表文件 s
4、tring reservefilename = cfgstring0; /類型種別碼表文件 string classfilename = cfgstring1; /需要分析的源文件 string sourcefilename = cfgstring2; /輸出文件 string outputfilename = cfgstring3;/ /創(chuàng)建詞法分析器 accidenceanalyser aa = new accidenceanalyser(); aa.setfilespath(reservefilename, classfilename, sourcefilename, outputfil
5、ename); /建立所需要的文件對(duì)象 /初始化詞法分析器 aa.initaa(); /初始化關(guān)鍵字表 aa.keywordtable.initkeywordtable(); /初始化類型種別碼表 aa.classidentity.initclassidentitytable(); /開(kāi)始進(jìn)行詞法分析 aa.startaa(); /分析完畢 /讀取配置文件 private static string loadaacfg(string name) throws exception string cfgstring = new string4; /*解析xml配置文件*/ try /*創(chuàng)建文檔工廠
6、*/ documentbuilderfactory factory = documentbuilderfactory.newinstance(); /*創(chuàng)建文檔解析器*/ documentbuilder builder = factory.newdocumentbuilder(); /*解析配置文件*/ document doc = builder.parse(name); /*規(guī)范化文檔*/ doc.normalize(); /*查找接點(diǎn)表*/ nodelist nlists = doc.getelementsbytagname("filepath"); for (int
7、 i = 0; i < nlists.getlength(); i+) element item = (element) nlists.item(i); /取得需要的配置屬性 /*/ cfgstring0 = item.getelementsbytagname("reservefilename").item(0). getfirstchild().getnodevalue().trim(); /*/ cfgstring1 = item.getelementsbytagname("classfilename").item(0).getfirstchi
8、ld().getnodevalue().trim(); /*/ cfgstring2 = item.getelementsbytagname("sourcefilename").item(0). getfirstchild().getnodevalue().trim(); /*/ cfgstring3 = item.getelementsbytagname("outputfilename").item(0). getfirstchild().getnodevalue().trim(); /*/ catch (exception e) e.printsta
9、cktrace(); throw new exception("error加載配置文件 " + name + " 錯(cuò)誤!"); /返回屬性數(shù)組 return cfgstring; 2) 詞法分析器主程序:accidenceanalyser.java/source file: d:jaccidenceanalyseaccidenceanalyser.javapackage jaccidenceanalyse;import java.io.*;import java.util.*;import jaccidenceanalyse.buffer.*;publi
10、c class accidenceanalyser private java.io.file sourcefile; private java.io.file reservefile; private java.io.file classfile; private java.io.file outputfile; public pretreatment pretreatment; public keywordtable keywordtable; public classidentity classidentity; public scaner scaner; public concretes
11、canbufferfactory csbfactory; /* * roseuid 3d9bb93303d0 */ public accidenceanalyser() system.out.println("infor已經(jīng)建立詞法分析器!"); /* * roseuid 3d9baef9029f */ public void initaa() /創(chuàng)建緩沖工廠 this.csbfactory = new concretescanbufferfactory(); /創(chuàng)建字符串掃描對(duì)象 scaner = new scaner(this); /創(chuàng)建pre處理對(duì)象 pretreat
12、ment = new pretreatment(sourcefile, this); /創(chuàng)建關(guān)鍵字表對(duì)象 keywordtable = new keywordtable(reservefile); /創(chuàng)建對(duì)象種別碼表對(duì)象 classidentity = new classidentity(classfile); system.out.println("infor已經(jīng)初始化詞法分析器!"); /* * roseuid 3d9baf12022d */ public void setfilespath(string reservefilename, string classfil
13、ename, string sourcefilename, string outputfilename) /創(chuàng)建文件對(duì)象 sourcefile = new java.io.file(sourcefilename); /創(chuàng)建文件對(duì)象 reservefile = new java.io.file(reservefilename); /創(chuàng)建文件對(duì)象 classfile = new java.io.file(classfilename); /創(chuàng)建文件對(duì)象 outputfile = new java.io.file(outputfilename); /如果文件已經(jīng)存在,先刪除,然后建立新文件 if (o
14、utputfile.exists() outputfile.delete(); try outputfile.createnewfile(); catch (exception e) e.printstacktrace(system.err); try /創(chuàng)建文件隨機(jī)讀取對(duì)象 java.io.randomaccessfile routputfile = new java.io.randomaccessfile(this. outputfile, "rw"); /提示信息 routputfile.write("/n". getbytes(); routpu
15、tfile.write( ("/jaccidenceanalyser version " + getversion() + " design by yellowicq/n").getbytes(); routputfile.write("/java詞法分析器/n".getbytes(); routputfile.write("/使用java語(yǔ)言開(kāi)發(fā)/n". getbytes(); routputfile.write("/n". getbytes(); routputfile.write(&quo
16、t;詞法分析結(jié)果如下:n".getbytes(); /關(guān)閉文件流 routputfile.close(); catch (exception e) e.printstacktrace(system.err); /* * roseuid 3d9bafab0089 */ public void startaa() /從預(yù)處理開(kāi)始詞法分析 this.pretreatment.startpretreatment(); /* * roseuid 3d9bb0b40383 */ public void outputaccidence(string outputstring) /把分析出來(lái)的單詞寫(xiě)
17、入文件 outputstring = "n第" + this.pretreatment.filerow + "行n" + outputstring; try /創(chuàng)建文件隨機(jī)讀取對(duì)象 java.io.randomaccessfile routputfile = new java.io.randomaccessfile(this. outputfile, "rw"); /移動(dòng)指針到文件末尾 routputfile.seek(routputfile.length(); /start appending! routputfile.write(
18、outputstring.getbytes(); /關(guān)閉文件流 routputfile.close(); catch (exception e) e.printstacktrace(system.err); /將分析的單詞結(jié)果輸出到終端 system.out.print(outputstring); /* * roseuid 3d9bb0ce02c2 */ public void controlthread() /控制掃描器啟動(dòng)掃描 scaner.controlthread(); /獲得版本號(hào) public string getversion() return "1.0"
19、3) 預(yù)處理子程序:pretreatment.java /source file: d:jaccidenceanalysepretreatment.javapackage jaccidenceanalyse;import jaccidenceanalyse.buffer.*;import java.io.*;public class pretreatment private string tmpstring; private string outputstring; private int buffer_size = 100; private accidenceanalyser aa; pub
20、lic inputbuffer inputbuffer; /輸入緩沖區(qū)-共享 private java.io.file sourcefile; /文件對(duì)象 private java.io.randomaccessfile randomafile; /隨機(jī)文件對(duì)象 public static int filerow = 0; /* * roseuid 3dab7c530399 */ public pretreatment(file sourcefile, accidenceanalyser aa) try this.sourcefile = sourcefile; this.randomafil
21、e = new java.io.randomaccessfile(this.sourcefile, "r"); catch (filenotfoundexception e) e.printstacktrace(system.err); this.aa = aa; inputbuffer = aa.csbfactory.createinputbuffer(buffer_size); system.out.println("infor預(yù)處理器已經(jīng)創(chuàng)建!"); /* * roseuid 3d9bafe20331 */ public void putsourc
22、etoinbuffer(string tmpstring) this.inputbuffer.data = tmpstring.tochararray(); /* * roseuid 3d9bb0400169 */ public void putfintoscbuffer(string filtratedstring) aa.scaner.scanbuffer.data = filtratedstring.tochararray(); /* * roseuid 3d9bb05e00a4 */ public void controlthread() int intlength; int resc
23、ounter = 0; string tmpstring; string filtratedstring; system.out.println("infor開(kāi)始單詞分析/"); try if (sourcefile.exists() /文件存在 /讀文件內(nèi)容到緩沖區(qū) while ( (tmpstring = this.randomafile.readline() != null) +filerow; /分割符 system.out.println(".begin row " + this.filerow + "."); /開(kāi)始這一行
24、分析 system.out.println("infor正在處理行: " + string.valueof(filerow); /放入輸入緩沖區(qū) this.putsourcetoinbuffer(tmpstring); /處理字符串 filtratedstring = this.filtratesource(this.inputbuffer.data); system.out.println("infor已過(guò)濾句子: " + filtratedstring); /放入掃描緩沖區(qū) this.putfintoscbuffer(filtratedstring)
25、; aa.controlthread(); system.out.println( "infor分析完畢/"); else /文件不存在 system.err.println("error源文件不存在!"); catch (exception e) e.printstacktrace(system.err); /* * roseuid 3d9bb07d0239 */ public string filtratesource(char data) string filtratedstring = string.valueof(data).trim(); r
26、eturn filtratedstring; /* * roseuid 3d9bb9350315 */ public void startpretreatment() this.controlthread(); 4) 掃描子程序:scaner.java/source file: d:jaccidenceanalysescaner.javapackage jaccidenceanalyse;import jaccidenceanalyse.buffer.*;public class scaner public scanbuffer scanbuffer; /掃描緩沖區(qū)-共享 private st
27、ring finalaccidence; private accidenceanalyser aa; private int buffer_size = 100; private string todelstring; private int senlength = 0; private char sentencechar = new char1000; private string token; private char char; private int index = 0; private string identity = "identity" private st
28、ring digit = "digit" private string word_error_inf = "在此行發(fā)現(xiàn)不能識(shí)別的單詞,此行分析終止!" private boolean astate = true; /* * roseuid 3d9bb9370213 */ public scaner(accidenceanalyser aa) this.aa = aa; initbuffer(); this.finalaccidence = "" system.out.println("infor掃描處理器已經(jīng)創(chuàng)建!"
29、;); /* * roseuid 3d9bb2860329 */ public string readfrombuffer(char data) string todelstring = string.valueof(data); return todelstring; /* * param tmpstring * return string * roseuid 3d9bb2d5008d */ public string scan(string todelstring) sentencechar = todelstring.tochararray(); this.senlength = sen
30、tencechar.length; int i = 0; /分析單詞 while (this.index <= this.senlength) /state0: this.token = "" this.char = getbc(sentencechar); if (this.char = '') break; /''表示這一行結(jié)束 /進(jìn)入狀態(tài)判斷 switch (this.char) /judge lettercase 'a':case 'b':case 'c':case 'd&
31、#39;:case 'e':case 'f':case 'g':case 'h':case 'i':case 'j':case 'k':case 'l':case 'm':case 'n':case 'o':case 'p':case 'q':case 'r':case 's':case 't':case 'u':case &
32、#39;v':case 'w':case 'x':case 'y':case 'z':case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':case 'g':case 'h':case 'i':case 'j':case 'k':case 'l':case 'm':
33、case 'n':case 'o':case 'p':case 'q':case 'r':case 's':case 't':case 'u':case 'v':case 'w':case 'x':case 'y':case 'z': /do this.token = this.contact(token, char); /state1 char = this.getchar(senten
34、cechar); while (this.isletter(char) | this.isdigit(char) this.token = this.contact(this.token, char); char = this.getchar(sentencechar); this.retract(); /state2 if (aa.keywordtable.iskeyword(token) this.finalaccidence = this.finalaccidence + "保留字 " + this.returnaword(token) + "n"
35、 else this.finalaccidence = this.finalaccidence + "標(biāo)識(shí)符 " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(identity) + "n" /clear up token this.token = "" break; /judge ditital case '0':case '1':case '2':case
36、'3':case '4':case '5':case '6':case '7':case '8': case '9': /do this.token = this.contact(token, char); /state3 char = this.getchar(sentencechar); while (this.isdigit(char) this.token = this.contact(token, char); char = this.getchar(sentencecha
37、r); this.retract(); /state4 this.finalaccidence = this.finalaccidence + "數(shù)字 " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(digit) + "n" /clear up token this.token = "" break; case '=': /state5 this.token = this.contact(t
38、oken, char); this.finalaccidence = this.finalaccidence + "等號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case '+': /state6 this.token = this.contact(to
39、ken, char);this.finalaccidence = this.finalaccidence + "加號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case '*': /state7 this.token = this.contact(toke
40、n, char); char = this.getchar(sentencechar); /state8 if (char = '*') this.token = this.contact(token, char); this.finalaccidence = this.finalaccidence + "乘方 " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n"
41、 /state9 else this.finalaccidence = this.finalaccidence + "乘號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case ',': /state10 this.token = this.contact(
42、token, char); this.finalaccidence = this.finalaccidence + "逗號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case '(': /state11 this.token = this.contact(
43、token, char); this.finalaccidence = this.finalaccidence + "左括號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case ')': /state12 this.token = this.contact
44、(token, char); this.finalaccidence = this.finalaccidence + "右括號(hào) " +this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case '': /state13 this.token = this.contact(
45、token, char); this.finalaccidence = this.finalaccidence + "左大括號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case '': /state14 this.token = this.contact
46、(token, char); this.finalaccidence = this.finalaccidence + "右大括號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case '': /state15 this.token = this.contac
47、t(token, char); this.finalaccidence = this.finalaccidence + "左中括號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up token this.token = "" break; case '': /state16 this.token = this.contact(token, char); this.finalaccidence = this.finalaccidence + "右中括號(hào) " + this.returnaword(token) + "種別碼 " + string.valueof(aa.classidentity.findkey(string.valueof(char) + "n" /clear up t
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 創(chuàng)新金融工具在糧食安全保障中的應(yīng)用前景
- 產(chǎn)學(xué)研合作模式促進(jìn)藝術(shù)學(xué)理論人才創(chuàng)新能力培養(yǎng)
- 2025至2030中國(guó)騎行服市場(chǎng)營(yíng)銷渠道與供求平衡預(yù)測(cè)分析報(bào)告
- 遼寧省遼陽(yáng)市二中學(xué)教育協(xié)作2025屆九上化學(xué)期末質(zhì)量跟蹤監(jiān)視模擬試題含解析
- 湖南省株洲市荷塘區(qū)2024年七上數(shù)學(xué)期末復(fù)習(xí)檢測(cè)模擬試題含解析
- 內(nèi)蒙古自治區(qū)通遼市奈曼旗2024年化學(xué)九年級(jí)第一學(xué)期期末聯(lián)考試題含解析
- 2025至2030中國(guó)景觀設(shè)計(jì)行業(yè)市場(chǎng)深度調(diào)研及競(jìng)爭(zhēng)格局與投資發(fā)展?jié)摿?bào)告
- 吉林省長(zhǎng)春寬城區(qū)四校聯(lián)考2024-2025學(xué)年八年級(jí)物理第一學(xué)期期末預(yù)測(cè)試題含解析
- 餐飲商鋪?zhàn)赓U及品牌孵化合同
- 醫(yī)療設(shè)備質(zhì)量管理實(shí)踐案例分析
- 中風(fēng)腦梗死恢復(fù)期中醫(yī)護(hù)理方案課件
- 新生兒重癥監(jiān)護(hù)室母乳使用專家共識(shí)(2024版)解讀
- 病毒性腦炎診療指南(兒科)
- 樂(lè)器設(shè)備供貨項(xiàng)目實(shí)施方案及售后服務(wù)方案
- 中共黨史知識(shí)競(jìng)賽試題及答案
- 2020年杭州學(xué)軍中學(xué)高一入學(xué)分班考試英語(yǔ)試卷及答案
- (高清版)AQ 1044-2007 礦井密閉防滅火技術(shù)規(guī)范
- 死亡醫(yī)學(xué)證明書(shū)填寫(xiě)培訓(xùn)
- 做自己的心理壓力調(diào)節(jié)師智慧樹(shù)知到期末考試答案章節(jié)答案2024年嘉興大學(xué)
- 學(xué)術(shù)期刊推廣方案
- 安檢設(shè)備采購(gòu)安裝調(diào)試方案
評(píng)論
0/150
提交評(píng)論