Liar design Pattern Python implementation-interpreter mode

Source: Internet
Author: User

Interpreter pattern (interpreter pattern): Given a language, defines a representation of its grammar and defines an interpreter that uses that representation to interpret sentences in the language.

Here is a demo of the Interpreter mode:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 __author__='Andy'5 """6 Liar design Mode7 design mode-interpreter mode8 interpreter pattern (interpreter pattern): Given a language, defines a representation of its grammar and defines an interpreter that uses that representation to interpret sentences in the language.9 """Ten #abstract an interpreter class One classabstractexpression (object): A  -     defInterpreter (self, context): -         Pass the  - #specific interpreter--terminal inheritance abstract interpreter - classterminalexpression (abstractexpression): -  +     defInterpreter (self, context): -         Print "Terminal Interpreter", Context +  A #specific interpreter--terminal inheritance abstract interpreter at classnotterminalexpression (abstractexpression): -  -     defInterpreter (self, context): -         Print "non-terminal interpreter", Context -  -  in classContext (object): -  to     def __init__(self): +Self.name ="" -  the if __name__=="__main__": *Context =Context () $Context.name ='Andy'Panax NotoginsengArr_list =[Notterminalexpression (), Terminalexpression (), Terminalexpression ()] -      forEntryincharr_list: theEntry.interpreter (context)

The design of the above class is as follows:

When a language needs to be executed and a sentence of that language can be expressed as an abstract syntax tree, you can use the interpreter pattern


Andy
Source: http://www.cnblogs.com/onepiece-andy/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the author's consent must be in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Liar design Pattern Python implementation-interpreter mode

Related Article

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.