编译原理-递归下降的预测分析

编译原理-递归下降的预测分析

package bianyishangji4;
import java.*;
public class diguixiajiang {
 public String yuju;
 public String lookahead;
 public int x1,x2;
 public String temp;
 public diguixiajiang() {
  yuju="i+i*i#";
  x1=0;
  x2=1;
  lookahead=yuju.substring(0,1);
  temp="0";
  // TODO Auto-generated constructor stub
 }
 public void shan() {
  String q1=yuju.substring(0,x1);
  String q2=yuju.substring(x2,yuju.length());
  yuju=q1+q2;
  x1=0;
  x2=1;
  lookahead=yuju.substring(0,1);
 }
 public void erro() {
  if(lookahead.equals("i")) {
   System.out.println("错误,多了i,i弹栈");
   shan();
  }
  else if(lookahead.equals("+")) {
   System.out.println("错误,多了+,+弹栈");
   shan();
  }
  else if(lookahead.equals("*")) {
   System.out.println("错误,多了*,*弹栈");
   shan();
  }
  else if(lookahead.equals(")")) {
   System.out.println("错误,括号不匹配,) 弹栈");
   shan();
  }
  else if(lookahead.equals("(")) {
   System.out.println("错误,括号不匹配,( 弹栈");
   shan();
  }
  System.out.println("!!!");
  E1("0");
//  String jString="";
//  for(int i=(temp.length());temp.substring(i-1,i).equals("0")&&temp.substring(i-1,i).equals("a")&&temp.substring(i-1,i).equals("b");i--) {
//   jString=temp.substring(i-1,i);
//  }
//  switch (jString) {
//  case "1":
//   E1("0");
//   break;
//  case "2":
//   E2("0");
//   break;
//  case "3":
//   T1("0");
//   break;
//  case "4":
//   T2("0");
//   break;
//  case "5":
//   F("0");
//   break;
//  default:
//   break;
//  }
 }
 public void takenext() {
  x1++;
  x2++;
  lookahead=yuju.substring(x1,x2);
  if(lookahead.equals("#")) {
   System.exit(0);
  }
 }
 public boolean match (String t,String d,String a) {
  if(lookahead.equals(a)) {
   t=t+d;
   //System.out.println(t);
   for(int i=0;i<t.length();i++) {
    switch (t.substring(i,i+1)) {
    case "0":
     break;
    case "1":
     System.out.println("E->TE'");
     break;
    case "2":
     System.out.println("E'->+TE'");
     break;
    case "3":
     System.out.println("T->FT'");
     break;
    case "4":
     System.out.println("T'->*FT'");
     break;
    case "5":
     if(t.substring(i+1,i+2).equals("a")) {
      System.out.println("F->(E)");
      i++;
     }
     else if(t.substring(i+1,i+2).equals("b")) {
      System.out.println("F->id");
      i++;
     }
     break;
    default:
     break;
    }
   }
   System.out.println("");
   temp="0";
   takenext();
   return true;
  }
  else {
   return false;
  }
 }
 void E1(String a) {
  temp=temp+a;
  if(lookahead.equals("(") || lookahead.equals("i")) {T1("1");}
  if(lookahead.equals("+") || lookahead.equals("e")) {E2("1");}
  //else erro();
 }
 void E2(String a) {
  temp=temp+a;
  match(temp,"2","+");
  if(lookahead.equals("(") || lookahead.equals("i")) {T1("2");}
  if(lookahead.equals("+") || lookahead.equals("e")) {E2("2");}
  //else erro();
 }
 void T1(String a) {
  temp=temp+a;
  if(lookahead.equals("(") || lookahead.equals("i")) {F("3");}
  if(lookahead.equals("*") || lookahead.equals("e")) {T2("3");}
  //else erro();
 }
 void T2(String a) {
  temp=temp+a;
  match(temp,"4","*");
  if(lookahead.equals("(") || lookahead.equals("i")) {F("4");}
  if(lookahead.equals("*") || lookahead.equals("e")) {T2("4");}
  //else erro();
 }
 void F(String a) {
  temp=temp+a;
  if(match(temp,"5a","(")) {
   if(lookahead.equals("(") || lookahead.equals("i"))  {E1("5");}
   if(match(temp,"5a",")")) {}
   else {
    System.out.println("错误,括号不匹配,( 弹栈");
    yuju.replace("(", "");
    E1("0");
   }
  }
  else if(match(temp,"5b","i")) {
   
  }
  //else erro();
 }
 public static void main(String[] args) {
  diguixiajiang aDiguixiajiang =new diguixiajiang();
  //System.out.println(aDiguixiajiang.temp);
  aDiguixiajiang.E1("0");
  while(true) {
   aDiguixiajiang.erro();
  }
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值