1、1、 分析文法,将给出的文法转化为LL(1)文法;2、 学习预测分析程序的结构,设计合理的预测分析程序;3、 编写测试程序,包括表达式的读入和结果的输出;4、 测试程序运行效果,测试数据可以参考下列给出的数据。六、测试数据 输入数据:编辑一个文本文文件expression.txt,在文件中输入如下内容: 10;1+2;(1+2)*3+(5+6*7);(1+2)*3+4;1+2+3+(*4+5);(a+b)*(c+d);(ab3+de4)*5)+1;正确结果:(1)10;输出:正确(2)1+2;(3)(1+2)*3+(5+6*7);(4)(1+2)*3+4错误(5)1+2+3+(*4+5)(6)
2、(a+b)*(c+d)(7)(ab3+de4)*5)+1七、实验报告要求实验报告应包括以下几个部分:1、 给定文法的LL(1)形式;2、 预测分析程序的算法和结构;3、 程序运行流程;4、 程序的测试结果和问题;5、 实验总结。package compile2;import java.util.Stack;public class text2 /* * 给定文法GE: */ static String gram = E-TA, A-+TAT-FBB-*FB, F-P(E) ; static String followE = )# static String followEA = static
3、String followT = + static String followTB = static String followF = * static String firstE = i( static String firstEA = static String firstT = static String firstTB = static String firstF = static String list = , E, null, null, , null, null ,A, null, TBF, null, null ; public static void main(String
4、args) throws Exception String infile = D:/expression.txt; String outfile = /result2.txt Stack tmpword = new Stack20; expression = new Stack20; for (int i = 0; i tmpword.length; i+) tmpwordi = new Stack(); expressioni = new Stack 2) String tmp = expressioni.toArray(new String0); printArray(tmp); isLL
5、1(tmpwordi); i+; public static void printArray(String s) s.length; i+) System.out.print(si); System.out.println(); public static void isLL1(Stack tmpword) String input = tmpword.toArray(new String0); int inputCount = 0; status = new Stack= 0; i-) status.push( + lista0a1.charAt(i); /inputCount+; else
6、 flag = false; result = false; if (result) System.out.println(正确 else错误 public static boolean isVt(String s) for (int i = 1; list0.length - 1; if (s.equals(list0i) return true; return false; public static int indexInList(String m, String a) int i = 0, j = 0; for (int c = 1; c list.length; c+) if (m.
7、equals(listc0) i = c; list0.length ; if (a.equals(list0c) j = c; return new int i, j ;import java.io.*;import java.util.*;public class main static String key_word = mainifthenwhiledointelse static String cal_word = -/=!=+=-=*=/=/program.txt/result.txt Stack word = new Stack5; Stack expression = new
8、Stackword.length; word.length; wordi = new Stack scan(infile, outfile, word, expression); public static void scan(String infile,String outfile, Stack word, Stack expression) throws Exception java.io.File file = new java.io.File(infile); Scanner input = new Scanner(file); java.io.PrintWriter output =
9、 new PrintWriter(outfile); int count = 0; wordcount.push( while (input.hasNext() String tmp = input.next(); int i = 0; while (i tmp.length() if (tmp.charAt(i) 1) /检查十进制数字 String num = while (tmp.charAt(i) 0) num += tmp.charAt(i); i+; if (i = tmp.length() break; output.println( + 1 + + num + wordcoun
10、t.push( expressioncount.push(num); if (i + 2 tmp.length()/ 检查十六进制数字 if (tmp.charAt(i) = tmp.charAt(i + 1) = x i += 2; String num = while (tmp.charAt(i) ) | (tmp.charAt(i) fa) num += tmp.charAt(i); i+; if (i = tmp.length() break; output.println( + 3 + wordcount.push( expressioncount.push(num); if (i
11、+ 1 tmp.length()/ 检查八进制数字 while (tmp.charAt(i) 7 + 2 + / 检查关键字和变量 if (i if (i tmp.length() & & tmp.charAt(i) tmp_word += tmp.charAt(i); boolean is_keyword = false; for (int j = 0; j wordcount.push(key_wordj); expressioncount.push(key_wordj); is_keyword = true; if (!is_keyword) output.println( + 0 +
12、+ tmp_word + wordcount.push( expressioncount.push(tmp_word); / 检查运算符以及 if (i + 1 if (tmp.charAt(i + 1) = = for (int j = 0; cal_word.length; if (cal_wordj.equals( + tmp.charAt(i) + tmp.charAt(i + 1) output.println( + cal_wordj + , + wordcount.push(cal_wordj); expressioncount.push( if (wordcount.peek(
13、) = wordcount.pop(); wordcount.push( count+; i += 2; break; for (int j = 0; if (cal_wordj.equals( + tmp.charAt(i) wordcount.push(cal_wordj); expressioncount.push(cal_wordj); if (wordcount.peek() = wordcount.pop(); wordcount.push( count+; input.close(); output.close();八、思考题1、 如果使用递归下降分析法来进行语法分析,为什么文法必须先转化为LL(1)文法再做递归下降分析?
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1