Building Program Understanding Tools Using Visitor Combinators

Building Program Understanding Tools Using Visitor Combinators

Building Program Understanding Tools Using Visitor Combinators Arie van Deursen Joost Visser CWI, P.O. Box 94079 CWI, P.O. Box 94079 1090 GB Amsterdam, The Netherlands 1090 GB Amsterdam, The Netherlands http://www.cwi.nl/˜arie/ http://www.cwi.nl/˜jvisser/ ABSTRACT Visiting source models The intent of the visitor design pat- tern is to “represent an operation to be performed on the el- Program understanding tools manipulate program represen- ements of an object structure. A visitor lets you define a tations, such as abstract syntax trees, control-flow graphs, or new operation without changing the classes of the elements data-flow graphs. This paper deals with the use of visitor on which it operates” [8]. Often, visitors are constructed to combinators to conduct such manipulations. Visitor combi- traverse an object structure according to a particular built-in nators are an extension of the well-known visitor design pat- strategy, such as top-down, bottom-up, or breadth-first. tern. They are small, reusable classes that carry out specific A typical example of the use of the visitor pattern in pro- visiting steps. They can be composed in different constella- gram understanding tools involves the traversal of abstract tions to build more complex visitors. We evaluate the expres- syntax trees. The pattern offers an abstract class Visitor, which siveness, reusability, ease of development, and applicability defines a series of methods that are invoked when nodes of a of visitor combinators to the construction of program under- particular type (expressions, statements, etc.) are visited. A standing tools. To that end, we conduct a case study in the use concrete Visitor subclass refines these methods in order to per- of visitor combinators for control-flow analysis and visualiza- form specific actions when accepted by a given syntax tree. tion as used in a commercial Cobol program understanding tool. Visitors are useful for analysis and transformation of source models for several reasons. Using visitors makes it 1998 ACM Computing Classification System: D.2.9, D.2.2, easy to traverse structures that consist of many different kinds D.2.5, D.2.7 of nodes, while conducting actions on only a selected num- ber of them. Moreover, visitors help to separate traversal from Keywords and Phrases: Program analysis, program compre- representation, making it possible to use a single source model hension, visitor design pattern, software visualization. for various sorts of analysis. Note: Work carried out under projects SEN 1.1, Software Ren- Visitor Combinators Recently, visitor combinators have ovation and SEN 1.3, Domain-Specific Languages. been proposed as an extension of the regular visitor design pattern [14]. The aim of visitor combinators is to compose Note: To appear in Proceedings of the 10th International complex visitors from elementary ones. This is done by sim- Workshop on Program Comprehension (IWCP), IEEE Com- ply passing them as arguments to each other. Furthermore, puter Society, 2002. visitor combinators offer full control over the traversal strat- egy and applicability conditions of the constructed visitors. The use of visitor combinators leads to small, reusable 1. Introduction classes, that have little dependence on the actual structure of the concrete objects being traversed. Thus, they are less brit- Program analysis and source models Program analysis is tle with respect to changes in the class hierarchy on which a crucial part of many program understanding tools. Program they operate. In fact, many combinators (such as the top-down analysis involves the construction of source models from the or breadth-first combinators) are completely generic, relying program source text and the subsequent analysis of these mod- only on a minimal Visitable interface. As a result, they can be els. Depending on the analysis problem, these source models reused for any concrete visitor instantiation. might be represented by tables, trees, or graphs. More often than not, the models are obtained through a se- Goals of the paper The concept of visitor combinators is quence of steps. Each step can construct new models or refine based on solid theoretical ground, and it promises to be a pow- existing ones. Usually, the first model is an (abstract) syntax erful implementation technique for processing source models tree constructed during parsing, which is then used to derive in the context of program analysis and understanding. Now graphs representing, for example, control or data flow. 1 Library Name Args Description Framework Visitable Visitor Identity Do nothing getChildCount visit(Visitable) getChildAt Fail Raise VisitFailure exception setChildAt Not v Fail if v succeeds, and v.v. Sequence v1, v2 Do v1, then v2 Operations Instantation Visitable Visitor Choice v1, v2 Try v1, if it fails, do v2 visitA All v Apply v to all immediate children visitB One v Apply v to one immediate child Hierarchy IfThenElse c,t, f If c succeeds, do t, otherwise do f A B Fwd Try v Choice(v,Identity) TopDown v Sequence(v,All(TopDown(v))) Figure 1. The architecture of JJTraveler. BottomUp v Sequence(All(BottomUp(v)),v) OnceTopDown v Choice(v,One(OnceTopDown(v))) OnceBottomUp v Choice(One(OnceBottomUp(v)),v) this concept needs to be put to the test of practice. AllTopDown v Choice(v,All(AllTopDown(v))) We have implemented ControlCruiser, a tool for analyzing AllBottomUp v Choice(All(AllBottomUp(v)),v) and visualizing intra-program control flow for Cobol. In this paper, we explain by reference to ControlCruiser how visi- Figure 2. JJTraveler’s library (excerpt). tor combinators can be used to develop program understand- ing tools. We discuss design tactics, programming techniques, Instantiation To use JJTraveler, one needs to instantiate the unit testing, implementation trade-offs, and other engineering framework for the class hierarchy of a particular application. practices related to visitor combinator development. Finally, To do this, the hierarchy is turned into a visitable hierarchy by we asses the risks and benefits of adopting visitor combinators letting every class implement the Visitable interface. Also, the for building program understanding tools. generic Visitor interface is extended with specific visit meth- ods for each class in the hierarchy. Finally, a single imple- mentation of the extended visitor interface is provided in the 2. Visitor Combinators form of a visitor combinator Fwd. This combinator forwards every specific visit call to a generic default visitor given to it Visitor combinator programming was introduced in [14] and is at construction time. Concrete visitors are built by providing supported by JJTraveler: a combination of a framework and li- Fwd with the proper default visitor, and overriding some of its brary that provides generic visitor combinators for Java. This specific visit methods. section briefly discusses the key elements of JJTraveler. Though instantiation of JJTraveler’s framework can be done manually, automated support for this is provided by a 2.1. The architecture of JJTraveler generator, called JJForester [10]. This generator takes a gram- mar as input. From this grammar, it generates a class hierar- Figure 1 shows the architecture of JJTraveler (upper half) and chy to represent the parse trees corresponding to the grammar, its relationship with an application that uses it (lower half). the hierarchy-specific Visitor and Visitable interfaces, and the JJTraveler consists of a framework and a library. The applica- Fwd combinator. In addition to framework instantiation, JJ- tion consists of a class hierarchy, an instantiation of JJTrav- Forester provides connectivity to a generalized LR parser [2]. eler’s framework for this hierarchy, and the operations on the Operations After instantiation, the application programmer hierarchy implemented as visitors. can implement operations on the class hierarchy by specializ- Framework The JJTraveler framework offers two generic ing, composing, and applying visitors. interfaces, Visitor and Visitable. The latter provides the min- The starting point of hierarchy-specific visitors is Fwd. imal interface for nodes that can be visited. Visitable nodes Typical default visitors provided to Fwd are Identity and Fail. should offer three methods: to get the number of child nodes, Furthermore, Fwd contains a method visitA for every class A to get a child given an index, and to modify a given child. The in the hierarchy, which can be overridden in order to construct Visitor interface provides a single visit method that takes any specific visitors. As an example, an A-recognizer IsA (which visitable node as argument. Each visit can succeed or fail, only does not fail on A-nodes) can be obtained by an appro- which can be used to control traversal behavior. Failure is in- priate specialization of method visitA of Fwd(Fail). dicated by a VisitFailure exception. Visitors are combined by passing them as (constructor) ar- Library The library consists of a number of predefined vis- guments. For example, All(IsA) is a visitor which checks that itor combinators. These rely only on the generic Visitor and any of the direct child nodes are of class A, and OnceTop- Visitable interfaces, not on any specific underlying class hi- Down(IsA) is a visitor checking whether a tree contains any erarchy. An overview of the library combinators is shown in A-node. Visitors are applied to visitable objects through the Figure 2. They will be explained in more detail below. visit method, such as IsA.visit(myA) (which does nothing), or 2 public class Sequence implements Visitor { public class TopDownWhile extends Choice { Visitor v1; public TopDownWhile(Visitor v1, Visitor v2) { Visitor v2; super(null,v2); public Sequence(Visitor v1, Visitor v2) { setArgument(1,new Sequence(v1,new All(this))); this.v1 = v1; } this.v2 = v2; public TopDownWhile(Visitor v) { } this(v,new Identity()); public void visit(Visitable x) { } } v1.visit(x); v2.visit(x); Figure 5. The TopDownWhile combinator. } } ¢ Figure 3. The Sequence combinator. visitor combinator TopDownWhile v1 ¡ v2 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us