XTRAN Example — Tally RPG Statement Types
Scenario -- you're the software development manager for an RPG shop, and you want to monitor your developers' use of RPG's statement types, to ensure they're taking advantage of the useful ones and avoiding those that might be problematic.
XTRAN to the rescue!
The following example uses an XTRAN rules file comprising only 21 non-comment lines of "meta-code" (XTRAN's rules language) to tally the occurrence of statement types in RPG.
These rules are not specific to RPG; they can be used unchanged to tally statement type occurrences in any language.
We then use another set of XTRAN rules, comprising 77 noncomment lines, to format the results into a report. Because these rules manipulate only text, the XTRAN version used is "rules only". NOTE that these rules can report any kind of tallies, regardless of the source.
The code mining rules for this example can easily be enhanced
to produce DSV output that can be interactively queried using
existing XTRAN rules.
How can such powerful and generalized code analysis and reporting be automated in only 98 total code lines of XTRAN rules? Because there is so much capability already available as part of XTRAN's rules language. These rules take advantage of the following functionality:
- Text file input and output
- Text manipulation
- Environment variable manipulation
- Regular expression matching
- Delimited list manipulation
- Content-addressable data bases
- "Per statement" recursive iterator
- Access to XTRAN's Internal Representation (XIR)
- XIR's language-independent properties
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:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- RED for
code - PURPLE for text data files

Input to XTRAN:
00100C FUNC BEGSR 00200C MOVE 1 FLD2 00300C FLD1 IFEQ 1 00400C FLD2 IFEQ 1 00500C FLD2 ADD 1 FLD2 00600C FLD2 IFLE 1 00700C 1 ADD FLD2 FLD2 00800C END 00900C ELSE 01000C FLD1 IFGT 1 01100C 1 ADD FLD2 FLD2 01200C END 01300C 1 ADD FLD2 FLD2 01400C END 01500C END 01600C FLD2 ADD 1 FLD2 01700C ENDSR
Output from XTRAN code analysis rules:
ADD,5 BEGSR,1 ELSE,1 END,4 ENDSR,1 IFEQ,2 IFGT,1 IFLE,1 MOVE,1
Output from XTRAN tally reporting rules:
RPG Statement Type Tallies ADD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 BEGSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ELSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 END . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 ENDSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 IFEQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 IFGT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 IFLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 MOVE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1