How to analyze Java source files with Clojure

后端 未结 2 1864
遇见更好的自我
遇见更好的自我 2021-01-18 21:16

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

相关标签:
2条回答
  • 2021-01-18 21:38

    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)
    0 讨论(0)
  • 2021-01-18 21:44

    ... 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.

    0 讨论(0)
提交回复
热议问题