asad
asad

Reputation: 23

How to programatically parse a java code (method) into control flow graph

Is there any API or reference library available to parse a java code or java method into control flow graph (CFG). I tried to convert AST into CFG but couldn't do it using AST Parser but didnt find any way to do so. I am working on Eclipse IDE JAVA (J2SE).
Kindly help.

Upvotes: 2

Views: 1970

Answers (2)

Omar Al Iraqi
Omar Al Iraqi

Reputation: 1

yes, the is " code visualizer" you can use it with Java Eclipse to automatically generate CFG for code: https://marketplace.eclipse.org/content/control-flow-graph-factory

Upvotes: 0

Hot Licks
Hot Licks

Reputation: 47699

doSomethingA;
while(B) {
    doSomethingC;
    doSomethingD;
    if (E) {
        doSomethingF;
    }
    else {
        doSomethingG;
    }
    doSomethingH;
    doSomethingI;
}
doSomethingJ;

Basic blocks:

  1. doSomethingA;
  2. while test
  3. doSomethingC; doSomethingD;
  4. doSomethingF;
  5. doSomethingG;
  6. doSomethingH; doSomethingI;
  7. doSomethingJ;

Arcs:

  • entry -> 1
  • 1 -> 2
  • 2 -> 3
  • 2 -> 7
  • 3 -> 4
  • 3 -> 5
  • 4 -> 6
  • 5 -> 6
  • 6 -> 2
  • 7 -> exit

As a data structure, a basic block has a list of statements and a list of exit arcs. One may also keep a list of entry arcs, for certain types of analysis, and one may choose to have a data structure to represent each arc, or simply have blocks point to other blocks.

Upvotes: 1

Related Questions