Home  >  Article  >  Backend Development  >  A brief introduction to C++ design patterns and interpreter pattern

A brief introduction to C++ design patterns and interpreter pattern

黄舟
黄舟Original
2017-01-17 13:25:381330browse

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

Problems solved by the interpreter pattern: If a particular type of problem occurs frequently enough, it may be worthwhile to express each instance of the problem as a sentence in a simple language. This makes it possible to build an interpreter that solves the problem by interpreting these sentences.

Regular expression is one of its applications. The interpreter defines a grammar for regular expressions and represents a specific regular expression, as well as how to interpret this regular expression.

Four roles:

AbstractExpression: declares an abstract interpretation operation. This interface is shared by all nodes in the abstract syntax tree.

TerminalExpression Terminal expression: implements the interpretation operation associated with the terminal symbols in the grammar.

NonterminalExpression: Nonterminal expression, which implements interpretation operations for nonterminal symbols in the grammar. A specific nonterminal expression class is required for each rule R1, R2...Rn in the grammar.

Context: Contains some global information outside the interpreter.

Mode implementation:

[code]//Context
class Context{
private:
    std::string input;
public:
    std::string Input(std::string in){
        input = in;
        return input;
    }
};

//抽象表达式
class AbstractExpression{
public:
    virtual void Interpret(Context *context) = 0;
};

//终结符表达式
class TerminalExpression: public AbstractExpression{
public:
    void Interpret(Context *context)override{
        std::cout << "TerminalExpression\n";
    }
};

//非终结符表达式
class NonterminalExpression: public AbstractExpression{
public:
    void Interpret(Context *context)override{
        std::cout << "NonterminalExpression\n";
    }
};

Client:

[code]//Client
int main(){
    Context *context = new Context;
    std::list<AbstractExpression*> list;
    list.push_back(new TerminalExpression);
    list.push_back(new NonterminalExpression);
    list.push_back(new TerminalExpression);
    list.push_back(new TerminalExpression);

    for(auto i : list)
        i->Interpret(context);
    // Output:
    // TerminalExpression
    // NonterminalExpression
    // TerminalExpression
    // TerminalExpression

    return 0;
}

Interpreter mode benefits:

Usually there is a language that needs to be interpreted and executed, and the language can be The interpreter mode can be used when the sentences in are represented as an abstract syntax tree.

The above is the content of a brief introduction to the interpreter mode of C++ design patterns. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn