編譯原理詞法分析器java_第1頁
編譯原理詞法分析器java_第2頁
編譯原理詞法分析器java_第3頁
編譯原理詞法分析器java_第4頁
編譯原理詞法分析器java_第5頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、編譯原理實驗報告 計算機與信息學院實驗一 詞法分析器的設計一、實驗目的1. 理解詞法分析器的任務和輸出形式2. 理解掃描器的工作原理3. 掌握狀態轉換圖的繪制以及單詞的識別技術4. 掌握詞法分析器的設計過程,能夠使用某種高級語言實現一個詞法分析器二、實驗環境Myeclipse三、實驗要求給出一個簡單的詞法語言規則描述,其中:1開頭的種別碼為關鍵詞,2開頭的為算符,3開頭的為界符,4開頭的為標識符,5開頭的為常數,標識符為字母開頭,以字母和數字組成的任意符號串,常數為整數,即以數字組成的符號串。四、實驗難點 1. 對整數的二進制轉換,以及對指針的操作2. 標識符的設置五、實驗代碼1. ciFa.

2、Javapackage com.yaoer.test1;import javax.swing.*;import javax.swing.border.TitledBorder;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;SuppressWarnings("serial")public class ciFa extends JFrameprivate JButton jbtShow = new JButton("進行詞法分析"

3、;);/按鈕private JTextArea jta = new JTextArea();/輸入文本框private JTextArea jtaOut = new JTextArea();/輸出文本框private JPanel jpl=new JPanel();private String intput =""private String output =""private compiler comp = new compiler();/* * param args */public static void main(String args)ciFa

4、 frame = new ciFa();frame.setTitle("詞法分析器");/frame.setLocationRelativeTo(frame);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(500, 400);frame.setVisible(true);public ciFa()jta.setWrapStyleWord(true);jta.setLineWrap(true);jtaOut.setWrapStyleWord(true);jtaOut.setLineWrap

5、(true);jtaOut.setEditable(false);JScrollPane scrollPane = new JScrollPane(jta);JScrollPane scrollPane2 = new JScrollPane(jtaOut);scrollPane.setPreferredSize(new Dimension(300,300);scrollPane2.setPreferredSize(new Dimension(300,300);jtaOut.setBorder(new TitledBorder("詞法分析結果");jta.setBorder(

6、new TitledBorder("請在這輸入");jpl.setLayout(new GridLayout(2,2);jpl.add(jta);jpl.add(jtaOut);add(jbtShow,BorderLayout.SOUTH);add(jpl);jbtShow.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)intput = jta.getText();output=puterComp(intput);jtaOut.append(output););

7、2. compiler.Javapackage com.yaoer.test1;public class compiler public String computerComp(String str)String output=""int index=0;int k = 0;while(index<str.length()if(isJieFu(str.charAt(index)!=-1) /判斷界符output+=("( 30"+isJieFu(str.charAt(index)+" "+str.charAt(index)+&q

8、uot; 界符)"); index+;else if(isMath(str.charAt(index) /判斷常數int index1=index;output+=("( ");String sub;String result;while(isMath(str.charAt(index)output+=(str.charAt(index);index+;if(index>=str.length()if(str.charAt(index1)='0') if(index1+1>=str.length()sub =str.substring(

9、index1, index); result = Zhuan(sub);output+=(" "+result+" 數字)");elseif(isMath(str.charAt(index1+1) output+=(" 非法字符)"); else sub =str.substring(index1, index); result = Zhuan(sub);output+=(" "+result+" 數字)");else sub =str.substring(index1, index); res

10、ult = Zhuan(sub);output+=(" "+result+" 數字)"); return output;if(isLetter(str.charAt(index)if(str.charAt(index)=' '|str.charAt(index)='n')/空格或者回車處理 index+;while(!isMath(str.charAt(index)|(isLetter(str.charAt(index)output+=(str.charAt(index);index+; if(index>=str.

11、length() output+=(" 非標識符)");return output; output+=(" 非法字符)");else if(str.charAt(index1)='0') if(isMath(str.charAt(index1+1) output+=(" 非法字符)"); else sub =str.substring(index1, index); result = Zhuan(sub);output+=(" "+result+" 數字)");else sub

12、=str.substring(index1, index); result = Zhuan(sub);output+=(" "+result+" 數字)"); else if(isLetter(str.charAt(index) /標識符判斷int i=index; String sub;while(isLetter(str.charAt(index)|isMath(str.charAt(index)index+;if(index>=str.length()/System.out.println(index);sub =str.substring(

13、i, index); if(isKeyword(sub)!=0) if(isKeyword(sub)>=10)output+=("( "+" 1"+isKeyword(sub)+" "+sub+" 關鍵字)");else output+=("( "+" 10"+isKeyword(sub)+" "+sub+" 關鍵字)");elseoutput+=("( "+sub+" 標識符)"); re

14、turn output; sub = str.substring(i, index); /判斷是不是關鍵字 if(isKeyword(sub)!=0) if(isKeyword(sub)>=10)output+=("( "+" 1"+isKeyword(sub)+" "+sub+" 關鍵字)");elseoutput+=("( "+" 10"+isKeyword(sub)+" "+sub+" 關鍵字)");elseoutput+=

15、("("+sub+" 標識符)"); else if(isCompu(str.charAt(index)!=0)if(index+1>=str.length()if(str.charAt(index)='&'|str.charAt(index)='|')output+=("("+str.charAt(index)+" 非法字符)"); index+;elseif(isCompu(str.charAt(index)<=8)output+=("("

16、+" 20"+isCompu(str.charAt(index)+" "+str.charAt(index)+" 運算符)"); else if(str.charAt(index)='!')output+=("( 218 "+str.charAt(index)+" 運算符)"); index+;elseif(isCompu(str.charAt(index+1)=0)if(str.charAt(index)='&'|str.charAt(index)=&#

17、39;|')output+=("("+str.charAt(index)+" 非法字符)"); index+;elseif(isCompu(str.charAt(index)<=8)output+=("("+" 20"+isCompu(str.charAt(index)+" "+str.charAt(index)+" 運算符)"); else if(str.charAt(index)='!')output+=("("+&quo

18、t; 218"+str.charAt(index)+" 運算符)"); index+;elseif(index+2>=str.length()if( isCompu(str.charAt(index+1)!=0)if(str.charAt(index)='=')if(str.charAt(index+1)='=')output+=("("+" 210 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;in

19、dex+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='<' )switch (str.charAt(index+1) case '=':output+=("("+" 209 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;ind

20、ex+;break;case '<':output+=("( 215 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;break;case '>':output+=("( 211 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;break;default:output+=("("+str.cha

21、rAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;break;else if(str.charAt(index)='>' )if(str.charAt(index+1)='=')output+=("( 207 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;else if(str.charAt(index+1)='>')output+=(

22、"( 214 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='&')if(str.charAt(index+1)='&')output+=("( 216 "+s

23、tr.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='|')if(str.charAt(index+1)='|')output+=("( 217 "+str.charAt(index)+str.charAt(

24、index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='+')if(str.charAt(index+1)='+')output+=("( 212 "+str.charAt(index)+str.charAt(index+1)+" 運算符)");

25、 index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='-')if(str.charAt(index+1)='-')output+=("( 213 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=(

26、"("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)"); index+;index+;return output;elseif( isCompu(str.charAt(index+1)!=0)if(str.charAt(index)='=')if(str.charAt(index+1)='

27、=')output+=("("+" 210 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='<' )switch (str.charAt(index+1) case '=

28、':output+=("("+" 209 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;break;case '<':output+=("("+" 215 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;break;case '>':output+=("(

29、 211 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;break;default:output+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;break;else if(str.charAt(index)='>' )if(str.charAt(index+1)='=')output+=("( 207 &qu

30、ot;+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;else if(str.charAt(index+1)='>')output+=("( 214 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)&quo

31、t;);index+;index+;else if(str.charAt(index)='&')if(str.charAt(index+1)='&')output+=("( 216 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;el

32、se if(str.charAt(index)='|')if(str.charAt(index+1)='|')output+=("( 217 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='

33、;+')if(str.charAt(index+1)='+')output+=("( 212"+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='-')if(str.charAt(index+1

34、)='-')output+=("( 213 "+str.charAt(index)+str.charAt(index+1)+" 運算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)"); i

35、ndex+;index+;else if(str.charAt(index)=' '|str.charAt(index)='n')/空格或者回車處理 index+;else /其他字符output+=("("+str.charAt(index)+" 非法字符)");index+; k+;if(k%10=0)output+=("n");return output;/* * 1.判斷是不是關鍵字 * param str * return */public int isKeyword(String str)i

36、nt result = 0;String arr="void","main","int","char","if","else","for","while","return","cout","cin" ;for(int i=0;i<arr.length;i+)if(str.equals(arri)result = i+1;break;else result = 0;retu

37、rn result;/* * 2.判斷是不是運算符 * param charr * return */public int isCompu(char charr) char arr='+','-','*','/','=','>','&','<','!','|' for(int i=0;i<arr.length;i+) if (charr=arri)return i+1;return 0;/* * 3.判斷是不是界符 * param charr * return */public int isJieFu(char charr)char arr='(',')','','','','',',','&

溫馨提示

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

評論

0/150

提交評論