谁来帮我看看这个java程序哪有错误
一个语法分析程序。importjava.io.*;publicclassTestyufa{publicstaticvoidmain(String[]args){Syste...
一个语法分析程序。
import java.io.*;
public class Testyufa
{ public static void main(String[] args)
{System.out.println("请输入字符串,以#结尾");
String m[][]={
{"eT","error","error","eT","error","error"},
{"error","eT","error","error","",""},
{"tF","error","error","tF","error","error"},
{"error","","tF*","error","",""},
{"i","error","error",")E(","error","error"}
};
char a[]=new char[50];//定义char型数组存放输入字符串
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));//缓冲池
try{a=in.readLine().toCharArray();}catch(IOException e){}//输入串存入数组a
String n="EeTtF";//非终结符集合
String t="i+*()#";//终结符集合
String b="#E";//'#''E'进栈
for (int i=0;i<a.length ;i++ )
{
char x=b.charAt(b.length() - 1);//上托栈顶符号放入x
int p=t.indexOf(x);
if (p==-1)//说明x不属于t终结符
{ if (x=='#')
{if (a[i]=='#')
{System.out.println("分析成功,该符号串是此文法的句子");
}else {System.out.println("出错");}
}else {
int q=n.indexOf(x);
if (m[q][i]=="error")//M[x,a]不是产生式
{System.out.println("出错");
}else {b = b.substring(0, b.length() - 1) + m[q][i];}
}
}else{ if (x!=a[i])
{System.out.println("出错");
}else break;
}
}
}
} 展开
import java.io.*;
public class Testyufa
{ public static void main(String[] args)
{System.out.println("请输入字符串,以#结尾");
String m[][]={
{"eT","error","error","eT","error","error"},
{"error","eT","error","error","",""},
{"tF","error","error","tF","error","error"},
{"error","","tF*","error","",""},
{"i","error","error",")E(","error","error"}
};
char a[]=new char[50];//定义char型数组存放输入字符串
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));//缓冲池
try{a=in.readLine().toCharArray();}catch(IOException e){}//输入串存入数组a
String n="EeTtF";//非终结符集合
String t="i+*()#";//终结符集合
String b="#E";//'#''E'进栈
for (int i=0;i<a.length ;i++ )
{
char x=b.charAt(b.length() - 1);//上托栈顶符号放入x
int p=t.indexOf(x);
if (p==-1)//说明x不属于t终结符
{ if (x=='#')
{if (a[i]=='#')
{System.out.println("分析成功,该符号串是此文法的句子");
}else {System.out.println("出错");}
}else {
int q=n.indexOf(x);
if (m[q][i]=="error")//M[x,a]不是产生式
{System.out.println("出错");
}else {b = b.substring(0, b.length() - 1) + m[q][i];}
}
}else{ if (x!=a[i])
{System.out.println("出错");
}else break;
}
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询