Design mode 23: Interpreter mode

Source: Internet
Author: User

Interpreter mode:
Given a language, a representation of its grammar is defined, and an interpreter is defined that uses that representation to interpret the sentences in the language.
Given a language, define a representation for it grammar along with an interpreter that uses the representation to INTERP RET sentences in the language.

UML diagram:

Mainly include:

    1. Abstractexpression: Declares an interface for performing an operation.
    2. Terminalexpression: Terminator expression that implements the operation associated with Terminator in the grammar.
    3. Nonterminalexpression: Non-terminator expression, the interpretation operation is implemented for the Terminator of Grammar. For every rule in grammar r1,r2,r3 ... a specific non-Terminator expression class is required.
    4. Context: Contains some global information outside the interpreter.
    5. Client: Defines an abstract syntax tree that is used to represent specific sentences in a language. The syntax tree is composed of nonterminalexpression and terminalexpression.

C + + implementation:

#include <iostream>#include <list>using namespace STD;classcontext{};classabstractexpression{ Public:Virtual voidInterpret (Context * c) =0;};classTerminalexpression: Publicabstractexpression{ Public:voidInterpret (Context *c) {cout<<"Terminalexpression interpret"<<endl; }};classNonterminalexpression: Publicabstractexpression{ Public:voidInterpret (Context *c) {cout<<"Nonterminalexpression interpret"<<endl; }};intMain () {cout<<"Interpreter mode Code"<<endl; list<abstractexpression *>Lists Context *c=NewContext; Abstractexpression * te1=NewTerminalexpression ();    Lists.push_back (TE1); Abstractexpression * te2=NewTerminalexpression ();    Lists.push_back (TE2); Abstractexpression * te3=NewTerminalexpression ();    Lists.push_back (TE3); Abstractexpression * te4=NewTerminalexpression ();    Lists.push_back (TE4); Abstractexpression * nte1=NewNonterminalexpression (); Lists.push_back (NTE1); list<AbstractExpression*>:: Iterator Iter=lists.begin (); for(; Iter!=lists.end (); iter++)    {(*iter)->interpret (c); }DeleteCDeleteTE1;DeleteRetDeleteTe3;DeleteTe4;Deletente1;return 0;}

Execution output:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design mode 23: 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.