The Java Interpreter pattern (Interpreter pattern) is a behavioral design pattern that defines the syntax representation of a language and defines an interpreter to interpret the syntax.
The core of this pattern is the interpreter (Interpreter), which defines an expression interface and a specific expression implementation class. The expression interface defines an interpretation method, and the specific expression implementation class implements the interpretation method. Use Used to explain grammar.
The Java interpreter mode includes the following four roles:
Abstract Expression (Abstract Expression): defines what an interpreter needs to implement The interface usually contains an interpreter method (interpreter) for interpreting expressions.
Terminal Expression (Terminal Expression): implements the interpreter method of the abstract expression interface, used for interpretation Terminal symbols of a language, such as variables, constants, etc.
Non-Terminal Expression: implements the interpretation method of the abstract expression interface, usually consisting of multiple Terminal expressions are combined and used to interpret non-terminal symbols in the language, such as arithmetic operators, logical operators, etc.
Environment (Context): Contains the language that needs to be interpreted Contextual information, such as variables, constants, etc. In the interpreter mode, the environment object is usually passed as a parameter to the interpreter object.
The following is a simple Examples of expressions used to explain addition and subtraction
Abstract expression
public interface Expression { /** * 解释表达式 * * @return */ int interpreter(); }
Terminal expression
public class NumberExpression implements Expression{ private int num; public NumberExpression(int num) { this.num = num; } /** * 解释表达式 * * @return */ @Override public int interpreter() { return num; } }
Non-terminal expression
public class AddExpression implements Expression { /** * 左表达式 */ private Expression leftExpression; /** * 右表达式 */ private Expression rightExpression; public AddExpression(Expression leftExpression, Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; } /** * 解释表达式 * * @return */ @Override public int interpreter() { return leftExpression.interpreter() + rightExpression.interpreter(); } } public class SubtractExpression implements Expression { /** * 左表达式 */ private Expression leftExpression; /** * 右表达式 */ private Expression rightExpression; public SubtractExpression(Expression leftExpression, Expression rightExpression) { this.leftExpression = leftExpression; this.rightExpression = rightExpression; } /** * 解释表达式 * * @return */ @Override public int interpreter() { return leftExpression.interpreter() - rightExpression.interpreter(); } }
Test
public class Demo { public static void main(String[] args) { // 创建一个复杂表达式,用于计算5+3-2+1的结果 Expression expression = new AddExpression( new SubtractExpression( new AddExpression( new NumberExpression(5), new NumberExpression(3)), new NumberExpression(2)), new NumberExpression(1)); // 使用解释器模式来解释表达式,并输出计算结果 System.out.println(expression.interpreter()); } }
In the above example, we defined an expression interface Expression (abstract expression) and two specific expression implementation classes AddExpression (non-terminal symbol expression) and SubtractExpression (non-terminal expression). In the AddExpression and SubtractExpression classes, we implemented the interpreter method respectively to interpret addition and subtraction expressions, and defined a NumberExpression (terminal expression) to implement Expression The interpreter method is used to interpret numeric expressions. In the above example, the environment object is not explicitly defined, but the environment is simulated by creating expression objects and combining them.
Strong scalability: The interpreter mode can implement new behaviors by extending expressions and grammar rules in the language. For example: new terminal expressions and non-terminal expressions can be added Terminal expressions to implement more complex grammar rules.
Easy to implement: The interpreter mode is relatively simple to implement, and only needs to implement abstract expressions and concrete expression classes.
Easy to change grammar rules: Since the interpreter mode represents grammar rules as objects, you can change the grammar rules by modifying the way the interpreter objects are combined.
Requires a large number of classes: Implementing a complex language may require defining a large number of classes, which will increase the complexity of the code and maintenance costs.
Poor readability: Because the interpreter mode represents syntax rules as objects, the code may not be very readable, especially for developers who are not familiar with the mode.
When a language that needs to interpret and execute specific grammatical rules, such as mathematical expressions, regular expressions, etc.
When it is necessary to extend the syntax rules of the language, such as adding new operators or commands.
When it is necessary to encapsulate and execute complex interpretation logic in the code, such as compilation Interpreter, parser, etc.
Try not to use the interpreter mode in important modules, otherwise maintenance will be a big problem. In the project You can use shell, JRuby, Groovy and other scripting languages to replace the interpreter mode to make up for the shortcomings of Java compiled languages.
The above is the detailed content of How to implement interpreter mode for custom language and expression parsing in Java. For more information, please follow other related articles on the PHP Chinese website!