t_addminus.java

来自「中山大学编译原理课程的一个实验」· Java 代码 · 共 41 行

JAVA
41
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package symbols;import exceptions.MissingOperandException;import exceptions.TypeMismatchedException;import java.util.ArrayList;/** * * @author Beeven */public class T_ADDMINUS extends Terminal implements TerminalReduce{    public Expr reduce(ArrayList<Expr> stack) throws TypeMismatchedException, MissingOperandException {        int post=stack.size()-1;        Expr a=stack.get(post-2);        Expr o=stack.get(post-1);        Expr b=stack.get(post);        if((b.type==Type.addminus)) throw new MissingOperandException();        if(a instanceof BoolExpr)            throw new TypeMismatchedException();        else if(!(a instanceof ArithExpr))            throw new MissingOperandException();        ArithExpr ae;        if(o.tag==Tag.ADD)            ae=new ArithExpr(((ArithExpr)a).value+((ArithExpr)b).value);        else            ae=new ArithExpr(((ArithExpr)a).value-((ArithExpr)b).value);        stack.remove(post);        stack.remove(post-1);        stack.remove(post-2);        stack.add(ae);        return ae;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?