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
TO statements in COBOL by imposing
algorithms use recursive statement and expression pattern matching and
replacement capabilities provided by XTRAN's rules
language, involving "match" and "replace"
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:
- Statement and expression pattern matching and replacement
- Text manipulation
- Text formatting
- Access to XTRAN's Internal Representation (XIR)
- XIR's language-independent properties
- Navigation in XIR
- Creating new meta-functions written in meta-code, which we call user meta-functions
In the patterns below,
THIS indicates COBOL elements and
<this> indicates meta (rules
language) elements. Also, the matching of each occurrence
<label> in each pattern is
constrained by a condition — the matching label must have only
GO TO to it.
Structure with IF
IFs, we use the following "match" and
IF "match" pattern has the form:
IF <expr> THEN GO TO <label>; END-IF; <stmts> <label>. <stmt>
<expr> matches any
matches any label,
one or more statements containing no
IF at the top level and no
<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.
IF "match" pattern is the same as the
first except that it allows
IF statements at the top level of
IF "replace" pattern has the form:
IF NOT (<expr>) THEN <stmts>; END-IF; <label>. <stmt>
<stmt> are whatever matched them in the
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:
- Automatically simplifies
NOT (<expr>)where possible; for example,
NOT (a < b)becomes
a >= b.
- Reports, in the run log, the number of
GO TOs eliminated.
Here is a flowchart for this process, in which the elements are color coded:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- RED for
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