Professional Documents
Culture Documents
Loren P° Meissner
Lawrence Laboratory, University of California
Berkeley, Calif. 94720
193
programs written in a "structured" language more the beginning and end of each block of statements
or less resembling Fortran, and translates them in an alternative clause or a repetitive clause.
into Standard Fortran programs which can then be Furthermore, Hull would restrict the use of "go
processed in the ordinary way. However, the pre- to" statements to those ways that are necessary in
processor approach introduces an additional lan- implementing structured programming constructs.
guage level during debugging, and therefore these However, it is not clear whether the use of "cor-
implementations may prove to be important princi- rect" structural principles can be adequately
pally as experimental tools or testing grounds for enforced in a manual system of this kind.
the evaluation of the Fortran extension ideas they
embody. Meanwhile, further experiments of this Using flow graph information to expose program
nature should certainly he encouraged, and wide structure
dissemination of various proposals (along with
reports of experiences of their users) should be A simple listing of Fortran program statements
promoted. is not adequate for revealing program structure.
The extensions and informal techniques described
Informal techniques. Other proposals involve above attempt to correct this deficiency with aux-
the use of an informal language to develop a iliary information that is inserted manually in
pseudo-program which the programmer then trans- the listing, either during a separate preprocess-
lates by hand into Standard Fortran. One such ing stage or during the normal coding process.
proposal is the "Programming Design Language
(PDL)" [22]. The control structures are expressed We propose to tap an independent source of
using keywords such as "if ... then ... else" structural information, and to make the program
along with systematic indentation, while the flow graph available to the user. Although this
statements controlled by these keywords are writ- information exists (in a sense) as soon as the
ten out in plain English. In principle, such a program has been designed, it is most readily cap-
pseudo-program could be translated with equal ease tured by the computer after the program statements
into Fortran, Cobol, Algol, PL/I, or any other have been written. We have implemented some soft-
language. ware which will scan a Fortran program or subpro-
gram, and will generate and display its flow
T. E. Hull [23] proposes that comment cards be graph. The flow graph is also used to produce a
manually inserted into Standard Fortran programs restructured listing of the program statements, as
in certain prescribed ways, e.g., to insert key- illustrated in Fig. 1 (which is based on an exam-
words such as "if ... then ... else" or to mark ple discussed by Hull [23]). The techniques
*C start
I ÷ (2) E P S = 1.0E-5
*C LIO: begin iterative s t r u c t u r e
2 ~ (10, 3) DO 4 I = 1, 11
*C ...
X = 1.0 + F L O A T (I - 1) / 10.0
T E R M = 1.0
S U M = 1.0
R N = 1.0
*C L5: begin i t e r a t i v e structure
3 ÷ (4) 1 TFRM = - TERM * X / RN
SUM = SUM + TERM
4 ÷ (7, 5) I F (ABS (TERM) .GE. EPS) GO TO 3
*C ...
*C sequence break
7 ÷ (8) 3 R N = R N + 1.0
8 ~ (3)t GO TO 1
*C r e t u r n arc
*C sequence break
*C end L5
5 ÷ (6) 2 WRITE (6, 100) X, S U M
100 F O R M A T (1X, F 4.1, F 10.5)
6 ÷ (9) GO TO 4
*C sequence break
9 ÷ (2)+ 4 CONTINUE
*C r e t u r n arc
~C end LIO
10 ÷ (11)
11 ~ (12) STOP
12 END
Figure i. Restructured listing based on the linearized flow graph of a program (adapted from Hull [23]) to
compute a table of values of the exponential function for negative arguments. On the left is a
representation of the flow graph in numeric form, using node n~wbers that have been assigned con-
secutively (and do not necessarily agree with statement labels). Comments preceded by an asterisk
were generated by the analysis algorithm.
194
described in the remainder of this paper are based
on a synthesis and extension of the studies and
proposals of Hecht and Ullman [24] and of Peter-
son, Kasami, and Tokura [25]. (These two papers SINGLE MULTIPLE
are hereinafter referred to as HU and PKT, respec- ENTRY ENTRY
tively.)
195
ward" flow of control, or even to produce an
"upward" or backward flow in a manner that is
equivalent to the normal flow of control in a "do"
loop. An improper use of the " g o t o" statement,
on the other hand, would be one which introduces
\ more than one entry into a strongly connected sub-
graph of the program flow graph. (Our algorithm
does not proceed with the flow graph analysis
after it finds such an "improper" "go to," but
instead it returns the program, along with some
5
flow graph information, to the originator for cor-
rection.)
I0
Conversion of well structured flow graphs to
11
nested form. In a well structured program flow
graph, each strongly connected subgraph has a
unique entry node. If we delete all return arcs
(arcs leading to the entry node of a strongly con-
nected subgraph, from within the subgraph), the
resulting graph will have no strongly connected
components. Nevertheless, it may contain sub-
graphs of "hammock" form, corresponding to alter-
native clauses in the program. That is, there may
be a pair of nodes (such as nodes 2 and 8 in Fig.
3) that are joined by more than one path in the
same direction. PKT shows how to arrange the
nodes of such a graph (having no strongly connec-
Figure 3. Linearized flow graph of an interpola-
ted subgraphs) into a single linear sequence or
tion subprogram, constructed according
vector, in such a manner that no arc goes from any
to Peterson's algorithm [25]. In an
node to another node that precedes it in this
actual application, many more nodes
sequence 4. Thus all flow is "downward" except for
might appear between nodes i0 and ii.
the return arcs that have been temporarily removed
Note that these nodes will be incorpo-
from consideration.
rated within the iteration structure.
The key to this algorithm (and, incidentally,
the most complex part from the computational
cannot be composed from the four "permissible"
standpoint) involves the discovery of the lowest
structural units 3 .
cover for each merge node. [A merge node is any
node with more than one arc leading to it; and its
HU shows that a program flow graph is reduci-
lowest cover is the "lowest" node (in the sense
ble (in a certain sense which is important for
that arcs of the flow graph are directed "down-
program verification and especially for optimiza-
ward") through which every path passes that leads
tion) if and only if it contains no strongly con-
to the merge node.] Whenever it is discovered
nected subgraph with more than one entry point.
that the next node (to be chosen as an element of
Thus the set of programs having reducible flow
the vector) is the lowest cover for some merge
graphs also corresponds exactly to the set D u E
node, that merge node is pushed onto a stack. It
of Fig. 2. HU also shows that any Fortran program
is shown in PKT that this guarantees no node will
whose transfers to previous statements are all
be included twice in the vector. On the other
caused by the normal termination of "do" loops is
hand, when a node in the vector has all its suc-
reducible.
cessors already on the stack, the next node is
obtained by popping the stack.
A comparison of HU and PKT suggests an objec-
tive criterion for distinguishing between correct
This use of an auxiliary pushdown stack indu-
and incorrect uses of the "gn to " statement in
ces an implicit nesting relation among the nodes
Fortran programs. In a well structured program,
of the flow graph. The level of nesting of a node
such a statement may be used to implement a "down-
may be defined to be the depth of the stack at the
time the node was placed in the vector. Pushing a
node on the stack increases the nesting depth, and
PKT shows how to correct a program that is
therefore begins a subsequence of nodes that are
not well structured, by using a transforma-
all at (or deeper than) a certain nesting level.
tion called "node splitting." This is a way
This subsequence is, in effect, a block within the
of preserving one entry node of each strongly
flow graph, corresponding to a block of program
connected subgraph and removing all the
statements. This block ends at the point where
others. Each entry node to be removed is
the node is popped from the stack and incorporated
duplicated, along with that portion of the
in the vector.
subgraph connecting it to the remaining
entry node. Programs whose flow graphs can
be reduced by node splitting to D-charts cor-
An alternative node sequencing algorithm, due
respond to the region F in Fig. 2. Any pro-
to R. Tarjan [29], was brought to our atten-
gram flow graph can be reduced by node split-
tion while this paper was in preparation.
ting to the set D u E.
196
Automatic detection of exit arcs. The proce- end of each block, the statements from which flow
dure described so far is based closely upon the returns to a loop entry point, and the statements
algorithm described in PKT. However, some exper- causing control to exit from a block.
ience with this procedure drew our attention to an
anomalous result. When the exit from a loop forms In this listing, the labelled statements and
the only path to a certain part of the program, other statements for which a node was generated
the information contained in the program flow are printed in sequence according to the node num-
graph makes it appear that that part of the pro- bers in the vector generated by our modified PKT
gram belongs entirely inside the loop. For exam- algorithm. (Following each such statement in the
ple, consider the interpolation program illustra- listing are those non-control statements that fol-
ted in Fig. 3. A loop is used to search for a lowed it in the original source program.)
certain value of an index, and when an appropriate
index value is found, control exits from the loop Thus the statements in the restructured list-
and the entire remainder of the program is then ing are not, in general, in the original source
executed. The PKT algorithm incorporates virtu- program sequence; this original sequence is indi-
ally the entire program within the loop. cated by the node numbers appearing at the extreme
left side of the listing, and breaks in the orig-
We have extended the algorithm to detect arcs inal sequence are signalled by "sequence break"
that exit from a loop. The strongly connected comment lines. Such sequence breaks obviously
subgraphs (labelled by their entry nodes) form a garble the flow among the statements of the
tree. An exit arc may be defined as an arc lead- restructured listing, and these statements would
ing from one strongly connected subgraph to have to be modified to some extent to correct the
another one that is not contained within it. We flow and thus convert the restructured listing
find the outermost strongly connected subgraph into a correct program. In Fig. i, for example,
containing the source node of the exit arc but not the statement GO TO 4 (node 6) should be deleted,
containing its destination node. Before the entry and the test at node 4 should be reversed to read
node of this subgraph is placed in the vector, the I F (ABS (TERM) .LT. EPS) GO TO 2 . We have not
destination node of the exit arc is pushed onto undertaken, in the present version of our algo-
the auxiliary stack, thus creating an additional rithm, to produce the needed corrections automat-
block level. The resulting nested flow graph is ically.
shown in Fig. 4.
Remarks
Reconstituting the program listing
This same technique can, in principle, be
We use the results of this flow graph analy- applied to programs written in languages other
sis to produce a restructured version of the orig- than Fortran; the main requirement would be a sim-
inal program (see Fig. i). Nesting levels are ple adaptation of the process of generating the
displayed by means of successive indentations. flow graph from the program statements. However,
Comments are inserted to mark the beginning and it is the Fortran language that seems most clearly
to be in need of automatic aids to program struc-
ture recognition.
197
even a fairly conservative programmer may succumb 15. A. Ralston, "The future of higher level lan-
to the temptation to code a jump to the processing guages (in teaching)"
segment inside the loop. Proceedings International Computing Symposium
1973, North-Holland, Amsterdam, 1974
Further information. An expanded version of
this paper (LBL Report 3004) is available from the 16. C. A. Steele and A. E. Sedgwick, "DEFT: a dis-
author upon request. ciplined extension of Fortran"
Technical Report, Department of Computer Sci-
Bibliography ence, University of Toronto, 1973
i. C. Bohm and G. Jacopini, "Flow diagrams, Tur- 17. E. F. Miller, "Extensions to Fortran to sup-
ing machines, and languages with only two port structured programming"
formation rules" SIGPLAN Notices 8:6, 1973
Comm. ACM 9:3, 1966
18. L. Miller, "LINUS: a structured language for
2. E. W. Dijkstra, "Go to statement considered instructional use"
harmful" SIGCSE Bulletin 6:1, 1974
Comm. ACM 11:3, 1968
19. A. J. Cook, "A user's guide to CDC 7600/6600
3. R. M. Burstall, "Proving properties of'pro- MORTRAN"
grams by structural induction" TM 150.1, Computation Group, Stanford Linear
Computer Journal 12:1, 1969 Accelerator, Stanford, 1973
4. D. E. Cooper, "Programs for mechanical program 20. J. Flynn, "SFTran user's guide"
verification" Report 914-ICM-337, Jet Propulsion Labora-
Machine Intelligence 6, 1971 tory, Pasadena, 1973
198