Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to analyze Java source files with Clojure

Tags:

clojure

antlr

I'm trying to analyze Java source files with Clojure but I couldn't find a way to do that.

First, I thought using Eclipse AST plugin(by copying necessary JAR's to my Clojure project) but I gave up after seeing Eclipse AST's API(visitor based walker).

Then I've tried creating a Java parser with ANTLR. I can only find one Java 1.6 grammar for ANTLR( http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g ) and it doesn't compile with latest ANTLR(here's the errors I'm getting ).

Now I have no idea how can I do that. At worst I'll try to go with Eclipse AST.

Does anyone know a better way to parse Java files with Clojure?

Thanks.

Edit: To clarify my point: I need to find some specific method calls in Java projects and inspect it's parameters(we have multiple definitions of the method, with different type of parameters). Right now I have a simple solution written in Java(Eclipse AST) but I want to use Clojure in this project as much as possible.

like image 767
sinan Avatar asked Feb 21 '12 10:02

sinan


2 Answers

... and it doesn't compile with latest ANTLR ...

I could not reproduce that.

Using ANTLR v3.2, I got some warnings, but no errors. Using both ANTLR v3.3 and v3.4 (latest version), I have no problems generating a parser.

You didn't mention how you're (trying) to generate a lexer/parser, but here's how it works for me:

java -cp antlr-3.4.jar org.antlr.Tool Java.g

EDIT 1

Here's my output when running the commands:

ls
wget http://www.antlr.org/download/antlr-3.4-complete.jar
wget http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g
java -cp antlr-3.4-complete.jar org.antlr.Tool Java.g
ls

enter image description here

As you can see, the .java files of the lexer and parser are properly created.

EDIT 2

Instead of generating a parser yourself (from a grammar), you could use an existing parser like this one (Java 1.5 only AFAIK) and call it from your Clojure code.

like image 126
Bart Kiers Avatar answered Sep 22 '22 17:09

Bart Kiers


It depends a bit on what you want to do - what are you hoping to get from the analysis?

If you want to actually compile Java or at least build an AST, then you probably need to go the ANTLR or Eclipse AST route. Java isn't that bad of a language to parse, but you still probably don't want to be reinventing too many wheels..... so you might as well build on the Eclipse and OpenJDK work.

If however you are just interesting in parsing the basic syntax and analysing certain features, it might be easier to use a simpler general purpose parser combinator library. Options to explore:

  • fnparse (Clojure, not sure how well maintained)
  • jparsec (Java, but can probably be used quite easily from Clojure)
like image 40
mikera Avatar answered Sep 23 '22 17:09

mikera