Building the Abstract Syntax Trees Lecture 23 Section 5.3 Robb T. Koether Hampden-Sydney College Wed, Mar 18, 2015 Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 1 / 38
1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 2 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 3 / 38
The Abstract Syntax Tree Each statement will be represented as a distinct AST. Once the AST is constructed, we will traverse the tree, emitting x86 assembly code as appropriate. Because our parsing is bottom-up, our AST construction will also be bottom-up. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 4 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 5 / 38
TreeNode Constructors TreeNode Constructors TreeNode(int n); TreeNode(IdEntry id); TreeNode(int op, TreeNode lf, TreeNode rt); TreeNode(int n) Construct a NUM tree node containing the value n. TreeNode(IdEntry id) Construct an ID tree node containing the IdEntry. TreeNode(int op, TreeNode lf, TreeNode rt) Construct a tree node with the operation op at its root and with left and right subtrees lf and rt. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 6 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 7 / 38
NUM Nodes The production for numbers is num NUM Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 8 / 38
NUM Nodes A number node is of the form NUM value Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 9 / 38
NUM Nodes NUM Nodes new TreeNode(NUM.lexval); Construct a NUM node. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 10 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 11 / 38
ID Nodes Two productions for identifiers are dcl type ID lval ID Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 12 / 38
ID Nodes An identifier node is of the form ID entry Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 13 / 38
ID Nodes NUM Nodes new TreeNode(ID.entry); Construct an ID node. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 14 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 15 / 38
Declarations The production for declarations is dcl type ID ; Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 16 / 38
Declarations The action taken by a declaration is to allocate memory for the variable. That will be represented by an allocation node (ALLOC) in the syntax tree. The left subtree will be the ID node. The right subtree will be a NUM node whose value is the number of bytes to allocate. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 17 / 38
ALLOC Trees An allocation tree is of the form ALLOC type ID entry NUM size Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 18 / 38
ALLOC Trees ALLOC Trees TreeNode lf = new TreeNode(ID.entry); TreeNode rt = new TreeNode(NUM.size); new TreeNode(ALLOC, lf, rt); Construct an allocation tree. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 19 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 20 / 38
Declarations The production that requires dereferencing an identifier is expr lval Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 21 / 38
Dereferencing When a variable (lval) is used in an expression, it must be dereferenced to convert it to an r-value. That will be represented by an dereference node (DEREF) in the syntax tree. The left subtree will be the ID node. There is no right subtree. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 22 / 38
DEREF Trees An dereference tree is of the form DEREF type ID entry Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 23 / 38
DEREF Trees DEREF Trees new TreeNode(DEREF, id.tree, null); Construct a dereference tree. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 24 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 25 / 38
Expressions There are many productions for expressions. One such production is expr expr + expr Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 26 / 38
PLUS Trees The tree for addition is PLUS type expr type expr type Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 27 / 38
PLUS Trees PLUS Trees new TreeNode(PLUS, expr1.tree, expr2.tree); Construct a tree for addition. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 28 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 29 / 38
Assignments The production for assignments (not statements) is expr lval = expr Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 30 / 38
Assignments Assignments a = b = c = 0; if (str1[i++] = str2[i++]); These are expressions, not statements, because they can be used wherever that an expression is expected. Even though that is thought by many not to be good practice. The value of the expression a = b is the value that was assigned to a. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 31 / 38
Assignments An assignment will be represented by an assignment tree (ASSIGN). The left subtree will be the lval. The right subtree will be the expression. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 32 / 38
ASSIGN Trees An ASSIGN tree is of the form ASSIGN type ID entry expr type Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 33 / 38
ASSIGN Trees ASSIGN Trees new TreeNode(ASSIGN, id.tree, expr.tree); Construct an assignment tree. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 34 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 35 / 38
Other Productions Other Productions expr ( expr ) expr - expr stmt READ lval stmt PRINT lval How would we build the syntax trees for the above productions? Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 36 / 38
Outline 1 Building the AST 2 TreeNode Constructors NUM Nodes ID Nodes Declarations Dereferencing Expressions Assignments 3 Other Productions 4 Assignment Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 37 / 38
Assignment Assignment p. 323: 1. Robb T. Koether (Hampden-Sydney College) Building the Abstract Syntax Trees Wed, Mar 18, 2015 38 / 38