![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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051812/602c27ac94c9955f3a7e227f/html5/thumbnails/17.jpg)
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)