基本介绍:
给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
1. 文法:即语法规则。在解释器模式中每一个语法都将对应一个解释器对象,用来处理相应的语法规则。它对于扩展、改变文法以及增加新的文法规则都很方便。
2. 解释器模式描述了如何为简单的语言定义一个文法,如何在该语言中表示一个句子,以及如何解释这些句子。
3. 在解释器模式中可以通过一种称之为抽象语法树(Abstract Syntax Tree, AST)的图形方式来直观地表示语言的构成,每一棵抽象语法树对应一个语言实例
示例代码:
package org.brando; /** * * 类说明: 测试类 * @author Brando 2018年3月29日 下午2:03:59 */ public class Launcher { public static void main(String[] args) { Calculator calculator = new Calculator("5 - 2 + 3"); System.out.println(calculator.calculate()); } }
package org.brando; /** * * 类说明: 算数解释器-抽象类 * @author Brando 2018年5月9日 上午11:02:47 */ public abstract class ArithmeticExpression { /** * * 方法说明: 解释函数. * @author Brando 2018年5月9日 上午11:03:11 * @return */ public abstract int interpret(); }
package org.brando; /** * * 类说明: 数字解释器 * @author Brando 2018年5月9日 上午11:04:22 */ public class NumExpression extends ArithmeticExpression { /**上下文**/ private int context; public NumExpression(int context) { this.context = context; } @Override public int interpret() { return context; } }
package org.brando; /** * * 类说明: 操作符解释器 * @author Brando 2018年5月9日 上午11:04:22 */ public abstract class OperatorExpression extends ArithmeticExpression { protected ArithmeticExpression arithmeticExpression1; protected ArithmeticExpression arithmeticExpression2; public OperatorExpression(ArithmeticExpression arithmeticExpression1, ArithmeticExpression arithmeticExpression2) { this.arithmeticExpression1 = arithmeticExpression1; this.arithmeticExpression2 = arithmeticExpression2; } }
package org.brando; /** * * 类说明: 加法解释器 * @author Brando 2018年5月9日 上午11:04:22 */ public class AdditionExpression extends OperatorExpression { public AdditionExpression(ArithmeticExpression arithmeticExpression1, ArithmeticExpression arithmeticExpression2) { super(arithmeticExpression1, arithmeticExpression2); } @Override public int interpret() { return arithmeticExpression1.interpret() + arithmeticExpression2.interpret(); } }
package org.brando; /** * * 类说明: 减法解释器 * @author Brando 2018年5月9日 上午11:04:22 */ public class SubtractionExpression extends OperatorExpression { public SubtractionExpression(ArithmeticExpression arithmeticExpression1, ArithmeticExpression arithmeticExpression2) { super(arithmeticExpression1, arithmeticExpression2); } @Override public int interpret() { return arithmeticExpression1.interpret() - arithmeticExpression2.interpret(); } }
package org.brando; import java.util.Stack; /** * * 类说明: 业务逻辑类 * @author Brando 2018年5月9日 下午3:15:04 */ public class Calculator { /**生成的语法树**/ protected Stack<ArithmeticExpression> mArithmeticExpressionStack = new Stack<ArithmeticExpression>(); public Calculator(String expression) { ArithmeticExpression arithmeticExpression1, arithmeticExpression2; String[] elements = expression.split(" "); for (int i = 0; i < elements.length; i++) { switch (elements[i]) { case "+": arithmeticExpression1 = mArithmeticExpressionStack.pop(); arithmeticExpression2 = new NumExpression(Integer.valueOf(elements[++i])); mArithmeticExpressionStack.push(new AdditionExpression(arithmeticExpression1, arithmeticExpression2)); break; case "-": arithmeticExpression1 = mArithmeticExpressionStack.pop(); arithmeticExpression2 = new NumExpression(Integer.valueOf(elements[++i])); mArithmeticExpressionStack.push(new SubtractionExpression(arithmeticExpression1, arithmeticExpression2)); break; default: mArithmeticExpressionStack.push(new NumExpression(Integer.valueOf(elements[i]))); break; } } } /** * * 方法说明: 执行语法树. * @author Brando 2018年5月9日 下午4:14:29 * @return */ public int calculate() { ArithmeticExpression exp = mArithmeticExpressionStack.pop(); int res = exp.interpret(); return res; } }