《编译原理-太原理工大学(共12页).doc》由会员分享,可在线阅读,更多相关《编译原理-太原理工大学(共12页).doc(12页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上编译原理实验报告本科实验报告课程名称: 编译原理D 实验地点: 实验室 专业班级: 学 号: _学生姓名: java编译 指导教师: 成 绩: 2015年 月 日实验一、 无符号数的词法分析程序(4学时)内容:掌握词法分析的基本思想,并用高级语言编写无符号数的词法分析程序。要求:从键盘上输入一串字符(包括字母、数字等),最后以“;”结束,编写程序识别出其中的无符号数。无符号数文法规则可定义如下: .E比例因子E +- 0 1 2 3. 9 读无符号数的程序流程图见下图实验代码:package text_1;import java.util.*;public class
2、 Text1 public static void main(String args) int p = 0, w = 0, w1 = 0, j = 0, i = 0, d = 0, e = 1;/定义初值double w2 = 0;String str;System.out.println(请输入一串字符串(以;结束):);Scanner m = new Scanner(System.in);str = m.nextLine();char ch1 = str.toCharArray(); /字符串转化为字符数组/* * 检测字符数组 for(i=0;ich1.length;i+) System
3、.out.print(ch1i+ ); */while (i 9 | ch1i = 0 & ch1i = 0 & ch1i = 0 & ch1i 1) w2 = w / (Math.pow(10.0, j - 1);System.out.println(实型数为:+w2+*10+ +(e*(p-j+1);/科学计数法输出实型j = 0;w2 = 0;w = 0;p = 0; elseSystem.out.println(输入错误!);if (ch1i = 0 & ch1i = 0 & ch1i 1) w2 = w / (Math.pow(10.0, j - 1);System.out.prin
4、tln(实型数为:+w2+*10+ +(e*(p-j+1);j = 0;w2 = 0;w = 0;p = 0; elseSystem.out.println(输入错误!);else i+;if (ch1i = 0 & ch1i = 0 & ch1i = 0 & ch1i = 0 & ch1i 1) w2 = w / (Math.pow(10.0, j - 1);System.out.println(实型数为: + w2 + *10 + + (e * (p - j + 1);j = 0;w2 = 0;w = 0;p = 0; elseSystem.out.println(输入错误!);if (c
5、h1i = 0 & ch1i = 0 & ch1i 1) w2 = w / (Math.pow(10.0, j - 1);System.out.println(实型数为:+w2+*10 + +(e*(p-j+1);j = 0;w2 = 0;w = 0;p = 0; elseSystem.out.println(输入错误!);else if (ch1i != E) /输出小数System.out.println(小数为: + w + . + w1);w = 0;w1 = 0;j = 0;运行结果:实验二、 逆波兰式生成程序内容:掌握语法分析的基本思想,并用高级语言编写逆波兰式生成程序(4学时)要
6、求:利用逆波兰式生成算法编写程序,将从键盘上输入的算术表达式(中缀表达式)转化成逆波兰式。逆波兰表达式的生成过程涉及到运算符的优先级,下表中列出几个常用运算符的优先关系。右关系左常用运算符优先关系矩阵+-*/()+-*/()如上表所示的优先关系矩阵表示了+,-,*,/,(,)等七种运算符之间的相互优先关系。“、”三种符号分别代表“大于”、“小于”、“相等”三种优先关系。左边的“”与右边的“(”之间没有优先关系存在,所以表中为空白。逆波兰表达式生成算法的关键在于比较当前运算符与栈顶运算符的优先关系,若当前运算符的优先级高于栈顶运算符,则当前运算符入栈,若当前运算符的优先级低于栈顶运算符,则栈顶运
7、算符退栈。下面给出了逆波兰表达式生成算法的流程图。(为了便于比较相邻运算符的优先级,需要设立一个工作栈,用来存放暂时不能处理的运算符,所以又称运算符栈。实验代码:package text_2import java.util.*;public class Text2 private char Operator_Precedence_Relation_Matrix = , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ;private char Infix_Expression; /
8、字符串infix用于表示要处理的中缀表达式private String Reverse_Polish_Expression = new String();/ 字符串reversePlishExpression用于表示处理结果逆波兰式private String Analysis_Stack = new String();/字符串analysisStack用于表示分析栈private int Length_Infix_Expression = 0;/ 中缀表达式的长度,初始值为0private int match_Parentheses = 1;/ 用来查看左右括号是否配对正确private i
9、nt count = 0;private void init(String str) Infix_Expression = str.toCharArray();Length_Infix_Expression = Infix_Expression.length;private int Operator_Judgement(char currentOperator) int flag = -1;switch (currentOperator) case +:flag = 0;break;case -:flag = 1;break;case *:flag = 2;break;case /:flag
10、= 3;case :flag = 4;break;case (:flag = 5;break;case ):flag = 6;break;return flag;void convert_Process(String str) init(str);while (true) match_Parentheses = 0;if (count = Length_Infix_Expression) / 检测输入串为空while (Analysis_Stack.length() != 0) / 检测分析栈if (Analysis_Stack.charAt(Analysis_Stack.length() -
11、 1) = () System.out.println(n您输入的中缀表达式中有无法配对的(括号,请仔细核实!);System.exit(0); else Reverse_Polish_Expression += Analysis_Stack.charAt(Analysis_Stack.length() - 1);Analysis_Stack = Analysis_Stack.substring(0,Analysis_Stack.length() - 1);/ 退栈输出System.out.println(逆波兰式为:+ Reverse_Polish_Expression);System.ex
12、it(0); else if (Operator_Judgement(Infix_Expressioncount) = -1) Reverse_Polish_Expression += Infix_Expressioncount; else while (Analysis_Stack.length() != 0) / 检测分析栈if (Operator_Precedence_Relation_MatrixOperator_Judgement(Analysis_Stack.charAt(Analysis_Stack.length() - 1)Operator_Judgement(Infix_Ex
13、pressioncount) = ) Analysis_Stack += Infix_Expressioncount;break; else if (Infix_Expressioncount != ) Reverse_Polish_Expression += Analysis_Stack.charAt(Analysis_Stack.length() - 1);Analysis_Stack = Analysis_Stack.substring(0,Analysis_Stack.length() - 1); else while (Analysis_Stack.length() = 0| Ana
14、lysis_Stack.charAt(Analysis_Stack.length() - 1) != () if (Analysis_Stack.length() = 0) System.out.println(n您输入的中缀表达式中有无法配对的)括号,请仔细核实!);System.exit(0); else Reverse_Polish_Expression += Analysis_Stack.charAt(Analysis_Stack.length() - 1);Analysis_Stack = Analysis_Stack.substring(0,Analysis_Stack.lengt
15、h() - 1);if (Analysis_Stack.charAt(Analysis_Stack.length() - 1) = () Analysis_Stack = Analysis_Stack.substring(0, Analysis_Stack.length() - 1);match_Parentheses = 1;break;if (Analysis_Stack.length() = 0)if (Infix_Expressioncount != )Analysis_Stack += Infix_Expressioncount;else if (match_Parentheses != 1) System.out.println(n您输入的中缀表达式中有无法配对的)括号,请仔细核实!);System.exit(0);count+;public static void main(String args) System.out.println(请输入中缀表达式:);Scanner Expression = new Scanner(System.in);String str1 = Expression.nextLine();new Text2().convert_Process(str1);实验结果:专心-专注-专业
限制150内