XTRAN Example — Translate Pascal String Operations 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.
Input to XTRAN:
TYPE strng = PACKED ARRAY [0..255] OF CHAR; { strng = PACKED ARRAY [0..255] ..} { .. OF CHAR; } VAR strng1, strng2 : strng; { strng1, strng2 : strng; } strng3 : PACKED ARRAY [255] OF CHAR; { strng3 : PACKED ARRAY .. } { .. [255] OF CHAR; } vary1, vary2 : VARYING [255] of CHAR; { vary1, vary2 : .. } { .. VARYING[255] OF CHAR; } BEGIN strng1 := strng1 + ' ADD '; { strng1 := strng1 + ' ADD '; } strng3 := 'STRFNC'; { strng3 := 'STRFNC'; } IF strng1 < strng2 THEN { IF strng1 < strng2 THEN } strng2 := strng1; { strng2 = strng1; } IF strng1 + vary1 >= strng2 + vary2 THEN { IF strng1 + vary1 >= .. } { .. strng2 + vary2 THEN } strng2 := strng1; { strng2 = strng1; } strng1 := strng1 + ' '; { strng1 = strng1 + ' '; } IF strng1 + strng2 = vary1 THEN { IF strng1 + strng2 .. } { .. = vary1 THEN } strng1 := strng2 { strng1 := strng2 } END.
Output from XTRAN:
typedef char Strng[256]; /* strng = PACKED ARRAY [0..255] .. */ /* .. OF CHAR; */ Strng strng1, strng2; /* strng1, strng2 : strng; */ char strng3[255]; /* strng3 : PACKED ARRAY .. */ /* .. [255] of char; */ char vary1[255], vary2[255]; /* vary1, vary2 : .. */ /* .. varying[255] of char; */ strcat(strng1, " ADD "); /* strng1 := strng1 + ' ADD '; */ strcpy(strng3, "STRFNC"); /* strng3 := 'STRFNC'; */ if (strcmp(strng1, strng2) < 0) /* IF strng1 < strng2 THEN */ strcpy(strng2, strng1); /* strng2 = strng1; */ { char p_str_tmp1[255]; /*temp string buffer*/ char p_str_tmp2[255]; /*temp string buffer*/ if (strcmp(stradd(p_str_tmp1, 2, strng1, vary1), stradd( p_str_tmp2, 2, strng2, vary2)) >= 0) /* IF strng1 + vary1 >= .. */ /* .. strng2 + vary2 THEN */ strcpy(strng2, strng1); /* strng2 = strng1; */ } strcat(strng1, " "); /* strng1 = strng1 + ' '; */ { char p_str_tmp1[255]; /*temp string buffer*/ if (strcmp(stradd(p_str_tmp1, 2, strng1, strng2), vary1) == 0) /* IF strng1 + strng2 .. */ /* .. = vary1 THEN */ strcpy(strng1, strng2); /* strng1 := strng2 */ }