23 design modes (14): Interpreter Mode

Source: Internet
Author: User

Definition:Given a language, it defines a representation of its syntax and an interpreter that uses this representation to explain sentences in the language.

Type:Behavior mode

Class diagram:

The interpreter mode is rarely used, and I have never used this mode before. Let's take a look at the interpreter mode.

 

Interpreter mode structure

  • Abstract Interpreter: declares an abstract interface (or abstract class) that must be implemented by all specific expressions. An interface is mainly an interpret () method, which is called an interpreted operation. The specific interpretation task is completed by its implementation classes. The specific interpreter is completed by the terminalexpression and nonterminalexpression, respectively.
  • Terminator expression: implements the interpretation operation associated with the elements in the grammar. Generally, there is only one Terminator expression in an interpreter mode, but there are multiple instances that correspond to different terminologies. Half of the Terminator is the unit of operation in the grammar. For example, there is a simple formula r = R1 + R2 in which R1 and R2 are the Terminator, the interpreter for parsing R1 and R2 is the terminator expression.
  • Non-terminator expression: each rule in the grammar corresponds to a non-terminator expression. Non-terminator expressions are generally operators or other keywords in the grammar, for example, in the formula r = R1 + R2, + is a non-Terminator, And the parser + is a non-terminator expression. Non-terminator expressions increase based on the complexity of the 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 of each terminator in the grammar. For example, if 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 to use map to act as the Environment role.

Code Implementation

Class context {} abstract class expression {public abstract object interpreter (context CTX);} class terminalexpression extends expression {public object interpreter (context CTX) {return NULL ;}} class nonterminalexpression extends expression {public nonterminalexpression (expression... expressions) {} public object interpreter (context CTX) {return NULL ;}} public class client {public static void main (string [] ARGs) {string expression = ""; char [] chararray = expression. tochararray (); Context CTX = new context (); stack <expression> stack = new stack <expression> (); For (INT I = 0; I <chararray. length; I ++) {// for syntax judgment, recursive call} expression exp = stack. pop (); exp. interpreter (CTX );}}

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 a syntax set. Each non-terminator expression interprets a minimum syntax unit, and then recursively combines these syntax units into a complete syntax. This is the interpreter mode.

 

Advantages and disadvantages of interpreter Mode

The interpreter is a simple syntax analysis tool. Its most significant advantage is scalability. to modify a syntax rule, you only need to modify the corresponding non-Terminator. If you want to extend the syntax, you only need to add a non-terminator class.

However, the interpreter mode causes class expansion. Each syntax requires a non-terminator expression. When syntax rules are complex, a large number of class files may be generated, it brings a lot of trouble to maintenance. At the same time, because recursive call methods are used, each non-terminator expression only cares about its own expressions. Each expression needs to know the final result and must be recursive, whether it is an object-oriented language or a process-oriented language, recursion is not recommended. Because a large number of loops and Recursion are used, efficiency cannot be ignored. Especially when it is used to explain a complex and lengthy parsing syntax, efficiency is intolerable.

 

Application 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 rmconversion Based on the SQL statement, we can use the interpreter mode to explain the statement.
  • Some repeated problems, such as addition, subtraction, multiplication, division, and four arithmetic operations, but the formula is different each time, sometimes a + B-c * D, sometimes a * B + c-d, and so on. The formulas are ever-changing, but they are all connected by four non-terminologies of addition, subtraction, multiplication, division, and so on. Then we can use the interpreter mode.

Notes

The interpreter mode is really a relatively less useful mode, because it is too troublesome to maintain it. Imagine a lump of non-terminator interpreters, if you do not know grammar rules in advance, or the grammar is very simple, it is difficult to understand its logic. The interpreter mode is rarely used in actual system development because it causes efficiency, performance, maintenance, and other problems.

From: http://blog.csdn.net/zhengzhb/article/details/7666020

Definition:Given a language, it defines a representation of its syntax and an interpreter that uses this representation to explain sentences in the language.

Type:Behavior mode

Class diagram:

The interpreter mode is rarely used, and I have never used this mode before. Let's take a look at the interpreter mode.

 

Interpreter mode structure

  • Abstract Interpreter: declares an abstract interface (or abstract class) that must be implemented by all specific expressions. An interface is mainly an interpret () method, which is called an interpreted operation. The specific interpretation task is completed by its implementation classes. The specific interpreter is completed by the terminalexpression and nonterminalexpression, respectively.
  • Terminator expression: implements the interpretation operation associated with the elements in the grammar. Generally, there is only one Terminator expression in an interpreter mode, but there are multiple instances that correspond to different terminologies. Half of the Terminator is the unit of operation in the grammar. For example, there is a simple formula r = R1 + R2 in which R1 and R2 are the Terminator, the interpreter for parsing R1 and R2 is the terminator expression.
  • Non-terminator expression: each rule in the grammar corresponds to a non-terminator expression. Non-terminator expressions are generally operators or other keywords in the grammar, for example, in the formula r = R1 + R2, + is a non-Terminator, And the parser + is a non-terminator expression. Non-terminator expressions increase based on the complexity of the 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 of each terminator in the grammar. For example, if 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 to use map to act as the Environment role.

Code Implementation

Class context {} abstract class expression {public abstract object interpreter (context CTX);} class terminalexpression extends expression {public object interpreter (context CTX) {return NULL ;}} class nonterminalexpression extends expression {public nonterminalexpression (expression... expressions) {} public object interpreter (context CTX) {return NULL ;}} public class client {public static void main (string [] ARGs) {string expression = ""; char [] chararray = expression. tochararray (); Context CTX = new context (); stack <expression> stack = new stack <expression> (); For (INT I = 0; I <chararray. length; I ++) {// for syntax judgment, recursive call} expression exp = stack. pop (); exp. interpreter (CTX );}}

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 a syntax set. Each non-terminator expression interprets a minimum syntax unit, and then recursively combines these syntax units into a complete syntax. This is the interpreter mode.

 

Advantages and disadvantages of interpreter Mode

The interpreter is a simple syntax analysis tool. Its most significant advantage is scalability. to modify a syntax rule, you only need to modify the corresponding non-Terminator. If you want to extend the syntax, you only need to add a non-terminator class.

However, the interpreter mode causes class expansion. Each syntax requires a non-terminator expression. When syntax rules are complex, a large number of class files may be generated, it brings a lot of trouble to maintenance. At the same time, because recursive call methods are used, each non-terminator expression only cares about its own expressions. Each expression needs to know the final result and must be recursive, whether it is an object-oriented language or a process-oriented language, recursion is not recommended. Because a large number of loops and Recursion are used, efficiency cannot be ignored. Especially when it is used to explain a complex and lengthy parsing syntax, efficiency is intolerable.

 

Application 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 rmconversion Based on the SQL statement, we can use the interpreter mode to explain the statement.
  • Some repeated problems, such as addition, subtraction, multiplication, division, and four arithmetic operations, but the formula is different each time, sometimes a + B-c * D, sometimes a * B + c-d, and so on. The formulas are ever-changing, but they are all connected by four non-terminologies of addition, subtraction, multiplication, division, and so on. Then we can use the interpreter mode.

Notes

The interpreter mode is really a relatively less useful mode, because it is too troublesome to maintain it. Imagine a lump of non-terminator interpreters, if you do not know grammar rules in advance, or the grammar is very simple, it is difficult to understand its logic. The interpreter mode is rarely used in actual system development because it causes efficiency, performance, maintenance, and other problems.

From: http://blog.csdn.net/zhengzhb/article/details/7666020

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.