Interpreter mode details-design mode (22)

Source: Internet
Author: User

Source of the Interpreter mode:

Interpreter (interpreter) mode is a special design pattern that establishes an interpreter (interpreter) for a specific computer programming language used to interpret pre-defined grammars. Simply put, the interpreter pattern is a simple syntax interpreter architecture. The interpreter pattern belongs to the behavior pattern, given a language, defines a representation of its grammar, and defines an interpreter that uses that representation to interpret sentences in the language.

Interpreter Mode effect:

as its name, this pattern is mostly used to explain some (custom) unique syntax, such as reading an XML file in some game development engines, or WindowsPhone a XAML file in development, using this pattern. Rather than a pattern, it is more accurate to have a general specification of behavior.

The interpreter mode UML structure is shown in Figure 1:


Interpreter Pattern Composition:

(1). Abstract expression Role (Abstractexpression): declares an abstract interpretation operation that is implemented for all specific expression roles.
(2). Terminator expression Role (Terminalexpression): Implements an interpreted operation associated with an element in the grammar, usually with only one terminator expression in an interpreter pattern, but with multiple instances corresponding to different terminator.
(3). Terminator is the basic element used in language: it can no longer be decomposed, such as: X-XA, where a is terminator, because no other rule can change a to another symbol, but x is a different symbol, so X is a non-terminator.
(4). Non-Terminator expression role (nonterminalexpression): Each rule in the grammar corresponds to a non-terminating expression, and non-finalization expressions are incremented according to the complexity of the logic, in principle each grammar rule corresponds to a non-terminator expression.
(5). Environment Role (context): contains some global information outside of the interpreter.

Interpreter Pattern code Example:

Interpreter.h

#ifndef _interpreter_h_#define _interpreter_h_#include<string>typedef std::string Context; class Abstractexpression{public:abstractexpression () {}~abstractexpression () {}virtual void interpreter (const Context & context) = 0;}; Class Terminalexpression:public Abstractexpression{public:terminalexpression (char kc); ~terminalexpression () {} virtual void Interpreter (const context& Context);p Rivate:char KeyChar;}; Class Nonterminalexpression:public abstractexpression{public:nonterminalexpression (AbstractExpression* ae); ~ Nonterminalexpression () {}virtual void interpreter (const context& Context);p rivate:abstractexpression* Terminalexpression;}; #endif

Interpreter.cpp

#include "Interpreter.h" #include <iostream>terminalexpression::terminalexpression (char kc) {KeyChar = KC;} void Terminalexpression::interpreter (const context& Context) {int length = 0;while (context[length]! = ' + ') {length+ +;} int Numberarray[100];int index = 0;for (int i = 0;i < length;i++) {if (KeyChar  = = Context[i]) numberarray[index++] = i; }for (int i = 0;i < index;i++) {std::cout<<numberarray[i];} Std::cout<<std::endl;} Nonterminalexpression::nonterminalexpression (abstractexpression* ae) {terminalexpression = AE;} void Nonterminalexpression::interpreter (const context& Context) {std::cout<< "The password is:"; Terminalexpression->interpreter (context);}

Main.cpp

#include "Interpreter.h" #include <iostream>using namespace Std;int main () {abstractexpression* terminal = new Terminalexpression (' C '); abstractexpression* nonterminal = new nonterminalexpression (terminal);//first test.string Contextfirst = "character of The cars in this chart. "; cout<< "Context:" <<contextfirst<<endl;nonterminal->interpreter (Contextfirst);cout<< Endl;//second test.terminal = new Terminalexpression (' O '); nonterminal = new nonterminalexpression (terminal); string Contextsecond = "Thanks everyone for your long time supporting."; cout<< "Context:" <<contextsecond<<endl;nonterminal->interpreter (Contextsecond); return 0;}

Interpreter Mode Applicability:

(1). You can represent a sentence in a language that needs to interpret execution as an abstract syntax tree.

(2). Some recurring problems can be expressed in a simple language.

(3). The grammar of a language is simpler.

(4). Implementation efficiency is not a critical issue. (Note: Efficient interpreters are not usually implemented by directly interpreting abstract syntax trees, but rather they need to be converted to other forms, and the use of interpreter patterns is not efficient.) )

Interpreter Model Pros and cons Summary:

Interpreter Mode Advantages:

(1). Easy to change and expand grammar. Because the class is used in the interpreter pattern to represent the grammar rules of the language, it is possible to change or extend the grammar through mechanisms such as inheritance.

(2). Each grammar rule can be represented as a class, so it is easy to implement a simple language.

(3). It is easier to implement grammar. Each expression node class in the abstract syntax tree is implemented in a similar way, and the code for these classes is not particularly complex, and the node class code can be generated automatically by some tools.

(4). It is more convenient to add new explanatory expressions. If the user needs to add a new explanatory expression that only needs to add a new terminator expression or non-Terminator expression class, the original expression class code does not need to be modified to conform to the "open and closed principle".

Interpreter Mode Disadvantages:

(1). Difficult to maintain for complex grammars. In interpreter mode, each rule needs to define at least one class, so if a language contains too many grammatical rules, the number of classes will increase dramatically, making the system difficult to manage and maintain, and consider using a parser to replace the interpreter pattern.

(2). less efficient execution. Because of the large number of loops and recursive calls in the interpreter pattern, it is slow to interpret more complex sentences, and the code debugging process is cumbersome.

Interpreter Mode Usage Summary:

Try not to use the interpreter mode in important modules because maintenance is difficult. In your project, you can use the scripting language instead of the interpreter pattern.

Interpreter mode details-design mode (22)

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.