pla an traversal fu unctions 1
play

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


  1. 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 something interesting at t a few nodes – Step 1: define syntax ● Example: count the iden E l t th id ntifiers in a program tifi i – Step 2: define a type che ecker ● Using a recursive (induc ctive) definition: – Step 3: define an evaluat Step 3: define an evaluat tor tor – # of equations is equal to o number of syntax rules – Step 4: define a compile er – think about Cobol or Jav think about Cobol or Jav va with hundreds of rules va with hundreds of rules ● Traversal functions T l f ti ● Traversal functions auto omate recursion ● Methodology Introduction to ASF+SDF 1 Introduction to ASF+SDF 2 Traversal Fu unctions (2) Top-down vers sus Bottom-up There are two important as spects of traversal Top-down Bottom-up functions: functions: 1 7 – the kind of traversal 2 5 3 ● accumulate a value durin ng traversal 6 ● transform the tree during g traversal – the order of traversal h d f l 3 4 6 7 1 2 5 4 ● top-down versus bottom- -up ● left-to-right versus right- -to-left (we only have the first) ● break or continue after a visit Introduction to ASF+SDF 3 Introduction to ASF+SDF 4

  2. Traversal Cube: v Traversal Cube: v visiting behaviour visiting behaviour Three kinds of traversals ● Accumulator: traversal (accu) – accumulate a value durin l l d i ng traversal l ● Transformer: traversal( (trafo) Top-down – perform local transforma ations mer: traversal(accu, trafo) mer: traversal(accu trafo) ● Accumulating transform ● Accumulating transform B tt Bottom-up Right-to-left – accumulate and transform m Left-to-right Break Break Conti Conti inue inue Introduction to ASF+SDF 5 Introduction to ASF+SDF 6 Simple e Trees Count node s (classical) Count the nodes in a tree module Tree-cnt module Tree-syntax Simple trees containing imports Tree-syntax numbers as leaves and imports Naturals exports p constructors f g or h f , g , or h exports t constructors context-free syntax sorts TREE These equations are needed cnt(TREE) -> NAT context-free syntax to visit all nodes in the tree equations equations NAT -> TREE E [1] cnt(N) = 1 f(TREE, TREE) -> TREE E A new equation has to be [2] cnt(f(T1,T2)) = 1+cnt(T1)+cnt(T2) ) g(TREE TREE) -> TREE g(TREE, TREE) > TREE E added for each new constructor added for each new constructor [3] cnt(g(T1,T2)) = 1+cnt(T1)+cnt(T2) ( ( )) ( ) ( ) ) ) h(TREE, TREE) -> TREE E [4] cnt(h(T1,T2)) = 1+cnt(T1)+cnt(T2 ) variables “N”[0-9]* -> NAT “N”[0 9]* NAT T Count this node Count this node Count nodes in both subtrees “T”[0-9]* -> TREE E cnt( f( g( f(1,2), 3 ), 11 g( g( , ), g( g(4,5), 6 )), )), ) Introduction to ASF+SDF 7 Introduction to ASF+SDF 8

  3. Exam mple Using Acc cumulators cnt( f( g( 7, 8), 9) ) Le L f ft-most innermost reduction: i d i ● Goal: traverse term and accumulate a value [2] cnt(f(T1,T2)) = 1+cnt(T1)+cnt(T2) [ ● fun(Tree, Accu) -> Accu { f ( , ) { {traversal(accu, ...)} { ( , )} 1 + cnt(g( 7, 8)) + cnt(9) ● Tree: term to be traversed [3] cnt(g(T1,T2)) = 1+cnt(T1)+cnt(T2) [ (always the first argument) 1 1 1 + 1 +cnt(7) + cnt(8) + cnt(9) t(7) t(8) t(9) ● Accu : value to be accumul ated (always second Addition of integers argument) 2 +cnt(7) + cnt(8) + cnt(9) ( ) ( ) ( ) [1] cnt(N) = 1 ● Important: the sorts of seco ond argument and result are 2 + 1 + cnt(8) + cnt(9) always equal. y q ● Optional: extra arguments ... Similar reductions 5 5 ● fun(Tree, Accu, A1, ...) -> f (T A A1 ) A Accu {traversal(...)} {t l( )} Introduction to ASF+SDF 9 Introduction to ASF+SDF 10 Example: accu, Count nodes s (traversals) ,bottom-up,continue A bottom up accumulator that A bottom-up accumulator that cnt( f( g( 7, 8), 9), 0 ) module Tree-cnt 5 continues after each matching node imports Tree-syntax cnt( , 0) cnt( , 0) exports exports f f context-free syntax cnt(TREE, NAT) -> NAT {traversa al(accu,bottom-up,continue)} equations g 9 [1] cnt(T, N) = N + 1 Acc cumulated value Traversed tree (mat tches every node) 7 7 8 8 cnt( f( g( f(1,2), 3 ), g( g(4,5), 6 )), 11 [1] cnt(T,N) = N + 1 [1] cnt(T,N) N 1 0) 0) Introduction to ASF+SDF 11 Introduction to ASF+SDF 12

  4. Using Tra ansformers Example: accu, bottom-up,continue cnt( f( g( 7, 8), 9), 0 ) 5 ● fun(Tree) -> Tree {trave ersal(trafo, ...)} cnt( , 0) ( , ) f ● Tree: term to be traversed cnt( , 4) (always the first argument) ● Important: the sorts of the f p first argument and result are g g 9 cnt( , 3) c always equal. cnt( , 2) ● Optional: extra arguments Optional: extra arguments ● fun(Tree, A1, A2, ...) -> Tree {traversal(...)} cnt( , 0) ( 7 , ) 8 cnt( , 1) cnt( , 1) [ ] [1] cnt(T, N) = N + 1 ( , N) N Introduction to ASF+SDF 13 Introduction to ASF+SDF 14 Incremen nt leaves Exam mple trafo,bottom-up,continue A bottom-up transformer that A bottom-up transformer that module Tree-inc d l T i inc( f( g( 7, 8), 9)) f( g( 8, 9), 10) continues after each matching node imports Tree-syntax exports p f inc( ) nc( ) f f inc( ) context-free syntax inc(TREE) -> TREE {traversa al(trafo,bottom-up,continue)} equations equations g g 9 10 [1] inc(N) = N + 1 i inc( ) inc( ) replaced by N+1 is leaf N inc( ) ( 7 ) 8 inc( ) inc( ) 8 9 i nc( f( g( f(1,2), 3 ), f( g( f(2,3), 4 ), g( g(4,5), 6 )) ) g( g(5,6), 7 )) [1] inc(T, N) = N + 1 Introduction to ASF+SDF 15 Introduction to ASF+SDF 16

  5. Increment leaves w with explicit amount Exam mple trafo,bottom-up,continue module Tree-incp d l T i A bottom-up transformer that A bottom up transformer that inc( f( g( 7, 8), 9), 5) f( g( 12, 13), 14) continues after each matching node imports Tree-syntax exports p inc( , 5) nc( , 5) f f f inc( , 5) context-free syntax Amount inc(TREE, NAT) -> TREE {traver rsal(trafo,bottom-up,continue)} equations equations g 9 g [1] inc(N1, N2) = N1 + N2 i inc( , 5) 14 inc( , 5) lace N1 by N1+N2 repl amount N2 leaf N1 inc( , 5) ( 7 , ) 8 inc( , 5) inc( , 5) 12 12 13 13 i nc( f( g( f(1,2), 3 ), f( g( f( 8, 9), 10), g( g(4,5), 6 )), [1] inc(N1, N2) = N1 + N2 g( g(11,12), 13)) g( g( , ), )) 7 ) 7 ) Introduction to ASF+SDF 17 Introduction to ASF+SDF 18 Term Rep placement Deep rep lacement Auxiliary constructor i Auxiliary constructor i module Tree-drepl d l T d l imports Tree-syntax ● Deep replacement: repla ace only occurrences close exports A bottom-up transformer that to the leaves to the leaves context-free syntax stops after first matching node f fi hi d i(TREE, TREE) -> TREE ● Shallow replacement: re eplace only occurrences drepl(TREE) -> TREE {tra drepl(TREE) TREE {tra versal(trafo,bottom up,break)} versal(trafo bottom-up break)} close to the root l t th t equations [1] drepl(g(T1, T2)) = i(T1, T2) ● Full replacement: replac ce all occurrences nces of g Only the deepest occurren Shallow are replaced f( i( f(1,2), 3 ), Full drepl( f( g( f(1,2), 3 ), Deep g( i(4,5), 6 )) g( g(4,5), 6 )) ) Introduction to ASF+SDF 19 Introduction to ASF+SDF 20

  6. Shallow re eplacement Exam mple trafo,bottom-up,break module Tree-srepl d l T l drepl( g( g( 7, 8), 9)) g( i( 7, 8), 9) imports Tree-syntax A top-down transformer that drepl( ) p ( ) exports g g g g stops after first matching node stops after first matching node context-free syntax i(TREE, TREE) -> TREE srepl(TREE) -> TREE {tra srepl(TREE) TREE {tra aversal(trafo, top down, break)} aversal(trafo top-down break)} g 9 equations i dr repl( ) drepl( ) 9 [1] srepl(g(T1, T2)) = i(T1, T2) ) ences of g Only the outermost occurre are replaced drepl( ) p ( 7 ) 8 drepl( ) drepl( ) 7 7 8 8 f( i( f(1,2), 3 ), srepl( f( g( f(1,2), 3 ), i( g(4,5), 6 )) g( g(4,5), 6 )) ) [1] drepl(g(T1, T2)) = i(T1, T2) Introduction to ASF+SDF 21 Introduction to ASF+SDF 22 Full repl lacement Exam mple trafo, top-down, break module Tree-frepl d l T f l srepl( g( g( 7, 8), 9)) i( g( 7, 8), 9) imports Tree-syntax A top-down transformer that srepl( ) p ( ) exports g g i i continues after each matching node continues after each matching node context-free syntax i(TREE, TREE) -> TREE frepl(TREE) TREE {tra frepl(TREE) -> TREE {tra aversal(trafo top-down continue)} aversal(trafo,top down,continue)} g 9 equations g 9 [1] frepl(g(T1, T2)) = i(T1, T2) ) top-down and bottom-up have bottom up have g are re here the same effect All occurrences of eplaced 7 8 7 7 8 8 f( i( f(1,2), 3 ), frepl( f( g( f(1,2), 3 ), i( i(4,5), 6 )) g( g(4,5), 6 )) ) [1] srepl(g(T1, T2)) = i(T1, T2) Introduction to ASF+SDF 23 Introduction to ASF+SDF 24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend