“This is the 25th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

Java interpreter schema

The Interpreter Pattern, which provides a way to evaluate the syntax or expressions of a language, is a behavioral Pattern. This pattern implements an expression interface that interprets a particular context. This pattern is used in SQL parsing, symbol processing engines, and so on.

introduce

Intent: Given a language, define its grammatical representation and define an interpreter that uses this identifier to interpret sentences in the language.

Main solution: for some fixed grammar to build an interpreter to explain sentences.

When to use: If a particular type of problem occurs often enough, it may be worthwhile to express individual instances of the problem as sentences in a simple language. This allows you to build an interpreter that solves the problem by interpreting the sentences.

How to solve it: Build a syntax tree and define terminals and non-terminals.

Key code: Build environment classes that contain some global information outside of the interpreter, typically a HashMap.

Application example: compiler, operation expression calculation.

Advantages: (1) Good scalability and flexibility. ② New ways of interpreting expressions are added. ③ Easy to implement simple grammar.

Disadvantages: ① Less available scenarios. ② It is difficult to maintain complex grammar. ③ Interpreter mode causes class bloat. ④ The interpreter mode adopts recursive call method.

Use scenarios: ① You can represent a sentence in a language that needs to be interpreted as an abstract syntax tree. ② Some recurring problems can be expressed in a simple language. ③ A scene where simple grammar needs to be explained.

Note: There are few available scenarios, and JAVA can use expression4J instead.

implementation

We will create an interface Expression and an entity class that implements the Expression interface. Defines the TerminalExpression class that is the main interpreter in the context. Other classes OrExpression and AndExpression are used to create combinative expressions.

InterpreterPatternDemo, our demo class uses the Expression class to create rules and parse demo expressions.

Step 1

Create an expression interface.

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

Step 2

Create an entity class that implements the above interface.

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; }}Copy the code
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); }}Copy the code
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); }}Copy the code

Step 3

The InterpreterPatternDemo uses the Expression class to create rules and parse them.

Public Class InterpreterPatternDemo {// Rule: Public static Expression getMaleExpression(){Expression Robert = new TerminalExpression("Robert"); Expression john = new TerminalExpression("John"); return new OrExpression(robert, john); } // Rule: Julie is a married female 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")); }}Copy the code

Step 4

Execute the program and output the result:

John is male? true
Julie is a married women? true
Copy the code