Pla an Traversal Fu unctions (1) Booleans Many functions have - - PowerPoint PPT Presentation

pla an traversal fu unctions 1
SMART_READER_LITE
LIVE PREVIEW

Pla an Traversal Fu unctions (1) Booleans Many functions have - - PowerPoint PPT Presentation

Pla an Traversal Fu unctions (1) Booleans Many functions have the e characteristic that they traverse the tree recursiv traverse the tree recursiv vely and only do vely and only do Steps towards a Pico e S d Pi nvironment i


slide-1
SLIDE 1

Pla

  • Booleans

S d Pi

  • Steps towards a Pico e

– Step 1: define syntax – Step 2: define a type che – Step 3: define an evaluat

Step 3: define an evaluat

– Step 4: define a compile

T l f ti

  • Traversal functions
  • Methodology

Introduction

an

i nvironment

ecker tor tor er

to ASF+SDF 1

Traversal Fu

  • Many functions have the

traverse the tree recursiv traverse the tree recursiv something interesting at E l t th id

  • Example: count the iden
  • Using a recursive (induc

– # of equations is equal to

think about Cobol or Jav

– think about Cobol or Jav

  • Traversal functions auto

Introduction

unctions (1)

e characteristic that they vely and only do vely and only do t a few nodes tifi i ntifiers in a program ctive) definition:

  • number of syntax rules

va with hundreds of rules va with hundreds of rules

  • mate recursion

to ASF+SDF 2

Traversal Fu

There are two important as functions: functions:

– the kind of traversal

  • accumulate a value durin
  • transform the tree during

h d f l

– the order of traversal

  • top-down versus bottom-
  • left-to-right versus right-
  • break or continue after a

Introduction

unctions (2)

spects of traversal

ng traversal g traversal

  • up
  • to-left (we only have the first)

visit

to ASF+SDF 3

Top-down vers

Top-down 1 2 5 3 4 6 7

Introduction

sus Bottom-up

Bottom-up 7 3 6 1 2 4 5

to ASF+SDF 4

slide-2
SLIDE 2

Three kinds

  • Accumulator: traversal

l l d i

– accumulate a value durin

  • Transformer: traversal(

– perform local transforma

  • Accumulating transform
  • Accumulating transform

– accumulate and transform

Introduction

  • f traversals

(accu) l ng traversal (trafo) ations

mer: traversal(accu trafo) mer: traversal(accu, trafo)

m

to ASF+SDF 5

Traversal Cube: v Traversal Cube: v

Top-down B tt Bottom-up Break Conti Break Conti

Introduction

visiting behaviour visiting behaviour

inue Left-to-right Right-to-left inue

to ASF+SDF 6

Simple

module Tree-syntax imports Naturals t exports sorts TREE context-free syntax NAT -> TREE f(TREE, TREE) -> TREE g(TREE TREE) -> TREE g(TREE, TREE) > TREE h(TREE, TREE) -> TREE variables “N”[0 9]* NAT “N”[0-9]* -> NAT “T”[0-9]* -> TREE

Introduction

e Trees

Simple trees containing numbers as leaves and constructorsf g or h constructors

f, g, or h

E E E E T E

to ASF+SDF 7

Count node

module Tree-cnt imports Tree-syntax exports p context-free syntax cnt(TREE) -> NAT equations equations [1] cnt(N) = 1 [2] cnt(f(T1,T2)) = 1+cnt(T1)+cnt(T2) ( ( )) ( ) ( ) [3] cnt(g(T1,T2)) = 1+cnt(T1)+cnt(T2) [4] cnt(h(T1,T2)) = 1+cnt(T1)+cnt(T2

Count this node

cnt( f( g( f(1,2), 3 ), g( g(4,5), 6 )),

Count this node

Introduction

g( g( , ), )), )

s (classical)

Count the nodes in a tree These equations are needed to visit all nodes in the tree

) )

A new equation has to be added for each new constructor

) )

added for each new constructor

11

Count nodes in both subtrees

to ASF+SDF 8

slide-3
SLIDE 3

Exam

L

cnt( f( g( 7, 8), 9) ) [

Le

1 + cnt(g( 7, 8)) + cnt(9) [ 1 1 t(7) t(8) t(9) 1 + 1 +cnt(7) + cnt(8) + cnt(9) 2 +cnt(7) + cnt(8) + cnt(9) ( ) ( ) ( ) 2 + 1 + cnt(8) + cnt(9) 5

Introduction

5

mple

f i d i

[2] cnt(f(T1,T2)) = 1+cnt(T1)+cnt(T2)

ft-most innermost reduction:

[3] cnt(g(T1,T2)) = 1+cnt(T1)+cnt(T2) Addition of integers [1] cnt(N) = 1 ... Similar reductions

to ASF+SDF 9

Using Acc

  • Goal: traverse term and
  • fun(Tree, Accu) -> Accu {

f ( , ) {

  • Tree: term to be traversed
  • Accu: value to be accumul

argument)

  • Important: the sorts of seco

always equal. y q

  • Optional: extra arguments

f (T A A1 )

Introduction

  • fun(Tree, Accu, A1, ...) ->

cumulators

accumulate a value

{traversal(accu, ...)} { ( , )}

(always the first argument) ated (always second

  • nd argument and result are

A {t l( )}

to ASF+SDF 10

Accu {traversal(...)}

Count nodes

module Tree-cnt imports Tree-syntax exports exports context-free syntax cnt(TREE, NAT) -> NAT {traversa equations [1] cnt(T, N) = N + 1

Traversed tree (mat Acc

cnt( f( g( f(1,2), 3 ), g( g(4,5), 6 )), 0)

Introduction

0)

s (traversals)

A bottom up accumulator that A bottom-up accumulator that continues after each matching node

al(accu,bottom-up,continue)}

tches every node) cumulated value

11

to ASF+SDF 11

Example: accu,

cnt( f( g( 7, 8), 9), 0 ) f

cnt( , 0)

f

cnt( , 0)

g 7 8 7 8 [1] cnt(T,N) = N + 1

Introduction

[1] cnt(T,N) N 1

,bottom-up,continue

5 9

to ASF+SDF 12

slide-4
SLIDE 4

Example: accu,

cnt( f( g( 7, 8), 9), 0 )

cnt( , 0)

f

( , ) cnt( , 4) c cnt( , 2)

g

cnt( , 0) cnt( , 1)

7 8

( , ) cnt( , 1)

[1] cnt(T, N) = N + 1

Introduction

[ ] ( , N) N

bottom-up,continue

5

cnt( , 3)

9

to ASF+SDF 13

Using Tra

  • fun(Tree) -> Tree {trave
  • Tree: term to be traversed
  • Important: the sorts of the f

p always equal.

  • Optional: extra arguments

Optional: extra arguments

  • fun(Tree, A1, A2, ...) ->

Introduction

ansformers

ersal(trafo, ...)} (always the first argument) first argument and result are g Tree {traversal(...)}

to ASF+SDF 14

Incremen

d l T i module Tree-inc imports Tree-syntax exports p context-free syntax inc(TREE) -> TREE {traversa equations equations [1] inc(N) = N + 1

is leaf N

inc( f( g( f(1,2), 3 ),

g( g(4,5), 6 )) )

Introduction

nt leaves

A bottom-up transformer that A bottom-up transformer that continues after each matching node

al(trafo,bottom-up,continue)}

replaced by N+1

f( g( f(2,3), 4 ), g( g(5,6), 7 ))

to ASF+SDF 15

Exam

inc( f( g( 7, 8), 9))

inc( )

f

nc( ) inc( ) i inc( )

g

inc( ) inc( )

7 8

( ) inc( )

[1] inc(T, N) = N + 1

Introduction

mple trafo,bottom-up,continue

f f( g( 8, 9), 10) f

inc( )

10 9 g 8 9

to ASF+SDF 16

slide-5
SLIDE 5

Increment leaves w

d l T i module Tree-incp imports Tree-syntax exports p context-free syntax inc(TREE, NAT) -> TREE {traver equations equations [1] inc(N1, N2) = N1 + N2

repl amount N2 leaf N1 inc( f( g( f(1,2), 3 ),

g( g(4,5), 6 )), 7 )

Introduction

7 )

with explicit amount

A bottom up transformer that A bottom-up transformer that continues after each matching node

rsal(trafo,bottom-up,continue)}

Amount lace N1 by N1+N2

f( g( f( 8, 9), 10), g( g(11,12), 13))

to ASF+SDF 17

g( g( , ), ))

Exam

inc( f( g( 7, 8), 9), 5)

inc( , 5)

f

nc( , 5) inc( , 5) i inc( , 5)

g

inc( , 5) inc( , 5)

7 8

( , ) inc( , 5)

[1] inc(N1, N2) = N1 + N2

Introduction

mple

trafo,bottom-up,continue f f( g( 12, 13), 14) f

inc( , 5)

9 14 g 12 13 12 13

to ASF+SDF 18

Term Rep

  • Deep replacement: repla

to the leaves to the leaves

  • Shallow replacement: re

l t th t close to the root

  • Full replacement: replac

Shallow Deep

Introduction

placement

ace only occurrences close eplace only occurrences ce all occurrences

Full

to ASF+SDF 19

Deep rep

d l T d l module Tree-drepl imports Tree-syntax exports context-free syntax i(TREE, TREE) -> TREE drepl(TREE) -> TREE {tra drepl(TREE) TREE {tra equations [1] drepl(g(T1, T2)) = i(T1, T2)

Only the deepest occurren are replaced

drepl( f( g( f(1,2), 3 ), g( g(4,5), 6 )) )

Introduction

lacement

Auxiliary constructor i Auxiliary constructor i A bottom-up transformer that f fi hi d

versal(trafo bottom-up break)}

stops after first matching node

versal(trafo,bottom up,break)}

nces of g

f( i( f(1,2), 3 ), g( i(4,5), 6 ))

to ASF+SDF 20

slide-6
SLIDE 6

Exam

drepl( g( g( 7, 8), 9))

drepl( )

g

p ( )

g

dr drepl( )

g

drepl( ) drepl( )

7 8

p ( ) drepl( )

[1] drepl(g(T1, T2)) = i(T1, T2)

Introduction

mple

trafo,bottom-up,break g g( i( 7, 8), 9) g

repl( )

9 9 i 7 8 7 8

to ASF+SDF 21

Shallow re

d l T l module Tree-srepl imports Tree-syntax exports context-free syntax i(TREE, TREE) -> TREE srepl(TREE) -> TREE {tra srepl(TREE) TREE {tra equations [1] srepl(g(T1, T2)) = i(T1, T2)

Only the outermost occurre are replaced

srepl( f( g( f(1,2), 3 ), g( g(4,5), 6 )) )

Introduction

eplacement

A top-down transformer that stops after first matching node

aversal(trafo top-down break)}

stops after first matching node

aversal(trafo, top down, break)} )

ences of g

f( i( f(1,2), 3 ), i( g(4,5), 6 ))

to ASF+SDF 22

Exam

srepl( g( g( 7, 8), 9))

srepl( )

g

p ( )

g g 7 8 [1] srepl(g(T1, T2)) = i(T1, T2)

Introduction

mple

trafo, top-down, break i i( g( 7, 8), 9) i 9 9 g 7 8 7 8

to ASF+SDF 23

Full repl

d l T f l module Tree-frepl imports Tree-syntax exports context-free syntax i(TREE, TREE) -> TREE frepl(TREE) -> TREE {tra frepl(TREE) TREE {tra equations [1] frepl(g(T1, T2)) = i(T1, T2)

All occurrences of

g are re

frepl( f( g( f(1,2), 3 ), g( g(4,5), 6 )) )

Introduction

lacement

A top-down transformer that continues after each matching node

aversal(trafo top-down continue)}

continues after each matching node

aversal(trafo,top down,continue)} )

top-down and bottom-up have bottom up have here the same effect eplaced

f( i( f(1,2), 3 ), i( i(4,5), 6 ))

to ASF+SDF 24

slide-7
SLIDE 7

Exam

frepl( g( g( 7, 8), 9))

frepl( )

g

p ( )

g g

frepl( ) fr

7 8

frepl( ) frepl( )

[1] frepl(g(T1, T2)) = i(T1, T2)

p ( ) p ( )

Introduction

mple

trafo, top-down, continue i i( i( 7, 8), 9) i 9 i 9

repl( )

7 8 7 8

to ASF+SDF 25

Exam

frepl( g( g( 7, 8), 9))

frepl( )

g

p ( )

g g

frepl( ) fr

7 8

frepl( ) frepl( )

[1] frepl(g(T1, T2)) = i(T1, T2)

p ( ) p ( )

Introduction

mple

trafo, bottom-up, continue i i( i( 7, 8), 9) i 9 i 9

repl( )

7 8 7 8

to ASF+SDF 26

A real example: Co

  • Cobol 75 has two forms

– “IF” Expr “THEN” Sta

p

– “IF” Expr “THEN” stat

Th id ti l (d

  • These are identical (dan

IF expr THEN IF expr THEN stats stats ELSE

Introduction

stats

  • bol transformation

s of conditional:

ts “END-IF”? ts “ELSE” Stats “END-IF”?

li l bl ) ngling else problem):

IF expr THEN IF expr THEN IF expr THEN stats stats ELSE stats

to ASF+SDF 27

A real example: Co

module End-If-Trafo module End If Trafo imports Cobol exports context-free syntax context-free syntax addEndIf(Program)-> Program {traver down)} variables variables "Stats"[0-9]* -> StatsOptIfNotClo "Expr"[0-9]* -> L-exp "OptThen"[0 9]* > OptThen OptThen [0-9] -> OptThen equations [1] addEndIf(IF Expr OptThen Stats) IF Expr OptThen Stats END I IF Expr OptThen Stats END-I [2] addEndIf(IF Expr OptThen Stats1 IF Expr OptThen Stats1 ELSE

Introduction

IF Expr OptThen Stats1 ELSE

  • bol transformation

END IF

Add missing END-IF keywords

rsal(trafo,continue,top-

  • sed

Impossible to do with regular expression tools like grep since conditionals can be nested

= IF

conditionals can be nested

IF ELSE Stats2) = Stats2 END IF

Equations for the two cases

to ASF+SDF 28

Stats2 END-IF

slide-8
SLIDE 8

A funny Pico

  • Replace all variables by

– x +3  type(natural) +

  • Simplify type correct ex

p y yp

– type(natural) + type(na

R ll

  • Remove all type correct

– type(natural) := type(n

yp ( ) yp (

  • A type correct program

Oth i l i

Introduction

  • Otherwise, only incorrec
  • typechecker

y their declared type:

+ type(natural)

xpressions: p

atural)  type(natural)

t statements:

natural)

reduces to empty t t t t i

to ASF+SDF 29

ct statements remain

Exam

begin declare x : natural, y : natural, s : string; x := 10; s := "abc"; x : 10; s : abc ; if x then x := x + 1 ls else s := x + 2 fi;

E

y := x + 2; end

E

Introduction

mple

Yields after typechecking:

begin declare;

Yields after typechecking:

declare; type(string) := type(natural); end

Erroneous statement leaves a residue Erroneous statement leaves a residue

to ASF+SDF 30

Pico-type

module Pico-typecheck imports Pico-syntax exports context-free syntax type(TYPE) -> ID replace(STATS, ID-TYPE) -> STATS {tr replace(EXP , ID-TYPE) -> EXP {trave

The traversal function

repla

b f i t E may be of various sorts. Ea has to be declared here.

Introduction

echeck (1)

Extend identifiers so that we can replace them with type information

raversal(trafo,bottom-up,break)} ersal(trafo,bottom-up,break)}

  • ce. In the equations, the first argument

h i t th t i d i th ti ach variant that is used in the equations

to ASF+SDF 31

Pico-type

equations [0] begin declare Id-type, Decl*; Stat* e begin declare Decl*; replace(Stat*, I end [1] replace(Id , Id : Type) = type(Type) [2] replace(Nat-con, Id : Type) = type(nat [3] replace(Str-con, Id : Type) = type(str [4] type(string) || type(string) = type(str [5] type(natural) + type(natural) = type(na [6] type(natural) - type(natural) = type(na

Introduction

echeck (2)

Vi it h i bl d l ti

nd =

Visit each variable declaration and use replace to replace the variable by its type

d-type)

Replace variables and

) tural) ring)

Replace variables and constants by their type

ring) atural)

Replace type-correct expressions by their type

atural)

to ASF+SDF 32

slide-9
SLIDE 9

Pico-type

[7] Stat*1; if type(natural) then [7] Stat*1; if type(natural) then = Stat*1; Stat*2; Stat*3; St [8] Stat*1; while type(natural) d [8] Stat*1; while type(natural) d = Stat*1; Stat*2; Stat*3 [9] Stat*1; type(Type) : type(T [9] Stat*1; type(Type) := type(T = Stat*1; Stat*2

Introduction

echeck (3)

n Stat*2 else Stat*3 fi ; Stat*4 n Stat*2 else Stat*3 fi ; Stat*4 tat*4 do Stat*2 od; Stat*3 do Stat*2 od; Stat*3 Type); Stat*2 Type); Stat*2

Remove type-correct expressions and statements

to ASF+SDF 33

Disambiguation v Disambiguation v

S ti di t d di b

  • Semantic directed disamb

– Based on the concept of re – Applications

  • C typedefs

yp

  • Nested constructs in COBO

Introduction

via traversals (1) via traversals (1)

bi ti biguation

ewriting parse forests.

OL

to ASF+SDF

Disambiguation v Disambiguation v

A li ti C t d f

  • Application C typedefs:

– In C certain identifiers can be

variable identifiers due to oper variable identifiers due to oper { Bool *b1; } The above statement is either

– The above statement is either

  • a statement expression multiplyi
  • a declaration of a pointer variabl
  • a declaration of a pointer variabl

– The latter derivation is chosen

  • if Bool was declared to be a typ

earlier in the program, otherwise

  • the former derivation is chosen.

Introduction

via traversals (2) via traversals (2)

parsed as either type identifiers or rator overloading: rator overloading:

ing the Bool and b1 variables, le b1 to a Bool. le b1 to a Bool.

n

pe using a typedef statement somewhere e

to ASF+SDF

Disambiguation v Disambiguation v

A li ti C t d f

  • Applications C typedefs

– Compact specification tha

in the C language.

– Depending on the existenc

identifier is either interpre

  • a type name or
  • a variable name.
  • We construct an environm

types.

Introduction

via traversals (3) via traversals (3)

at filters one of the ambiguities ce of a typedef declaration an eted as

ment containing all declared

to ASF+SDF

slide-10
SLIDE 10

Disambiguation v Disambiguation v

A li ti C t d f

  • Applications C typedefs

– If the first Statement after a lis

  • f an identifier that is declared
  • f an identifier that is declared

tree is removed.

context-free syntax "types" "[[" Identifier* "]]" -> En filter(CompoundStatement, Env) -> C {traversal(accu,trafo,top-down,bre { ( , , p , equations [] Env = types[[Ids1 Id Ids2]] ====> filter(amb(CSs1,{Decls Id * Expr

Introduction

via traversals (4) via traversals (4)

st of Declarations is a multiplication d to be a type the corresponding sub d to be a type, the corresponding sub-

nv CompoundStatement eak)} )} r;Stats},CSs2),Env) = amb(CSs1,CSs2)

to ASF+SDF

Disambiguation v Disambiguation v

N t th f t C

  • Note the use of concrete C sy
  • The filter function searches a

h h fi statements where the first sta variable which was declared i il l dd d f

  • Similar rules are added for ev

an ambiguity is caused by the identifiers and variable identi identifiers and variable identi

  • This amounts to about a doze

– they solve the ambiguities and – document exactly where our C

Introduction

via traversals (5) via traversals (5)

t i thi l yntax in this example. and removes ambiguous block- id ifi atement uses an identifier as a earlier as a type. f h h very part of the C syntax where e overlap between type ifiers ifiers. en rules:

d C grammar is ambiguous.

to ASF+SDF

Disambiguation v Disambiguation v

Th l bl th

  • The example resembles th

construction

  • It is more complex due to

constructs are optional.

0001 ADD A TO B 0002 SIZE ERROR 0003 ADD C TO D 0004 NOT SIZE ERROR 0005 CONTINUE 0006 .

Introduction

via traversals (6) via traversals (6)

h d li l he dangling else

  • the fact that more

to ASF+SDF

Disambiguation v Disambiguation v

Th SIZE ERROR d NOT

  • The SIZE ERROR and NOT
  • ptional post-fixes of the AD

considered as a kind of excep considered as a kind of excep

  • In order to understand what i

COBOL grammar: COBOL grammar:

Add-stat ::= Add-stat-simple Size Size-error-phrases ::= Size-error Size error phrases :: Size error Size-error-stats ::= "SIZE" "ERRO Not-size-error-stats ::= "NOT" "S Statement-list ::= Statement*

Introduction

via traversals (7) via traversals (7)

SIZE ERROR t t SIZE ERROR constructs are DD statement. They can be ption handling ption handling. is going on we show a part of a

e-error-phrases r-stats? Not-size-error-stats? r stats? Not size error stats? OR" Statement-list SIZE" "ERROR" Statement-list

to ASF+SDF

slide-11
SLIDE 11

Disambiguation v Disambiguation v

Th h th t th

  • The grammar shows that the

not provide explicit scope de Statement-lists Statement lists.

  • The NOT SIZE ERROR can

statement on line 0001 or 000 statement on line 0001 or 000

  • The period on line 0006 close
  • The COBOL definition states

should always be taken with is the ADD statement on line is the ADD-statement on line

  • There are 16 of such ambigui

Introduction

via traversals (8) via traversals (8)

COBOL l d i d COBOL language design does limiters for some deeply nested be either part of the ADD- 03 03. es both statements. s that the “dangling” phrase the innermost construct, which e 0003 e 0003. ities in the COBOL definition.

to ASF+SDF

Disambiguation v Disambiguation v

Th t d d li t

  • The nested dangling construc

using a simple specification. Th i i f i

  • There is no context informati

structural analysis. h i l fil h d

  • The rewrite rule filters the de

block of code was not assign

equations [] amb(ASs1, AddSt tSi l 1 AddStatSimple1 SIZE ERROR Stats1 AddS NOT SIZE ERROR St t 2 NOT SIZE ERROR Stats2,

Introduction

via traversals (9) via traversals (9)

t i COBOL b filt d cts in COBOL can be filtered i i l d j i l ion involved, just a simple i i h h d li erivations where the dangling ed to the correct branch:

tatSimple2 AS 2) b(AS 1 AS 2) ASs2) = amb(ASs1, ASs2)

to ASF+SDF