In-depth interpreter mode

xiaoxiao2021-03-06  43

First, the primer

In fact, there is nothing wrong with the introduction of interpreter mode, because it describes how to make a simple language interpreter, primarily in the object-oriented language development compiler; in practical applications, we may rarely come to construct a The situation of the language of language.

Although you can't use this mode, you can still be inspired by it.

Second, definition and structure

The definition of the interpreter mode is as follows: Defining the language of the language and establish an interpreter to explain the sentence in the language. It belongs to the behavior mode of the class. The language here is code that uses the specified format and syntax.

In the book of GOF, it is pointed out that if a specific type of problem occurs high enough, it is possible to represent the various instances of this problem as sentences in a simple language. This can be constructed to construct an interpreter that solves this problem by explaining these sentences. And when the grammar is simple, the efficiency is not the key problem.

Oh, this is the environment of the interpreter mode.

Let's take a look at what the mysterious interpreter mode is made up.

1) Abstract expression role: Declare an abstract explanation operation, this interface is implemented in all specific expression roles (nodes in the abstract syntax tree).

What is the abstract grammar tree? The interpretation of "Java and Mode" is that every node of the abstract syntax tree represents a statement, and the interpretation method can be performed on each node. The execution of this interpretation method is explained by this statement. Since each statement is explained by this statement. Since each statement represents an instance of a common problem, the interpretation operation on each node represents an answer to a problem instance.

2) Final synonym expression character: Specific expression.

a) Realize the explanation operation associated with the end of the text method

b) and each end of the sentence requires one instance of this class and corresponding

3) Non-enda expression role: specific expression.

a) Each rule R :: = R1R2 ... RN in the grammat is required to take a non-end table strip role.

b) Example variables for maintaining an abstract expression role for each symbol from R1 to RN

c) Implement an explanation, explaining the interpretation of the objects that represent those objects from R1 to RN

4) Context (Environment) Role: Contains some global information outside of the interpreter.

5) Customer role:

a) Build (or given) an abstract syntax tree in a particular sentence in the language defined by the article

b) call interpretation operation

Place the interpreter structure class map, which is also from the book of GOF.

Oh, a detailed responsibility is given to each role, and the relationship between five roles is given in the class diagram. This is not very difficult, and a simple example will be given, I hope to deepen your understanding of the interpreter mode.

Third, for example

To raise an example of a reduction and subtraction, implement the idea from the example in Java and Mode. The functionality of each role is implemented in accordance with the specifications mentioned above.

// Context (environment) role, use HashMap to store values ​​corresponding to the variable

Class context

{

Private map valuemap = new hashmap ();

Public Void AddValue (variable x, int y)

{

Integer yi = new integer (y);

Valuemap.Put (x, yi);

}

Public int LookupValue (Variable X)

{

INT i = (Integer) Valuemap.get (x)). INTVALUE ();

Return I;

}

}

// Abstract expression role, you can also be implemented with an interface

Abstract class expression {

Public Abstract Int Interpret (CONTEXT CON);

}

// Final symbol expression role

Class Constant Extends Expression

{

Private INT i;

Public Constant (INT i)

{

THIS.I = I;

}

Public int Interpret (Context Con)

{

Return I;

}

}

Class Variable Extends Expression

{

Public int Interpret (Context Con)

{

// this is a Variable object that calls the interpret method.

Return Con.LookupValue (this);

}

}

// Non-enda expression role

Class Add Extends Expression

{

PRIVATE EXPRESSION LEFT, RIGHT;

Public Add (Expression LEFT, Expression Right)

{

THIS.LEFT = LEFT;

THIS.right = Right;

}

Public int Interpret (Context Con)

{

Return Left.Interpret (CON) Right.Interpret (CON);

}

}

Class Subtract Extends Expression

{

PRIVATE EXPRESSION LEFT, RIGHT;

Public Subtract (Expression Left, Expression Right)

{

THIS.LEFT = LEFT;

THIS.right = Right;

}

Public int Interpret (Context Con)

{

Right.Interpret (CON);

}

}

Class Multiply Extends Expression

{

PRIVATE EXPRESSION LEFT, RIGHT;

Public Multiply (Expression LEFT, Expression Right)

{

THIS.LEFT = LEFT;

THIS.right = Right;

}

Public int Interpret (Context Con)

{

RETURN LEFT.ITERPRET (CON) * Right.Interpret (CON);

}

}

Class Division Extends Expression

{

PRIVATE EXPRESSION LEFT, RIGHT;

Public Division (Expression LEFT, Expression Right)

{

THIS.LEFT = LEFT;

THIS.right = Right;

}

Public int Interpret (Context Con)

{

Try {

Return Left.Iterpret (CON) / Right.Interpret (CON);

} catch (ArithmeticException ae)

{

System.out.println ("Is 0 is 0!");

Return -11111;

}

}

}

// Test procedure, calculate (A * b) / (a-b 2)

Public Class Test

{

Private static expression ex;

Private static context con;

Public static void main (String [] args) {

Con = new context ();

/ / Set variable, constant

Variable a = new variable ();

Variable b = new variable ();

Constant C = New Constant (2);

/ / Assignment for variables

Con. A, 5);

C. ADVALUE (B, 7);

// Computing, the structure of the sentence is analyzed by us.

EX = New Division (New Multiply (A, B), New Add (New Subtract (A, B), C);

System.out.Println ("The result of the operation is:" ex.interpret (con));

}

}

The interpreter mode does not explain how to create an abstract syntax tree, so its implementation can be diverse, in which we are provided directly in Test, of course, there is better, more professional implementation.

For the end of the end, the GOF recommends sharing their copies because they have repeatedly repeated multiple times. However, considering the limitations of enjoyment mode, I suggest that when you repeat enough time, you will consider enjoying the yuan mode (regarding the enjoys mode, please refer to my "in-depth shallow enjoys").

Fourth, advantages and disadvantages

The interpreter mode provides a simple way to perform grammar, and easy to modify or extend syntax. Many classes in the general system use similar syntax, you can use an interpreter to replace an interpreter for each rule. And different rules in the interpreter are implemented by different classes, which makes it easy to add a new syntax rule.

However, the interpreter mode is difficult to maintain for complex literacy. You can imagine that every rule should correspond to a processing class, and these classes are also recursive to call abstract expression roles, how horrible is a horrible thing, such as messy classes!

Five, summary

This should have some general understanding of the interpreter mode. Because this mode is lacking, most of this article directly comes directly from GOF. However, instance code is personally implemented and debugged.

转载请注明原文地址:https://www.9cbs.com/read-71492.html

New Post(0)