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

A detailed lecture on abstract syntax trees (ast) in the context of compilers. It covers ast design, decoration, and construction, including type inference, scope analysis, and symbol table. The lecture also discusses the difference between derivation trees and ast, and the advantages and disadvantages of ast generation on-the-fly versus parser tree reduction. The document also includes a discussion on ll(1) parsing, first/follow sets, and parsing table, as well as ast data structure design and generation on-the-fly. The lecture is part of a university course on programming languages and compilers.

Typology: Lecture notes

2022/2023

Uploaded on 02/21/2024

keashyn-naidoo
keashyn-naidoo 🇺🇸

1 document

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 7 Abstract Syntax Tree
Xiaoyin Wang
CS 5363 Programming
Languages and Compilers
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

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. → 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