/
1 Topic 4: 	Abstract Syntax 1 Topic 4: 	Abstract Syntax

1 Topic 4: Abstract Syntax - PowerPoint Presentation

yvonne
yvonne . @yvonne
Follow
342 views
Uploaded On 2022-06-14

1 Topic 4: Abstract Syntax - PPT Presentation

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 ID: 917666

stmt int string symbol int stmt symbol string expr parse abstract datatype trees insertion functional table ast tree symbols

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "1 Topic 4: Abstract Syntax" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

1

Topic 4: Abstract Syntax Symbol Tables

COS 320

Compiling Techniques

Princeton University

Spring 2016

Lennart

Beringer

Slide2

2

Abstract Syntax

Slide3

3

Parse TreesWe have been looking at concrete

parse trees, in which

inner nodes are

nonterminals

, leaf nodes are

terminals

c

hildren 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, butthe tree structure already describes the program structure

stmt

stmt

stmt

SEMI

:

:

s

tmt

stmt

SEMI

stmt

s

tmt

 …

Slide4

4

Parse Tree Example

Slide5

5

Abstract parse trees (aka abstract syntac

t

ree –

AST

)

l

ike 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

Slide6

6

Abstract parse trees (aka abstract syntac

t

ree –

AST

)

l

ike 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

Compound

Stmt

Assign

Stmt

“a”

NumExpr(3)

Assign

Stmt

“b”

Num

Expr

(4)

d

atatype

s

tmt

=

CompoundStmt

of

stmt

*

stmt

|

AssignStmt

of string * expr;

datatype expr =

NumExpr

of

int

|

binopExpr

of expr *

binop

* expr;

Slide7

7

Abstract parse trees (aka abstract syntac

t

ree –

AST

)

l

ike 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

Compound

Stmt

Assign

Stmt

“a”

NumExpr(3)

Assign

Stmt

“b”

Num

Expr

(4)

First approximation

: nonterminal

synt

.

c

ategory; 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

d

atatype

s

tmt

=

CompoundStmt

of

stmt

*

stmt

|

AssignStmt

of string * expr;

datatype expr =

NumExpr

of

int

|

binopExpr

of expr *

binop

* expr;

Slide8

8

Semantic Analysis: Symbol Tables

Slide9

9

Symbol Table Example

f

unction 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) )

Slide10

10

Symbol Table Implementation

Slide11

11

Imperative Symbol Tables

Slide12

12

Functional Symbol Tables

Association list (

cf

HW 1) not efficient (lookup and delete linear)

Slide13

13

Functional Symbol Tables

Slide14

14

Functional Symbol Table using BST: lookupUse the “less than” relation to navigate down the tree

c -> real

f

->

int

d -> string

s

-> string

t

->

int

Slide15

15

Functional Symbol Table using BST: insertion

c -> real

f

->

int

d -> string

s

-> string

t

->

int

z

->

int

Insertion of z->

int

:

c

reate node

Slide16

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

:

c

reate node

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

Slide17

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

:

c

reate node

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

insert links to siblings in original (share subtree)