编译原理——赋值语句与简单表达式(六)

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];

	}


}


 

#include<iostream> #include<string> #include<cctype> using namespace std; #define INFEASIBLE -1 #define OVERFLOW -2 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef char SElemType; typedef char ElemType; struct STACK { SElemType * base; SElemType * top; int stacksize; }; int InitStack(STACK &S); char GetTop(STACK &S); int Push (STACK &S,SElemType e);//插入e为新的栈顶元素 int Pop (STACK &S,STACK &H,int m);//若栈不空,则删除S的栈顶元素 int EmptyStack(STACK &S);//判断栈S是否为空 int cmp(char a,char b);//判断运算符a的优先级是否高于运b void cmp(char a,char b,STACK &f,STACK &h);//判断运算符a和b的优先级 int main() { STACK h1; InitStack(h1);//定义并初始化逆序的逆波兰式栈h1 STACK h2; InitStack(h2);//定义并初始化正序的逆波兰式栈h2 STACK fh; InitStack(fh);//定义并初始化符号栈fh Push(fh,'#');//将#压入符合栈 string z; int a; kind1: cout<<"***************************************"<<endl; cout<<"请输入一个表达式,并以“#”结束:"<<endl; cout<<"***************************************"<<endl; cin>>z; for(int i=0;i<z.size ();i++)//对输入表达式的每个字符进行判断 { if(isalnum(z[i]))//如果当前字符是字母或数字 Push(h1,z[i]);//将当前字符压入逆序的逆波兰式栈h1 else if(z[i]=='(')//如果当前字符是"(" Push(fh,'(');//将当前字符压入符号栈fh else if(z[i]==')')//如果当前字符是")" { while(GetTop(fh)!='(')//一直删除符号栈fh的栈顶元素只至"(" Pop(fh,h1,1); //并将其压入逆序的逆波兰式栈h1 Pop(fh, 、、、、、、、、、、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值