Docsity

Visitor Pattern for Expression Trees: Adding Operations Without Modifying Existing Code, Papers of Computer Science

Partial preview of the text

Download Visitor Pattern for Expression Trees: Adding Operations Without Modifying Existing Code and more Papers Computer Science in PDF only on Docsity!

Software Design

CSE 335, Spring 2006

Visitors

Stephen Wagner

Michigan State University

Visitors

  • Suppose we have a complicated expression tree:
    • add, subtract, multiply
    • literals, variables, arrays
    • min, max, arbitrary functions
    • control logic
  • We can perform several operations
    • evaluate
    • type check
    • simplify

Visitors

  • With the visitor pattern you define two class hierarchies
    • one for the elements being operated on (e.g. the expression nodes)
    • one for the visitors that define operations on the elements
  • You create a new operation by adding a new class to the visitor class hierarchy
    • This will require writing lots of code
    • However we do not have to modify the expression node hierarchy

@@

@@

Expr

Literal Sum Product Func

ExprVisitor

EvaluateVisitor SimplifyVisitor

Operating Overloading

class ExprVisitor { public: virtual void Visit(Literal *); virtual void Visit(Sum *); virtual void Visit(Product *); virtual void Visit(Func *); // .... protected: Visitor(); }

  • Be careful!
  • All the Visit functions are different

Accept

  • Each concrete class implements an Accept operation that calls the matching Visit... operation on the visitor for that concrete class.
  • The operation that ends up getting called depends on both the class of the element and the class of the visitor

class Expr { class Literal : public Expr { public: public: virtual ~Expr(); Literal(Expr *, Expr *); virtual virtual void Accept(ExprVisitor &v) void Accept(ExprVisitor&)=0; { v.VisitLiteral(this); } protected: }; Expr(); };

Accept

class FuncExpr : public Expr { public: FuncExpr(); virtual void Accept(ExprVisitor &); private: list<Expr *> parameters; }

void FuncExpr::Accept (ExprVisitor & v) { for (list<Expr *>::iterator it=parameters.begin(); it!=parameters.end(); ++it) it->Accept(v); v.VisitFuncExpr(this); }

Double Dispatch

  • The Visitor pattern lets you add operations to classes without changing them
  • Visitor achieves this by using a technique called double-dispatch
  • single-dispatch:
    • the operation used to fulfill a request is determined by the name of the request and the type of the receiver
    • a->request();
    • C++ supports single-dispatch
  • double-dispatch:
    • the operation used to fulfill a request is determined by the name of the request and the type of two receivers
    • key to Visitor pattern

Evaluate Visitor

class EvaluateVisitor : public ExprVisitor { public: double GetValue();

virtual void VisitLiteral(Literal *); virtual void VisitSum(Sum *); virtual void VisitProduct(Product *); virtual void VisitMax(Max *);

private: stack values; };

void EvaluateVisitor::VisitLiteral(Literal *l) { values.push(l->value()); }

void EvaluateVisitor::VisitProduct(Product l) { double rightval, newval; rightval=values.top(); values.pop(); newval=values.top()rightval; values.pop(); values.push(newval); }

Simplify

class SimplifyVisitor : public ExprVisitor { public: bool check();

virtual void VisitLiteral(Literal *); virtual void VisitSum(Sum *); virtual void VisitProduct(Product *); virtual void VisitMax(Max *);

private: stack<Expr *> exprs; };

void SimplifyVisitor::VisitLiteral(Literal *l) { exprs.push(this->duplicate()); }

void SimplifyVisitor::VisitSum(Sum *l) { Expr *left=exprs.top(); exprs.pop(); Expr *right=exprs.top(); exprs.pop(); if (left->evaluate(leftvalue) && right->evaluate(rightvalue)) exprs.push_back(new Literal(leftvalue+rightvalue)); else if (left->evaluate(leftvalue) && leftvalue==0) exprs.push_back(right); else if (right->evaluate(rightvalue) && rightvalue==0) exprs.push_back(left); else exprs.push_back(new Sum(left,right)); }

Visitors

  • Visitors are not restricted to a class hierarchy

class Visitor { public: void VisitTypeOne(TypeOne *); void VisitTypeTwo(TypeTwo *); }

  • TypeOne and TypeTwo do not have to belong to the same hierarchy
  • Visitors can have state

Variations

  • Who is responsible for traversing the structure?
    • The components of the structure
    • The visitor
  • In previous examples, the nodes of the expression tree were responsible for traversing the tree
  • What are the advantages and disadvantages of the different methods?