Implementation of custom language-interpreter mode (2)

Source: Internet
Author: User

18.2 grammar rules and abstract syntax tree
The interpreter mode describes how to define a grammar for a simple language, how to represent a sentence in the language, and how to explain these sentences. Before formally analyzing the interpreter schema structure, let's first learn how to represent the grammar rules of a language and how to construct an abstract syntax tree.
In the addition/subtraction interpreter mentioned above, each input expression, such as "1 + 2 + 3-4 + 1", contains three language units, you can use the following grammar rules to define the rule:
Expression: = value | operation
Operation: = expression' + 'Expression | expression'-'Expression
Value: = an integer // an integer
The grammar rule contains three statements. The first one indicates the expression composition. value and operation are the definitions of the next two language units, each statement defines strings such as operation and value, which are called language constructor or language unit. The symbol "::=" indicates the meaning of "defined, the Language Unit on the left is described and defined on the right. The Language Unit corresponds to the terminator expression and non-terminator expression. If operation in this rule is a non-terminator expression, its composition element can still be an expression and can be further decomposed. value is a terminator expression, and its composition element is the most basic language unit, it cannot be further decomposed.
Some symbols can be used in the definition of grammar rules to express different meanings, such as "|" or, and "{" and, use "*" to indicate zero or multiple occurrences. The most frequently used symbol is "|" of the "or" relation, for example, the syntax rule "boolValue :: = 0 | 1 "indicates that the value of the final expression boolValue can be 0 or 1.
In addition to defining a language using grammar rules, you can also use an Abstract Syntax Tree (AST) in the interpreter mode) graphical representation of the language structure, each abstract syntax tree corresponds to a language instance, for example, the statement "1 + 2 + 3-4 + 1" in the addition/subtraction expression language can be expressed through the abstract syntax tree shown in 18-2:
 

Figure 18-2 abstract syntax tree
In the abstract syntax tree, you can use the terminator expression value and the non-terminator expression operation to form a complex statement. The language instance of each grammar rule can be expressed as an abstract syntax tree, that is, each specific statement can be represented by an abstract syntax tree similar to Figure 18-2. the instance of the terminator expression class in the graph serves as the leaf node of the tree, instances of the non-terminator expression class can be used as non-leaf nodes. They can use instances of the terminator expression class and subexpressions containing the Terminator and non-terminator instances as their subnodes. The abstract syntax tree describes how to construct a complex sentence. By analyzing the abstract syntax tree, you can identify the terminator class and non-terminator class in a language.
Author: Liu Wei

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.