Design Patterns - Interpreter Patterns

Interpreter Pattern - Interpreter Pattern

Interpreter Pattern : Defines the grammar of a language and builds an interpreter to interpret sentences in the language, where "language" refers to code that uses a specified format and grammar. The interpreter pattern is a behavior-like pattern.

My understanding: there is a little bit like a combination pattern, a little bit. Use non-terminal symbols as division, each two or several are processed into a terminal symbol, and finally processed into a terminal symbol.

Abstract interpreter: Declare an abstract interface (or abstract class) that all concrete expressions must implement. The interface is mainly an interpret() method, which is called an interpretation operation. The specific interpretation task is completed by its various implementation classes, and the specific interpreter is completed by the terminal interpreter and the non-terminal interpreter respectively. Terminal expression: Implements interpretation operations associated with elements in the grammar. Usually, there is only one terminal expression in an interpreter mode, but there are multiple instances, corresponding to different terminals. Half of the terminal symbols are the operation units in the grammar. For example, there is a simple formula R=R1+R2, in which R1 and R2 are the terminal symbols, and the corresponding interpreter that parses R1 and R2 is the terminal symbol expression. Non-terminal expression: each rule in the grammar corresponds to a non-terminal expression. Non-terminal expressions are generally operators or other keywords in the grammar. For example, in the formula R=R1+R2, + is non-terminal. Terminal, the interpreter that parses + is a non-terminal expression. Nonterminal expressions increase according to the complexity of the logic, and in principle each grammar rule corresponds to a nonterminal expression. Environment role: The task of this role is generally used to store the specific value corresponding to each terminal symbol in the grammar, such as 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 for us to use Map to act as the environment role.
Referenced in this place: https://www.jianshu.com/p/c138a1d2be5e

Abstract interpreter:

public interface AbstractNode {
    Integer interpret();
}
Nonterminal expression:

public class AndNode implements AbstractNode {
    AbstractNode left,right;

    public AndNode(AbstractNode left, AbstractNode right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public Integer interpret() {
        return left.interpret()+right.interpret();
    }
}
terminator expression:

public class VariableNode implements AbstractNode {
    private Integer num;

    public VariableNode(Integer num) {
        this.num = num;
    }

    @Override
    public Integer interpret() {
        return num;
    }
}

Processing class uses stack as environment role

public class Handler {

    Stack stack = new Stack();
    AbstractNode node;
    public void handler(String instruction){
        // split the string with spaces
        String[] words = instruction.split(" ");
        for (int i = 0; i < words.length; i++) {
            if(words[i].equalsIgnoreCase("+")){
                //If it is "+", use the pop from the stack as the left node
                AbstractNode left = (AbstractNode) stack.pop();
                //Use "+" as the right node
                AbstractNode right = new VariableNode(Integer.parseInt(words[++i]));
                //The left and right nodes are combined into a non-terminal expression
                this.node = new AndNode(left,right);
                //The result of the combination is put on the stack
                stack.push(this.node);
            }else{
                //If a final expression is stored for the number
                AbstractNode abn1 = new VariableNode(Integer.parseInt(words[i]));
                // put on stack
                stack.push(abn1);
            }
        }
        //end the loop and remove the result from the stack
        this.node = (AbstractNode) stack.pop();
    }
    public Integer output() {
        Integer result = this.node.interpret(); //interpret expression
        return result;
    }
}
Client:

public class Client {

    public static void main(String[] args) {
        String instroction1 = "1 + 2 + 3 + 4";
        String instroction2 = "1 + 2 + 3";
        String instroction3 = "1 + 23";
        Handler math = new Handler();
        math.handler(instroction1);
        System.out.println(math.output());
        math.handler(instroction2);
        System.out.println(math.output());
        math.handler(instroction3);
        System.out.println(math.output());
    }
}





Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326942831&siteId=291194637