23 design patterns (15) Java Interpreter pattern

23 design patterns Part 14: Java Interpreter pattern

Definition: given a language, define a representation of its grammar and define an interpreter that uses the representation to interpret sentences in the language.

Type: behavior class pattern

Class diagram:

Interpreter mode is a less used mode, and I haven't used this mode before. Let's take a look at the interpreter mode.

Structure of Interpreter pattern

Abstract interpreter: declare an abstract interface (or abstract class) to be implemented by all concrete expressions. The interface is mainly an interpret () method, which is called interpretation operation. The specific interpretation task is completed by its implementation classes, and the specific interpreter is completed by the terminator interpreter terminalexpression and the non terminator interpreter nonterminalexpression respectively.

Terminator expression: implements the interpretation operation associated with elements in grammar. Usually, there is only one terminator expression in an interpreter mode, but there are multiple instances corresponding to different terminators. Half of the terminator is an operation unit in grammar. For example, there is a simple formula r = R1 + R2, in which R1 and R2 are terminators, and the corresponding interpreter that parses R1 and R2 is terminator expression.

Non terminator expression: each rule in grammar corresponds to a non terminator expression. Non terminator expression is generally an operator or other keyword in grammar. For example, in formula r = R1 + R2, + is a non terminator, and the interpreter parsing + is a non terminator expression. Non terminator expressions increase according to the complexity of logic. In principle, each grammar rule corresponds to a non terminator expression.

Environment role: the task of this role is generally used to store the specific values corresponding to each terminator in the grammar, such as R = R1 + R2. We assign 100 to R1 and 200 to R2. This information needs to be stored in the environment role. In many cases, it is enough for us to use map to act as the environment role.

code implementation

The code part of grammar recursion needs to be implemented according to the specific situation, so it is not reflected in the code. Abstract expressions are the key to generating syntax sets. Each non terminator expression interprets a smallest syntax unit, and then combines these syntax units into a complete grammar through recursion, which is the interpreter mode.

Advantages and disadvantages of interpreter mode

Interpreter is a simple syntax analysis tool. Its most significant advantage is extensibility. To modify syntax rules, you only need to modify the corresponding non terminator. If you extend syntax, you only need to add non terminator classes. However, the Interpreter pattern will cause class expansion. Each syntax needs to generate a non terminator expression. When the syntax rules are complex, a large number of class files may be generated, which brings a lot of trouble for maintenance. At the same time, due to the recursive call method, each non terminator expression only cares about the expression related to itself. Each expression needs to know the final result and must be recursive. Recursion is not recommended in both object-oriented and process oriented languages. Due to the use of a large number of loops and recursion, efficiency is a problem that can not be ignored. Especially when it is used to explain a complex and lengthy syntax, the efficiency is unbearable.

Applicable scenarios of interpreter mode

The interpreter mode can be used in the following cases: there is a simple syntax rule, such as an SQL statement. If we need to perform RM conversion according to the SQL statement, we can use the interpreter mode to interpret the statement.

For some repeated problems, such as addition, subtraction, multiplication and division, the formulas are different every time, sometimes a + B-C * D, sometimes a * B + C-D, etc. the formulas are changeable, but they are connected by four non terminators of addition, subtraction, multiplication and division. At this time, we can use the interpreter mode.

matters needing attention

The interpreter mode is really a less used mode, because its maintenance is too troublesome. Imagine that lumps of non terminator interpreters are difficult to understand its logic if they do not know the rules of grammar in advance, or the grammar is very simple. Interpreter pattern is rarely used in actual system development, because it will cause problems such as efficiency, performance and maintenance.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>