《编译原理LL1文法分析报告器实验java.doc》由会员分享,可在线阅读,更多相关《编译原理LL1文法分析报告器实验java.doc(11页珍藏版)》请在课桌文档上搜索。
1、编译原理LL(1)文法分析器实验本程序是基于已构建好的某一个语法的预测分析表来对用户的输入字符串进展分析,判断输入的字符串是否属于该文法的句子。根本实现思想:接收用户输入的字符串字符串以“#表示完毕后,对用做分析栈的一维数组和存放分析表的二维数组进展初始化。然后取出分析栈的栈顶字符,判断是否为终结符,假如为终结符如此判断是否为“#且与当前输入符号一样,假如是如此语法分析完毕,输入的字符串为文法的一个句子,否如此出错假如不为“#且与当前输入符号一样如此将栈顶符号出栈,当前输入符号从输入字符串中除去,进入下一个字符的分析。假如不为“#且不与当前输入符号一样,如此出错。假如栈顶符号为非终结符时,查看
2、预测分析表,看栈顶符号和当前输入符号是否构成产生式,假如产生式的右部为,如此将栈顶符号出栈,取出栈顶符号进入下一个字符的分析。假如不为,将产生式的右部逆序的入栈,取出栈顶符号进入下一步分析。程序流程图:本程序中使用以下文法作对用户输入的字符串进展分析:ETEE+TE|TFTT*FT|Fi|(E)该文法的预测分析表为:1、显示预测分析表,提示用户输入字符串2、输入的字符串为正确的句子:3、输入的字符串中包含了不属于终结符集的字符4、输入的字符串不是该文法能推导出来的句子程序代码:package zhuangms ;import java.io.*;public class LL String V
3、n = E, E, T, T, F ; / 非终结符集String Vt = i, +, *, (, ), # ; / 终结符集String P = new String56; / 预测分析表String fenxi ; / 分析栈int count = 1; / 步骤int count1 = 1;/分析栈指针int count2 = 0, count3 = 0;/预测分析表指针String inputString = ; / 输入的字符串boolean flag;public void setCount(int count, int count1, int count2, int count
4、3)this.count = count; this.count1 = count1;this.count2 = count2;this.count3 = count3;flag = false;public void setFenxi() / 初始化分析栈fenxi = new String20;fenxi0 = #;fenxi1 = E;public void setP() / 初始化预测分析表for (int i = 0; i 5; i+) for (int j = 0; j TE;P03 = -TE;P11 = -+TE;P14 = -;P15 = -;P20 = -FT;P23 =
5、-FT;P31 = -;P32 = -*FT;P34 = -;P35 = -;P40 = -i;P43 = -(E);/ 打印出预测分析表System.out.println( 已构建好的预测分析表);System.out.println(-);for (int i=0; i6; i+) System.out.print( +Vti);System.out.println();System.out.println(-);for (int i=0; i5; i+) System.out.print( +Vni+ );for (int j=0; j0) l = 10-Pij-1.length();
6、for (int k=0; k= 0) for (int i=0; i6; i+) if (fenxicount1.equals(Vti) / 判断分析栈栈顶的字符是否为终结符flage = true;break;if (flage) / 为终结符时if (fenxicount1.equals(inputChar) if (fenxicount1.equals(#)&inputString.length()=1) / 栈顶符号为完毕标志时/ System.out.println(最后一个);String fenxizhan = ;for (int i=0; i=P.length; i+) /
7、拿到分析栈里的全部内容滤去nullif (fenxii = null) break; else fenxizhan = fenxizhan + fenxii;/ 输出当前分析栈情况,输入字符串,所用产生式或匹配System.out.print( + count);String countToString = Integer.toString(count);int farWay = 14 - countToString.length();for (int k=0; kfarWay; k+) System.out.print( );System.out.print(fenxizhan);farWa
8、y = 20 - fenxizhan.length();for (int k=0; kfarWay; k+) System.out.print( );System.out.print(inputString);farWay = 25 - inputString.length();for (int k=0; kfarWay; k+) System.out.print( );System.out.println(承受);flag = true;return true; else / 分析栈栈顶符号不为完毕标志符号时String fenxizhan = ;for (int i=0; i=P.leng
9、th; i+) / 拿到分析栈里的全部内容滤去nullif (fenxii = null) break; else fenxizhan = fenxizhan + fenxii;/ 输出当前分析栈情况,输入字符串,所用产生式或匹配System.out.print( +count);String countToString = Integer.toString(count);int farWay = 14 - countToString.length();for (int k=0; kfarWay; k+) System.out.print( );System.out.print(fenxizh
10、an);farWay = 20 - fenxizhan.length();for (int k=0; kfarWay; k+) System.out.print( );System.out.print(inputString);farWay = 25 - inputString.length();for (int k=0; k 1) / 当当前输入字符串的长度大于1时,将当前输入字符从输入字符串中除去inputString = inputString.substring(1, inputString.length(); else / 当前输入串长度为1时inputChar = inputStr
11、ing;/ System.out.println( +count+ +fenxizhan+/ +inputString + +Pcount3count2);/ System.out.println(count + inputChar + 匹配 );count+;judge();else / 判断与与输入符号是否一样为完毕标志System.out.println( 分析到第 + count + 步时出错!);flag = false;return false; else / 非终结符时boolean fla = false; for (int i=0; i6; i+) / 查询当前输入符号位于终
12、结符集的位置if (inputChar.equals(Vti) fla = true;count2 = i;break;if(!fla)System.out.println( 分析到第 + count + 步时出错!);flag = false;return false;for (int i=0; i5; i+) / 查询栈顶的符号位于非终结符集的位置if (fenxicount1.equals(Vni) count3 = i;break;if (Pcount3count2 != error) / 栈顶的非终结符与输入的终结符存在产生式时String p = Pcount3count2;Str
13、ing s1 = p.substring(2, p.length(); / 获取对应的产生式if (s1.equals() / 产生式推出“时String fenxizhan = ;for (int i=0; i=P.length; i+) if (fenxii = null) break; else fenxizhan = fenxizhan + fenxii;/ 输出当前分析栈情况,输入字符串,所用产生式或匹配System.out.print( + count);String countToString = Integer.toString(count);int farWay = 14 -
14、 countToString.length();for (int k=0; kfarWay; k+) System.out.print( );System.out.print(fenxizhan);farWay = 20 - fenxizhan.length();for (int k=0; kfarWay; k+) System.out.print( );System.out.print(inputString);farWay = 25 - inputString.length();for (int k=0; kfarWay; k+) System.out.print( );System.ou
15、t.println(fenxicount1 + Pcount3count2);/ 将栈顶符号出栈,栈顶指针指向下一个元素fenxicount1 = null;count1 -= 1;count+;judge(); else / 产生式不推出“时int k = s1.length();String fenxizhan = ;for (int i=0; i=P.length; i+) if (fenxii = null) break; else fenxizhan = fenxizhan + fenxii;/ 输出当前分析栈情况,输入字符串,所用产生式或匹配System.out.print( +c
16、ount);String countToString = Integer.toString(count);int farWay = 14 - countToString.length();for (int o=0; ofarWay; o+) System.out.print( );System.out.print(fenxizhan);farWay = 20 - fenxizhan.length();for (int o=0; ofarWay; o+) System.out.print( );System.out.print(inputString);farWay = 25 - inputSt
17、ring.length();for (int o=0; ofarWay; o+) System.out.print( );System.out.println(fenxicount1 + Pcount3count2);for (int i=1; i=k; i+) / 将产生式右部的各个符号入栈String s2 = s1.substring(s1.length() - 1, s1.length();s1 = s1.substring(0, s1.length() - 1);if (s2.equals() s2 = s1.substring(s1.length() - 1, s1.length(
18、)+ s2;i+;s1 = s1.substring(0, s1.length() - 1);fenxicount1 = s2;if (i k)count1+;/ System.out.println(count1= + count1);/ System.out.println( +count+ +fenxizhan+/ +inputString + +Pcount3count2);count+;/ System.out.println(count);judge(); else System.out.println( 分析到第 + count + 步时出错!);flag = false;ret
19、urn false;return flag;public static void main(String args) LL l = new LL();l.setP();String input = ;boolean flag = true;while (flag) try InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);System.out.println();System.out.print(请输入字符串(输入exit退出):);input
20、 = br.readLine(); catch (Exception e) e.printStackTrace();if(input.equals(exit)flag = false;elsel.setInputString(input);l.setCount(1, 1, 0, 0);l.setFenxi();System.out.println();System.out.println(分析过程);System.out.println(-);System.out.println( 步骤 | 分析栈 | 剩余输入串 | 所用产生式 );System.out.println(-);boolean b = l.judge();System.out.println(-);if(b)System.out.println(您输入的字符串+input+是该文发的一个句子);elseSystem.out.println(您输入的字符串+input+有词法错误!);