Study Notes on Visitor: Implementing Analysis | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Hicks; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Spring 2006;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-2jw-1
koofers-user-2jw-1 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2006
Visitor Design Pattern
2
Visitor: Implementing Analyses
Often want to implement multiple analyses on the
same kind of object data
Book example: computing with Menus
Project example: Generating code for and analyzing an
Abstract Syntax Tree (AST) in a compiler
One solution: implement each analysis as a
method in each object
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Study Notes on Visitor: Implementing Analysis | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2006

Visitor Design Pattern

Visitor: Implementing Analyses

• Often want to implement multiple analyses on the

same kind of object data

  • Book example: computing with Menus
  • Project example: Generating code for and analyzing an Abstract Syntax Tree (AST) in a compiler

• One solution: implement each analysis as a

method in each object

3

Abstract Syntax Trees

public interface Node { } public class Number extends Node { public int n; } public class Plus extends Node { public Node left; public Node right; }

Traversing Abstract Syntax Trees

public interface Node { public int sum(); } public class Number extends Node { public int n; public int sum() { return n; } } public class Plus extends Node{ public Node left; public Node right; public int sum() { return left.sum() + right.sum(); } }

7

Use a Visitor

• Alternatively, can define a separate visitor class

  • A visitor encapsulates the operations to be performed on an entire structure, e.g., all elements of a parse tree

• Allows operations to be separate from structure

  • But doesn’t necessarily require putting all of the structure traversal code into each visitor/operation

Sample Visitor class

9

How to perform traversal?

• Now that we have a visitor class, how do we apply

its analysis to the objects of interest?

  • Add accept (visitor) method to each structure class, that will invoke the given visitor on this
  • Builds on Java’s dynamic dispatch
  • Use an iteration algorithm (like an Iterator) to call accept() on each relevant object

Sample visited objects

13

Change to AST Classes

public interface Node { public void accept(Visitor v); } public class Number extends Node { … public void accept(Visitor v) {v.visitNumber(this);} } public class Plus extends Node { … public void accept(Visitor v) {v.visitPlus(this);} }

Visitor pattern

• Name

  • Visitor or double dispatching

• Applicability

  • Related objects must support different operations and actual op depends on both the class and the op type
  • Distinct and unrelated operations pollute class defs
  • Key : object structure rarely changes, but ops changed often

15

Visitor Pattern Structure

• Define two class hierarchies

  • One for object structure
    • AST in compiler, Menus and MenuItems in book example
  • One for each operation family, called visitors
    • One for typechecking, code generation, pretty printing in compiler
    • One for printing menus, figuring out the per/item average cost, etc.

Structure of Visitor Pattern

19

Using Overloading in a Visitor

• You can name all of the visitXXX(XXX x)

methods just visit(XXX x)

  • Calls to Visit (AssignmentNode n) and Visit(VariableRefNode n) distinguished by compile-time overload resolution

Visitors Can Forward Common Behavior

• Useful for composites

  • If subclasses of a particular object all treated the same
  • Can have visit(SubClass) call visit(SuperClass)

• For example

  • visit(BinaryPlusOperatorNode) can just forward call to superclass visit(BinaryOperatorNode)

21

State in a Visitor Pattern

• A visitor can contain state

  • E.g., the results of typechecking the program so far class TypeCheckingVisitor extends Visitor { private TypeMap map; void visit(VariableDefNode n) { … map.add(n,t) … } }

• Or visitors pass around a separate state object

  • Impacts the type of the Visitor superclass

Implementing Traversal

• Who is responsible for traversing object structure?

• Plausible answers:

  • Visitor
    • But, must replicate traversal code in each concrete visitor
  • Object structure
    • Define operation that performs traversal while applying visitor object to each component
  • Iterator
    • Iterator sends message to visitor with current element as arg

25

acceptAndTraverse Methods

• Accept method could be responsible for traversing

children

  • Assumes all visitors have same traversal pattern
    • E.g., visit all nodes in pre-order traversal
  • Could provide previsit and postvisit methods to allow for more complicated traversal patterns - Still visit every node - Can’t do out of order traversal - In-order traversal requires inVisit method

Visitor/Process Methods

• Can have two parallel sets of methods in visitors

  • Visit() methods
  • Process() methods

• How it works: the visit() method on a node:

  • Calls process() method of visitor, passing node as an argument
  • Calls accept() on all children of the node (passing the visitor as an argument)

• Allows finer-grained subtyping of Visitor classes

that include traversal

  • Subclass a visitor, and just change the process method

27

Preorder Visitor

• Class PreorderVisitor {

void visit(BinaryPlusOperatorNode n) {

process(n);

n.lhs.accept(this);

n.rhs.accept(this);

Visit/Process, Continued

• Can define a PreorderVisitor

  • Extend it, and just redefine process method
    • Except for the few cases where something other than preorder traversal is required

• Can define other traversal visitors as well

  • E.g., PostOrderVisitor