Homework 8 Functional Visitors in DJ Use functional

  • Slides: 13
Download presentation
Homework 8 Functional Visitors in DJ

Homework 8 Functional Visitors in DJ

 • Use functional visitors to write a program that stores at each node

• Use functional visitors to write a program that stores at each node in the abstract syntax tree the set of free variables. • //Program. cd • Program = Node. List. • Node : lookahead (@ 2 @) App. Node | Let. Node | lookahead (@ 2 @) Assign. Node | look ahead (@ 2 @) Add. Node| Term | Proc. Node. • Let. Node = "let" Bindings "in" Node. List. • Add. Node = <a> Term "+" <b> Term. • Term : Var. Node | Lit. Node. • Var. Node = Ident. • Lit. Node = <v> int. • Proc. Node = "proc" "(" [ Formal. Parameters ] ")" Node. List. • App. Node = Ident "(" [ Actual. Parameters ] ")". • Assign. Node = Ident "=" Node.

 • • • Bindings = List(Binding). Binding = Ident "=" Node. Formal. Parameters

• • • Bindings = List(Binding). Binding = Ident "=" Node. Formal. Parameters = NList(Formal. Parameter). Formal. Parameter = Ident. Actual. Parameters = NList(Actual. Parameter). Actual. Parameter = Node. List = "begin" List(Statement) "end". Statement = Node "; ". NList(S) ~ S {", " S}. List(S) ~ {S}.

begin //a let x = 1 y = 2 //a in begin proc(x, y)

begin //a let x = 1 y = 2 //a in begin proc(x, y) begin x+y; end; let h=3 //a, x, y in begin h = a(x, y); end; ll = 1; end

Functional Visitors • Enable functional-style computation combinations, which are desirable for a lot of

Functional Visitors • Enable functional-style computation combinations, which are desirable for a lot of recursive computation scenarios. • Object combine(Object[] values)specify the desired computation combination. • Enable the flexibility of users to control the traversal according to the computation of Visitor itself. • Object apply(String label) go down to the edge labeled as label and return the value from the sub traversal;

Let. Node let h=3 bindings in begin h = a(x, y); Bindings end; nodelist

Let. Node let h=3 bindings in begin h = a(x, y); Bindings end; nodelist Node. List * {h, a, x, y} – {h} Binding + {} = {a, x, y} Ident Node Free. Variables(Let. Node) = (Free. Variables(Node. List) – Idents) + Free. Variables(Nodes)

Solution one /proj/demeter/com 3362/w 02/students/wupc] 1: dehg fine around method to class Let. Node,

Solution one /proj/demeter/com 3362/w 02/students/wupc] 1: dehg fine around method to class Let. Node, Ident, Lit. Node, Binding, Proc. Node, Formal. Parameter. 2. Use List to store free variables, and Hash. Map to store bindings

Solution two /proj/demeter/com 3362/w 02/students/lbenitez 1. Use a stack to store the nodes visited

Solution two /proj/demeter/com 3362/w 02/students/lbenitez 1. Use a stack to store the nodes visited in the traversal, and a stack to store the bindings. 2. Define around method to class Node. List to compute free variables of the nodes in the node stack. 3. Define before and after methods to class Let. Node, Proc. Node to add a new binding field to the binding stack 4. Define before method to class Binding and Formal. Parameter to add a new binding to the current binding field 5. Define before method to subclasses of class Node to store the free variables of this node into the node stack.

Solution three /proj/demeter/com 3362/w 02/students/qianyi 1: define around methods to node classes, same as

Solution three /proj/demeter/com 3362/w 02/students/qianyi 1: define around methods to node classes, same as the solution one. Some are not necessary. 2. Use Hash. Set store free variables and bindings

Test example begin let x = 1 y = 2 in begin proc(x, y)

Test example begin let x = 1 y = 2 in begin proc(x, y) begin x+y; end; let h=3 in begin h = a(x, y); end; ll = 1; end

 • • • • • Lit. Node@60 d 49() Lit. Node@5251 a 3()

• • • • • Lit. Node@60 d 49() Lit. Node@5251 a 3() Var. Node@75 d 386(x) Var. Node@121 f 1 d(y) Add. Node@38 e 059(x, y, ) Node. List@110040(x, y, ) Proc. Node@2786 c 3(x, y, ) Lit. Node@88 c 0() Var. Node@122221(x) Var. Node@64 f 6 cd(y) App. Node@2 bb 514(x, a, y, ) Assign. Node@7 d 5 d 2 a(x, a, y, ) Node. List@6 fa 474(x, a, y, ) Let. Node@15 c 083(x, a, y, ) //2 Node. List@11 d 8 c 1(x, a, y, ) Let. Node@2 d 9 c 06(a, ) //1 Lit. Node@7 b 6889() Assign. Node@c 2 ff 5() Node. List@39240 e(a, )

Caching aspect • • Parent Chind Caching Back My. Caching My. Back Free. Variable.

Caching aspect • • Parent Chind Caching Back My. Caching My. Back Free. Variable. Caching

Caching aspect • pointcut caching(Parent c): call(void universal_trv 0(. . )) && target(c); //?

Caching aspect • pointcut caching(Parent c): call(void universal_trv 0(. . )) && target(c); //? ? • Question: When we use a functional visitor in a traversal, which method is called of every node visited by this visitor.