Application of interpreter model in parsing Java design pattern programming _java

Source: Internet
Author: User
Tags arithmetic

definition: Given a language, defines a representation of his grammar, and defines an interpreter that uses the representation to interpret a sentence in a language.
Type: behavior class pattern
class Diagram:

Interpreter mode is a less used mode, I have not used this model before. Now let's take a look at the interpreter pattern.

structure of the interpreter pattern
Abstract Interpreter: declares an abstract interface (or abstract class) in which all concrete expressions are implemented, primarily a interpret () method, called an interpretation operation. The specific interpretation task is accomplished by its various implementation classes, and the concrete interpreters are completed by the Non-terminal interpreter Terminalexpression and the Non-terminal interpreter nonterminalexpression respectively.
Non-terminal expression: Implements an interpretation operation associated with an element in a grammar, usually with only one non-terminal expression in an interpreter pattern, with multiple instances corresponding to different terminators. Non-terminal is half the arithmetic unit in the grammar, for example, there is a simple formula R=R1+R2, in which R1 and R2 are non-terminal, the corresponding parsing R1 and R2 interpreter is the non-terminal expression.
Non-non-terminal expressions: Each rule in a grammar corresponds to a non-terminal expression, and a non-terminal expression is generally an operator or other keyword in a grammar, such as a formula R=R1+R2, + is not a non-terminal, and an interpreter for parsing + is a non-non-terminal expression. Non-non-terminal expressions increase according to the complexity of the logic, in principle each grammar rule corresponds to a non-terminal expression.
Environment role: The task of this role is generally used to store the specific values of each non-terminal in the grammar, such as R=R1+R2, we give the R1 value 100, to R2 assigned a value of 200. This information needs to be stored in the environment role, and in many cases it is sufficient that we use the map to act as an environmental role.

Example
to give a subtraction example, the implementation of ideas from the Java and schema in the example. The functions of each role are implemented according to the specification mentioned above.

Context (Environment) role, using HASHMAP to store the value of the variable's corresponding 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 use an interface to implement the abstract class Expression {public abstract int interpret (context con); 
 
    }//Non-terminal expression role class Constant extends Expression {private int i; 
 
    Public Constant (int i) {this.i = i; 
 
    public int interpret (context con) {return i; The class Variable extends Expression {public int interpret (context con) {//this for calling inte Rpret method of variable object return con. 
 
    LookupValue (this); }//non-non-terminal 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] {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 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.interpret (Con)/right.int 
 
       Erpret (con); }catch (ArithmeticException ae) {System.out.println ("divisor is 0!") 
 
           "); 
 
       return-11111; 
 
    }}///test program, COMPUTE (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); 
 
       Assign a value to a variable Con.addvalue (a, 5); 
 
Con.addvalue (b, 7); 
 
       operation, the structure of the sentence is analyzed by ourselves, constructed 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 pros and cons of the interpreter pattern
interpreter is a simple syntax analysis tool, its most significant advantage is extensibility, modify grammar rules only need to modify the corresponding non-terminal can be, if extended syntax, only need to increase the non-terminal class on it.
However, the interpreter pattern causes the class to swell, each syntax needs to produce a non-terminal expression, the grammar rule is more complex, may produce the massive class file, causes the maintenance to have the very many trouble. Meanwhile, with recursive invocation methods, each non-terminal expression is concerned only with its own expression, each expression needs to know the final result, it must be recursive, whether it is an object-oriented language or a process-oriented language, recursion is an deprecated way. Due to the use of a large number of cycles and recursion, efficiency is a problem that can not be ignored. Especially when it comes to interpreting a complex, lengthy syntax, efficiency is intolerable.

application scenario for interpreter mode
You can use the interpreter mode in the following situations:
There is a simple syntax rule, such as an SQL statement, that can be interpreted using an interpreter pattern if we need to perform RM conversions based on SQL statements.
Some recurring problems, such as subtraction arithmetic, but the formula is different every time, sometimes a+b-c*d, sometimes a*b+c-d, and so on, and so on, and so on, the formula is changeable, but all are connected by the subtraction four non-non-terminal, then we can use the interpreter mode.


Attention Matters
The interpreter pattern is really a less used pattern, because it is too cumbersome to maintain, imagine, a lump of non-terminal interpreter, if not in advance to the rules of grammar, or the grammar is particularly simple, it is difficult to understand its logic. The interpreter pattern is rarely used in actual system development because it causes problems such as efficiency, performance, and maintenance.

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.