Click here to Skip to main content
6,292,426 members and growing! (9,880 online)
Email Password   helpLost your password?
Languages » C / C++ Language » Utilities     Intermediate License: A Public Domain dedication

Converting Fortran into usable C++ pseudo-code

By jc3po

How to convert existing Fortran code into C++ code
C++, Windows, Visual Studio, Dev
Posted:3 Aug 2007
Updated:9 Aug 2007
Views:12,743
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
1 vote for this article.
Popularity: 0.00 Rating: 3.00 out of 5

1

2
1 vote, 100.0%
3

4

5
Screenshot - programoutput.jpg

Introduction

This C++ program is a simple example of how a language like Fortran 77 can be converted into usable C++ code. The C++ code leaves the original intent of the program intact, but the current example does not handle special cases like string formatting, such as the FORMAT statement. In addition, GOTO statements are converted as they are in the original Fortran program, which in some cases may lead to spaghetti-like code.

The program works by first asking the user for a file list after loading the configuration file. The configuration file allows the program settings to be modified without recompiling. The main program loops the strings of files and processes them one at a time. All of the file I/O is handled within the convertFortran subroutine. After the entire input file is read to memory into QStringList, which is only a list of strings, each line of the program is processed one at a time.

Qt Library

The C++ example program was also written using Qt to allow the program to be compiled across many platforms, as many Fortran source code files may be located on legacy systems. Please go to Qt-TrollTech for the latest Qt library download.

Using the Code

The subroutine determineFortranType takes the Fortran code in and passes back, by reference: the stripped Fortran statement, the label and the indication if the line is a continuation line. The first item that is processed is the replacement of tabs with the correct number of spaces, because column positions are important. Next, the subroutine determines if the line is empty. If so, an enum eBlank is returned. The subroutine tries to determine if the statement is a comment or a # statement, as dictated by the first character. Then the reserved statement numbers are stripped from the line, the continuation character is determined and the code statement is stripped from the Fortran line.

FortranType determineFortranType( QString fullLine, 
    QString &code, QString &label, bool &isCont )
{
    isCont        = false;
    code        = "";
    label        = "";
    QString line    = fullLine;

    // Convert tabs into spaces first

    line.replace( "\t", "      " );

    int length        = line.length();
    if ( length == 0 ) 
    {
        return eBlank;
    }

    // Is it a comment?

    if ( line[0] == 'c' || line[0] == 'C' || line[0] == '*' ) 
    {
        
        if ( length > 2 )
            code = line.right( length - 2 );
        return eComment;
    }

    // Does this have the special # character

    if ( line[0] == '#' ) 
    {
    
        code = line;
        if ( line.find( "include", 0, FALSE ) == 1 )
            return eInclude;
        else if ( line.find( "define", 0, FALSE ) == 1 )
            return eDefine;
        else if ( line.find( "ifdef", 0, FALSE ) == 1 )
            return eIfDef;
        else if ( line.find( "endif", 0, FALSE ) == 1 )
            return eEndDef;
        else
            return eUnknown;
    }

    QString reserved, cont;
    
    // Pull the six reserved characters off and get 

    // continuation character and the code

    reserved    = line.left( 6 );
    cont    = line.mid( 5, 1 );
    code    = line.right( length-6 ).stripWhiteSpace();

    // Is this a continuation line, process it as one

    if ( ( cont != "0" && cont != " " ) || reserved[0] == '&' ) 
    {

        isCont = true;
        if ( reserved[0] == '&' ) 
        {
            reserved = "";
            label = "";
            return eCode;
        }
        
        if ( cont != "0" ) 
        {
            reserved = reserved.left( 5 );
        }
    }

    // Get label if any

    label = reserved.stripWhiteSpace();

    if ( isCont )
        return eCode;

    if ( code.find( "program", 0, FALSE ) == 0 ) 
    {
        return eBeginProgram;
    } 
    else if ( code.find( "subroutine", 0, FALSE ) == 0 ) 
    {
        return eSubroutine;
    } 
    else if ( code.find( "parameter", 0, FALSE ) == 0 ) 
    {
        return eParameter;
    } 
    else if ( code.find( "write", 0, FALSE ) == 0 ) 
    {
        return eWrite;
    } 
    ...
    } 
    else if ( code.isEmpty() && !label.isEmpty() ) 
    {
        return eLabel;
    } 
    else if ( code.isEmpty() ) 
    {
        return eBlank;
    }

    // Is this a Fortran variable?

    for ( int ii = 0; ii < _ReservedLength; ii++ ) 
    {
    
        if ( code.find( FortranReserved[ii], 0, FALSE ) == 0 )
            return eVariable;
    }

    // Must be code at this point

    return eCode;
}

If the line is a continuation line, the enum eCode is returned. This is because each continuation line is added to the previous one in the subroutine convertFortran. Lastly, the first keyword of the stripped code statement is matched to determine the Fortran type that is returned back as an enum. Also, if a match is not found, the program assumes that the line must be code, such as an assignment. In this case, it returns the default enum eCode.

Points of Interest

The work is mostly accomplished inside the subroutine determineFortranType. The above code snippet is part of the function that determines: the statement type, the code section, the label (if any) and finally, whether or not the line is a Fortran continuation line. Because Fortran code must follows the rules below, coding was made easier.

  1. Columns 1 through 5 are reserved for statement numbers or what are also called labels.
  2. Column 6 allows any non-blank character except a zero to indicate the continuation of a Fortran line. I've also seen the character & in column 1 used for this in addition.
  3. Fortran statements start in column 7 and can extend to column 72.

Also, remember that this process in not complete. Indices must be changed from the default one-based to the C++ zero-based. Also, multi-dimensional arrays in C/C++ are opposite from the Fortran language. Some key words will require rework for the converted code. As an example, the ENTRY and DATA statements will both require some rework. Also, GOTO statements can be eliminated or reworked as C++ switches. In the example provided, the GOTO to label 1 has an IF statement that could be switched out to a C++ while statement, which totally eliminates the need for a GOTO statement. Additional note: be careful with the SIND and COSD intrinsic functions, as they use degrees and not radians.

Web pages that may help:

History

  • Version 1.0: Program creation. -- July 21, 2007
  • Version 1.1: Added Fortran types to the enum list. Added COMMON conversion. Moved all local variables into one section. -- August 02, 2007

I can alternately be contacted at jciii@earthlink.net

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

jc3po


Member
jciii@earthlink.net
Occupation: Software Developer (Senior)
Location: United States United States

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Generaljust for fun Pinmembercharlie_craft4:40 16 Aug '07  
GeneralRe: just for fun Pinmemberjc3po6:07 16 Aug '07  
GeneralRe: just for fun PinmemberJeffBilkey19:37 23 Aug '07  
GeneralRe: just for fun Pinmemberjc3po3:41 24 Aug '07  
Generalf2c Pinmembermata-nui0:53 14 Aug '07  
GeneralRe: f2c Pinmemberjc3po6:12 16 Aug '07  
GeneralRe: f2c PinmemberLornad2:59 12 Jan '09  
GeneralRe: f2c PinmemberLornad3:07 12 Jan '09  
GeneralThere WAS a commercial package that did this PinmemberJeffBilkey12:53 13 Aug '07  
GeneralRe: There WAS a commercial package that did this Pinmemberjc3po13:48 13 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Aug 2007
Editor: Genevieve Sovereign
Copyright 2007 by jc3po
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project