Deepen Java-based interpreter design mode

xiaoxiao2021-03-06  40

First, the primer is actually nothing good example introduces the interpreter mode because it describes how to make a simple language interpreter, mainly in the use of object-oriented language development compilers; in practical applications, we may rarely To construct the situation of a language of language. Although you can't use this mode, you can still be inspired by it. Second, define the definition of the structural interpreter mode as follows: Define 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. This is also the environment where the interpreter mode is applied. 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) Implementing the explanation operation B in the form of the characterization B) and each end of the sentence requires one example and corresponding 3) Non-termament expression character: Specific expression. a) Each rule R :: = R1R2 ... RN in the grammat is needed to interpret a non-terminating table belt role B) Explain for instance variables C) to maintain an abstract expression role from each symbol of R1 to RN. Operation, interpretation is generally to recursively invoke an explanation operation 4 of 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 b in which a particular sentence in the language defined by the article is called an interpretation operation to place an interpreter structure class map, which is from GOF Book in the book.

Detailed responsibilities are 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 the addition and subtraction, the idea is 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, can also be used interface to achieve abstract class expression {public abstract int interpret (Context con);} // terminator expression character class Constant extends expression {private int i; public Constant (int i) {this.i = i;} public int interpret Class Variable Extends Expression {public int interpret (context con) {// this is a variable object Return Conturn Conturn Conturn Conturn (this) for calling the interpret method;}} // Non-end express 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 Extension Expression {Private Expression Left, Right; Public Subtract (Expression left, Expression right) {this.left = left; this.right = right;} public int interpret (Context con) {return left.interpret (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.interpret (con) * right.interpret ( CON);}} Class Division Extension Expression {Private Expression Left, Right; Public Division (Expression Left, Expression Right) {this.right = left; this.right = right;

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

New Post(0)