编译原理-太原理工大学.doc
【精品文档】如有侵权,请联系网站删除,仅供学习与交流编译原理-太原理工大学.精品文档.编译原理实验报告本科实验报告课程名称: 编译原理D 实验地点: 实验室 专业班级: 学 号: _学生姓名: java编译 指导教师: 成 绩: 2015年 月 日实验一、 无符号数的词法分析程序(4学时)内容:掌握词法分析的基本思想,并用高级语言编写无符号数的词法分析程序。要求:从键盘上输入一串字符(包括字母、数字等),最后以“;”结束,编写程序识别出其中的无符号数。无符号数文法规则可定义如下: <无符号数><无符号实数><无符号整数> <无符号实数><无符号整数>.<数字串>E<比例因子<无符号整数>E<比例因子> <比例因子><有符号整数> <有符号整数>+-<无符号整数> <无符号整数><数字串> <数字串><数字><数字> <数字>0 1 2 3. 9 读无符号数的程序流程图见下图实验代码:package text_1;import java.util.*;public class 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;i<ch1.length;i+) System.out.print(ch1i+" "); while (i < ch1.length) if (ch1i > '9' | ch1i < '0') /查到第一个数字i+;else do d = ch1i - '0'w = w * 10 + d;j+;i+; while (ch1i >= '0' && ch1i <= '9');if (ch1i != '.') if (ch1i != 'E') System.out.println("整数为:" + w); /输出整数w = 0;j = 0;else i+;if (ch1i = '-') e = -1;i+;if (ch1i >= '0' && ch1i <= '9') do d = ch1i - '0'p = p * 10 + d;i+; while (ch1i >= '0' && ch1i <= '9');if (j > 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 <= '9') do d = ch1i - '0'p = p * 10 + d;i+; while (ch1i >= '0' && ch1i <= '9');if (j > 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 i+;if (ch1i >= '0' && ch1i <= '9') do d = ch1i - '0'w1 = w1 * 10 + d;j+;i+; while (ch1i >= '0' && ch1i <= '9');elseSystem.out.println("输入错误!");if (ch1i = 'E') i+;if (ch1i = '-') e = -1;i+;if (ch1i >= '0' && ch1i <= '9') do d = ch1i - '0'p = p * 10 + d;i+; while (ch1i >= '0' && ch1i <= '9');if (j > 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 <= '9') do d = ch1i - '0'p = p * 10 + d;i+; while (ch1i >= '0' && ch1i <= '9');if (j > 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学时)要求:利用逆波兰式生成算法编写程序,将从键盘上输入的算术表达式(中缀表达式)转化成逆波兰式。逆波兰表达式的生成过程涉及到运算符的优先级,下表中列出几个常用运算符的优先关系。右关系左常用运算符优先关系矩阵+-*/()+-*/()如上表所示的优先关系矩阵表示了+,-,*,/,(,)等七种运算符之间的相互优先关系。“、”三种符号分别代表“大于”、“小于”、“相等”三种优先关系。左边的“”与右边的“(”之间没有优先关系存在,所以表中为空白。逆波兰表达式生成算法的关键在于比较当前运算符与栈顶运算符的优先关系,若当前运算符的优先级高于栈顶运算符,则当前运算符入栈,若当前运算符的优先级低于栈顶运算符,则栈顶运算符退栈。下面给出了逆波兰表达式生成算法的流程图。(为了便于比较相邻运算符的优先级,需要设立一个工作栈,用来存放暂时不能处理的运算符,所以又称运算符栈。实验代码:package text_2import java.util.*;public class Text2 private char Operator_Precedence_Relation_Matrix = private char Infix_Expression; / 字符串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 int 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 = 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() - 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.exit(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_Expressioncount) = '<') 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| Analysis_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.length() - 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);实验结果: