Definition of interpreter schema

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

Its class diagram is as follows:

 

Role description:

  1. AbstractExpression Abstract interpreter: Specific interpretation tasks are performed by individual implementation classes
  2. TerminalExpression: Implements the interpretive operation associated with elements ina grammar. Usually there is only one TerminalExpression in an interpreter schema, but there are multiple instances of different terminalizers
  3. NonterminalExpression: Each rule ina grammar corresponds to a NonterminalExpression. Non-terminal expressions increase according to the complexity of the logic, and in principle there is a non-terminal expression for every grammar rule
  4. Context Context Role

Abstract expression code:

 

Abstract expressions usually have only one method. Abstract expressions are the key to generating syntactic collections. Each syntactic collection performs the specified parsing task by means of recursive calls, which are eventually parsed by the smallest syntactic unit

Terminal expression code:

 

In general, terminal expressions are relatively simple and deal mainly with the conversion of scene elements and data

Non-terminal expressions:

 

Each non-terminal expression represents a grammar rules, and each grammar rules surrounding the grammar rules are only care about their results, so this leads to call each non-terminal expression non-terminal expression of their surrounding, and then in the end, the smallest expression grammar rules is the terminator, terminator, the concept of expression is so so, Can no longer participate in a grammar operation smaller than oneself

Scenario class code:

 

Typically, Client is a wrapper class. The result of the wrapper is to pass in a canonical syntax file that is parsed and returned by the parser, avoiding the coupling between the caller and the parser

Application of the interpreter pattern

Advantages of interpreter mode:

The interpreter is a simple syntax analysis tool. Its most notable advantage is extensibility. To modify the grammar rules, you only need to modify the corresponding non-terminal expressions, and to extend the grammar, you only need to add non-terminal expressions

Disadvantages of the interpreter pattern:

  1. The interpreter pattern causes class bloat. Each grammar needs to produce a non-terminal expression, and when the grammar rules are complicated, a large number of class files may be generated, which brings a lot of trouble for maintenance
  2. The interpreter pattern uses recursive invocation methods. Each non-terminal expression is concerned with its own expression, each expression needs to know the final result, and must be peeled off layer by layer. In both object-oriented and procedural languages, recursion is used when necessary, which makes debugging very complicated.
  3. Efficiency problem. Because of the large amount of looping and recursion used in the interpreter pattern, efficiency is an issue that cannot be ignored, especially when parsing complex, lengthy syntax

The interpreter pattern uses the following scenarios:

  1. Recurring problems can be solved using the interpreter pattern. For example, multiple application servers generate a large number of logs every day, and log files need to be analyzed and processed. The log formats of each server are different, but the data elements are the same. According to the interpreter, terminal expressions are the same, but non-terminal expressions need to be specified.
  2. A scenario where simple syntax needs to be explained. Why simple? Looking at non-mediated expressions, the more rules you have, the more complexity you have, and the recursive calls you have to make between classes. Think about how much patience and confidence you need to troubleshoot problems with calls between classes. As a result, the interpreter pattern is generally used to parse more standard character sets, such as SQL parsing, although this part is gradually being replaced by specialized tools

Try not to use the interpreter pattern in important modules, or maintenance can be a major problem. You can use shell, JRuby and other scripting languages to replace the interpreter pattern in your projects.


Interpreter mode is rarely used in actual system development, because it causes problems such as efficiency, performance and maintenance. 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 really have a specific type of “problem when the frequency is high enough”, ready to use the interpreter pattern, consider Expression4J, MESP, Jep open source analysis toolkit, is extremely powerful, and easy to use, efficiency is also not bad, realize most of the math no problem at all.