XTRAN Example — Replace COBOL Arithmetic Statements with COMPUTEs
The following example uses a set
of XTRAN rules ("meta-code") for
re-engineering COBOL by replacing traditional arithmetic
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
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>.
any legal expressions.
The corresponding replacement patterns are
COMPUTE <expr2> = <expr2> + <expr1>. COMPUTE <expr3> = <expr1> + <expr2>.
<exprn> are whatever
expressions matched the
<exprn> in the
We use similar "match" and "replace"
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:
- Delimited list manipulation
- "Per statement" recursive iterators
- XTRAN's COBOL syntax extensions — in this case, a line continuation character for convenience in specifying patterns
- Statement-level pattern matching and replacement in XIR
- Navigation in XIR — in this case, selecting which code tree to process
- Creating new meta-functions written in meta-code, which we call user meta-functions
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.
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
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
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