XTRAN Example — Analyze Language Features Used

Scenario -- you're the software development manager for a PL/I shop, and you want to monitor your developers' use of PL/I's many language features, 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 114 non-comment lines of "meta-code" (XTRAN's rules language) to analyze use of language features in analyzed code.  The rules took about 2 hours to write and debug.

This example analyzes PL/I, but the XTRAN rules used for this example are not specific to PL/I; they can be used unchanged to analyze any language XTRAN handles.

An English paraphrase of these rules is as follows:

    For each statement
        Tally statement's type
        For each of statement's attributes
            Tally statement type / attribute combination
        For each of statement's expressions
            If expression has operator
                Tally expression operator
            Else if expression is declaration
                For each of declaration's data attributes
                    Tally data attribute
    Open output file (create or append)
    For each statement type seen
        Output statement type's tally
    For each statement type seen
        For each attribute seen on this statement type
             Output statement type / attribute combination's tally
    For each expression operator seen
        Output expression operator's tally
    For each data attribute seen
        Output data attribute's tally
    Close output file

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 be automated in only 2 hours and 114 lines of rules?  Because there is so much capability already available as part of XTRAN's rules language.  These rules take advantage of the following functionality:

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:

data flowchart

Input to XTRAN:

        DCL a(3, 2:5) BINARY FIXED(6, 2);
        DCL (b, c) DECIMAL FLOAT (6);
        DCL (d, e) BINARY FIXED(31);
        DCL i;

        IF (a(2, 2) > b) THEN DO;
            a(2, 2) = b;
            GO TO lbl1;
            END;
        ELSE DO;
            a(1, i) = b;
            c = c / 2.0;
            SELECT (a(1, i));
                WHEN (1, 2) DO;
                    b = c;
                    END;
                WHEN (3)
                    b = 2 * b;
                OTHERWISE DO;
                    b = 3 * b;
                    c = 3 * c;
                    END;
                END;
            END;
        d = 1 + d;
        DISPLAY (d) REPLY(c);
lbl1:
        IF (e < d) THEN
            GOTO lbl2;
        e = i + e;
        SELECT;
            WHEN (e > i) DO;
                e = i;
                i = i + 3;
                END;
            WHEN (e < i)
                i = e;
            OTHERWISE
                i = e + 2;
            END;
        IF (e > d) THEN DO;
            e = e + i;
            i = e + i;
            END;
        e = e - i;
lbl2:
        e = i - e;


Output from XTRAN:

Statement types
---------------
<expr> = 17
DECLARE = 4
DISPLAY = 1
DO = 6
END = 8
OTHERWISE = 2
SELECT = 2
WHEN = 4
XTR_ELSE = 1
XTR_GOTO = 2
XTR_IF = 3

Statement attributes, by statement type
---------------------------------------
DISPLAY/REPLY = 1

Expression operators
--------------------
* = 3
+ = 6
- = 2
/ = 1
< = 2
= = 17
> = 3

Data attributes
---------------
BINARY = 2
DECIMAL = 1
FIXED = 2
FLOAT = 1
PREC = 3
XTR_DIM = 2
XTR_DIMS = 1
XTR_LWRBND = 1
XTR_UPRBND = 2