📄 yucefenxi.java
字号:
/***********************************************************************
*文件名:YUCEFENXI.java
*文件描叙:预测分析程序.分析如下方法:
* E ::=T|E+T|E-T
T ::=F|T*F|T/F
F ::=P|(E)
P ::=G
G ::= i
*为了方便把G ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z
*简化为G::= i
*输出:输出该文法的LL(1)分析表
*输入:每行输入一个表达式
*输出:输出该表达式的预测分析步骤
************************************************************************/
import java.io.*;
import java.util.*;
public class YUCEFENXI
{public static String[] wenfa={"E->TA","A->+TA","A->-TA","A->ε","T->FB","B->*FB","B->/FB","B->ε","F->P","F->(E)","P->G","G->i"};
static String s1;
static String s2;
static String[] followE={")","#"}; //下面定义E,E1,T,T1,F,P,G的FOLLOW集
static String[] followE1={")","#"};
static String[] followT={"+","-","#"};
static String[] followT1={"+","-","#"};
static String[] followF={"*","/","#"};
static String[] followP={"*","/","#"};
static String[] followG={"*","/","#"};
static String[] firstTE1={"(","i"}; //下面定义各个产生式的first集
static String[] firstTE2={"+"};
static String[] firstTE3={"-"};
static String[] firstFT1={"(","i"};
static String[] firstFT2={"*"};
static String[] firstFT3={"/"};
static String[] firstP={"i"};
static String[] firstE1={"("};
static String[] firstG={"i"};
static String[] firsti={"i"};
static String Sender;
static String biao[][]={{" ","i","+","-","*","/","(",")","#"},
{"E"," "," "," "," "," "," "," "," "},
{"A"," "," "," "," "," "," "," "," "},
{"T"," "," "," "," "," "," "," "," "},
{"B"," "," "," "," "," "," "," "," "},
{"F"," "," "," "," "," "," "," "," "},
{"P"," "," "," "," "," "," "," "," "},
{"G"," "," "," "," "," "," "," "," "}};
//**********************************************************************************************************
public static void main(String[] args)throws IOException
{
System.out.println("要分析的文法为:");//输出文法信息
System.out.println("E->T|E+T|E-T");
System.out.println("T->F|T*F|T/F");
System.out.println("F->P|(E)");
System.out.println("P->G");
System.out.println("G->i");
System.out.println("该文法的LL(1)分析表为:");
String sre=new String("该文法的LL(1)分析表是");
String sr;
JIANBIAO();
int len=biao.length;
for(int s=0;s<len;s++)
{int len1=biao[s].length;
for(int n=0;n<len1;n++)
{
System.out.print(biao[s][n]);
sr=biao[s][n];
System.out.print("\t");
}
System.out.print("\n");
System.out.print("\n");
}
try{
FileReader read=new FileReader("fenxi.txt"); //从fenxi.txt取出算术表达式
BufferedReader in=new BufferedReader(read);
while((Sender=in.readLine())!=null)
{ System.out.println("请输入算式表达式");
System.out.println(Sender);
System.out.print("\n");
System.out.println("请进行预测分析!");
System.out.print("步骤"+"\t"+"\t"+"符号栈"+"\t"+"\t"+"输入串"+"\t"+"\t"+"所用产生式");
System.out.print("\n");
FENXI(); //对取得的算术表达式进行预测分析
}
in.close();
read.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
//对每个终结符a属于first(b),把A->b加至M[A,a]中,若ε属于first(b),且c属于FOLLOW(A)把A->b加至M[A,a]中
//*******************************************************************************************************
public static void JIANBIAO() //构造分析表
{
{for(int i=0;i<firstTE1.length;i++)
{s1=firstTE1[i];
for(int j=0;j<biao[0].length;j++)
{
s2=biao[0][j];
if(s1.equals(s2))
{
biao[1][j]=wenfa[0];
}
}
}
}
for(int i=0;i<firstTE2.length;i++)
{s1=firstTE2[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[2][j]=wenfa[1];
}
}
}
for(int i=0;i<firstTE3.length;i++)
{s1=firstTE3[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[2][j]=wenfa[2];
}
}
}
for(int i=0;i<firstFT1.length;i++)
{s1=firstFT1[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[3][j]=wenfa[4];
}
}
}
for(int i=0;i<firstFT2.length;i++)
{s1=firstFT2[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[4][j]=wenfa[5];
}
}
}
for(int i=0;i<firstFT3.length;i++)
{s1=firstFT3[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[4][j]=wenfa[6];
}
}
}
for(int i=0;i<firstP.length;i++)
{s1=firstP[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[5][j]=wenfa[8];
}
}
}
for(int i=0;i<firstE1.length;i++)
{s1=firstE1[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[5][j]=wenfa[9];
}
}
}
for(int i=0;i<firstG.length;i++)
{s1=firstG[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[6][j]=wenfa[10];
}
}
}
for(int i=0;i<firsti.length;i++)
{s1=firsti[i];
for(int j=0;j<biao[0].length;j++)
{s2=biao[0][j];
if(s1.equals(s2))
{
biao[7][j]=wenfa[11];
}
}
}
for(int i=0;i<followE1.length;i++)
{s1=followE1[i];
for(int j=0;j<biao[0].length;j++)
{
s2=biao[0][j];
if(s1.equals(s2))
{
biao[2][j]=wenfa[3];
}}
}
for(int i=0;i<followT1.length;i++)
{s1=followT1[i];
for(int j=0;j<biao[0].length;j++)
{
s2=biao[0][j];
if(s1.equals(s2))
{
biao[4][j]=wenfa[7];
}}
}
}
//*************************************************************************************************
public static void FENXI() //对算术表达式进行分析
{ String fuhao=new String("#E");
String str=new String(" ");
String str1=new String(" ");
String str2=new String(" ");
String str3=new String(" ");
String str4=new String(Sender);
int len1;
int con=0;
int len2;
int len3;
int i=0;
int len=Sender.length();
System.out.print(con+"\t"+"\t"+fuhao+"\t"+"\t"+Sender+"\t"+"\t"+str2);//首先输出初始的符号栈和输入串
System.out.print("\n");
str=Sender.substring(0,1);
str1=fuhao.substring(1,2);
fuhao=fuhao.substring(0,1);
p1:while(!str1.equals("#"))
{
if(str1.equals(str))
{
++i;
str=Sender.substring(i,i+1); //如果符号栈里的最后一个字符与输入串里的字符相等,则两个字符都弹出栈
str4=Sender.substring(i);
con++;
System.out.print(con+"\t"+"\t"+fuhao+"\t"+"\t"+str4+"\t"+"\t");
System.out.print("\n");
}
else
{
for(int v=0;v<8;v++) //符号栈里的字符和分析表对照,把对应的产生式,放入符号栈里
{
if(str1.equals(biao[v][0]))
{ for(int s=0;s<9;s++)
{
if(str.equals(biao[0][s]))
str2=biao[v][s];
}
}
}
len1=str2.length();
for(int t=len1;t>3;t--)
{
str3=str2.substring(t-1,t);
if(str3.equals(" "))
{
System.out.println("error"); //如果对应的产生式是空字,则输出error
break p1;
}
if(!str3.equals("ε")) //如果对应的字符不是ε,则把对应的字符串反序输入符号栈
{
fuhao=fuhao.concat(str3);
}
}
con++;
System.out.print(con+"\t"+"\t"+fuhao+"\t"+"\t"+str4+"\t"+"\t"+str2);
System.out.print("\n");
}
len2=fuhao.length();
str1=fuhao.substring(len2-1,len2);
fuhao=fuhao.substring(0,len2-1);
}
p2:while(str1.equals("#")) //如果符号栈的字符与输入串里的字符都等于#时,输出accept
{if(str1.equals("#"))
{ if(str.equals("#"))
{
System.out.println("accept");
break p2;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -