The Interpreter Mode of Zen of Design Mode

1. Definition of interpreter mode

The interpreter mode is a solution that parses according to the specified grammar. It is rarely used in current projects. Its definition is as follows:
given a language, define a representation of its grammar, and define an interpreter, which The processor uses this representation to interpret sentences in the language.

Interpreter pattern general class:

(1) AbstractExpression-abstract interpreter

The specific interpretation task is completed by each implementation class, and the specific interpreter is completed by TerminalExpression and Non-terminalExpression

(2) TerminalExpression-terminal expression

To achieve the interpretation operation associated with the elements in the grammar, usually there is only one terminal expression in an interpreter mode, but there are multiple instances corresponding to different terminals. Specifically, our example is the Var-Expression class. Each terminal in the expression generates a VarExpression object on the stack.

(3) NonterminalExpression-non-terminal expression

Each rule in the grammar corresponds to a non-terminal expression. Specifically, our example is that the addition and subtraction rules correspond to the two classes AddExpression and SubExpression. Non-terminal expressions increase according to the complexity of logic. In principle, each grammar rule corresponds to a non-terminal expression.

(4) Context-environment role

Specific to our example is to use Hash-Map instead.

Second, the application of interpreter mode

1. Advantages of interpreter mode

The interpreter is a simple grammatical analysis tool. Its most obvious advantage is its scalability. To modify the grammar rules, you only need to modify the corresponding non-terminal expression. If you extend the grammar, you only need to add the non-terminal symbol class.

2. Disadvantages of the interpreter mode

(1) The interpreter mode will cause class expansion

Each grammar must produce a non-terminal expression. When the grammar rules are more complicated, a large number of class files may be generated, which causes a lot of trouble for maintenance.

(2) The interpreter mode uses a recursive call method

Each non-terminal expression only cares about the expressions related to itself, and it must be peeled off layer by layer. Whether it is a process-oriented language or an object-oriented language, recursion is used under necessary conditions, which leads to debugging very complicated. Think about it, if you want to troubleshoot a grammatical error, do we want to debug with a breakpoint until the smallest grammatical unit.

(3) Efficiency issues

Because the interpreter mode uses a lot of loops and recursion, efficiency is a problem that cannot be ignored, especially when it is used to parse complex and lengthy grammars, the efficiency is unbearable.

3. Scenes used in interpreter mode

(1) Repeated problems can use the interpreter mode

For example, multiple application servers generate a lot of logs every day, and the log files need to be analyzed and processed. Because the log format of each server is different, but the data elements are the same, according to the interpreter, the terminal expressions are all the same , But non-terminal expressions need to be formulated. In this case, the problem can be solved once and for all through procedures.

(2) A simple grammar needs explanation

Why is it simple?
Looking at non-terminal expressions, the more grammar rules, the higher the complexity, and the recursive calls between classes. Think about what kind of patience and confidence you need to troubleshoot when calling between multiple classes. Therefore, the interpreter mode is generally used to parse more standard character sets, such as SQL syntax analysis, but this part is gradually replaced by special tools.

4. Notes on interpreter mode

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 lack of Java compiled languages. We use JRuby for calculation processing in a bank's analytical project, avoiding the four operations of the interpreter mode, and the performance and performance are good in all aspects.

3. Best Practice

The interpreter mode is very rarely used in actual system development, because it will cause problems such as efficiency, performance, and maintenance. Generally, it can be found in large and medium-sized framework projects, such as some data analysis tools, report design tools, Scientific computing tools, etc. If you do encounter "a particular type of problem occurs frequently enough", when preparing to use the interpreter mode, you can consider the open source analysis toolkits such as Expression4J, MESP, Jep, etc. , And it's very easy to use, and the efficiency is not bad. There is no problem in implementing most mathematical operations. There is no need to write an interpreter from scratch.
Code example:
https://github.com/developers-youcong/DesignPatternPractice/tree/master/Interpreter

Guess you like

Origin www.cnblogs.com/youcong/p/12724877.html