Compiling to and from Fortran (William B. Clodius)
Tue, 17 Oct 1995 01:21:34 GMT

          From comp.compilers

Related articles
Compiling to and from Fortran (1995-10-17)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (William B. Clodius)
Keywords: Fortran, C, performance
Organization: Compilers Central
References: <danpop.813847837@rscernix>
Date: Tue, 17 Oct 1995 01:21:34 GMT (Norman L. Reitzel ) wrote:
> (Dan Pop) writes:
> >In <> (Gordon
>Watts) writes:
> >>You can do anything in one language that you can do in another, if they
> >>are compiling to the same target machine... Sometimes it is a pain, but
> >>it is possible (look at ZEBRA from CERN as a memory manager (and more)).
> >Yeah? Could you point me to a C to Fortran converter?
> Shame on you! C had it's semantic roots in RatFor, which is precisely
> the kind of thing you are talking about. Gordon is precisely correct. I
> once spent some time working on a distillation simulation, and some parts
> were easier in C (C++ actually) while others were more natural in Fortran.
> <snip>

More precisely I believe RatFor has its roots in C and Fortran. It
implements some of the structured concepts Ritchie had already used
in C, but otherwise had the semantics of Fortran. This allowed it to
be compiled to Fortran and have good efficiency.

The above topic is really related to several questions

1. Can you do anything in a general programming language?
A. Yes, all languages are Turing complete. However, being Turing
complete is not the same as being able to do the same thing with equal
efficiency in any language. Some things are so difficult to do in one
language than it would be to do in another that it would never be done
in the first language.

2. Can you write a compiler in Fortran?
A. Yes. The first attempt at a LISP interpreter was done in FORTRAN II.
The first PROLOG interpreter was done in FORTRAN IV. Several Fortran
compilers are still implemented in Fortran, and some public domain
Fortran pretty printers, etc., which perform lexical analysis, etc. on
Fortran code, are also written in Fortran..

However, FLISP had so many problems that LISP 1-1.3 were not written in
Fortran. (LISP 1.3 served as the first full implementation of LISP.)
While the first PROLOG interpreter was efficient and portable, the lack
of a standardized IF ... THEN ... ELSE ... structure in the FORTRAN IV
language resulted in difficult to maintain code. Further,
the lack of recursion, pointer data structures, or recursive types
made it very difficult to implement in F77 certain common compilation
and lexical analysis techniques that can be easilly implemented in
other languages. Therefore, Fortran has not been commonly used for
this task at least since the mid-70s, when other languages with better
capabilities became more widely available. Typically, C (or C++)
are now used becaus of their wide availability and the availability of
YACC, LEX, and other related tools, or other, speciallized lanugages
and tools are used, e.g., see

Tools alone don't make C perfect for this task.

F90 has recursion and pointers, and with the right tools could serve
as a replacement for C and C++ (the Fortran equivalent of YACC, etc.
do not exist in a non-proprietary form), but it does not have the
capabilities of the specialized languages. At least some F9x compilers
will continue to be written at least partially in F9x as a test of the
compilers performance immediately apparent to the developers. Whether
compilers for other languages, or other specialized Fortran lexical
analysis tools, will be written in Fortran remains to be seen. Who is
going to create and make available the tools necessary for that task?

3. Can Fortran be the target language of a compiler?
A. Yes, see the SISAL project, It is
also the target language of many Fortran extensions.

4. Can Fortran be the target language for the full C language?
A. No, not unless you want horrible performance. A few syntactical
constructs in C, in particular the potential of aliasing, can only
be correctly mapped to the Fortran 90 language by accepting large
performance penalties. Some constructs, e.g., function pointers,
require libraries with at least a small performance penalty.

5. Can Fortran (90) be the target language for a useable subset of C?
A. Possibly. In most of their structure C and Fortran are similar
enough that translation could be relatively automatic. Care would need
to be taken with Fortran's lack of short circuiting, libraries would be
needed for function pointers, etc. Macros would be another source of
difficulty in the tranlation, although they might be translated to other
Fortran compatible macro processors. A bigger problem would be that
while most scientific code does not have aliasing, if yours is the
code with that property, then it will be impractical for the compiler
to determine the existence of aliasing, and hence rare, potentially
subtle, and Fortran compiler dependent errors will be introduced in the
translated code. In effect, an unsafe optimization switch would be
used. Finally, I/O syntax is sufficiently different in the two
languages that the I/O part of the resulting code will probably be
unmaintainable (See F2C output).

Given the above, would anyone want to use F90 as a target language for
C or most other commonly used languages?

I am cross posting this to comp. compilers for more knowledgeable

Note: This thread started on comp.lang.fortran and two other Macintosh
newsgrous. It covered amy other points before C to Fortran translation
came up. I cross posted this first without sending it as email to, and it is appearing on my news server. I hope this
doesn't cause problems.

  William B. Clodius Phone (505) 665-9370
  Los Alamos Natl. Lab. NIS-1 FAX (505) 665-7395
  PO Box 1663, MS-D466 Group Office (505) 667-2701
  Los Alamos, NM 87545 email

  NIS-1: Nonproliferation and International Security Div./
                Space and Atmospheric Physics Group


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.