4

I would like to create an automatic Flowchart-like visualization to simple Java Logic, for this I need to parse Java Source code, I have 2 candidates, ANTLR and javax.lang.model of Java 6. Neither are easy.

I have yet to find a single working example that will be even remotely close to what I want to achieve.

I want to find simple variable declarations, assignments, and flows (if, for, switch, boolean conditions etc)

Is there a simple example or tutorial for either of these? I found very few ANTLR examples (non of them are working out of the box without significant "homework") and absolutely none for javax.lang.model

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
Eran Medan
  • 44,555
  • 61
  • 184
  • 276
  • Good point, thanks, if you had this as an answer, it might have been the accepted one... By the way, the first google result for this, as you probably know, is ANTLR ;) http://www.google.com/search?q=get+ast+java+source – Eran Medan Mar 22 '11 at 00:59
  • The next google result that is helpful is this (from SO, where else): http://stackoverflow.com/questions/1967987/how-to-generate-ast-from-java-source-code – Eran Medan Mar 22 '11 at 01:06
  • I expanded my comment a bit and posted it as an answer. – Bart Kiers Mar 22 '11 at 06:59

3 Answers3

2

Is there a particular reason you want to generate a parser yourself? IMO, it would be easier to let an existing parser create an AST for you which you "simply" traverse in order to collect your data about the source file(s). Using your favorite search engine with the keywords "get ast java source" will result in many relevant hits.

At a first glance, these look like suitable candidates:

You could use ANTLR of course, there are many ANTLR grammars for Java available for you, but there is quite the learning curve when choosing this tool (or some other parser generator, for that matter). If you do choose ANTLR, I'm more than happy to answer any questions regarding it.

Best of luck!

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
1

I suggest you to go with Java Soot a Java Optimization Framework. Which will help you to parse the java source, can generate the CFG and various options available.

Also you can find eclipse plugin here

Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84
1

I would try that with Eclipse Xtext, it uses ANTLR under the hood, it is also (relative) easy to use it together with Eclipse GMF for visualization.

moritz
  • 5,094
  • 1
  • 26
  • 33