Design Pattern: Interpreter Pattern

Interpreter mode (interpreter): Given a language, define a representation of its grammar, and define an interpreter that uses that representation to interpret sentences in the language.

      The interpreter pattern addresses that, if a particular type of problem occurs frequently enough, it may be worthwhile to express individual instances of that problem as sentences in a simple language, so that an interpreter can be constructed that explains The generator solves the problem by interpreting these sentences.


AbstractExpression (abstract expression), indicating an abstract operation interpretation, this interface is shared by all nodes in the abstract syntax tree.

abstract class AbstractExpression{
    public abstract void Interpret(Context context);
}

TerminalExpression (terminal expression), implements the interpretation operation associated with the terminal in the grammar, implements the interface required in the abstract expression, mainly an Interpret() method, each terminal in the grammar has a specific terminal The expression corresponds to it.

class TerminalExpression implements AbstractExpression{
    public void Interpret(Context context){
        System.out.println("Terminal Interpreter");
    }
}

NonterminalExpression (terminal expression), a specific nonterminal expression class is required for each rule R1, R2, ~~Rn in the grammar. Interpret() method of abstract expression is used to realize the interpretation operation, the interpretation operation Recursively call the above-mentioned instance variables representing the symbols in R1, R2~~Rn.

class NonterminalExpression implements AbstractExpression{
    public void Interpret(Context context){
        System.out.println("Non-terminal interpreter");
    }
}

Context, which contains global information outside the interpreter.

class Context{
    private String input;
    public void setInput(String input){
        this.input = input;
    }

    public String getInput(){
        return input;
    }

    private String output;

      public void setOutput(String output){
        this.output = output;
    }

    public String getOutput(){
        return output;
    }
}

Client code that constructs an abstract syntax tree representing a particular sentence in the language defined by this grammar. Calls the interpret operation.

Context context = new Context();
IList<AbstractExpression> list = new List<AbstractExpression>();
list.Add(new TerminalExpression());
list.Add(new NonterminalExpression());
list.Add(new TerminalExpression());
list.Add(new TerminalExpression());

foreach(AbstractExpression exp in list){
    exp.Interpret(context);
}

The results show that

terminal interpreter

non-terminal interpreter

terminal interpreter

terminal interpreter

Interpreter Mode Benefits

       Interpreter mode is used when there is a language that needs to be interpreted and you can represent sentences in the language as an abstract syntax tree.

      Used in the interpreter pattern, it means that the grammar can be easily changed and extended because the pattern uses classes to represent the grammar rules, you can use inheritance to change or extend the grammar, and it is easier to implement the grammar because the abstract syntax tree is defined The implementations of the classes for each node in the system are generally similar, and these classes are easy to write directly.

      The disadvantage of the interpreter mode is that the interpreter mode defines at least one class for each rule in the grammar, so grammars containing many rules may be difficult to manage and maintain. It is recommended that when the grammar is very complex, use other techniques such as parser or compiler generators to handle.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642041&siteId=291194637