[计算机软件及应用]编译原理计算器报告.doc

上传人:sccc 文档编号:4561932 上传时间:2023-04-27 格式:DOC 页数:37 大小:180.67KB
返回 下载 相关 举报
[计算机软件及应用]编译原理计算器报告.doc_第1页
第1页 / 共37页
[计算机软件及应用]编译原理计算器报告.doc_第2页
第2页 / 共37页
[计算机软件及应用]编译原理计算器报告.doc_第3页
第3页 / 共37页
[计算机软件及应用]编译原理计算器报告.doc_第4页
第4页 / 共37页
[计算机软件及应用]编译原理计算器报告.doc_第5页
第5页 / 共37页
点击查看更多>>
资源描述

《[计算机软件及应用]编译原理计算器报告.doc》由会员分享,可在线阅读,更多相关《[计算机软件及应用]编译原理计算器报告.doc(37页珍藏版)》请在三一办公上搜索。

1、编译原理课程设计2一、 题目:计算器设计(运用词法分析&语法分析方法)二、 运行环境:JAVA jdk1.6.0_21 三、 设计思路:1、为每种可能识别出来的合法单词创建一个类,类的属性包括该单词的各别码及值。2、构建异常类,以实现计算器的容错性能。3、构建一个词法分析类,用之提取单词。4、构建表达式中各单词间的优先关系表。5、构建一个语法分析类,对表达式进行规约。此类调用词法类获取单词,查询优先关系表进行规约操作。6、构建一个计算类,调用语法分析类以实现计算。7、构建一个GUI类,设计好操作界面,并内置计算类,以实现界面操作计算功能。系统总体调用关系视图如下:四、 基本工具UML及JAVA

2、语言五、 程序源代码:1、 实现GUI的mainWindow类代码package gui;import parser.Calculator;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.text.*;public class mainWindow /extends JFrame JFrame mainFrame; JTextField inputField; JTextField outputField; JButton button = new JButton30; String s=7,

3、8,9,4,5,6,1,2,3,0,.,=,backspace,c,+,e,+/-,-,sqrt,sin,cos,*,&,|,/,%,(,); int i; JPanel p1,p2,p3,p4,p5,p6,p7,p8; JLabel l1,l2; JMenuBar menubar; JMenuItem item1,item2,item3,item4,item5,item6,item7; public void display() mainFrame = new JFrame(计算器2010 Beta1); mainFrame.addWindowListener(new WindowAdapt

4、er() public void windowClosing(WindowEvent e) int i = JOptionPane.showConfirmDialog(null,是否确定退出系统,退出确认对话框,JOptionPane.YES_NO_CANCEL_OPTION);if (i = 0) System.exit(0); ); mainFrame.getContentPane().setLayout(new BorderLayout(); mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); JPanel io

5、Panel = new JPanel(); mainFrame.getContentPane().add(ioPanel); mainFrame.add(ioPanel); ioPanel.setLayout(new BorderLayout(); menubar = new JMenuBar(); mainFrame.setJMenuBar(menubar); JMenu menu1 = new JMenu(编辑); JMenu menu2 = new JMenu(操作); JMenu menu3 = new JMenu(帮助); menubar.add(menu1); menubar.ad

6、d(menu2); menubar.add(menu3); item1 = new JMenuItem(复制,C); item2 = new JMenuItem(粘贴,V); item3 = new JMenuItem(剪切,X); item4 = new JMenuItem(计算,G); item5 = new JMenuItem(清空所有,R); item6 = new JMenuItem(退出,Q); item7 = new JMenuItem(关于计算器,H); menu1.add(item1); menu1.add(item2); menu1.add(item3); menu2.ad

7、d(item4); menu2.add(item5); menu2.addSeparator(); menu2.add(item6); menu3.add(item7); /给菜单项注册监听器 item1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) outputField.setEnabled(true);outputField.selectAll();outputField.copy();outputField.setEnabled(false); ); item2.add

8、ActionListener(new ActionListener() public void actionPerformed(ActionEvent e) inputField.setText();inputField.paste(); ); item3.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) inputField.selectAll();inputField.cut(); ); item4.addActionListener(new ActionListener()

9、public void actionPerformed(ActionEvent e) button11.doClick(); ); item5.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) inputField.setText(); outputField.setText(); ); item6.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int i = JO

10、ptionPane.showConfirmDialog(null,是否确定退出系统,退出确认对话框,JOptionPane.YES_NO_CANCEL_OPTION);if (i = 0) System.exit(0); ); item7.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JOptionPane.showMessageDialog(mainFrame, getHelpMessage(); ); Font font = new Font(Arial,Font.BOLD

11、,24); l1 = new JLabel(算术表达式:); l2 = new JLabel(计算结果:); inputField = new JTextField(30); inputField.setFont(font); inputField.addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent ev) if (ev.getKeyChar() = KeyEvent.VK_ENTER) button11.doClick();); outputField = new JTextField(30); outputFiel

12、d.setFont(font); outputField.setEnabled(false); p3 = new JPanel(new FlowLayout(); p4 = new JPanel(new FlowLayout(); p1 = new JPanel(new GridLayout(2,2,10,10); p3.add(l1); p3.add(inputField); p4.add(l2); p4.add(outputField); p1.add(p3); p1.add(p4); ioPanel.add(p1,North); p2 = new JPanel(new GridLayou

13、t(1,2,20,10); p5 = new JPanel(new BorderLayout(); p6 = new JPanel(new GridLayout(4,3,10,10); p7 = new JPanel(new FlowLayout(); p8 = new JPanel(new GridLayout(4,4,10,10); p2.add(p5); p2.add(p8); p5.add(p6,Center); p5.add(p7,South); Font fontb = new Font(Arial,Font.BOLD,15); for(i=0;i12;i+) buttoni=ne

14、w JButton(si); buttoni.setFont(fontb); buttoni.addActionListener(new Act(); p6.add(buttoni); for(i=12;i14;i+) buttoni=new JButton(si); buttoni.setFont(fontb); buttoni.addActionListener(new Act(); p7.add(buttoni); for(i=14;i0) inputField.setText(inputField.getText().substring(0,inputField.getText().l

15、ength()-1); else if(e.getActionCommand()=+/-) getResult(); String ss = outputField.getText(); if (Character.isDigit(ss.charAt(0) outputField.setText(-+ss); else if (ss.charAt(0) = -) outputField.setText(ss.substring(1); else if(e.getActionCommand()=) getResult(); else inputField.setText(inputField.g

16、etText()+e.getActionCommand(); public void getResult() try Calculator calculator = new Calculator(); double result = calculator.calculate(inputField.getText().trim(); /String str = String.valueof(result); outputField.setText(+result); catch (Exception ex) outputField.setText(ex.getMessage(); String

17、getHelpMessage() String msg = ; msg += ttCopyright(C) 2009n; msg += tt20072341 & 20072343n; return msg; 2、 计算类Calculatorpackage parser;import exceptions.*;public class Calculator public double calculate(String expression) throws ExpressionException Parser parser = new Parser(expression+#); parser.gu

18、iyue(); return parser.result; 3、 Parser语法类package parser;/*parser类主要实现语法分析和语义分析。对经过词法分析的表达式进行计算,并抛出相应异常。*/import exceptions.*;import java.util.*;import parser.*;import java.math.*;import symbol.*;public class Parser public double result = 0; Opp_table table = new Opp_table(); ArrayList stack = new A

19、rrayList(); Scan scanner; int index = 0; public Parser(String temp) scanner = new Scan(temp); stack.add(new Pound(); /*push实现压栈操作*/ public void push(Token temp) if (temp.getType()!=15) index+; stack.add(temp); /*double_to_int将double型数值转为int型*/ public int double_to_int(double t) String s1 = String.va

20、lueOf(t); String s2 = s1.substring(0,s1.indexOf(.); int i = Integer.parseInt(s2);return i; /*一元操作 sqrt sin cos */ public void unary(Token temp) throws SqrtException, ExpressionException Token t; double tt; int tt2; t = stack.get(index); stack.remove(index); index-; tt = temp.get_value(); tt2 = doubl

21、e_to_int(tt); switch (t.getType() case 8: tt = (double)(tt2); break; case 10: if (tt0) throw new SqrtException(); tt = Math.sqrt(tt); break; case 11: tt = Math.sin(tt); break; case 12: tt = Math.cos(tt); break; default: throw new ExpressionException(); temp.set_value(tt); push(temp); /*二元操作 + - * /

22、& | % */ public void binary() throws DividedByZeroException, ExpressionException Token t1,t2,t3; double tt1,tt2,tt3; int ttt1,ttt2,ttt3; t3 = stack.get(index);/取第二个操作数 stack.remove(index); index-; tt3 = t3.get_value(); ttt3 = double_to_int(tt3); t2 = stack.get(index);/取操作符 stack.remove(index); index

23、-; t1 = stack.get(index);/取第一个操作数 stack.remove(index); index-; tt1 = t1.get_value(); ttt1 = double_to_int(tt1); switch (t2.getType() case 1: tt2 = tt1+tt3; break; case 2: tt2 = tt1-tt3; break; case 3: tt2 = tt1*tt3; break; case 4: if (tt3 = 0.0) throw new DividedByZeroException(); tt2 = tt1/tt3; bre

24、ak; case 5: tt2 = Math.pow(tt1,tt3); break; case 6: ttt2 = ttt1&ttt3; tt2 = (double)ttt2; break; case 7: ttt2 = ttt1|ttt3; tt2 = (double)ttt2; break; case 9: tt2 = tt1%tt3; break; default: throw new ExpressionException(); t1.set_value(tt2); push(t1); /*括号操作 () (i) (2+6) */ public void bracket() thro

25、ws SqrtException, DividedByZeroException, ExpressionException int w; Token t1,t2; t1 = stack.get(index); stack.remove(index); index-; if (t1.getType() = 13) return; t2 = stack.get(index); stack.remove(index); index-; if (t1.getType() = 0 & t2.getType() != 13) push(t2); push(t1);do binary(); t2 = sta

26、ck.get(index-1); if (t2.getType() = 13) break; while( true ); t1 = stack.get(index); stack.remove(index); index-; stack.remove(index); index-; push(t1); do t1 = stack.get(index); t2 = stack.get(index-1); w = t2.getType(); if (w = 8|w =10 | w=11 | w=12) stack.remove(index); index-; unary(t1); else br

27、eak; while (true); else if(t1.getType() = 0 & t2.getType() = 13) push(t1); do t1 = stack.get(index); t2 = stack.get(index-1); w = t2.getType(); if (w = 8|w =10 | w=11 | w=12) stack.remove(index); index-; unary(t1); else break; while (true); /*接受操作 + - * / & | % # 返回1表示规约结束 成功 返回2表示已在栈中的优先级高 并且已经规约 返

28、回3表示已在栈中的优先级低 */ public int accept(Token temp) throws DividedByZeroException, ExpressionException Token t1; int i,num1,num2; i = index - 1; t1 = stack.get(i); num1 = t1.getType(); num2 = temp.getType(); i = table.table2num1num2; if (i = 6) return 1; else if (i =-1) binary(); return 2; else if (i = 1

29、) return 3; else throw new ExpressionException(); /*guiyue函数实现规约操作,通过比较已读入的数据和要读入的数据的优先*关系进行对应于表的规约 */ public void guiyue() throws SqrtException,EmptyExpressionException,DividedByZeroException,ExpressionException int num1,num2,i,tt,flag1;Token t1,t2;flag1 = 0;while (true) t2 = scanner.getnextToken()

30、;/取下一单词 num2 = t2.getType(); t1 = stack.get(index); num1 = t1.getType(); i = table.tablenum1num2; switch(i) case 0: push(t2); break;case 1: unary(t2); /应取下一单词 break;case 3: bracket(); /应取下一单词 break;case 4: tt = 2; while (tt = 2) tt = accept(t2); if (tt = 1) flag1 = 1; /标志规约成功 else push(t2); break;de

31、fault: throw new ExpressionException(); /switch if (flag1 = 1) t1 = stack.get(index);result = t1.get_value();break; /while (true) /guiyue/类Parser4、 优先关系表类package parser;/*OPP算符优先关系表类*/public class Opp_table static int table = /* digit + - * / & | % sqrt sin cos ( ) # */ /*digit*/-1, 4, 4, 4, 4, 4, 4

32、, 4, -1, 4, -1, -1, -1, -1, 3, 4, /* + */ 0, -1, -1,-1,-1,-1,-1,-1,0,-1, 0, 0, 0, 0, -1,-1, /*-*/ 0, -1, -1,-1,-1,-1,-1,-1,0, -1, 0, 0, 0, 0, -1,-1, /* * */ 0, -1,-1,-1,-1,-1,-1,-1, 0, -1, 0, 0, 0, 0,-1,-1, /* / */0,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1,-1, /* */0,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1

33、,-1, /* & */0,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1,-1, /* | */0,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1,-1, /* */1,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1,-1, /* % */0,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1,-1, /* sqrt */1,-1,-1,-1, -1,-1,-1,-1,0,-1,0,0,0,0,-1,-1, /* sin */1,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,

34、0,-1,-1, /* cos */1,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1,-1, /* ( */0,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1,-1, /* ) */-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* # */0,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,0,0,-1,4 ;/*在这二维关系表中,行代表栈中数据,列代表要读入的数据。即判断已读的和要读入的算符关系。*0代表压栈操作。*1代表一元操作,包括: sqrt sin cos。*-1代表表达式有误。*3代表括号操作,包括函数的判断以及执行。*4代表接受状态*/ static int table2 = /* digit + - * / & | % sqrt sin cos ( ) # */ /*digit*/5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, /* + */ 5,-1,-1,1,1,1,-1,-1,0,1,0,0,0,0,-1,-1, /*-*/ 5,-1,-1,1,1,1,-1,-1,0,1,0,0,0,0,-1,-1, /* * */ 5,-1,-1,-1,-1,1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教育教学 > 成人教育


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号