f2p, c2p

sad at utkux.utcc.utk.edu sad at utkux.utcc.utk.edu
Fri Nov 24 21:52:05 CET 1995


> 
> Hello, folks!
> 
> Only one small question/suggestion:  Does there exist a f2p or c2p
> translator program which translates Fortran resp. C source to Pascal
> source?  If not, I suggest that somebody writes it because of the
> following reasons:

Hi there!
  I know there exist attempts to sovle that problem out there on the net but
they all are bound to be rather incomplete due to the very different nature
of the languages under consideration. In fact, this kind of thing is being
discussed quite often in all the involved language news groups (
comp.lang.fortran, comp.lang.c). Most of the tools I have heared of are
reported to ease the transition to pascal while leaving still much manual
work behind.
If you think of how much of the time that you actually spend porting a code
goes into which parts you will see that already things like changing the
comment and declaration parts and aligning them nicely (so that further
maintainance of the pascal code by a human is easily possible) are a big
step.
In fact, the fortran to c translator does not produce c code meant for the
human reader or for further maintainance of the code in C at all. It produces
code which has only the purpose to compile on a C compiler, the code is,
however, next to unreadable for a C programmer (and calls many library
routines to, e.g., emulate the Fortran output statements). Ihave yet to run
into a code that did not compile and run.
The pascal to c translator, on the other hand, produces rather reasonably
readable code, which doesn't always compile though.
So, there are two different approaches. For the transaltino of header files
seems to exist a solution since they are highly specific; as part of the
Prospero Extended Pascal for OS/2 you get (supposedly, I don't have it yet) a
bunch of translated header files and a translation utility that leaves only 
a bit manual work to be done.

> 
>   - Practical reason:  It would be nice to import libraries written
>     in C, C++ or Fortran automatically.  For most purposes, it would
>     be sufficient to have a program which ports C header files to
>     Pascal Unit (Module) interfaces.
> 

This one is a bit deceiving, since it in turn would lead to the conclusion
that assembler or machine code is superior to C, since the translation from
anything ends up in some such form. Of course, the  way back is neither
uniquely determined nor easily accomplished atomatically. The difference is
more the high- and low-level access to a language. You always could explain
Shakespeares M(a)cBeth to a five year old with rather limited vocabulary:
``Well, that guy on stage was to eager to get into power and in the end he
got killed because he's the bad guy'' but try to turn that sentence into
Shakespeare's language again ... See what I am saying? :-)

>   - Psychological reason:  The existence of a c2p would demonstrate
>     that Pascal is at least as powerful as C.  The existence of a p2c
>     (which does in fact *not* support everything which is possible in
>     Borland Pascal -- I am thinking of the Object extensions) is ac-
>     cepted by many people as a "proof" that C would be superior to 
>     Pascal, so we should "proove" the opposite direction. :-)
> 
> I am almost sure that at least the c2p program does not exist because
> Standard Pascal has indeed less abilities than C.  However, you can
> do everything which is possible in C with GNU Pascal (including e.g.
> pointer arithmetics -- just do a type cast to Integer), so the main
> difficulty will be to *understand* the C source, not to translate it.
> C++ could be more complicated as long as GNU Pascal does not (yet) have
> multiple inheritance.  For Fortran, I don't see any problems, since
> there are complex numbers in GNU (Extended) Pascal, but one should
> translate goto statements to structured statements wherever possible..

I really am not sure if it is so easy. Let's first look at C: All this malloc
stuff, and access to the memory address that contains the pointer to a
certain array element and such -- is that really easily accomplished in
E.Pascal? Or, my pet grief: Command line access ... what do you do with
int main(argc, *argv) { ...} or such? I think it won't be possible to achieve
a completely automatic translation when you come from C code, even though it
may be possible to code a solution to a give problem in both C and Pascal.
(Very often you find also lots of crap coded in C just to say something the
language Pascal would allow in one line, say, assignment of arrays: In c 
you'd have to assign array alement by array element since otherwise the
pointers would be the same, while in Pascal you can do an A:=B; if they are
just of compatible (or the same?) type.
So, even mechanical translation may not produce very Pascalish code, as one
could see from the first edition of the numerical recipes which had a very
Fortanish Pascal version of each routine in the appendix ...

Now Fortran: You would have to provide at leat a huge library (similar to the
one that comes with f2c) for a load of intrinsic Fortran functions,
input/output routines and the like, and you would have to find a way to deal
with Fortran  goodies like computed goto's,  multiple entry points for
subroutines or passing of arrays by passing the first element and the length
of the array (which frequently causes trouble when the array length is
different from the one passed).

So, being an optimist with life experience (as some define a pessimist) I
just want to be a bit cautious about too high expectatins of automated
translation.  I do agree, however, that it should be possible to write
something that could do a significant fraction of the porting work.



> 
> One idea how to write it:  Just use the front-ends of the GNU compi-
> lers and replace the assembler back-end by a Pascal back-end.  Once
> you have understood how the front-end passes information to the
> back-end, the job will be straightforward.  (One could even easily
> generalize it to a general x2y translator ...! :-)

Hm. So Shakespeare with the vocabulary of a five year old? Again, even if you
would be able to get something in E.P. out of such a back end it would be
very different from what an experienced Pascal Programmer would have written
and so it would probably end up being manually converted to a more efficient,
more readable, more Pascally code afterwards anyway ...
I think that a good, portable compiler and a good, portable conversion aid
utility are as much as we could have hoped for.
BTW, one way to push E.P. would be to convince the authors of the Numerical
Recipes to treat us to a new issue of their book for extended Pascal. They
gave up on Pascal a while back because there was no market, as they claimed,
and there are only Fortran and C versions left. An E.P. version would be
really a big step, I believe.

Later, gotta eat turkey now!   Stefan

> 
> I have not the time to do it myself :-( so I ask *you* if you are
> willing to do this important contribution.
> 
> Yours,
> 
>     Peter
> 
> --------------------------------------------------------------------------------
> Dipl. Phys. Peter Gerwinski
> Fachbereich Physik
> Universitaet-GH Essen                   Phone:  +49-201-183-2763
> D-45117 Essen                           Fax:    +49-201-183-2120
> Germany                                 e-mail: pege at mail.theo-phys.uni-essen.de
> --------------------------------------------------------------------------------
> 




More information about the Gpc mailing list