Java-based interpreter design pattern

Source: Internet
Author: User
Tags abstract expression

First, a primer

In fact, there is no good example to introduce the interpreter pattern, because it describes how to form a simple language interpreter, the main application in the use of object-oriented language development compiler; In practical applications, we may rarely encounter the syntax to construct a language.

Although you can hardly use this model, it is still possible to get some inspiration from the look.

II. Definition and structure

The interpreter pattern is defined as follows: Defines the grammar of a language and creates an interpreter to interpret the sentences in that language. It belongs to the behavior pattern of the class. The language here means code that uses the prescribed format and syntax.

In Gof's book: If a particular type of problem occurs frequently enough, it may be worthwhile to express each instance of the problem as a sentence in a simple language. This allows you to build an interpreter that interprets the sentences to solve the problem. And it works best when the grammar is simple and efficiency is not the key issue.

This is also the context in which the interpreter pattern is applied.

Let's see what the mystery of the interpreter model is made of.

1 Abstract expression role: declares an abstract interpretation operation, which is implemented for all concrete expression roles (nodes in the abstract syntax tree).

What is called an abstract syntax tree? The explanation given in Java and schema is that each node of the abstract syntax tree represents a statement, and the interpretation method can be performed on each node. The execution of this interpretation method means that the statement is interpreted. Because each statement represents this statement is interpreted. Because each statement represents an instance of a common problem, the interpretation operation on each node represents the answer to an instance of the problem.

2 non-terminal expression role: concrete expression.

A) Implementation of interpretation operations associated with non-terminal in the grammar

b) and each non-terminal in the sentence requires an instance of the class to correspond to it

3 non-non-terminal expression role: concrete expression.

(a) Each rule in the grammar r::=r1r2 ... RN needs a non non-terminal strap type role

B to maintain an abstract expression role's instance variable for each symbol from R1 to RN

(c) Implementation of interpretation operations, interpretation generally to recursively invoke the interpretation of those objects from R1 to RN

4) context (Environment) role: contains some global information outside the interpreter.

5) Customer Role:

(a) constructs (or is given) an abstract syntax tree that represents a particular sentence in the language defined by the grammar

b) invoke an interpretation operation

Put on the Interpreter structure class diagram, which is also from the Gof book.

Detailed responsibilities are given for each role, and the relationship between the five roles is given in the class diagram. This is not very difficult to implement, and here is a simple example, hoping to deepen your understanding of the interpreter model.

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.