Action表。
Action.java:
package per.eyuan.util;
public class Action {
/*
* SLR的action表,接受传递过来的状态和终结符,
* 返回action表中对应的动作r(归约)或者s(移进),
* Syntax,根据动作做相应的操作。
*/
private String action[][]=new String[15][8];
String statu;//状态
String vt;//终结符
int rol;//行,action表中的行,代表状态,
int col;//列,action表中的列,代表终结符,顺序为0"id",1"int",2"float",3",",4";",5"#"
public Action() {
super();
init();
}
public void init(){
for(int i=0;i<action.length;i++){
for(int j=0;j<action[i].length;j++)
action[i][j]=new String("err");
}
action[0][0]="s1";
action[1][1]="s2";
action[2][0]="s7";
action[2][4]="s6";
action[3][2]="s9";
action[3][6]="s8";
action[4][2]="r2";
action[4][3]="s10";
action[4][5]="r2";
action[4][6]="r2";
action[5][2]="r4";
action[5][3]="r4";
action[5][5]="r4";
action[5][6]="r4";
action[6][0]="s7";
action[6][4]="s6";
action[7][2]="r6";
action[7][3]="r6";
action[7][5]="r6";
action[7][6]="r6";
action[8][7]="acc";
action[9][0]="s7";
action[9][4]="s6";
action[10][0]="s7";
action[10][4]="s6";
action[11][2]="s9";
action[11][5]="s14";
action[12][2]="r1";
action[12][3]="s10";
action[12][5]="r1";
action[12][6]="r1";
action[13][2]="r3";
action[13][3]="r3";
action[13][5]="r3";
action[13][6]="r3";
action[14][2]="r5";
action[14][3]="r5";
action[14][5]="r5";
action[14][6]="r5";
}
public void getCol(){
//获取终结符对应的列值
if(vt.equals("id"))
col=0;
else if(vt.equals("="))
col=1;
else if(vt.equals("+"))
col=2;
else if(vt.equals("*"))
col=3;
else if(vt.equals("("))
col=4;
else if(vt.equals(")"))
col=5;
else if(vt.equals(";"))
col=6;
else if(vt.equals("#"))
col=7;
}
public void getRol(){
//获取状态对应的行值
rol=Integer.parseInt(statu);
}
public void setTable(String s,String v){
//Syntax调用该函数,传递状态和终结符
this.statu=s;
this.vt=v;
this.getCol();
this.getRol();
//System.out.println("获取的状态和终结符是"+s+"-"+v+"对应的行列为"+rol+"-"+col);
}
public String getAction(){
//Syntax调用该函数,获取动作
//System.out.println("返回的动作是"+this.action[rol][col]);
return this.action[rol][col];
}
}