Homework 8 Functional Visitors in DJ - PowerPoint PPT Presentation

About This Presentation
Title:

Homework 8 Functional Visitors in DJ

Description:

1. Use a stack to store the nodes visited in the traversal, and a stack to store the bindings. ... caching(Parent c): call(void universal_trv0(..)) && target(c) ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 14
Provided by: zhimin5
Category:

less

Transcript and Presenter's Notes

Title: Homework 8 Functional Visitors in DJ


1
Homework 8 Functional Visitors in DJ
2
  • 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 NodeList .
  • Node lookahead (_at_ 2 _at_) AppNode LetNode
    lookahead (_at_ 2 _at_) AssignNode look ahead (_at_ 2 _at_)
    AddNode Term ProcNode .
  • LetNode "let" Bindings "in" NodeList .
  • AddNode ltagt Term "" ltbgt Term .
  • Term VarNode LitNode .
  • VarNode Ident .
  • LitNode ltvgt int .
  • ProcNode "proc" "(" FormalParameters ")"
    NodeList .
  • AppNode Ident "(" ActualParameters ")" .
  • AssignNode Ident "" Node .

3
  • Bindings List(Binding) .
  • Binding Ident "" Node .
  • FormalParameters NList(FormalParameter) .
  • FormalParameter Ident .
  • ActualParameters NList(ActualParameter) .
  • ActualParameter Node .
  • NodeList "begin" List(Statement) "end" .
  • Statement Node "" .
  • NList(S) S "," S .
  • List(S) S .

4
  • begin //a
  • let x 1 y 2 //a
  • in
  • begin
  • proc(x,y) begin xy end
  • let h3 //a, x, y
  • in
  • begin
  • h a(x,y)
  • end
  • end
  • ll 1
  • end

5
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

6
LetNode
let h3 in begin
h a(x,y) end h, a, x, y
h a, x, y
bindings
nodelist
Bindings
NodeList

Binding
Ident
Node
FreeVariables(LetNode) (FreeVariables(NodeList)
Idents) FreeVariables(Nodes)

7
Solution one
  • /proj/demeter/com3362/w02/students/wupc
  • 1 dehg fine around method to class LetNode,
    Ident,
  • LitNode, Binding, ProcNode, FormalParameter .
  • 2. Use List to store free variables, and HashMap
    to store bindings

8
Solution two
  • /proj/demeter/com3362/w02/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 NodeList to
    compute free variables of the nodes in the node
    stack.
  • 3. Define before and after methods to class
    LetNode, ProcNode to add a new binding field to
    the binding stack
  • 4. Define before method to class Binding and
    FormalParameter 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.

9
Solution three
  • /proj/demeter/com3362/w02/students/qianyi
  • 1 define around methods to node classes, same as
    the solution one. Some are not necessary.
  • 2. Use HashSet store free variables and bindings

10
Test example
begin let x 1 y 2 in
begin proc(x,y) begin xy
end let h3 in
begin h
a(x,y) end end
ll 1 end
11
  • LitNode_at_60d49()
  • LitNode_at_5251a3()
  • VarNode_at_75d386(x)
  • VarNode_at_121f1d(y)
  • AddNode_at_38e059(x,y,)
  • NodeList_at_110040(x,y,)
  • ProcNode_at_2786c3(x,y,)
  • LitNode_at_88c0()
  • VarNode_at_122221(x)
  • VarNode_at_64f6cd(y)
  • AppNode_at_2bb514(x,a,y,)
  • AssignNode_at_7d5d2a(x,a,y,)
  • NodeList_at_6fa474(x,a,y,)
  • LetNode_at_15c083(x,a,y,) //2
  • NodeList_at_11d8c1(x,a,y,)
  • LetNode_at_2d9c06(a,) //1
  • LitNode_at_7b6889()
  • AssignNode_at_c2ff5()
  • NodeList_at_39240e(a,)

12
Caching aspect
  • Parent
  • Chind
  • Caching
  • Back
  • MyCaching
  • MyBack
  • FreeVariableCaching

13
Caching aspect
  • pointcut caching(Parent c) call(void
    universal_trv0(..)) target(c) //??
  • Question When we use a functional visitor in a
    traversal, which method is called of every node
    visited by this visitor.
Write a Comment
User Comments (0)
About PowerShow.com