static char a[]=new char[50];
static char b[]=new char[50];
static char d[]=new char[200];
static char e[]=new char[50];
static char ch;
static String ch1=new String();
static int n1,i1=0,flag=1,n=5;
static int total=0; public static void main(String[] args) { int f,p,j=0; char x; d[0]='E'; d[1]='='; d[2]='>'; d[3]='T'; d[4]='G'; d[5]='#'; System.out.println("請輸入長度不大于50且與#號結束的字串:"); Scanner sc=new Scanner(System.in); ch1=sc.nextLine(); do { ch=ch1.charAt(j); a[j]=ch; j++;
// System.out.println(a[j]+"----"); }while(ch!='#'); n1=j; ch=b[0]=a[0]; System.out.println("步驟\t文法\t分析串\t\t分析字符\t\t剩余串"); f=E1(); if(f==0) return; if(ch=='#') { System.out.println("Accept!Right Expression!"); p=0; x=d[p]; while(x!='#') { System.out.print(x); p=p+1; x=d[p]; } }else { System.out.println("Error!!!"); System.out.println("回車回傳"); return; } System.out.println();
} static int E1() { int f,t; System.out.print(total+"\tE-->TG\t"); total++; flag=1; input(); input1(); f=T(); if(f==0)return 0; t=G(); if(t==0) return 0; else return 1; } static int E() {
int f,t;
System.out.print(total+"\tE-->TG\t");
total++;
e[0]='E';e[1]='=';e[2]='>';e[3]='T';e[4]='G';e[5]='#';
output();
flag=1;
input();
input1();
f=F();
if(f==0)return 0;
t=G();
if(t==0) return 0;
else return 1 ; }
static int T() {
int f,t;
System.out.print(total+"\tT-->FS\t");
total++;
e[0]='T';e[1]='=';e[2]='>';e[3]='F';e[4]='S';e[5]='#';
output();
flag=1;
input();
input1();
f=F();
if(f==0)return 0;
t=S();
if(t==0) return 0;
else return 1 ;
}
//E'-->+TE'
static int G() {
int f;
if(ch=='+') { b[i1]=ch; System.out.print(total+"\tG-->+TG\t"); total++; e[0]='G';e[1]='=';e[2]='>';e[3]='+';e[4]='T';e[5]='G';e[6]='#'; output(); flag=0; input(); input1(); ch=a[++i1]; f=T(); if(f==0)return 0; G(); return 1 ;
}
System.out.print(total+"\tG-->^\t");
total++;
e[0]='G';e[1]='=';e[2]='>';e[3]='^';e[4]='#';
output();
flag=1;
input();
input1();
return 1;
}
static int S() { int f,t;
if(ch=='*') { b[i1]=ch; System.out.print(total+"\tS-->*FS\t"); total++; e[0]='S';e[1]='=';e[2]='>';e[3]='*';e[4]='F';e[5]='S';e[6]='#'; output(); flag=0; input(); input1(); ch=a[++i1]; f=F(); if(f==0)return 0; t=S(); if(t==0)return 0; else return 1 ; } System.out.print(total+"\tS-->^\t"); total++; e[0]='S';e[1]='=';e[2]='>';e[3]='^';e[4]='#'; output(); flag=1; a[i1]=ch; input(); input1(); return 1; }
static int F() {
int f;
if(ch=='(') { b[i1]=ch; System.out.print(total+"\tF-->(E)\t"); total++; e[0]='F';e[1]='=';e[2]='>';e[3]='(';e[4]='E';e[5]=')';e[6]='#'; output(); flag=0; input(); input1(); ch=a[++i1]; f=E(); if(f==0)return 0; if(ch==')') { b[i1]=ch; System.out.print(total+"\tF-->(E)\t"); total++; flag=0; input(); input1(); ch=a[++i1]; }else { System.out.println(); System.out.println("Error!!!"); return 0; } }else if(ch=='i') { b[i1]=ch; System.out.print(total+"\tF-->i\t"); total++; e[0]='F';e[1]='=';e[2]='>';e[3]='i';e[4]='#'; output(); flag=0; input(); input1(); ch=a[++i1]; }
else { System.out.println(); System.out.println("Error!"); return 0;
}
return 1;
}
static void input() {
int j=0;
for(;j<=i1-flag;j++)
System.out.print(b[j]);
System.out.print("\t\t");
System.out.print(ch+"\t\t"); }
static void input1() {
int j;
for(j=i1+1-flag;j<n1;j++) System.out.print(a[j]);
System.out.println();
}
static void output() {
int m,k,j,q;
int i=0;
m=0;k=0;q=0;
i=n;
d[n]='=';d[n+1]='>';
d[n+2]='#';
n=n+2;
i=n;
i=i-2;
while(d[i]!='>'&&i!=0) { i=i-1;
}
i=i+1; while(d[i]!=e[0]) { i=i+1;
} q=i;
m=q;k=q;
while(d[m]!='>')
{m=m-1;}
m=m+1;
while(m!=q) { d[n]=d[m]; m=m+1; n=n+1; }
d[n]='#';
for(j=3;e[j]!='#';j++) { d[n]=e[j]; n=n+1;
}
k=k+1;
while(d[k]!='=') { d[n]=d[k]; n=n+1; k=k+1;
}
d[n]='#';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/42865.html
標籤:Java相關
上一篇:框架原始碼入口應該怎么找?
