编译原理——变量声明语句(七)

Action动作表。

代码:

Action.java

package per.eyuan.util;

public class Action {
	/*
	 * SLR的action表,接受传递过来的状态和终结符,
	 * 返回action表中对应的动作r(归约)或者s(移进),
	 * Syntax,根据动作做相应的操作。
	 */
	private String action[][]=new String[10][6];
	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][1]="s3";
		action[0][2]="s4";
		action[1][5]="acc";
		action[2][3]="s6";
		action[2][4]="s5";
		action[3][0]="s7";
		action[4][0]="s8";
		action[5][5]="r1";
		action[6][0]="s9";
		action[7][3]="r2";
		action[7][4]="r2";
		action[8][3]="r3";
		action[8][4]="r3";
		action[9][3]="r4";
		action[9][4]="r4";
	}
	public void getCol(){
		//获取终结符对应的列值
		if(vt.equals("id"))
			col=0;
		else if(vt.equals("int"))
			col=1;
		else if(vt.equals("float"))
			col=2;
		else if(vt.equals(","))
			col=3;
		else if(vt.equals(";"))
			col=4;
		else if(vt.equals("#"))
			col=5;
	}
	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];

	}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值