4

My task is to make a control flow graph. First, I have managed to seperate my code into basic blocks. For example, this program here:

1 begin

2   int x, y, power;

3   float z;

4   input (x, y);

5   if (y<0)

6     power=-y;

7   else

8     power=y;

9   z=1;

10   while (power!=0){

11     z=z*x;

12   power=power-1;

13   }

14   if (y<0)

15     z=1/z;

16   output(z);

17 end

will turn into the following basic blocks:

BLOCK 1

Line  2   int x, y, power;
Line  3   float z;
Line  4   input (x, y);
Line  5   if (y<0)

BLOCK 2

Line  6     power=-y;

BLOCK 3

Line  8     power=y;

BLOCK 4

Line  9   z=1;

BLOCK 5

Line 10   while (power!=0){

BLOCK 6

Line 11     z=z*x;
Line 12   power=power-1;

BLOCK 7

Line 14   if (y<0)

BLOCK 8

Line 15     z=1/z;

BLOCK 9

Line 16   output(z);

I have done this by scanning through the file, and using Pattern and Matcher to split the program up based on if, while, etc statements. The basic blocks are an ArrayList and all these blocks are kept in an ArrayList>.

Next, I have kept a HashMap of the which basic blocks are connected to each other. For example, block 1 will be connected to block 2 and block 3 as it is an if statement (if this go one way, else go the other). This HashMap is of > for the block number, and the list of block numbers that it is connected to.

So, I have the basic blocks, and a list of connections between these basic blocks. My problem now is that I am not sure how to display this in a graphical form. Before, I was using the paint() method and drawing circles and lines to represent a simple control graph where each block only had one connection to the next block. However, I do not know how to do this when there are multiple connections. Is there any easy way of doing this?

Thanks!

MMH
  • 1,676
  • 5
  • 26
  • 43
Lauren
  • 147
  • 1
  • 3
  • 10
  • 1
    find some nice algorithm for creating layout for directed graph, ie sugiyama framework – user902383 Oct 29 '12 at 13:54
  • This has been covered elsewhere. https://stackoverflow.com/questions/16393985/getting-control-flow-graph-from-ansi-c-code/17844310 – Olsonist Oct 27 '20 at 05:45

1 Answers1

1

For drawing a graph, I usually use dot, it's pretty straight forward.

Oak
  • 26,231
  • 8
  • 93
  • 152
limi
  • 832
  • 7
  • 19