AST of QLanguage

Source: Internet
Author: User

1. Each AST node consists of two fields, which indicate the type and additional information of the current node respectively.
2. Each node of the AST contains an ordered table pointing to its subnodes.
3. Each AST node contains a pointer to the next node.
To sum up, we get the AST Node Code: 1 class CSyntaxTreeNode
2 {
3 public:
4 CSyntaxTreeNode (int _ type, int _ value): type (_ type), value (_ value ){}
5
6 inline List <NAutoPtr <CSyntaxTreeNode> & Child ()
7 {
8 return child;
9}
10
11 inline NAutoPtr <CSyntaxTreeNode> Next ()
12 {
13 return next;
14}
15
16 inline int & Type ()
17 {
18 return type;
19}
20
21 inline int & Value ()
22 {
23 return value;
24}
25 protected:
26 int type;
27 int value;
28 List <NAutoPtr <CSyntaxTreeNode> child;
29 NAutoPtr <CSyntaxTreeNode> next;
30}; then we provide some enumeration to identify the node type:
1 // for type
2 enum TYPE
3 {
4 stNull,
5 stDeclare,
6 stFunction,
7 stParamterList,
8 stIf,
9 stDo,
10 stExp,
11}; finally, the overall structure of an AST:
1 class CParserAnalyze
2 {
3 public:
4 inline void Push (NAutoPtr <CSyntaxTreeNode> & Node)
5 {
6 SyntaxTreeStack. Push (Node );
7}
8
9 inline NAutoPtr <CSyntaxTreeNode> Pop ()
10 {
11 return SyntaxTreeStack. Pop ();
12}
13
14 inline NAutoPtr <CSyntaxTreeNode> Top ()
15 {
16 return SyntaxTreeStack. Top ();
17}
18
19 inline NAutoPtr <CSyntaxTreeNode> Root ()
20 {
21 return SyntaxTreeRoot;
22}
23 protected:
24 NAutoPtr <CSyntaxTreeNode> SyntaxTreeRoot; // syntax Root Node
25 Stack <NAutoPtr <CSyntaxTreeNode> SyntaxTreeStack; // syntax tree Stack
26 };
Here we will briefly analyze the analysis process:
Taking the if statement as an example, the combined sub-code is:
1 if_desc = (str_if + exp_desc) [if_desc_first] +
2 (str_then + stmt_list) [if_desc_second] +
3 Parser_Combinator_Node: opt (str_else + stmt_list) [if_desc_third]) +
4 (str_end + str_if) [if_desc_fourth]; enter the code:
1 if a then
2 declare B as integer
3. end if syntax analysis:
1. Read if a, a is reduced to an exp to generate a node of Type exp and press it into the AST syntax tree stack.
2. if a is reduced to generate a node of the stIf type, and the exp node at the top of the stack pops up to fill in the first subnode of the newly generated stIf node.
3. Read the then declare B as integer, And the integer is reduced to generate a node of the generated type stDeclare and press it into the syntax tree stack.
4. declare B as integer is reduced to the stDeclare node at the top of the stack to fill in a child node with a B identifier.
5. The then declare B as integer is reduced. First, the stmt_list on the top of the stack is displayed. Because stDeclare indicates that stmt_list has content, the lowest position of stIf value range on the top of the stack is 1.
6. The else clause does not exist.
7. Overall reduction.
At this time, the top of the stack is a stIf node, which does not contain the next node. There are two subnodes, stExp and stDeclare, respectively.

The analysis process is as follows:
1.
2.
3.
4.
5.
6.

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.