Interpreter mode (c + + interpreter mode)

Source: Internet
Author: User

Interpreter The schema provides a framework for implementing a syntax interpreter that uses an interpreter to provide a user with an interpreter that defines the language syntax, and interprets the sentences in the language through this interpreter.

Interpreter patterns use classes to represent grammatical rules, making it easier to extend the grammar.

The code is as follows:

#include <iostream> #include <string>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" &LT;&LT;ENDL;} Private:string m_statement; };class nonterminalexpression:public abstractexpression{public:nonterminalexpression (AbstractExpression* ex,int Times) {m_ex=ex;m_times=times;} ~nonterminalexpression () {};void interpret (const context& c) {for (int i=0;i < m_times; i++) M_ex->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);d elete nabs;delete abs;delete c;} 


Interpreter mode (c + + interpreter mode)

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.