Pascal to C (and vice versa)

John Gateley gateley at m2.csc.ti.com
Tue Oct 24 14:34:28 AEST 1989


In article <4640 at mentor.cc.purdue.edu> acu at mentor.cc.purdue.edu (Floyd McWilliams) writes:
>In article <5164 at uhccux.uhcc.hawaii.edu> dillon at uhccux.uhcc.hawaii.edu
>  (Ian Dillon) writes:
>>I'm looking for a Pascal to C (and  back again) conversion program.
>	I'd like to see how a C-to-Pascal conversion program would handle this:
>	int	i, *pi;
>	pi = &i;

Basically, what you do is declare a huge array of integers; this represents
`memory'. Now, an address is an index into the array. Whats that? You
wanted it efficient too? :^)

>	Or this:
>	int	*A;
>	A = (int *) malloc((unsigned) (sizeof(int) * 20));
>	A[10] = 5;

Same process, except you have to write malloc in pascal.

>	Or, for that matter, how a Pascal-to-C converter would take:
>	var
>	  X : set of char;
>	X := ['a','b','c'];
>	if 'a' in X then
>	  X := X + 'A'
>	else
>	  X := X * ['b','c'];

This one can be done easily. A set can be represented as a bit vector
(also known as an integer), of appropriate lenght. So you get
int x[16] /* you need 256 bits */
for(i=0;i<16;i++) x[i]=0;

/* 'a' is 97 */
x[6]:=x[6] && 1 /* or &, I dunno, I dont speak C */

and so on, you get the idea.

>	Now I know all these conversions CAN be done (since C and Pascal
>are Turing-equivalent).  But can they be done by currently available
>programs?

Sure, you can write a program to do it. The question really is
"can they be done so the output is useful". In the first case
(addresses), Pascal doesn't have a mechanism for dealing with
them easily, you might be able to fake them with pointers, or go
with the approach I suggested above. In any case, there is an
overhead associated with them. 
In the second case (Pascal Sets in C), the output is likely to be
highly unreadable. I took a lot of shortcuts above, the code which
would be generated automatically would be grosser.

John
gateley at m2.csc.ti.com



More information about the Comp.lang.c mailing list