标签:padding var 计算器 字符串匹配 模型 one tran alt not
public static int add(int a,int b){ return a+b; } public static int add(int a,int b,int c){ return a+b-c; }
package function; @FunctionalInterface public interface Function1<A,B,C,D,E,F,G, R> { R xxxxx(A a,B b,C c,D d,E e,F f,G g); }
范式基本规则 |
::= 表示定义,由什么推导出
尖括号 < > 内为必选项;
方括号 [ ] 内为可选项;
大括号 { } 内为可重复0至无数次的项;
圆括号 ( ) 内的所有项为一组,用来控制表达式的优先级
竖线 | 表示或,左右的其中一个
引号内为字符本身,引号外为语法(比如 ‘for‘表示关键字for )
|
expression:=value | plus | minus
plus:=expression ‘+’ expression
minus:=expression ‘-’ expression
value:=integer
|
值的类型为整型数
有加法规则和减法规则
表达式可以是一个值,也可以是一个plus或者minus
而plus和minus又是由表达式结合运算符构成
可以看得出来,有递归嵌套的概念
|
expression:=value | plus | minus
plus:=expression ‘+’ expression
minus:=expression ‘-’ expression
value:=integer
|
package interpret; public abstract class AbstractExpression { public abstract int interpret(); }
package interpret; public class Value extends AbstractExpression { private int value; Value(int value){ this.value = value; } @Override public int interpret() { return value; } }
package interpret; public class Plus extends AbstractExpression { private AbstractExpression left; private AbstractExpression right; Plus(AbstractExpression left, AbstractExpression right) { this.left = left; this.right = right; } @Override public int interpret() { return left.interpret() + right.interpret(); } }
package interpret; public class Minus extends AbstractExpression { private AbstractExpression left; private AbstractExpression right; Minus(AbstractExpression left, AbstractExpression right) { this.left = left; this.right = right; } @Override public int interpret() { return left.interpret() - right.interpret(); } }
package interpret; public class Client { public static void main(String[] args) { AbstractExpression expression = new Minus( new Plus(new Plus(new Plus(new Value(1), new Value(2)), new Value(3)), new Value(4)), new Value(5)); System.out.println(expression.interpret()); } }
int f(int x) { if (1 == x) { return x; } else { return x+f(x-1); } }
package interpret; public class Variable extends AbstractExpression{ private String name; private Integer value; Variable(String name,Integer value){ this.name = name; this.value = value; } public void setValue(Integer value) { this.value = value; } @Override public int interpret() { return value; } }
package interpret; public class Client { public static void main(String[] args) { //定义变量X和Y,初始值都为0 Variable variableX = new Variable("x", 0); Variable variableY = new Variable("y", 0); //计算公式为: X+Y+X-1 AbstractExpression expression2 = new Minus(new Plus(new Plus(variableX, variableY), variableX), new Value(1)); variableX.setValue(1); variableY.setValue(3); System.out.println(expression2.interpret()); variableX.setValue(5); variableY.setValue(6); System.out.println(expression2.interpret()); } }
package interpret.refactor; public abstract class AbstractExpression { public abstract int interpret(Context ctx); }
package interpret.refactor; public class Value extends AbstractExpression { private int value; Value(int value) { this.value = value; } @Override public int interpret(Context ctx) { return value; } @Override public String toString() { return new Integer(value).toString(); } }
package interpret.refactor; public class Variable extends AbstractExpression { private String name; Variable(String name) { this.name = name; } @Override public int interpret(Context ctx) { return ctx.getValue(this); } @Override public boolean equals(Object obj) { if (obj != null && obj instanceof Variable) { return this.name.equals( ((Variable) obj).name); } return false; } @Override public int hashCode() { return this.toString().hashCode(); } @Override public String toString() { return name; } }
package interpret.refactor; public class Plus extends AbstractExpression { private AbstractExpression left; private AbstractExpression right; Plus(AbstractExpression left, AbstractExpression right) { this.left = left; this.right = right; } @Override public int interpret(Context ctx) { return left.interpret(ctx) + right.interpret(ctx); } @Override public String toString() { return "(" + left.toString() + " + " + right.toString() + ")"; } }
package interpret.refactor; public class Minus extends AbstractExpression { private AbstractExpression left; private AbstractExpression right; Minus(AbstractExpression left, AbstractExpression right) { this.left = left; this.right = right; } @Override public int interpret(Context ctx) { return left.interpret(ctx) - right.interpret(ctx); } @Override public String toString() { return "(" + left.toString() + " - " + right.toString() + ")"; } }
package interpret.refactor; import java.util.HashMap; import java.util.Map; public class Context { private Map<Variable, Integer> map = new HashMap<Variable, Integer>(); public void assign(Variable var, Integer value) { map.put(var, new Integer(value)); } public int getValue(Variable var) { Integer value = map.get(var); return value; } }
package interpret.refactor; public class Client { public static void main(String[] args) { Context ctx = new Context(); Variable a = new Variable("a"); Variable b = new Variable("b"); Variable c = new Variable("c"); Variable d = new Variable("d"); Variable e = new Variable("e"); Value v = new Value(1); ctx.assign(a, 1); ctx.assign(b, 2); ctx.assign(c, 3); ctx.assign(d, 4); ctx.assign(e, 5); AbstractExpression expression = new Minus(new Plus(new Plus(new Plus(a, b), c), d), e); System.out.println(expression + "= " + expression.interpret(ctx)); } }
/** * 解析字符串,构造抽象语法树 方法只是为了理解:解释器模式 方法默认输入为合法的字符串,没有考虑算法优化、效率或者不合法字符串的异常情况 * * @param sInput 合法的加减法字符串 比如 1+2+3 */ public static AbstractExpression getAST(String sInput) { //接收字符串参数形如 "1+2-3" //将字符串解析到List valueAndSymbolList中存放 List<String> valueAndSymbolList = new ArrayList<>(); //先按照 加法符号 + 拆分为数组,以每个元素为单位使用 +连接起来存入List //如果以+ 分割内部还有减法符号 - 内部以减法符号- 分割 //最终的元素的形式为 1,+,2,-,3 String[] splitByPlus = sInput.split("\\+"); for (int i = 0; i < splitByPlus.length; i++) { if (splitByPlus[i].indexOf("-") < 0) { valueAndSymbolList.add(splitByPlus[i]); } else { String[] splitByMinus = splitByPlus[i].split("\\-"); for (int j = 0; j < splitByMinus.length; j++) { valueAndSymbolList.add(splitByMinus[j]); if (j != splitByMinus.length - 1) { valueAndSymbolList.add("-"); } } } if (i != splitByPlus.length - 1) { valueAndSymbolList.add("+"); } } //经过前面处理元素的形式为 1,+,2,-,3 //转换为抽象语法树的形式 AbstractExpression leftExpression = null; AbstractExpression rightExpression = null; int k = 0; while (k < valueAndSymbolList.size()) { if (!valueAndSymbolList.get(k).equals("+") && !valueAndSymbolList.get(k).equals("-")) { rightExpression = new Value(Integer.parseInt(valueAndSymbolList.get(k))); if (leftExpression == null) { leftExpression = rightExpression; } } k++; if (k < valueAndSymbolList.size()) { rightExpression = new Value(Integer.parseInt(valueAndSymbolList.get(k + 1))); if (valueAndSymbolList.get(k).equals("+")) { leftExpression = new Plus(leftExpression, rightExpression); } else if (valueAndSymbolList.get(k).equals("-")) { leftExpression = new Minus(leftExpression, rightExpression); } k++; } } return leftExpression; }
解释器模式 Interpreter 行为型 设计模式(十九)
标签:padding var 计算器 字符串匹配 模型 one tran alt not
原文地址:https://www.cnblogs.com/noteless/p/10107675.html