XTRAN Example — Replace COBOL Arithmetic Statements with COMPUTEs

Scenario:  You have inherited some old COBOL code, written before the COMPUTE statement was available, and as a result it's hard to read.

XTRAN to the rescue!

The following example uses a set of XTRAN rules ("meta-code") for re-engineering COBOL by replacing traditional arithmetic statements (ADD, SUBTRACT, etc.) with COMPUTE statements where possible.

The rules comprise only 163 code lines of meta-code.  They employ XTRAN's powerful statement and expression pattern matching and replacement capabilities to automate the re-engineering, using sets of "match" statement patterns and corresponding "replace" statement patterns.  The rules took about one hour to create and debug (that's right, just one hour!).

In the patterns below, THIS indicates COBOL elements and <this> indicates meta elements.

For each traditional arithmetic statement type, we use statement match patterns and corresponding replacement patterns.  For instance, for the ADD statement, the match patterns are

        ADD <expr1> TO <expr2>.

        ADD <expr1> TO <expr2> GIVING <expr3>.

where <exprn> match any legal expressions.

The corresponding replacement patterns are

        COMPUTE <expr2> = <expr2> + <expr1>.

        COMPUTE <expr3> = <expr1> + <expr2>.

where <exprn> are whatever expressions matched the corresponding <exprn> in the "match" pattern.

We use similar "match" and "replace" patterns for SUBTRACT, MULTIPLY, and DIVIDE statements.

How can such powerful and generalized re-engineering be automated in only 163 code lines of XTRAN rules, created and debugged in about one hour?  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 rules language functionality:

NOTE that the rendered code shown as XTRAN's output below was done with default conditions; XTRAN provides many options for controlling the way it renders code for output.

The input to and output from XTRAN are untouched.


Process Flowchart

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

process flowchart

Input to XTRAN:

IDENTIFICATION DIVISION.
SOURCE-COMPUTER. Whiz-Bang 1000.
TARGET-COMPUTER. Whiz-Bang 2000.
DATE-WRITTEN.  07/04/77.
DATE-COMPILED.  05/19/15.
PROGRAM-ID.  DEMCMP-R.

DATA DIVISION.
WORKING-STORAGE SECTION.
        77 VAR1 COMP PIC 9(5)V99.
        77 VAR2 DISPLAY PIC 9(3)V99.
        77 VAR3 COMP-1.
        77 VAR4 COMP.
        77 VAR5 DISPLAY PIC 99.

PROCEDURE DIVISION.
        ADD VAR1 TO VAR3.
        ADD VAR5 TO VAR3 GIVING VAR2.
        SUBTRACT VAR1 FROM VAR3.
        MULTIPLY VAR3 BY VAR1 GIVING VAR4.
        MULTIPLY VAR2 BY VAR3.
        DIVIDE VAR3 BY VAR1.
        DIVIDE VAR2 BY VAR4 GIVING VAR3 REMAINDER VAR1.
        DIVIDE VAR1 INTO VAR2 GIVING VAR3.
        STOP RUN.


Output from XTRAN:

IDENTIFICATION DIVISION.
SOURCE-COMPUTER. Whiz-Bang 1000.
TARGET-COMPUTER. Whiz-Bang 2000.
DATE-WRITTEN. 07/04/77.
DATE-COMPILED. 05/19/15.
PROGRAM-ID. DEMCMP-R.

DATA DIVISION.
WORKING-STORAGE SECTION.
	77  VAR1 COMP PIC 9(5)V99.
	77  VAR2 DISPLAY PIC 9(3)V99.
	77  VAR3 COMP-1.
	77  VAR4 COMP.
	77  VAR5 DISPLAY PIC 99.

PROCEDURE DIVISION.
	COMPUTE VAR3 = VAR3 + VAR1.
	COMPUTE VAR2 = VAR5 + VAR3.
	COMPUTE VAR3 = VAR3 - VAR1.
	COMPUTE VAR4 = VAR3 * VAR1.
	COMPUTE VAR2 = VAR2 * VAR3.
	COMPUTE VAR3 = VAR3 / VAR1.
	DIVIDE VAR2 BY VAR4 GIVING VAR3 REMAINDER VAR1.
	COMPUTE VAR3 = VAR2 / VAR1.
	STOP RUN.

Note that the REMAINDER attribute on a DIVIDE statement defeated the pattern match, so no replacement was done.

Notation in XTRAN run log

        Statement replace/includes:
          9 requests, 233 tries, 7 matches, 7 replacements