Latest web development tutorials

Interpreter pattern

Interpreter pattern (Interpreter Pattern) provides a way to evaluate the expression syntax or language, which belongs to the type of model. This mode implements an expression interface that explain a particular context. This model is used in the SQL parsing, symbol processing engines.

Introduction

Intent: Given a language, define its grammar representation, and define an interpreter, the interpreter uses the logo to interpret sentences in the language.

Main Solution: For some fixed grammar constructing a sentence to explain interpreter.

When to use: If the frequency of a particular type of problem is sufficiently high, it may be worth each instance of the problem is expressed as a simple sentences in the language.So that you can build an interpreter, the interpreter to resolve the problem by interpreting these sentences.

How to fix: member syntax tree, the definition of terminator and nonterminal.

The key code: member class environment, some global information contained outside interpreter, usually HashMap.

Application examples: compiler, arithmetic expression evaluation.

Advantages: 1, better scalability, and flexible.2, added a new way to interpret the expression. 3, easy to implement simple grammar.

Disadvantages: 1, can take advantage of the scene is relatively small.2. For complex grammars more difficult to maintain. 3 explain the mode will cause the class to expand. 4 explain the mode using recursive method calls.

Usage scenarios: 1 can be interpreted a language sentences must be represented by an abstract syntax tree.2, some of the recurring problems can be a simple language to express. 3, a simple grammar to explain the scene.

Note: You can use relatively small scene, JAVA if you encounter can expression4J instead.

achieve

We will create and implement an interfaceExpressionExpression entity class interface. As defined in the context ofTerminalExpressionprincipal interpreter. Other classesOrExpression,AndExpression for creating modular expressions.

InterpreterPatternDemo,our demonstration class usesExpressionclass to create parsing rules and presentation expressions.

Interpreter pattern UML diagram

step 1

Create an expression interface.

Expression.java

public interface Expression {
   public boolean interpret(String context);
}

Step 2

Create entity class implements the above interfaces.

TerminalExpression.java

public class TerminalExpression implements Expression {
	
   private String data;

   public TerminalExpression(String data){
      this.data = data; 
   }

   @Override
   public boolean interpret(String context) {
      if(context.contains(data)){
         return true;
      }
      return false;
   }
}

OrExpression.java

public class OrExpression implements Expression {
	 
   private Expression expr1 = null;
   private Expression expr2 = null;

   public OrExpression(Expression expr1, Expression expr2) { 
      this.expr1 = expr1;
      this.expr2 = expr2;
   }

   @Override
   public boolean interpret(String context) {		
      return expr1.interpret(context) || expr2.interpret(context);
   }
}

AndExpression.java

public class AndExpression implements Expression {
	 
   private Expression expr1 = null;
   private Expression expr2 = null;

   public AndExpression(Expression expr1, Expression expr2) { 
      this.expr1 = expr1;
      this.expr2 = expr2;
   }

   @Override
   public boolean interpret(String context) {		
      return expr1.interpret(context) && expr2.interpret(context);
   }
}

Step 3

InterpreterPatternDemouse theExpressionclass to create rules and resolve them.

InterpreterPatternDemo.java

public class InterpreterPatternDemo {

   //规则:Robert 和 John 是男性
   public static Expression getMaleExpression(){
      Expression robert = new TerminalExpression("Robert");
      Expression john = new TerminalExpression("John");
      return new OrExpression(robert, john);		
   }

   //规则:Julie 是一个已婚的女性
   public static Expression getMarriedWomanExpression(){
      Expression julie = new TerminalExpression("Julie");
      Expression married = new TerminalExpression("Married");
      return new AndExpression(julie, married);		
   }

   public static void main(String[] args) {
      Expression isMale = getMaleExpression();
      Expression isMarriedWoman = getMarriedWomanExpression();

      System.out.println("John is male? " + isMale.interpret("John"));
      System.out.println("Julie is a married women? " 
      + isMarriedWoman.interpret("Married Julie"));
   }
}

Step 4

Verify output.

John is male? True
Julie is a married women? True