The following three graphs show the control flow graph of a procedural program. The nodes contain the text of statements as labels. Not all edges have labels. The visualized control flow comes from the following nonsense program, which consists of a procedure test and a main routine in a pseudo imperative language.
PROCEDURE test( VAR b : INTEGER; c : INTEGER );
BEGIN
b := c + 5;
END
BEGIN /* main routine of a nonsense program */
x := 1;
WHILE (x = 1) DO
x := 2;
test ( x, 1 );
x := 3;
OD;
WHILE (x = 1) DO
x := 4;
x := 5;
test ( x, 2 );
OD;
WHILE (x = 1) DO
x := 6;
IF (x = 7) THEN x := 8; ELSE test ( x, 3 );
FI;
OD;
END.
Example 7: Control Flow Graph 1
This example shows a simple visualization of the control
flow graph. The graph is shown in Figure 16.
<>01 >graph: { title: "CFG_GRAPH"
<>02 > splines: yes
<>03 > layoutalgorithm: dfs finetuning: no
<>04 > display_edge_labels: yes
<>05 > yspace: 55
<>06 > node: { title:"18" label: "test_b := test_c+5" }
<>07 > node: { title:"17" label: "Exit" }
<>08 > node: { title:"16" label: "test (x,3)" }
<>09 > node: { title:"15" label: "x := 8" }
<>10 > node: { title:"14" label: "x=7" }
<>11 > node: { title:"13" label: "x := 6" }
<>12 > node: { title:"12" label: "x=1" }
<>13 > node: { title:"11" label: "test (x,2)" }
<>14 > node: { title:"10" label: "x := 5" }
<>15 > node: { title:"9" label: "x := 4" }
<>16 > node: { title:"8" label: "x=1" }
<>17 > node: { title:"7" label: "x := 3" }
<>18 > node: { title:"6" label: "test (x,1)" }
<>19 > node: { title:"5" label: "x := 2" }
<>20 > node: { title:"4" label: "x=1" }
<>21 > node: { title:"3" label: "x := 1" }
<>22 > node: { title:"2" label: "Start" }
<>23 > node: { title:"1" label: "Exit point\n
test" }
<>24 > node: { title:"0" label: "Entry point\n
test" }
<>25 > edge: { thickness: 4 sourcename:"18" targetname:"1" }
<>26 > edge: { thickness: 4 sourcename:"0" targetname:"18" }
<>27 > edge: { thickness: 4 sourcename:"12" targetname:"17" label: "false" }
<>28 > edge: { thickness: 4 sourcename:"8" targetname:"12" label: "false" }
<>29 > edge: { thickness: 4 sourcename:"16" targetname:"12" label: "back" }
<>30 > edge: { thickness: 4 sourcename:"15" targetname:"12" label: "back" }
<>31 > edge: { thickness: 4 sourcename:"13" targetname:"14" }
<>32 > edge: { thickness: 4 sourcename:"14" targetname:"16" label: "false" }
<>33 > edge: { thickness: 4 sourcename:"14" targetname:"15" label: "true" }
<>34 > edge: { thickness: 4 sourcename:"12" targetname:"13" label: "true" }
<>35 > edge: { thickness: 4 sourcename:"4" targetname:"8" label: "false" }
<>36 > edge: { thickness: 4 sourcename:"11" targetname:"8" label: "back" }
<>37 > edge: { thickness: 4 sourcename:"10" targetname:"11" }
<>38 > edge: { thickness: 4 sourcename:"9" targetname:"10" }
<>39 > edge: { thickness: 4 sourcename:"8" targetname:"9" label: "true" }
<>40 > edge: { thickness: 4 sourcename:"3" targetname:"4" }
<>41 > edge: { thickness: 4 sourcename:"7" targetname:"4" label: "back" }
<>42 > edge: { thickness: 4 sourcename:"6" targetname:"7" }
<>43 > edge: { thickness: 4 sourcename:"5" targetname:"6" }
<>44 > edge: { thickness: 4 sourcename:"4" targetname:"5" label: "true" }
<>45 > edge: { thickness: 4 sourcename:"2" targetname:"3" }
<>46 >}
Example 8: Control Flow Graph 2
This example shows an improved visualization of the control
flow graph. The graph is shown in Figure 17.
The start, exit and branch nodes are drawn in different shapes
so that they can be better recognized. The edges closing
a cycle are specified as back edges in order to see the uniform
flow of control in the other edges. The edges at the branch nodes
are anchored at the left and right via the bent near edge specification.
<>01 >graph: { title: "CFG_GRAPH"
<>02 > layoutalgorithm: dfs
<>03 > finetuning: no
<>04 > display_edge_labels: yes
<>05 > yspace: 55
<>06 > node: { title:"18" label: "test_b := test_c+5" }
<>07 > node: { title:"17" label: "Exit" shape: ellipse }
<>08 > node: { title:"16" label: "test (x,3)" }
<>09 > node: { title:"15" label: "x := 8" }
<>10 > node: { title:"14" label: "x=7" shape: rhomboid }
<>11 > node: { title:"13" label: "x := 6" }
<>12 > node: { title:"12" label: "x=1" shape: rhomboid }
<>13 > node: { title:"11" label: "test (x,2)" }
<>14 > node: { title:"10" label: "x := 5" }
<>15 > node: { title:"9" label: "x := 4" }
<>16 > node: { title:"8" label: "x=1" shape: rhomboid }
<>17 > node: { title:"7" label: "x := 3" }
<>18 > node: { title:"6" label: "test (x,1)" }
<>19 > node: { title:"5" label: "x := 2" }
<>20 > node: { title:"4" label: "x=1" shape: rhomboid }
<>21 > node: { title:"3" label: "x := 1" }
<>22 > node: { title:"2" label: "Start" shape: ellipse }
<>23 > node: { title:"1" label: "Exit point\n
test" shape: ellipse }
<>24 > node: { title:"0" label: "Entry point\n
test" shape: ellipse }
<>25 > edge: { thickness: 4 sourcename:"18" targetname:"1" }
<>26 > edge: { thickness: 4 sourcename:"0" targetname:"18" }
<>27 > bentnearedge: { thickness: 4 sourcename:"12" targetname:"17" label: "false" }
<>28 > bentnearedge: { thickness: 4 sourcename:"8" targetname:"12" label: "false" }
<>29 > backedge: { thickness: 4 sourcename:"16" targetname:"12" label: "back" }
<>30 > backedge: { thickness: 4 sourcename:"15" targetname:"12" label: "back" }
<>31 > edge: { thickness: 4 sourcename:"13" targetname:"14" }
<>32 > bentnearedge: { thickness: 4 sourcename:"14" targetname:"16" label: "false" }
<>33 > bentnearedge: { thickness: 4 sourcename:"14" targetname:"15" label: "true" }
<>34 > bentnearedge: { thickness: 4 sourcename:"12" targetname:"13" label: "true" }
<>35 > bentnearedge: { thickness: 4 sourcename:"4" targetname:"8" label: "false" }
<>36 > backedge: { thickness: 4 sourcename:"11" targetname:"8" label: "back" }
<>37 > edge: { thickness: 4 sourcename:"10" targetname:"11" }
<>38 > edge: { thickness: 4 sourcename:"9" targetname:"10" }
<>39 > bentnearedge: { thickness: 4 sourcename:"8" targetname:"9" label: "true" }
<>40 > edge: { thickness: 4 sourcename:"3" targetname:"4" }
<>41 > backedge: { thickness: 4 sourcename:"7" targetname:"4" label: "back" }
<>42 > edge: { thickness: 4 sourcename:"6" targetname:"7" }
<>43 > edge: { thickness: 4 sourcename:"5" targetname:"6" }
<>44 > bentnearedge: { thickness: 4 sourcename:"4" targetname:"5" label: "true" }
<>45 > edge: { thickness: 4 sourcename:"2" targetname:"3" }
<>46 >}
Example 9: Control Flow Graph 3
This example shows another improved visualization of the control
flow graph of Example 7. The graph is shown in Figure 18.
Here an orthogonal layout is used so that the graph looks like a
typical flowchart. For orthogonal layout, a large down factor and
near factor and an up factor of zero is recommended. This improves
the layout of long vertical edges. Add the following lines to
Example 8 after line <>01>:
manhattan_edges: yes
layout_downfactor: 100
layout_upfactor: 0
layout_nearfactor: 0
xlspace: 12