Short Exercise for the Visitor Pattern

One operation a compiler may do is a renaming of the variables to put them in a uniform naming sequence. The source-level names of variables, which serve the purpose only of making the code more human-readable, are no longer necessary.

Write a visitor which passes over the code and renames the variables t1, t2, etc., and prints out the resulting code. For example, given

public class A {
   public static void main(String[] args) {
      int a;
      int z;
      boolean someName;

      a = 5;
      z = (a + 6);
      someName = ((a % 3) > (z * 2));
      if (someName)
         System.out.println(z);
      else
         System.out.println(someName);
   }
}

It would produce

public class A {
   public static void main(String[] args) {
      int t1;
      int t2;
      boolean t3;

      t1 = 5;
      t2 = (t1 + 6);
      t3 = ((t1 % 3) > (t2 * 2));
      if (t3)
         System.out.println(t2);
      else
         System.out.println(t3);
   }
}

You may use the code from PrettyPrintingVisitor as a starting place. You can find all the code from the example at

/cslab.all/ubuntu/cs335/vis-mj

Please send me your code by email by next Monday.


Last modified: Wed Apr 8 09:09:22 CDT 2009