Java to Python converter using JJTree

05 Apr 2007

I just came across this post which talks about a Java to Python converter that the author wrote and posted on Google code. It uses a Java 1.5 JJTree grammar and a Visitor that gets callbacks for pretty much all the AST nodes.

One nice thing that it does is print some type documentation for parameterized types, e.g.:

  printer.print("# Parameterized type: <");
  for (Iterator<typeparameter><p> i = n.typeParameters.iterator(); i
                  .hasNext();) {
          TypeParameter t = i.next();
          t.accept(this, arg);
          if (i.hasNext()) {
                  printer.print(", ");
          }
  }
  printer.printLn(">");</p></typeparameter>

Rather a clever idea.

The PMD jar file is in the project's lib directory, but it looks like the author ended up going with a different Java 1.5 grammar. That's understandable since the PMD grammar is full of all sorts of PMD-specific things like NOPMD lexical states and whatnot. Anyhow, seems like a nifty project!

My JavaCC/JJTree book is coming down the home stretch now... should have something in print in the next few months. w00t!