Building the Abstract Syntax Trees

Similar documents
Lecture 14 Sections Mon, Mar 2, 2009

Abstract Syntax Trees Synthetic and Inherited Attributes

Function Definition Syntax Tree

The x86 Instruction Set

Recursive Descent Parsers

The Critical-Path Algorithm

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Minimal Spanning Trees

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Introduction to Compiler Design

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Scope and Parameter Passing

Sampling Distribution Examples Sections 15.4, 15.5

The Class Construct Part 1

Lecture 20 Sections Wed, Apr 1, 2009

Scope and Parameter Passing

LR Parsing - Conflicts

Binary Tree Applications

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

The Decreasing-Time Algorithm

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Rotations and Translations

Dynamic Allocation of Memory

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Implementing Linked Lists

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Dynamic Allocation of Memory

Scheduling and Digraphs

The Graphics Pipeline

Density Curves Sections

The Pairwise-Comparison Method

The Traveling Salesman Problem Brute Force Method

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Street-Routing Problems

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Solving Recursive Sequences by Iteration

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Inheritance: The Fundamental Functions

Programming Languages

Programming Languages

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

PHP Queries and HTML Forms Lecture 23

Stacks and their Applications

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

XML and AJAX Lecture 28

Magnification and Minification

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

The Graphics Pipeline

The CYK Parsing Algorithm

Friends and Unary Operators

List Iterator Implementation

The Coefficient of Determination

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Recursive Linked Lists

Displaying Distributions - Quantitative Variables

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

The Plurality-with-Elimination Method

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

The Projection Matrix

Binary Tree Implementation

Binary Tree Implementation

Inheritance: The Fundamental Functions

The Normal Distribution

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

The x86 Architecture

JFlex Regular Expressions

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

The Traveling Salesman Problem Cheapest-Link Algorithm

LR Parsing - The Items

The Class Construct Part 2

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Nondeterministic Programming in C++

Introduction to Databases

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Lecture 25 Sections 8.4, 8.6. Fri, Apr 24, 2009

Insertions, Deletions, and Updates

XQuery FLOWR Expressions Lecture 35

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Recognition of Tokens

Fundamental Data Types

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Transcription:

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