Interpreter mode (C ++ Interpreter Mode)

Source: Internet
Author: User

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;}
   
  
 


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.