1 topic 4: abstract syntax symbol tables cos 320 compiling techniques princeton university spring...

17
1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer

Upload: harold-lane

Post on 20-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

3 Parse Trees We have been looking at concrete parse trees, in which inner nodes are nonterminals, leaf nodes are terminals children are labeled with the symbols in the RHS of the production Concrete parse trees are inconvenient to use, since they are cluttered with tokens containing no additional information: punctuation symbols (SEMI etc) needed to specify structure when writing code, but the tree structure already describes the program structure stmt SEMI :: stmt  stmt SEMI stmt stmt  …

TRANSCRIPT

Page 1: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

1

Topic 4: Abstract SyntaxSymbol Tables

COS 320

Compiling Techniques

Princeton University Spring 2016

Lennart Beringer

Page 2: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

2

Abstract Syntax

Page 3: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

3

Parse Trees

We have been looking at concrete parse trees, in which• inner nodes are nonterminals, leaf nodes are terminals• children are labeled with the symbols in the RHS of the production

Concrete parse trees are inconvenient to use, since they are cluttered with tokens containing no additional information:

• punctuation symbols (SEMI etc) needed to specify structure when writing code, but

• the tree structure already describes the program structure

stmt

stmt stmtSEMI: :

stmt stmt SEMI stmtstmt …

Page 4: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

4

Parse Tree Example

Page 5: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

5

Abstract parse trees (aka abstract syntac tree – AST)

• like concrete parse trees (e.g. inductive datatype, generated as semantic action by YACC)

• each syntactic category (expressions, statements,..) is represented as a separate datatype, with one constructor for each formation

• redundant punctuation symbols are left out

Page 6: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

6

Abstract parse trees (aka abstract syntac tree – AST)

• like concrete parse trees (e.g. inductive datatype, generated as semantic action by YACC)

• each syntactic category (expressions, statements,..) is represented as a separate datatype, with one constructor for each formation

• redundant punctuation symbols are left out

CompoundStmt

AssignStmt

“a” NumExpr(3)

AssignStmt

“b” NumExpr(4)

datatype stmt = CompoundStmt of stmt * stmt| AssignStmt of string * expr;

datatype expr = NumExpr of int| binopExpr of expr * binop * expr;

Page 7: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

7

Abstract parse trees (aka abstract syntac tree – AST)

• like concrete parse trees (e.g. inductive datatype, generated as semantic action by YACC)

• each syntactic category (expressions, statements,..) is represented as a separate datatype, with one constructor for each formation

• redundant punctuation symbols are left out

CompoundStmt

AssignStmt

“a” NumExpr(3)

AssignStmt

“b” NumExpr(4)

• First approximation: nonterminal synt. category; CFG rule constructor• But: AST is internal interface between components of compiler, so AST

design is up to compiler writer, not the language designer; may deviate from organization suggested by grammar/syntax

datatype stmt = CompoundStmt of stmt * stmt| AssignStmt of string * expr;

datatype expr = NumExpr of int| binopExpr of expr * binop * expr;

Page 8: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

8

Semantic Analysis: Symbol Tables

Page 9: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

9

Symbol Table Example

function f (b:int, c:int) = (print_int (b+c); let var j:= b var a := “x” in print (a); print_int (j) end; print_int (a) )

Page 10: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

10

Symbol Table Implementation

Page 11: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

11

Imperative Symbol Tables

Page 12: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

12

Functional Symbol Tables

Association list (cf HW 1) not efficient (lookup and delete linear)

Page 13: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

13

Functional Symbol Tables

Page 14: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

14

Functional Symbol Table using BST: lookup

Use the “less than” relation to navigate down the tree

c -> real

f -> int

d -> string s -> string

t -> int

Page 15: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

15

Functional Symbol Table using BST: insertion

c -> real

f -> int

d -> string s -> string

t -> int

z -> int

Insertion of z-> int: 1. create node

Page 16: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

16

Functional Symbol Table using BST: insertion

c -> real

f -> int

d -> string s -> string

t -> int

f -> int

t -> int

z -> int

Insertion of z-> int: 1. create node2. “search” for z in old tree; copy ancestor nodes

Page 17: 1 Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016…

17

Functional Symbol Table using BST: insertion

c -> real

f -> int

d -> string s -> string

t -> int

f -> int

t -> int

z -> int

Insertion of z-> int: 1. create node2. “search” for z in old tree; copy ancestor nodes3. insert links to siblings in original (share subtree)