简体   繁体   English

绘制控制流图

[英]Drawing a Control Flow Graph

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 第1块

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

BLOCK 2 第2块

Line  6     power=-y;

BLOCK 3 第3块

Line  8     power=y;

BLOCK 4 第4块

Line  9   z=1;

BLOCK 5 第5块

Line 10   while (power!=0){

BLOCK 6 第6块

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

BLOCK 7 第7块

Line 14   if (y<0)

BLOCK 8 第8块

Line 15     z=1/z;

BLOCK 9 第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. 我通过扫描文件,并使用Pattern和Matcher根据if,while,etc语句拆分程序来完成此操作。 The basic blocks are an ArrayList and all these blocks are kept in an ArrayList>. 基本块是ArrayList,所有这些块都保存在ArrayList>中。

Next, I have kept a HashMap of the which basic blocks are connected to each other. 接下来,我保留了一个HashMap,其中哪些基本块相互连接。 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). 例如,块1将连接到块2和块3,因为它是一个if语句(如果这是单向,否则去另一个)。 This HashMap is of > for the block number, and the list of block numbers that it is connected to. 这个HashMap是>用于块号,以及它所连接的块号列表。

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. 之前,我使用paint()方法并绘制圆和线来表示一个简单的控制图,其中每个块只有一个连接到下一个块。 However, I do not know how to do this when there are multiple connections. 但是,当有多个连接时,我不知道如何执行此操作。 Is there any easy way of doing this? 这有什么简单的方法吗?

Thanks! 谢谢!

为了绘制图形,我通常使用 ,它非常直接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM