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" <<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)