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, the interpreter mode can also be calledAbstract syntax tree (AST)Graphical representation of the language structure, each abstract syntax tree corresponds to a language instance, such as the statement "1 +" in the addition/subtraction Expression Language
2 + 3-4 + 1 ", which can be expressed by the abstract syntax tree shown in 18-2:
Figure 18-2
Abstract syntax tree
In this 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,In the figure, the instance of the terminator expression class serves as the leaf node of the tree, and the instance of the non-terminator expression class serves as the non-leaf node, they can use instances of the terminator expression class and subexpressions that contain 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
Http://blog.csdn.net/lovelion]