1. Interpreter Mode definition
Given a language, defines a representation of its grammar, and defines an interpreter that uses the representation to interpret a sentence in a language.
Description: The interpreter pattern is designed into grammar rules and abstract syntax trees.
2, the structure of the interpreter mode
The interpreter mode contains four roles:
1 abstract expression (abstractexpress): declares an abstract interpretation operation, which is a public parent class for all non-terminal expressions and non-terminal expressions.
2) non-terminal expression (terminalexpress): is a subclass of an abstract expression that implements the interpretation operation associated with the non-terminal in the grammar, and each non-terminal in the sentence is an instance of the class.
For non-terminal expressions, the code is relatively complex and can be combined into more complex structures through a non-terminal expression.
3) A non-terminal expression (nonterminalexpress): A subclass of an abstract expression that implements the interpretation of a non-terminal expression in a grammar. A non-terminal expression can be included in a non-terminal expression, or it can contain a non-terminal expression, and its interpretation operations are generally implemented by recursion.
4 Environment Class (context): Also known as the contextual class, used to store some global information outside the interpreter. In this class, you typically include a collection object of the type Hashtable or list, which stores a series of public information, such as the mapping of variable names to values (Key/value).
3, the advantages of the interpreter model
1) Easy to change or expand the grammar;
2 The realization of grammar is easier;
3) conforms to the opening and closing principle.
4, the shortcomings of the interpreter model
1 difficult to maintain for complex grammar;
2 implementation efficiency is low.