Docsity

Abstract Syntax Tree Design and Construction in Compilers, Lecture notes of Computer Science

Partial preview of the text

Download Abstract Syntax Tree Design and Construction in Compilers and more Lecture notes Computer Science in PDF only on Docsity!

Lecture 7 Abstract Syntax Tree

Xiaoyin Wang

CS 5363 Programming

Languages and Compilers

Abstract Syntax Tree (AST)

• The Most Important Intermediate

Presentation in Compilers

• AST Design

• AST Decoration

  • (^) Type Inference
  • (^) Scope Analysis
  • (^) Symbol Table

AST Design

  • (^) Do not use single class AST_node
  • (^) E.g., need information for if, while, +, *, ID,

NUM

class AST_node {

int node_type;

AST_node[ ] children;

String name; int value; …etc…

  • (^) Problem: must have fields for every

different kind of node with attributes

  • (^) Not extensible, Java type checking no help

Use Class Hierarchy

  • (^) Use subclassing to solve problem
    • (^) Use abstract class for each “interesting” set

of nonterminals (e.g., expressions)

E  E+E | E*E | -E | (E)

abstract class Expr { … }

class Add extends Expr { Expr left, right; … }

class Mult extends Expr { Expr left, right; … }

// or: class BinExpr extends Expr { Oper o; Expr l,

r; }

class Minus extends Expr { Expr e; …}

a - b - c Valid

a > b > c invalid

AST Construction

  • (^) Separate AST construction from semantic

checking phase

  • (^) Traverse AST (visit) and perform semantic

checks (or other actions) only after tree has

been built and its structure is stable

  • (^) Approach is more flexible and less error-prone
    • (^) It is better when efficiency is not a critical issue

7

Illustration of the Parsing Phase

  • (^) Parsing: LL(1)
  • (^) AST Data Structure Design
  • (^) AST Generation
    • (^) Tree reduction and transformation (parser tree to

abstract syntax tree)

  • (^) AST Generation on the fly
  • (^) AST Visitor Design
  • (^) Symbol Table Generation
  • (^) Type Annotation

Left-factoring

• The only production requiring left-factoring

  • (^) → + |
  • (^) →
  • (^) → + | ε

Left-factored Grammar for

Illustration

  1. → begin
  2. → | ε
  3. → int ident | bool ident
  4. → if then
  5. →
  6. → + | ε
  7. → ident

LL(1) Parsing: First/Follow Sets

  • (^) Follow Set
    • (^) Follow() = {$}
    • (^) Follow() = {begin}
    • (^) Follow() = {int, bool, begin}
    • (^) Follow() = {$}
    • (^) Follow() = Follow() = {$, then}
    • (^) Follow() = {+, then, $}

Left-factored Grammar for

Illustration

1. → begin

2. → | ε

3. → int ident | bool ident

4. → if then

5. →

6. → + | ε

7. → ε

8. → ident

Parser Tree Generation

  • (^) Input:

int x

bool y

begin

if y then x + x

Parser Tree Generation

  • (^) Derivation

-> begin

-> begin

-> int x begin

-> int x begin

=> int x bool y begin

-> int x bool y begin if then

-> int x bool y begin if then

begin

int

bool

ε

if

then

x

x ε

y

ε

y

x

AST Data Structure

  • (^) What are important nonterminals and terminals
    • (^) prog: decl + code
    • (^) decl: a list of defs
    • (^) def: type + identifier
    • (^) code: condition + executing expression
    • (^) expression: left, right
    • (^) identifier