Interpreter (interpreter)

Source: Internet
Author: User

1 Intent: Given a language, define a representation of its grammar, and define an interpreter that uses that representation to interpret sentences in the language.

2 motivation: 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.

Build an interpreter to explain these sentences to solve the problem.

3 Applicability:

The interpreter pattern is used when a language needs to be interpreted and executed, and you can represent a sentence in that language as an abstract parsing tree.

Best performance:

The class hierarchy of grammars becomes large and cannot be managed. Tools such as the parser generator are a better choice at this point. You can save time and space by not having to build a parse tree to interpret expressions.

Efficiency is not a key issue, and the most efficient interpreter is not implemented by directly interpreting the parse tree, but rather converting it to another form first.

4 participants:

. Abstractexpression: Declares an abstract interpretation operation that is shared by all nodes in the abstract syntax tree.

. Terminalexpression: Implements the interpretation operation associated with Terminator in the grammar. Each terminator in a sentence requires an instance of the class.

. Nonterminalexpression: The grammar of each rule r::=r1r2 ... RN needs a nonterminalexpression.

An instance variable of type abstrationexpression is maintained for each symbol from R1 to RN.

Implements the interpret operation, which recursively invokes the interpretation of those objects that represent R1 to RN.

. Context:

Contains some global information outside of the interpreter

. Client:

Constructs an abstract syntax tree. Invoke interpret operation

5 Collaboration:

. Client: Constructs a sentence, which is an abstract syntax tree for instances of nonterminalexpression and terminalexpression. The context is then initialized to invoke the interpret operation.

Each of the non-terminator expression nodes defines the interpretation of the corresponding sub-expression. The interpretation of each terminator expression forms the basis of recursion.

Each node's interpretation operation uses context to store and access the state of the interpreter

6 Effect:

1) Easy to adapt and expand

2) also easy to implement grammar

3) Complex grammar difficult to maintain

4) added a new way to interpret expressions

7 implementations:

Interpreter and composite are interlinked.

1) Creating an abstract syntax Tree interpreter does not create a syntax tree. Can be generated by the compiler or created manually, or provided by the customer.

2) Define the interpretation operation:

It is not always important to define an interpretation operation in an explanatory expression class, and if you create a new interpreter frequently, put an interpreter into a separate visitor object in visitor mode.

3) share terminator with flyweight mode:

8 Related modes:

Composite pattern: Abstract syntax tree is an instance of a composite pattern

Flyweight: How to share terminator in an abstract syntax tree

Iterator: The interpreter can traverse the structure with an iterator

Visitor: Maintains the behavior of the nodes in the parsing tree.

Interpreter (interpreter)

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.