F2cpp

From publications

f2cpp, a translation project

F2cpp is a FORTRAN IV to C++ translator that was developed in 1994 and in 2003 f2cpp was updated to support FORTRAN 77.

FORTRAN

FORTRAN, a contraction of FORmula TRANslation, was the first scientific and high-level compiled language. Although FORTRAN, or its variants, is still in use by the scientific community today, later generational languages such as C++ offer better user enhanceable typing and abstraction.


C++

C++ intrinsically supports object-oriented programming, and it can be enhanced through templates, types and operator overloading, and C++ has better type compatibility with modern languages and with operating systems that are often implemented in C. The ability to express the problem domain directly in the C++ language through abstraction coupled with the above features means that C++ is very suitable for data manipulation, control and visualisation in the modern computing environment.

Users can define their own mathematical types and with C++ operator overloading they can define infix operators that behave exactly as a mathematician or scientist expects.

f2cpp uses type abstraction and operator overloading when translating FORTRAN COMPLEX and DOUBLE COMPLEX types preserving both the infix operators as well as the mathematical behaviour.

Legacy

The object-oriented paradigm means that programming in the more historical languages is no longer popular and the demand for such languages is diminishing. FORTRAN is longer taught in mainstream computing, nor chosen by the younger generation, so I predict that it will no longer be the language of choice for the modern scientist. FORTRAN is loosing its cost effectiveness as an implementation language.

Organisations that have invested many years in the development of FORTRAN software are becomming increasingly aware that they have legacy code. However some relief may be provided because this legacy code may be translated verbatim by tools such as f2cpp. In the case of f2cpp the translated code recieves a second-lease of life for it can be directly incorporated and (compiled an linked) into software developed using C++. Because the translated code is translated verbatim employing infix operators and simple mappings the code can easily be maintained by a C++ developer.

f2cpp

f2cpp accepts FORTRAN (IV/77) source code as input and makes a direct and semantically equivalent translation to C++ source code as output. Each FORTRAN file is converted to a C++ file and a common header file is emitted for each type. The emitted source code is ANSI C++ compatible and may be compiled by Borland C++, Visual Studio C++ or gcc. This means f2cpp can be used to update FORTRAN source repositories to modern C++ source code that can readily be incoporated into new projects developed in C++ and this code will work on many platforms.

The f2cpp translator is like a compiler, it is composed of several elements namely: a low level scanner that converts ASCII text to a token stream, a parser that accepts tokens to form Symbol table elements and intermediate tree elements, and the code emitter that is implemented as a virtual method on the polymorphic intermediate tree element type hierarchy,

Enhancements

Embedded C++ source code

The f2cpp translator, although it will translate standard FORTRAN IV and 77, has been enhanced to support embedded C++ via the embedded directive E, which can be placed in column 1 and followed by standard C++ code on the remainder of the line. This means you can use C++ wrapped in FORTRAN to create specialist libraries or as replacement to standard routines.

External and Internal routines

The f2cpp translator also parses the C++ header files routine.h, inline.h and symboltab.h to obtain the definitions of all routines before parsing any FORTRAN files. External intrinsic routines are declared in routines.h while Internal (inline) intrinsic routines are defined in inline.h. Inline routines are expanded as C++ code inline with FORTRAN code that has been translated to C++. Both these files and the associated routines.cpp file may be modified by the f2cpp user to incorporate their own libraries or make their own efficiencies. The external routines may be written in C++ in routine.cpp, or any other C++ source file for that matter. Thus the f2cpp user can specify their own INTRINSIC and EXTERNAL routines and implement these routines in C++ or they can implement their library routines in FORTRAN. f2cpp employs inline intrinsics when there is a direct C++ equivalent, e.g. mathematical functions.

Parameter modes

FORTRAN (and f2cpp) normally employs the pass-by-address parameter mode so types, arrays of types and multi-dimensioned arrays of type can be treated with impunity. f2cpp additionally provides the C pass-by-value mode and the C++ pass-by-reference modes so translated FORTRAN code can interface with C++ libraries and take advantage of improved efficiency. The FORTRAN parameter mode of any FORTRAN source routine may be overriden with a # in front of the formal parameter name for pass-by-value, or an & for pass-by-reference, otherwise the regular pass-by-address mode (*) is assumed.

C++ Header files

Declarations are acquired for all routines during the translation process. When each symbol is subsequently defined it is added to the global symbol table for future reference. This symbol table is emitted into the symboltab.h file at the end of the translation process and this file is included into each of the translator output files. These same symbol table definitions are imported before processing the FOTRAN files so the FORTRAN files may be translated as many times as you wish.

Multi-pass processing

Whenever an undefined symbol is referenced during the translation process the current file is marked for recompilation. For these files with errors the the translation process becomes a two pass process; whenever an undefined symbol was found that file is translated in a second pass under the assumption that the symbol table declarations will be complete in the second pass. The f2cpp user may also choose to run f2cpp any number of times they like enabling them to make enhacements to symboltab.h on a progressive basis. The user may also choose to make a clean compile by deleting symboltab.h.