XTRAN Example — Translate PL/I to C
The following example uses the standard set of XTRAN rules for parsing PL/I 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:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- RED for
code
Input to XTRAN:
DCL a(3, 2:5) BINARY FIXED(6, 2); /*DCL a(3, 2:5) BINARY FIXED(6, 2);*/ DCL (b, c) DECIMAL FLOAT (6); /*DCL (b, c) DECIMAL FLOAT (6);*/ DCL (d, e) BINARY FIXED(31); /*DCL (d, e) BINARY FIXED(31);*/ DCL f, i; /*DCL f, i;*/ IF (a(2, 2) > b) THEN DO; /*IF (a(2, 2) > b) THEN DO;*/ a(2, 2) = b; /*a(2, 2) = b;*/ GO TO lbl1; /*GO TO lbl1;*/ END; ELSE DO; /*ELSE DO;*/ a(1, i) = b; /*a(1, i) = b;*/ c = c / 2.0; /*c = c / 2.0;*/ SELECT (a(1, i)); /*SELECT (a(1, i));*/ WHEN (1, 2) DO; /*WHEN (1, 2) DO;*/ b = c; /*b = c;*/ END; WHEN (3) /*WHEN (3)*/ b = 2 * b; /*b = 2 * b;*/ OTHERWISE DO; /*OTHERWISE DO;*/ b = 3 * b; /*b = 3 * b;*/ c = 3 * c; /*c = 3 * c;*/ END; END; END; d = 1 + d; /*d = 1 + d;*/ lbl1: IF (e < d) THEN /*IF (e < d) THEN*/ GOTO lbl2; /*GOTO lbl2;*/ e = i + e; /*e = i + e;*/ SELECT; /*SELECT;*/ WHEN (e > i) DO; /*WHEN (e > i) DO;*/ e = i; /*e = i;*/ i = i + 3; /*i = i + 3;*/ END; WHEN (e < i) /*WHEN (e < i)*/ i = e; /*i = e;*/ OTHERWISE /*OTHERWISE*/ i = e + 2; /*i = e + 2;*/ END; IF (e > d) THEN DO; /*IF (e > d) THEN DO;*/ e = e + i; /*e = e + i;*/ i = e + i; /*i = e + i;*/ END; e = e - i; /*e = e - i;*/ lbl2: e = i - e; /*e = i - e;*/
Output from XTRAN:
double a[3][4]; /*DCL a(3, 2:5) BINARY FIXED(6, 2);*/ double b, c; /*DCL (b, c) DECIMAL FLOAT (6);*/ long d, e; /*DCL (d, e) BINARY FIXED(31);*/ double f; long i; /*DCL f, i;*/ if (a[1][0] > b) /*IF (a(2, 2) > b) THEN DO;*/ { a[1][0] = b; /*a(2, 2) = b;*/ goto lbl1; /*GO TO lbl1;*/ } else /*else do;*/ { a[0][i - 2] = b; /*a(1, i) = b;*/ c /= 2.0; /*c = c / 2.0;*/ switch (a[0][i - 2]) /*SELECT (a(1, i));*/ { case 1 : /*when (1, 2) do;*/ case 2 : b = c; /*b = c;*/ break; case 3 : /*when (3)*/ b *= 2; /*b = 2 * b;*/ break; default : /*otherwise do;*/ b *= 3; /*b = 3 * b;*/ c *= 3; /*c = 3 * c;*/ break; } } ++d; /*d = 1 + d;*/ lbl1: if (e >= d) /*(XTR: reversed) IF (e < d) THEN*/ /*GOTO lbl2;*/ { e += i; /*e = i + e;*/ if (e > i) /*select;*/ /*WHEN (e > i) DO;*/ { e = i; /*e = i;*/ i += 3; /*i = i + 3;*/ } else if (e < i) /*WHEN (e < i)*/ i = e; /*i = e;*/ else /*otherwise*/ i = e + 2; /*i = e + 2;*/ if (e > d) /*IF (e > d) THEN DO;*/ { e += i; /*e = e + i;*/ i += e; /*i = e + i;*/ } e -= i; /*e = e - i;*/ } e = i - e; /*e = i - e;*/