Interpreter mode (C ++ Interpreter Mode)
The Interpreter mode provides a framework for implementing the syntax Interpreter. The purpose is to use an Interpreter to provide a Interpreter that defines the syntax representation of a language, the interpreter is used to explain sentences in the language.
The Interpreter mode uses classes to represent grammar rules, which facilitates syntax extension.
The Code is as follows:
#include
#include
using namespace std;class Context{public:Context(){}~Context(){}};class AbstractExpression{public:virtual ~AbstractExpression(){}virtual void Interpret(const Context& c){}protected:AbstractExpression(){}};class TerminalExpression:public AbstractExpression{public:TerminalExpression(const string& statement){m_statement=statement;}~TerminalExpression(){}void Interpret(const Context& c){cout<<"TEMINALEXPRESSION"<
Interpret(c);}private:AbstractExpression* m_ex;int m_times;};void main(){Context* c=new Context();AbstractExpression* abs=new TerminalExpression("Hello");AbstractExpression* nabs=new NonterminalExpression(abs,2);nabs->Interpret(*c);delete nabs;delete abs;delete c;}