XTRAN Example — Translate Pascal to C


The following example uses the standard set of XTRAN rules for parsing Pascal and translating it to C.  NOTE that the translation shown below was done with default conditions.  XTRAN provides many options for controlling the way it translates.

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:

        FUNCTION func1 : INTEGER; EXTERNAL; { FUNCTION func1 : INTEGER; ..}
                                        { ..EXTERNAL; }
        FUNCTION func2(arg1, arg2 : INTEGER) : { FUNCTION func2(arg1, arg2 :.. }
          INTEGER; EXTERNAL;            { .. INTEGER) : INTEGER; EXTERNAL; }

PROCEDURE proc(i, j, k : INTEGER);      { PROCEDURE proc(i, j, k : INTEGER); }

    TYPE
        int_type = INTEGER;             { int_type = INTEGER; }
    VAR
        l, m, n : int_type;             { l, m, n : int_type; }

BEGIN
        FOR i := 1 TO k + 1 DO          { FOR i := 1 TO k + 1 DO }
                                        { comment on blank line after FOR }
            BEGIN
(*
 * Comment block
 *)
            FOR j := i DOWNTO 1 DO      { FOR j := i DOWNTO 1 DO }
                IF j < n THEN           { IF j < k THEN (blank line follows) }

                    BEGIN
                    l := m;             { l := m; }
                    m := m + 1;         { m := m + 1; }
                    n := func1          { n := func1 }
                    END
            END;
        i := k;                         { i := k; }
        REPEAT                          { REPEAT }
            i := i + 1;                 { i := i + 1; }
            REPEAT                      { REPEAT }
                j := j + 1              { j := j + 1 }
            UNTIL j = i;                { UNTIL j = i; }        
            j := i + 5;                 { j := i + 5; }
            k := func2(i, j)            { k := func2(i, j) }
        UNTIL i > k + 5;                { UNTIL i > k + 5; }
        k := i;                         { k := i; }
        CASE j + 1 OF                   { CASE j + 1 OF }
            1, 2 :                      { 1, 2 : }
                BEGIN
                i := i + 1;             { i := i + 1; }
                i := i + 2              { i := i + 2 }
                END;
            3 :                         { 3 : }
                i := i + 3;             { i := i + 3; }
            OTHERWISE                   { OTHERWISE }
                i := i + 99             { i := i + 99 }
        END
END.



Output from XTRAN:

        extern long func1();            /* FUNCTION func1 : INTEGER; ../*
                                        /* ..EXTERNAL; */
        extern long func2(long arg1,
          long arg2);                   /* FUNCTION func2(arg1, arg2 :.. */
                                        /* .. INTEGER) : INTEGER; EXTERNAL; */

void proc(long i, long j, long k)       /* PROCEDURE proc(i, j, k : INTEGER); */


{
        typedef long Int_type;          /* int_type = INTEGER; */
        Int_type l, m, n;               /* l, m, n : int_type; */

        for (i = 1; i <= k + 1; i++)    /* FOR i := 1 TO k + 1 DO */
                                        /* comment on blank line after FOR */
            {
/*
 * Comment block
 */
            for (j = i; j >= 1; j--)    /* FOR j := i DOWNTO 1 DO */
                {
                if (j < n)              /* IF j < k THEN (blank line follows) */

                    {
                    l = m;              /* l := m; */
                    ++m;                /* m := m + 1; */
                    n = func1();        /* n := func1 */
                    }
                }
            }
        i = k;                          /* i := k; */
        do                              /* repeat */
                                        /* UNTIL i > k + 5; */
            {
            ++i;                        /* i := i + 1; */
            do                          /* repeat */
                                        /* UNTIL j = i; */
                ++j;                    /* j := j + 1 */
                while (j != i);
            j = i + 5;                  /* j := i + 5; */
            k = func2(i, j);            /* k := func2(i, j) */
            }
            while (i <= k + 5);
        k = i;                          /* k := i; */
        switch (j + 1)                  /* CASE j + 1 OF */
            {
            case 1 :                    /* 1, 2 : */
            case 2 :
                ++i;                    /* i := i + 1; */
                i += 2;                 /* i := i + 2 */
                break;
            case 3 :                    /* 3 : */
                i += 3;                 /* i := i + 3; */
                break;

            default :                   /* otherwise */
                i += 99;                /* i := i + 99 */
                break;
            }
}