A custom implementation of the language - interpreter mode (six)

Interpreter pattern summarized 18.6

      Interpreter pattern provides a solution for the custom design and implementation of the language, which is used to define a set of grammar rules, and to interpret these sentences in the language set by the grammar rules. Although the interpreter mode frequency of use is not particularly high, but it is in regular expressions, XML documents or explain other fields have been widely used. Similar interpreter mode, there is the birth of a lot of source code based on abstract syntax tree processing tools, such as Eclipse in Eclipse AST , which can be used to represent Java language constructs, the user can extend its functionality, create your own grammar rule.

      1. The main advantage

      The main advantage of the mode explains the following:

      (1)  easy to change, and extensible grammar. The use of classes in the interpreter mode represented grammar rules of a language and the grammar may be changed or extended by inheritance mechanisms.

      (2) each of the provisions of the legislation it can be represented as a class, so you can easily implement a simple language.

      (3) implemented grammar easier. In the abstract syntax tree implementations each expression node class are similar, coding these classes are not particularly complex, the node also generates a class code by tools.

      (4) Add a new interpretation of the expression is more convenient. If you need to add new interpretation requires only a corresponding increase in the expression of a new terminator or non-terminator expression based expression, the expression of the original class code without modification, in line with "on-off principle."

      2. The main drawback

      The main drawback of the mode explains the following:

      (1) For complex grammars difficult to maintain. In interpreter mode, each rule at least need to define a class, so if a language contains too many grammar rules, like the number will increase dramatically, leading to difficult to manage and maintain the system, then you can consider using a parser, etc. instead interpreted embodiment mode.

      (2) a lower efficiency. Since a large number of cycles and a recursive call mode in the interpreter, so it is very slow in the interpretation of more complex sentences, and code debugging process is relatively cumbersome.

      3. Applicable scene

      In the case of the following mode may be considered using an interpreter:

      (1) can be interpreted a sentence languages must be represented by an abstract syntax tree.

      (2) some of the recurring problems can use a simple language to express.

      (3) the grammar of a language is relatively simple.

      (4) the implementation of efficiency is not the key issue. [Note: not usually efficient interpreter achieved by direct interpretation of the abstract syntax tree, but the need to convert them into other forms, using an interpreter mode execution efficiency is not high. ]

 

Exercise

Sunny software companies prefer to give the database backup and the simultaneous development of a simple database synchronization instruction, the backup of the database structure and data by an instruction, e.g., the input command " COPY the FROM srcdb the TO desDB the VIEW " shows a database srcdb all of view (view) objects are copied to a database desDB ; input command " the mOVE srcdb the tO desDB the FROM tABLE Student " shows a database srcdb the Student table to move database desDB . Test patterns using an interpreter to design and implement the database synchronization instruction.

[Note: This exercise is a small tool in 2010, when I design mode House to a company that is developing! ]

      Interpreter pattern can be said that all design modes more difficult, use a lower frequency mode, if you stop and read these articles are, I believe you should have a more comprehensive explanation mode understanding, welcome to share and discuss with me.

      Thank you insist on reading this article about six interpreter mode laughing out loud!

Published 25 original articles · won praise 1 · views 769

Guess you like

Origin blog.csdn.net/qq_42770949/article/details/104953013