7

As the title says, I want to parse some Java source code in Java. I'm pretty sure there are other java libraries that already perform this, but I couldn't find any.

5 Answers 5

6

Antlr has a grammar file for Java. See this.

Sign up to request clarification or add additional context in comments.

Comments

3

Janino can parse, compile and execute Java code.

2 Comments

Awesome! I really didn't want to use generic parser-generators for this, because the tokens are probably more fine-grained then what I want.
@Jeeyoung Kim, be sure to read the Limitations section: most Java 5 (and up) features are not supported.
2

You may be looking for something like ANTLR: http://www.antlr.org/

1 Comment

I didn't, but there should be a tool or library or something. You don't want to have to hack together the Java grammar.
2

Eclipse exposes the Syntax Tree of it's own Java compiler. You can simply access the elements.

See here.

Comments

1

JavaCC has a Java 1.5 grammar including generics.

the tokens are probably more fine-grained then what I want

Your first requirement is to get an accurate parse, and you will realistically only get that from an existing parser. What you do with the result is up to you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.