XTRAN Example — Restructure COBOL

The following example uses a rule set written in XTRAN's rules language ("meta-code") that implements a set of code structuring algorithms to eliminate GO TO statements in COBOL by imposing IF statements.  The algorithms use recursive statement and expression pattern matching and replacement capabilities provided by XTRAN's rules language, involving "match" and "replace" statement patterns.

The rules comprise 515 non-comment lines of "meta-code" (XTRAN's rules language), and they are not very specific to COBOL; they are easily adapted to work for any 3GL language.  In fact, they were adapted from similar rules we already had for C/C++/C# and PL/I.  It only took about 2 hours to do the adaptation.

How can such powerful and generalized code quality improvement be adapted to a new language in 2 hours and comprise only 515 lines of meta-code?  Because there is so much capability already available as part of XTRAN's rules language.  The rules used for this example take advantage of the following functionality provided by that rules language:

In the patterns below, THIS indicates COBOL elements and <this> indicates meta (rules language) elements.  Also, the matching of each occurrence of <label> in each pattern is constrained by a condition — the matching label must have only one GO TO to it.




Structure with IF

To impose IFs, we use the following "match" and "replace" patterns.

The first IF "match" pattern has the form:

        IF <expr> THEN
            GO TO <label>;
            END-IF;
        <stmts>
<label>.
        <stmt>

where <expr> matches any conditional expression, <label> matches any label, <stmts> matches one or more statements containing no IF at the top level and no labels, and <stmt> matches any statement.  Note that the second occurrence of <label> must be the same symbol as the first occurrence in order for the pattern to match.

The second IF "match" pattern is the same as the first except that it allows IF statements at the top level of <stmts>.

The IF "replace" pattern has the form:

        IF NOT (<expr>) THEN
            <stmts>;
            END-IF;
<label>.
        <stmt>

where <expr>, <stmts>, <label>, and <stmt> are whatever matched them in the "match" patterns.




Strategy

For each "match" pattern, the meta-code repeatedly scans the code to be processed, using a facility built into XTRAN's rules language that visits each statement (recursively) once in each pass, looking for matches and replacing them with the corresponding "replace" patterns, until it can find no more matches to be replaced.  The meta-code also tells XTRAN to delete any label that has no references, at the end of each pass, so if the structuring deletes all references to a label, the label will disappear.  This has the effect that a successful match and replacement can trigger more structuring on the next pass.

The meta-code also:




Process Flowchart

Here is a flowchart for this process, in which the elements are color coded:

process flowchart

COBOL Code Before Structuring:

IDENTIFICATION DIVISION.
SOURCE-COMPUTER. Whiz-Bang 1000.
TARGET-COMPUTER. Whiz-Bang 2000.
DATE-WRITTEN. 07/04/77.
DATE-COMPILED. 02/30/99.
PROGRAM-ID. DEMSTR-R.

DATA DIVISION.
WORKING-STORAGE SECTION.
        77 var1 COMP.
        77 var2 COMP-1.

PROCEDURE DIVISION.

lbl0.   MOVE 0 TO var1;
        IF var2 = 0 THEN
            GO TO lbl2;
            END-IF;
        MOVE 1 TO var1;
        IF var1 > 1 THEN
            GO TO lbl1;
            END-IF;
        MOVE 2 TO var1;
lbl1.   MOVE 3 TO var2;
        GO TO lbl3;
lbl2.   MOVE 4 TO var1;
lbl3.   MOVE 5 TO var1;
        STOP RUN.



COBOL Code After Structuring with XTRAN rules:

IDENTIFICATION DIVISION.
SOURCE-COMPUTER. Whiz-Bang 1000.
TARGET-COMPUTER. Whiz-Bang 2000.
DATE-WRITTEN. 07/04/77.
DATE-COMPILED. 02/30/99.
PROGRAM-ID. DEMSTR-R.

DATA DIVISION.
WORKING-STORAGE SECTION.
	77  var1 COMP.
	77  var2 COMP-1.

PROCEDURE DIVISION.

	MOVE 0 TO var1;
	IF var2 NOT = 0 THEN
	    MOVE 1 TO var1;
	    IF var1 <= 1 THEN
		MOVE 2 TO var1;
	        END-IF;
	    MOVE 3 TO var2;
	    GO TO lbl3;
	    END-IF;
	MOVE 4 TO var1;
lbl3.	MOVE 5 TO var1;
	STOP RUN.



Notation in XTRAN run log

XTRAN eliminated 2 GO TO's through structuring