topic 4: abstract syntax symbol tables · topic 4: abstract syntax symbol tables cos 320 compiling...

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

Upload: others

Post on 05-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

1

Topic 4: Abstract Syntax

Symbol Tables

COS 320

Compiling Techniques

Princeton University Spring 2016

Lennart Beringer

Page 2: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

2

Abstract Syntax

Page 3: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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 stmt

stmt…

Page 4: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

4

Parse Tree Example

Page 5: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

8

Semantic Analysis: Symbol Tables

Page 9: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

10

Symbol Table Implementation

Page 11: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

11

Imperative Symbol Tables

Page 12: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

12

Functional Symbol Tables

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

Page 13: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

13

Functional Symbol Tables

Page 14: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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 node

2. “search” for z in old tree; copy ancestor nodes

Page 17: Topic 4: Abstract Syntax Symbol Tables · Topic 4: Abstract Syntax Symbol Tables COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer. 2 Abstract Syntax

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 node

2. “search” for z in old tree; copy ancestor nodes

3. insert links to siblings in original (share subtree)