Implementation of custom language-interpreter mode (3)

Source: Internet
Author: User
18.3 interpreter mode Overview

The interpreter mode is a design mode that uses a relatively low frequency but is difficult to learn. It is used to describe how to use object-oriented languages to form a simple language interpreter. In some cases, to better describe certain types of problems, we can create a new language that has its own expressions and structures, that is, grammar rules, instances of these problems correspond to sentences in the language. In this case, you can use the interpreter mode to design this new language. Learning the interpreter mode can deepen our understanding of object-oriented ideas and understand the interpretation process of Chinese grammar rules in programming languages.

The interpreter mode is defined as follows:

Interpreter pattern: defines the grammar of a language and creates an interpreter to explain the sentences in the language, the "language" here refers to the code that uses the prescribed format and syntax. Interpreter mode is a type of behavior mode.

Since expressions can be divided into Terminator expressions and non-terminator expressions, the structure of the interpreter mode is similar to that of the combination mode, but the interpreter mode contains more components, its structure is 18-3:

Figure 18-3
Interpreter mode structure

The interpreter mode structure includes the following roles:

● Abstractexpression: Specifies an abstract interpretation operation in an abstract expression. It is a common parent class for all Terminator expressions and non-terminator expressions.

● Terminalexpression (Terminator expression): A Terminator expression is a subclass of an abstract expression. It implements interpretation operations associated with terminologies in grammar. Each terminator in a sentence is an instance of this class. Generally, there are only a few Terminator expression classes in an interpreter mode. Their instances can form complex sentences through non-terminator expressions.

● Nonterminalexpression (non-terminator expression): A non-terminator expression is also a subclass of an abstract expression. It implements non-terminator interpretation operations in grammar. Because a non-terminator expression can contain a terminator expression, you can also continue to include non-terminator expressions. Therefore, the interpretation operation is generally performed recursively.

● Context: the context class is used to store global information outside the interpreter. Generally, it temporarily stores the statements to be interpreted.

In interpreter mode, each Terminator and non-terminator has a specific class corresponding to it, because classes are used to represent each grammar rule, therefore, the system will be flexible and scalable. For all terminologies and non-terminologies, we first need to abstract a common parent class, that is, an abstract expression class. The typical code is as follows:

Abstract class abstractexpression {

Public abstract void interpret (context CTX );

}

The Terminator expression and non-terminator expression classes are subclasses of the abstract expression class. For a terminator expression, the code is very simple. It mainly processes the terminator element. The typical code is as follows:

Class terminalexpression extends actexpression {

Public void interpret (context CTX ){

// Interpreted operation of the terminator expression

}

}

For non-terminator expressions, the code is relatively complex, because the expressions can be combined into a more complex structure through non-Terminator, for non-terminator expression classes that contain two operation elements, the typical code is as follows:

Class nonterminalexpression extends actexpression {

Private comment actexpression left;

Private comment actexpression right;

Public nonterminalexpression (condition actexpression left, condition actexpression right ){

This. Left = left;

This. Right = right;

}

Public void interpret (context CTX ){

// Call the interpret () method of each component recursively

// Specify the connection mode of the component during recursive calls, that is, the non-terminator function.

}

}

In addition to the classes used to represent expressions, an environment context is usually provided in the interpreter mode to store some global information, generally, the context contains a set object of the hashmap or arraylist type (or the collection class such as hashmap can act as the Environment class directly) to store a series of public information, for example, the key/value ing relationship between variable names and values is used to obtain relevant information during specific interpretation operations. The typical code snippets are as follows:

Class context {

Private hashmap map = new hashmap ();

Public void assign (string key, string value ){

// Set the value to the Environment class

}

Public String Lookup (string key ){

// Obtain the value stored in the Environment class

}

}

When the system does not need to provide global public information, the Environment class can be omitted, and the Environment class can be determined based on the actual situation.

Thoughts

Draw the class diagram of the addition/subtraction interpreter and write the core implementation code.

【Author】 Liu Wei
Http://blog.csdn.net/lovelion]

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.