Docsity

Spell Checking and the Hyphenation - Lecture Slides | CMSC 433, Papers of Programming Languages

Partial preview of the text

Download Spell Checking and the Hyphenation - Lecture Slides | CMSC 433 and more Papers Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Fall 2005

Design Patterns

October 4th, 2005

Spell-Checking and Hyphenation

• Must do textual analysis

  • Multiple operations and implementations

• Must add new functions and operations easily

• Must efficiently handle scattered information and

varied implementations

  • Different traversal strategies for stored information

• Should separate actions from traversal

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 { public Node left; public Node right; public int sum() { return left.sum() + right.sum(); } }

Tradeoffs with this Approach

• Follows idea “objects are responsible for themselves”

• But many analyses will occlude the object’s main code

• Result is classes that are hard to maintain

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

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

Sample Visitor Class

public interface Visitor { public void visitNumber(Number n); public void visitPlus(Plus p); } public class SumVisitor implements Visitor { int sum; public void visitNumber(Number n) { sum += n; } public void visitPlus(Plus p) { p.left.accept(this); p.right.accept(this); }

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 Structure

• Define two class hierarchies

  • One for object structure
    • AST in compiler, Glyphs in Lexi
  • One for each operation family, called visitors
    • One for typechecking, code generation, pretty printing in compiler
    • One for spellchecking or hyphenation in Lexi

Structure of Visitor Pattern

Double-Dispatch

• Accept code is always trivial

  • Just dynamic dispatch on argument, with runtime type

of structure node taking into account in method name

• A way of doing double-dispatch

  • Traversal routine takes two arguments, the visitor and

the object to traverse

  • o.accept(aVisitor) will dispatch on the actual identity of o (the object being considered)
  • ...and accept will internally dispatch on the identity of aVisitor (the object visiting it)

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