Big talk design pattern C + + implementation-27th-Interpreter mode

Source: Internet
Author: User
Tags gettext

First, UML diagram



Second, the concept

interpreter mode (interpreter): given a language , defines a representation of its grammar , and defines an interpreter that uses the expression to explain the sentences in the language.


Third, the description

Which roles are included?

(1) abstractexpression (abstract expression): declares an abstract interpretation operation that is shared by all nodes in the abstract syntax tree.

(2) terminalexpression (terminator expression): implements the interpretation operation associated with Terminator in the grammar. Implementing an interface that is required in an abstract expression is primarily a interpreter () method. Every terminator in grammar has a specific ending expression corresponding to it.

(3) nonterminalexpression (non-terminator expression): The interpretation operation is implemented for non-terminator in grammar.

(4) Context: contains some global information outside of the interpreter.

What does the interpreter mode need to solve?

If a particular type of problem occurs at a sufficiently high frequency, it might be worthwhile to describe each instance of the problem as a sentence in a simple language. This allows you to build an interpreter that solves the problem by interpreting these sentences.

When do I use the interpreter mode?

In general, you can use the interpreter pattern when there is a language that needs to be interpreted and executed, and you can represent sentences in that language as an abstract syntax tree.

What are the benefits of using the interpreter mode?

Using the interpreter pattern means that the grammar can be easily changed and extended because the pattern uses classes to represent grammar rules, and you can use inheritance to change or extend the grammar. It is also easier to implement grammars, because the classes that define the various nodes of the abstract syntax tree are broadly similar in their implementation, and these classes are easy to write directly.

The inadequacy of the interpreter mode?

The interpreter pattern defines at least one class for each rule in the grammar, so grammars that contain many rules can be difficult to manage and maintain. It is recommended to use other techniques, such as a parser or compiler builder, when the grammar is very complex.


Iv. implementation of C + +

(1) Expression.h

#ifndef flyweight_h#define flyweight_h#include <string> #include <iostream> #include <vector># Include <sstream>//context, where the content class playcontext{private:std::string text;public:void SetText (std:: String text) {This->text=text;} std::string GetText () {return this->text;}};/ /abstractexpression, here is the expression class Expression{public:void interpret (playcontext* context) {std::string s1=context- >gettext (); std::string buf;std::string s2;if (Context->gettext (). Length () ==0) {return;} Else{std::vector<std::string> Vec;std::stringstream SS (Context->gettext ()); while (SS >> buf) Vec.push _back (BUF); std::string playkey=vec[0];std::string playvalue=vec[1]; Excute (Playkey,playvalue); Vec.erase (Vec.begin (), Vec.begin () +2); Std::vector<std::string>::iterator it;for ( It=vec.begin (); It!=vec.end (); it++) {s2+=*it;if (It!=vec.end ()-1) s2+= "";} Context->settext (s2);}} virtual void Excute (std::string key,std::string value) =0;};/ /concreteexpression, here is the note class Note:public ExpresSion{public:void Excute (std::string key,std::string value) {std::string note;switch (key[0]) {case ' C ': note= "1"; Case ' D ': note= "2"; Break;case ' E ': note= "3"; Break;case ' F ': note= "4"; Break;case ' G ': note= "5"; Break;case ' A ': note= "6"; Break;case ' B ': note= "7"; break;} std::cout<<note<< "";}};/ /concreteexpression, here is the scale class Scale:public expression{public:void Excute (std::string key,std::string value) {std:: String Scale;switch (Value[0]) {case ' 1 ': scale= "bass"; Break;case ' 2 ': scale= "alto"; Break;case ' 3 ': scale= "treble"; break;} std::cout<<scale<< "";}}; #endif


(2) Client.cpp

#include "Flyweight.h" #include <iostream> #include <cstdlib> #include <vector> #include <string > #include <sstream> using namespace std; Clients, client void Main () {Playcontext context;std::cout<< "on beach:" <<std::endl;context. SetText ("O 2 E 0.5 g 0.5 a 3 e 0.5 g 0.5 D 3 e 0.5 g 0.5 A 0.5 o 3 C 1 O 2 A 0.5 g 1 C 0.5 E 0.5 D 3"); expression* expression;while (context. GetText (). Length () >0) {char c=context. GetText () [0];switch (c) {case ' O ': expression=new scale (), Break;case ' C ': Case ' D ': Case ' E ': Case ' F ': Case ' G ': Case ' A ': Case ' B ': Case ' P ': expression=new Note (); Expression->interpret (&context);                Delete expression;}    System ("Pause");}


(3) operation


Big talk design pattern C + + implementation-27th-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.