根据给定文法编制调试LL(1)分析程序,以便对任意输入的符号串进行分析。
相关知识点
(1)、LL(1)分析法的功能
LL(1)分析法的功能是利用LL(1)控制程序根据显示栈栈顶内容、向前看符号以及LL(1)分析表,对输入符号串自上而下的分析过程。
(2)、LL(1)分析法的前提
改造文法:消除二义性、消除左递归、提取左因子,判断是否为LL(1)文法,
(3)、LL(1)分析法设计思想及算法
程序输入/输出示例:
对下列文法,用LL(1)分析法对任意输入的符号串进行分析:
(1)E->TG
(2)G->+TG|—TG
(3)G->ε
(4)T->FS
(5)S->*FS|/FS
(6)S->ε
(7)F->(E)
(8)F->i
输出的格式如下:
(1) 输入一以#结束的符号串(包括+—*/()i#)。(2)输出过程如下:
(3)输入符号串为非法符号串(或者为合法符号串)
(4)上述描述的输出过程只是其中一部分的。
注意:1.表达式中允许使用运算符(+-*/)、分割符(括号)、字符i,结束符#;
详细程序实现如下:
import java.io.*;
public class LL1 {
String Vn[] = {"E", "G", "T", "S", "F"};
String Vt[] = {"i", "+", "-", "*", "/", "(", ")", "#"};
String P[][] = new String[5][8];
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 count3){
this.count = count;
this.count1 = count1;
this.count2 = count2;
this.count3 = count3;
flag = false;
}
public void setFenxi() {
fenxi = new String[20];
fenxi[0] = "#";
fenxi[1] = "E";
}
public void setP() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 8; j++)
{
P[i][j] = "null";
}
}
P[0][0] = "->TG";
P[0][5] = "->TG";
P[1][1] = "->+TG";
P[1][2] = "->-TG";
P[1][6] = "->ε";
P[1][7] = "->ε";
P[2][0] = "->FS";
P[2][5] = "->FS";
P[3][1] = "->ε";
P[3][2] = "->ε";
P[3][3] = "->*FS";
P[3][4] = "->/FS";
P[3][6] = "->ε";
P[3][7] = "->ε";
P[4][0] = "->i";
P[4][5] = "->i";
System.out.println("该LL(1)文法的预测分析表为:");
System.out.println("-----------------------------------------------------------------------");
System.out.print(" ");
for (int i1=0; i1<8; i1++) {
System.out.print("\t"+Vt[i1]);
}
System.out.println();
System.out.println("------------------------------------------------------------------------");
for (int i2=0; i2<5; i2++) {
System.out.print(Vn[i2]+"\t");
for (int j=0; j<8; j++) {
System.out.print(P[i2][j]+"\t");
}
System.out.println();
}
System.out.println("----------------------------------------------------------------------");
}
public void setInputString(String input) {
this.inputString = input;
}
public boolean judge() {
String inputChar = inputString.substring(0, 1);
boolean flage = false;
if (count1 >= 0) {
for (int i=0; i<6; i++) {
if (fenxi[count1].equals(Vt[i])) {
flage = true;
break;
}
}
}
if (flage) {
if (fenxi[count1].equals(inputChar)) {
if (fenxi[count1].equals("#")&&inputString.length()==1)
{
String fenxizhan = "";
for (int i=0; i<=P.length; i++) {
if (fenxi[i] == null) {
break;
} else {
fenxizhan = fenxizhan + fenxi[i];
}
}
System.out.print(" " + count);
String countToString = Integer.toString(count);
int farWay = 14 - countToString.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.print(fenxizhan);
farWay = 20 - fenxizhan.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.print(inputString);
farWay = 25 - inputString.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.println("接受");
flag = true;
return true;
} else {
String fenxizhan = "";
for (int i=0; i<=P.length; i++) {
if (fenxi[i] == null) {
break;
} else {
fenxizhan = fenxizhan + fenxi[i];
}
}
System.out.print(" "+count);
String countToString = Integer.toString(count);
int farWay = 14 - countToString.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.print(fenxizhan);
farWay = 20 - fenxizhan.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.print(inputString);
farWay = 25 - inputString.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.println("\"" + inputChar + "\"" + "匹配");
fenxi[count1] = null;
count1 -= 1;
if(count1<0)
{
flag = true;
return true;
}
if (inputString.length() > 1) {
inputString = inputString.substring(1, inputString
.length());
} else {
inputChar = inputString;
}
count++;
judge();
}
}else {
System.out.println(" 分析到第" + count + "步时出错!");
flag = false;
return false;
}
} else {
boolean fla = false;
for (int i=0; i<8; i++) {
if (inputChar.equals(Vt[i])) {
fla = true;
count2 = i;
break;
}
}
if(!fla){
System.out.println(" 分析到第" + count + "步时出错!");
flag = false;
return false;
}
for (int i=0; i<5; i++) {
if (fenxi[count1].equals(Vn[i])) {
count3 = i;
break;
}
}
if (P[count3][count2] != "null") {
String p = P[count3][count2];
String s1 = p.substring(2, p.length());
if (s1.equals("ε")) {
String fenxizhan = "";
for (int i=0; i<=P.length; i++) {
if (fenxi[i] == null) {
break;
} else {
fenxizhan = fenxizhan + fenxi[i];
}
}
System.out.print(" " + count);
String countToString = Integer.toString(count);
int farWay = 14 - countToString.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.print(fenxizhan);
farWay = 20 - fenxizhan.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.print(inputString);
farWay = 25 - inputString.length();
for (int k=0; k<farWay; k++) {
System.out.print(" ");
}
System.out.println(fenxi[count1] + P[count3][count2]);
fenxi[count1] = null;
count1 -= 1;
if(count1<0)
{
flag = true;
return true;
}
count++;
judge();
} else {
int k = s1.length();
String fenxizhan = "";
for (int i=0; i<=P.length; i++) {
if (fenxi[i] == null) {
break;
} else {
fenxizhan = fenxizhan + fenxi[i];
}
}
System.out.print(" "+count);
String countToString = Integer.toString(count);
int farWay = 14 - countToString.length();
for (int o=0; o<farWay; o++) {
System.out.print(" ");
}
System.out.print(fenxizhan);
farWay = 20 - fenxizhan.length();
for (int o=0; o<farWay; o++) {
System.out.print(" ");
}
System.out.print(inputString);
farWay = 25 - inputString.length();
for (int o=0; o<farWay; o++) {
System.out.print(" ");
}
System.out.println(fenxi[count1] + P[count3][count2]);
for (int i=1; i<=k; i++) {
String s2 = s1.substring(s1.length() - 1, s1.length());
s1 = s1.substring(0, s1.length() - 1);
fenxi[count1] = s2;
if (i < k)
count1++;
}
count++;
judge();
}
} else {
System.out.println(" 分析到第" + count + "步时出错!");
flag = false;
return false;
}
}
return flag;
}
public static void main(String args[]) {
LL1 l = new LL1();
l.setP();
String input = "";
boolean flag = true;
while (flag) {
try {
InputStreamReader str = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(str);
System.out.print("输入一以#结束的符号串(包括+—*/()i#,退出输入exit即可):");
input = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if(input.equals("exit")){
flag = false;
}else{
l.setInputString(input);
l.setCount(1, 1, 0, 0);
l.setFenxi();
System.out.println("输出过程如下:");
System.out.println("----------------------------------------------------------------------");
System.out.println("步骤 "+"\t\t"+"|分析栈 "+"\t\t"+"|剩余输入串 "+"\t\t"+"|所用产生式 "+"\t\t");
System.out.println("----------------------------------------------------------------------");
boolean b = l.judge();
System.out.println("----------------------------------------------------------------------");
if(b){
System.out.println("输入符号串为合法符号串!");
}else{
System.out.println("输入符号串为非法符号串!");
}
}
}
}
}