⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t_rp.java

📁 中山大学编译原理课程的一个实验
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package symbols;import exceptions.ExpressionException;import exceptions.FunctionCallException;import exceptions.MissingOperandException;import exceptions.SyntacticException;import exceptions.TypeMismatchedException;import java.util.ArrayList;/** * * @author Beeven */public class T_RP extends Terminal implements TerminalReduce{    public Expr reduce(ArrayList<Expr> stack) throws ExpressionException {        int post=stack.size()-1;        Expr a= stack.get(post);  // )        Expr o= stack.get(post-1); //bool or ae or ael //error if operand        Expr b= stack.get(post-2); // ( or ,        Expr c= stack.get(post-3);//$ or func or ArithExpr                if(o.type==Type.lp || o.type==Type.comma)        {            throw new MissingOperandException();        }        else if(o instanceof BoolExpr) // (be) to be        {            stack.remove(post);            stack.remove(post-2);            return stack.get(post-2);        }        else if(o instanceof ArithExprList)        {            if(!(c instanceof ArithExpr)) throw new TypeMismatchedException();            ArithExprList ael=new ArithExprList((ArithExpr)c,(ArithExprList)o);            Expr d= stack.get(post-4);            Expr f=stack.get(post-5);            ArithExpr ae=null;            if(d.type==Type.lp && f.type==Type.func) // max(ae,ael) to ae            {                switch(f.tag)                {                    case Tag.MAX:                        ae=new ArithExpr(ael.max); break;                    case Tag.MIN:                        ae=new ArithExpr(ael.min); break;                    default:                        throw new FunctionCallException();                }                stack.remove(post);  //)                stack.remove(post-1);//ael                stack.remove(post-2);//,                stack.remove(post-3);//ae                stack.remove(post-4);//(                stack.remove(post-5);//max min                stack.add(ae);                return ae;            }            else                          // ae,ael) to ael)            {                stack.remove(post-1);                stack.remove(post-2);                stack.set(post-3, ael);                return ael;            }        }        else if(o instanceof ArithExpr)        {            if(b.type==Type.comma) // ae,ae) to ae,ael)            {                ArithExprList ael = new ArithExprList((ArithExpr)o);                stack.set(post-1, ael);                return ael;            }            else if(c.type==Type.func) //sin(ae)            {                if(!(o instanceof ArithExpr)) throw new TypeMismatchedException();                ArithExpr ae=null;                switch(c.tag)                {                    case Tag.SIN:                        ae=new ArithExpr(Math.sin(((ArithExpr)o).value)); break;                    case Tag.COS:                        ae=new ArithExpr(Math.cos(((ArithExpr)o).value)); break;                    default:                        throw new MissingOperandException();                }                stack.remove(post);                stack.remove(post-1);                stack.remove(post-2);                stack.remove(post-3);                stack.add(ae);                return ae;            }            else //(ae) to ae            {                stack.remove(post);                stack.remove(post-2);                return stack.get(post-2);            }        }        throw new SyntacticException();    }}

⌨️ 快捷键说明

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