




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、計算機(jī)專業(yè)軟件類課程實驗報告課程名稱:編譯原理實驗題目:詞法分析器設(shè)計實驗小組成員:實驗小組組長:任課教師:專業(yè)名稱:計算機(jī)科學(xué)與技術(shù)班級名稱:計科1班實驗起止時間:2014-5-82014-5-15一、實驗?zāi)康?、要求設(shè)計交互界面,能輸入能轉(zhuǎn)換能輸出,形式和風(fēng)格自定。2、設(shè)計一個詞法分析程序,理解詞法分析器實現(xiàn)的原理,掌握程序設(shè)計語言中的各類單詞的詞法分析方法,加深對詞法分析原理的理解。2、 實驗內(nèi)容1、 對給定的程序通過詞法分析器能夠識別一個個單詞符號,并以三元式(單詞行號,單詞符號的屬性值,單詞符號類別)顯示。如果有錯誤,統(tǒng)計總共有多少個錯誤,并且將每個錯誤所在行號,以及屬于哪種錯誤顯示
2、出來。2.本程序自行規(guī)定:(1)關(guān)鍵字program, example, const, var, interger,begin, void, String, System, out, println, end, if,else, for ,char,to,do,then(2) 運(yùn)算符=, =, +, :, |, *, +, %,-,/(3) 界符;, , , (, ), ,(4) 標(biāo)識符 以字母或下劃線開頭的字符串表示(5) 常量 用數(shù)字表示(6)空格、回車、換行符跳過3、 在屏幕上顯示如下:(1)Token表中顯示:1program保留字1example保留字1;界符2const保留字2k標(biāo)
3、識符2=運(yùn)算符2200常量(2)錯誤信息顯示:2errors42a格式不正確4034不能以0開頭三、實驗需求1、界面部分:需要對文件有打開、保存、另存為的功能;對文本有撤銷、剪切、復(fù)制、粘貼、復(fù)制、刪除、全選的功能2、 詞法分析器:對讀入的程序一個字符一個字符的進(jìn)行詞法分析,識別出一個個單詞符號,并以三元式(單詞行號,單詞符號的屬性值,單詞符號類別)顯示。如果有錯誤,統(tǒng)計總共有多少個錯誤,并且將每個錯誤所在行號,以及屬于哪種錯誤顯示出來。4、 主要數(shù)據(jù)結(jié)構(gòu)介紹使用三個數(shù)組分別來存放關(guān)鍵字、運(yùn)算符、界符,凡是不屬于這三個數(shù)組的所有合法字符串當(dāng)做標(biāo)識符處理,凡是不屬于這三個數(shù)組的所有合法數(shù)字當(dāng)做常
4、量處理,其余情況則進(jìn)行出錯處理5、 主要模塊算法介紹1、 對輸入的程序進(jìn)行處理:空格、回車、換行符跳過,并按照;、=、,、:等符號將字符串切割開來,同時對字符串使用toCharArray()轉(zhuǎn)換成單個字符,求出切割符號在字符串中的位置,并將它們與切割好的字符串重新組合,使它們與輸入程序相同2、 對第一步得到的字符串?dāng)?shù)組進(jìn)行一個一個的檢查,如果字符串在table1中,則表明是運(yùn)算符,將狀態(tài)標(biāo)記為1;如果字符串在table2中,則表明是保留字,將狀態(tài)標(biāo)記為2;如果字符串在table3中,則表明是界符,將狀態(tài)標(biāo)記為33、 如果對當(dāng)前的字符串不在這三個數(shù)組中,且數(shù)字序列又是合法的,則表明是常量,將狀態(tài)
5、標(biāo)記為4;如果對當(dāng)前的字符串不在這三個數(shù)組中,且字符序列又是合法的,則表明是標(biāo)識符,將狀態(tài)標(biāo)記為54、 如果對當(dāng)前的輸入不滿足前5個狀態(tài),且字符序列不是以下劃線、字母開頭,則表明輸入的格式不正確,將狀態(tài)標(biāo)記為6;如果對當(dāng)前的輸入不滿足前5個狀態(tài),且字符序列是以0開頭的數(shù)字序列,則表明輸入的常量是以0開頭的,這是非法格式,將狀態(tài)標(biāo)記為75、將最終的結(jié)果打印輸出到文本框中6、 程序?qū)崿F(xiàn)環(huán)境及使用說明本次實驗采用Eclipse進(jìn)行代碼的編寫、編譯及運(yùn)行;編寫語言為java語言;程序的運(yùn)行環(huán)境為jdk1.8;系統(tǒng)為windows8.17、 實驗測試用例設(shè)計說明1、正常的測試,這種情況下不會報錯prog
6、ram example;const k=2;vara:interger;beginend2、錯誤的測試,這個例子中含有非法標(biāo)識符和非法常量的錯誤program example;const k=2a,c=300,d=034;vara:interger;beginend八、實驗結(jié)果測試情況9、 小組對實驗結(jié)果的自我評價通過這次實驗,我對詞法分析器有了一定的了解,進(jìn)一步的鞏固了這部分的知識。懂得了詞法分析器的工作原理。在編程過程中,遇到了不少的問題,在同學(xué)的幫助下,問題一步一步的得到了解決。先從實現(xiàn)最簡單的掃描和輸出,再實現(xiàn)掃描的范圍擴(kuò)大和輸出的結(jié)果更加具體,雖然詞法分析器的功能實現(xiàn)了,但是對于小數(shù)
7、不能進(jìn)行較好的分析。在算法上雖然是弄懂了詞法分析器,但具體實現(xiàn)起來還有一些不足。由于編程能力和時間的不足,這個分析器還有待完善,沒有把雙目運(yùn)算符考慮進(jìn)來。對于出錯處理也考慮得不夠周到。望老師多多指教。十、任課教師對實驗結(jié)果的評分源碼FrameView.javapackage view;import java.awt.BorderLayout;import java.awt.Event;import java.awt.FileDialog;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;impo
8、rt java.awt.ScrollPane;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import ja
9、va.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import javax
10、.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JTextArea;import javax.swing.KeyStroke;import javax.swing.event.DocumentEvent;import javax.swing.e
11、vent.DocumentListener;import javax.swing.event.UndoableEditEvent;import javax.swing.event.UndoableEditListener;import javax.swing.undo.UndoManager;import operation.Analysis;public class FrameView extends JFrame implements ActionListener,DocumentListener, UndoableEditListener JFrame f = new JFrame(詞法
12、分析器);ScrollPane jspa = new ScrollPane();ScrollPane jspb = new ScrollPane();ScrollPane jspc = new ScrollPane();JPopupMenu pop = new JPopupMenu();JTextArea ta = new JTextArea();JTextArea tb = new JTextArea();JTextArea tc = new JTextArea();JPanel pa = new JPanel();JPanel pb = new JPanel();JOptionPane j
13、op = new JOptionPane();UndoManager um = new UndoManager();JMenuBar bar = new JMenuBar();JMenu file = new JMenu(文件);JMenu edit = new JMenu(編輯);JMenu analysis = new JMenu(詞法分析);JMenu help = new JMenu(幫助);JMenuItem open = new JMenuItem(打開);JMenuItem save = new JMenuItem(保存);JMenuItem saveNew = new JMen
14、uItem(另存為);JMenuItem exit = new JMenuItem(退出);JMenuItem back = new JMenuItem(撤銷);JMenuItem cut = new JMenuItem(剪切);JMenuItem copy = new JMenuItem(復(fù)制);JMenuItem paste = new JMenuItem(粘貼);JMenuItem del = new JMenuItem(刪除);JMenuItem all = new JMenuItem(全選);JMenuItem lexical = new JMenuItem(詞法分析器);JMenu
15、Item sHelp = new JMenuItem(查看幫助);JMenuItem about = new JMenuItem(關(guān)于);JMenuItem back1 = new JMenuItem(撤銷);JMenuItem cut1 = new JMenuItem(剪切);JMenuItem copy1 = new JMenuItem(復(fù)制);JMenuItem paste1 = new JMenuItem(粘貼);JMenuItem del1 = new JMenuItem(刪除);JMenuItem all1 = new JMenuItem(全選);FileDialog openDi
16、a = new FileDialog(f, 打開, FileDialog.LOAD);FileDialog saveDia = new FileDialog(f, 保存, FileDialog.SAVE);AboutFile prompta;File fe;Clipboard clipboard = null;boolean isSave, change, isLine;String select;public FrameView() isSave = false;change = false;isLine = true;setup();f.addWindowListener(new Wind
17、owAdapter() public void windowClosing(WindowEvent we) if (isSave = false) & (change = true) Prompt dg = new Prompt(f, 詞法分析器, true); elseSystem.exit(0););ta.setFont(new Font(宋體, 0, 20);ta.setLineWrap(true);tb.setEditable(false);tc.setEditable(false);pack();f.setBounds(300, 100, 750, 550);f.setVisible
18、(true);public void setup() setLayout(new BorderLayout();f.setJMenuBar(bar);jspa.add(ta);jspb.add(tb);jspc.add(tc);pb.setLayout(new GridLayout(2, 1);pb.add(jspb);pb.add(jspc);pa.setLayout(new GridLayout(1, 2);pa.add(jspa);pa.add(pb);f.add(pa);bar.add(file);bar.add(edit);bar.add(analysis);bar.add(help
19、);file.add(open);file.add(save);file.add(saveNew);file.addSeparator();file.add(exit);edit.add(back);edit.addSeparator();edit.add(cut);edit.add(copy);edit.add(paste);edit.add(del);edit.addSeparator();edit.addSeparator();edit.add(all);edit.addSeparator();analysis.add(lexical);help.add(sHelp);help.add(
20、about);pop.add(back1);pop.addSeparator();pop.add(cut1);pop.add(copy1);pop.add(paste1);pop.add(del1);pop.addSeparator();pop.add(all1);open.setMnemonic(O);open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK);save.setMnemonic(S);save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.V
21、K_S,Event.CTRL_MASK);saveNew.setMnemonic(A);saveNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.SHIFT_MASK);exit.setMnemonic(X);exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.SHIFT_MASK);back.setMnemonic(Z);back.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.
22、CTRL_MASK);cut.setMnemonic(X);cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK);copy.setMnemonic(C);copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);paste.setMnemonic(V);paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);del.set
23、Mnemonic(L);del.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);all.setMnemonic(A);all.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK);lexical.setMnemonic(L);lexical.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,Event.CTRL_MASK);sHelp.setMnemonic(H);sHelp.setA
24、ccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,Event.SHIFT_MASK);about.setMnemonic(B);about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,Event.SHIFT_MASK);back1.setMnemonic(Z);back1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK);cut1.setMnemonic(X);cut1.setAccelerator(K
25、eyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK);copy1.setMnemonic(C);copy1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);paste1.setMnemonic(V);paste1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);del1.setMnemonic(L);del1.setAccelerator(KeyStroke.get
26、KeyStroke(KeyEvent.VK_DELETE, 0);all1.setMnemonic(A);all1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK);open.addActionListener(this);save.addActionListener(this);saveNew.addActionListener(this);exit.addActionListener(this);back.addActionListener(this);cut.addActionListener(thi
27、s);copy.addActionListener(this);paste.addActionListener(this);del.addActionListener(this);all.addActionListener(this);lexical.addActionListener(this);about.addActionListener(this);sHelp.addActionListener(this);back1.addActionListener(this);cut1.addActionListener(this);copy1.addActionListener(this);p
28、aste1.addActionListener(this);del1.addActionListener(this);all1.addActionListener(this);ta.getDocument().addDocumentListener(this);ta.getDocument().addUndoableEditListener(this);ta.addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent me) if (me.getButton() = me.BUTTON3) pop.show(t
29、a, me.getX(), me.getY(););public void openFile() openDia.setVisible(true);String dirPath = openDia.getDirectory();String fileName = openDia.getFile();if (dirPath = null | fileName = null)return;ta.setText();fe = new File(dirPath, fileName);try BufferedReader in = new BufferedReader(new FileReader(fe
30、);String line = null;while (line = in.readLine() != null)ta.append(line + rn);in.close();ta.setCaretPosition(0); catch (IOException ioe) System.err.println(ioe);public void saveFile() if (fe = null) saveDia.setVisible(true);String dirPath = openDia.getDirectory();String fileName = openDia.getFile();
31、if (dirPath = null | fileName = null)return;ta.setText();File fe = new File(dirPath, fileName);isSave = true;change = false;try BufferedWriter out = new BufferedWriter(new FileWriter(fe);String text = ta.getText();out.write(text);out.flush();out.close(); catch (IOException ioe) System.err.println(io
32、e);public void undoableEditHappened(UndoableEditEvent ue) um.addEdit(ue.getEdit();public void changedUpdate(DocumentEvent e) change = true;public void insertUpdate(DocumentEvent e) change = true;public void removeUpdate(DocumentEvent e) change = true;public void actionPerformed(ActionEvent ae) if (t
33、a.getText().equals()change = false;if (ae.getSource() = open) openFile();change = false;if (ae.getSource() = save) if (isSave = false)saveFile();if (ae.getSource() = saveNew) saveFile();if (ae.getSource() = exit) if (isSave = false) & (change = true) Prompt dg = new Prompt(f, 詞法分析器, true); elseSyste
34、m.exit(0);if (ae.getSource() = back | ae.getSource() = back1) if (um.canUndo() um.undo();if (ae.getSource() = cut | ae.getSource() = cut1) try clipboard = getToolkit().getSystemClipboard();select = ta.getSelectedText();if (select.length() != 0) StringSelection text = new StringSelection(select);clip
35、board.setContents(text, null);ta.replaceRange(, ta.getSelectionStart(),ta.getSelectionEnd(); catch (Exception e) System.err.println(e);if (ae.getSource() = copy | ae.getSource() = copy1) try clipboard = getToolkit().getSystemClipboard();select = ta.getSelectedText();if (select.length() != 0) StringS
36、election text = new StringSelection(select);clipboard.setContents(text, null); catch (Exception e) System.err.println(e);if (ae.getSource() = paste | ae.getSource() = paste1) Transferable contents = clipboard.getContents(this);DataFlavor flavor = DataFlavor.stringFlavor;if (contents.isDataFlavorSupp
37、orted(flavor)try String str;str = (String) contents.getTransferData(flavor);ta.insert(str, ta.getCaretPosition(); catch (Exception e) System.err.println(e);if (ae.getSource() = del | ae.getSource() = del1) ta.replaceSelection(null);if (ae.getSource() = all | ae.getSource() = all1) ta.selectAll();if
38、(ae.getSource() = lexical) String tastring=ta.getText().toString();Analysis as=new Analysis(tastring);as.match();String tbstring=t-Token表-n+as.list;/tb.setText(-Token表-n);tb.setText(tbstring);String tcstring=t+as.count+errorsn+as.error;/tc.setText(as.count+errorsn);tc.setText(tcstring);/System.out.p
39、rintln(as.error);if (ae.getSource() = sHelp) jop.setBounds(300, 300, 250, 150);jop.showMessageDialog(null, 無法啟動“Windows幫助和支持”, 查看,jop.ERROR_MESSAGE);if (ae.getSource() = about) AboutFile af = new AboutFile(f, 詞法分析器, true);public static void main(String args) FrameView fv = new FrameView();Prompt.jav
40、apackage view;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing
41、.JDialog;import javax.swing.JLabel;import javax.swing.JPanel;public class Prompt extends JDialog implements ActionListenerJButton ok = new JButton(保存);JButton no = new JButton(不保存);JButton cancel = new JButton(取消);public Prompt(Frame parent,String title,Boolean d) super(parent,title,true);addWindowL
42、istener(new WindowAdapter() public void windowClosing(WindowEvent we) dispose(););JLabel lb = new JLabel(是否需要將更改保存?, JLabel.CENTER);setLayout(new BorderLayout();setBackground(Color.WHITE);JPanel pl = new JPanel();add(South, pl);add(Center, lb);pl.setLayout(new FlowLayout();pl.setBackground(Color.LIG
43、HT_GRAY);pl.add(ok);pl.add(no);pl.add(cancel);ok.setBackground(Color.WHITE);no.setBackground(Color.WHITE);cancel.setBackground(Color.WHITE);setBounds(450, 300, 250, 150);ok.addActionListener(this);no.addActionListener(this);cancel.addActionListener(this);setVisible(true);public void actionPerformed(
44、ActionEvent e) if(e.getSource()=ok)FrameView my = new FrameView();my.saveFile();if(e.getSource()=no)System.exit(0);if(e.getSource()=cancel)this.dispose();AboutFile.javapackage view;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Frame;import java.awt.GridLayout;import java.awt.eve
45、nt.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;public class AboutFile extends JDialog impl
46、ements ActionListenerpublic AboutFile(Frame parent,String title,Boolean k) super(parent,title,true);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent we) dispose(););JPanel pla = new JPanel();JPanel plb = new JPanel();JLabel lba = new JLabel(感謝老師和各位隊友的幫助, JLabel.CENTER);JLa
47、bel lbb = new JLabel(版權(quán)人:崔立志, JLabel.CENTER);JLabel lbc = new JLabel(出版日期:2014年, JLabel.CENTER);lba.setOpaque(true);setLayout(new BorderLayout();setBackground(Color.WHITE);plb.setLayout(new GridLayout(3, 1);plb.add(lba);plb.add(lbb);plb.add(lbc);add(South, pla);add(Center, plb);JButton confirm = new JButton(確定);pla.add(confirm);JTextArea t=new JTextArea();t.setEditable(false);setBounds(490, 300, 250, 150);confirm.addActionListener(this);setVisible(true);public void actionPerformed(Action
溫馨提示
- 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è)單位招考易考易錯模擬試題(共500題)試卷后附參考答案
- 2025年上半年安徽池州市建筑活動綜合技術(shù)服務(wù)中心招聘2人易考易錯模擬試題(共500題)試卷后附參考答案
- 2025年上半年安徽合肥廬陽區(qū)事業(yè)單位考試項目易考易錯模擬試題(共500題)試卷后附參考答案
- 2025年上半年寧波市寧海縣事業(yè)單位招考及易考易錯模擬試題(共500題)試卷后附參考答案
- 2024西安澤達(dá)航空制造有限責(zé)任公司招聘(23人)筆試參考題庫附帶答案詳解
- 2025年手持式應(yīng)變儀項目可行性研究報告
- 2025年彎形拱坑圓壓圓壓痕線項目可行性研究報告
- 2025年大鑼項目可行性研究報告
- 北京市第四中學(xué)高中地理人口數(shù)量的變動學(xué)案含解析新人教版
- 江蘇專用2025版高考物理一輪復(fù)習(xí)第2章相互作用第3節(jié)共點力的平衡教案
- 2023-2028年中國油畫行業(yè)市場發(fā)展現(xiàn)狀及投資規(guī)劃建議報告
- 2024年干式電力電容器項目可行性研究報告
- 河南12系列建筑設(shè)計圖集一(12YJ1)
- 2025年村三會一課工作計劃表
- DB31-T 1296-2021 電動汽車智能充電樁智能充電及互動響應(yīng)技術(shù)要求
- 網(wǎng)絡(luò)游戲游戲運(yùn)營及營銷策略規(guī)劃方案
- 建筑垃圾粉碎合同范例
- ANCA相關(guān)性血管炎-3
- 2023年廣西公務(wù)員考試申論試題(C卷)
- 流體壓強(qiáng)與流速的關(guān)系市公開課一等獎?wù)f課公開課獲獎?wù)n件百校聯(lián)賽一等獎?wù)n件
- 第25課+中華人民共和國成立和向社會主義的過渡+課時作業(yè) 高一上學(xué)期統(tǒng)編版(2019)必修中外歷史綱要上
評論
0/150
提交評論