C to fortran converter

I wonder if ROSE can be adapted to translate C to Fortran.

Developed at Lawrence Livermore National Laboratory (LLNL), ROSE is an open source compiler infrastructure to build source-to-source program transformation and analysis tools for large-scale C (C89 and C98), C++ (C++98 and C++11), UPC, Fortran (77/95/2003), OpenMP, Java, Python and PHP applications.

I work with one of the ROSE developers. I put you in touch with her if you’re interested in using it and need more info.

Thanks a lot. You can contact me at hongyi.zhao@gmail.com.

I am trying to understand advanced features of Fortran.
Can I understand Fortran derived types as C structs and Fortran classes as C++ classes ?

Fortran derived types are like C++ classes…mostly. If you want a procedure to only accept a specific type as argument, specify that argument as type(thing). If you want it to also be able to accept any type which extends it, specify it as class(thing). If you want a variable to be polymorphic (i.e. at run time could be of type thing or anything that extends it), it has to be declared as class(thing), allocatable :: my_thing. There are some other nuances, but that’s the gist.

@everythingfunctional
Thank you. After learning OOP in c++ I am finding it a little difficult to comprehend in fortran. May be I should start afresh.

The fundamental concepts aren’t so different, but the terminology is different, and some of the semantics have nuanced differences. You’ll start to recognize things the more you study it.

I’m trying to compile Beliavsky C2F.F90 using gfortran with the Simply Fortran IDE.
I get countless errors, some I can fix, such as.

54 | (/‘TYPE (???_PTR_1D)’,‘TYPE (INT2_PTR_1D)’,‘TYPE (INT_PTR_1D)’, &
| 1
Error: Different CHARACTER lengths (17/18) in array constructor at (1)

131 | C_if = 70, & ! #if.... | 1 Fatal Error: Invalid character '’ at (1). Use ‘-fdollar-ok’ to allow it as an extension
compilation terminated.

337 | v.name = SP
| 1
Error: Unclassifiable statement at (1)

But here are the first two that I don’t know what to do with:

2532 | READ (Cline(c+2:c+n-1),‘(O)’) k
| 1
Error: Nonnegative width required in format string at (1)


.\C2F.F90:2539:49:

2539 | ELSE IF (Cline(c:c+9) == ‘CALL FREE(’) THEN
| 1
Error: Unexpected ELSE IF statement at (1)

Has anyone actually compiled this code with gfortran in Windows, and knows what to do with these and other errors?

Btw, I don’t know how you format these to look like other posts.

Thanks
Kale

DId you follow the link posted in the Readme?

The gfortran mailing list discussed in 2017 Paul Richard Thomas - Output from C2F how to make the program compilable with gfortran.

Also, if you just need an executable on Windows, one is included in the repo.

Yes, I read that discussion. It doesn’t supply any code or touch on the issues I encountered.

Concerning the exe: I’d like to modify the code to be a subroutine where I pass the c file.

Try: substituting an asterisk for ‘(O)’ in the read statement, The other error would require more context to diagnose.

Preformatted text 

is an option labeled </> on the menu bar above the input box provided by Discourse for replies.

Thank you
I get many many of these type errors:

5580 |          elseif(a_z(ICHAR(ch))) then
      |                1
Error: IF clause at (1) requires a scalar LOGICAL expression

.\C2F.F90:5712:18:

 5712 |          if( .not. a_z(ICHAR(ch))) then    ! syn: x = 1
      |                  1
Error: Operand of .not. operator at (1) is INTEGER(4)


8025 |      IF ( .not. identifier(ICHAR(FF(f-1))) .and. identifier(ICHAR(FF(f)))) EXIT
      |               1
Error: Operand of .not. operator at (1) is INTEGER(1)

4385 |       IF (identifier(ICHAR(CC(c2+2)))) THEN     ! while (--k) or (++k)
      |          1
Error: IF clause at (1) requires a scalar LOGICAL expression

Where:

INTEGER*1 :: a_z(0:127) = (/ 0,0,0,0,0,0,0,0, &
             0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, &
             0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1,1,1, &
             1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,0,0,0,0,0,0,1, &
             1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,0,0,0,0,0 /)

INTEGER*1 :: identifier(0:127) = (/ 0,0,0,0,0,0,0,0, &              !  $ %
             0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,1,1, &
             0,0,0,0,0,0,0,0,1,0, 1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,1,1,1, &
             1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,0,0,0,0,1,0,1, &
             1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,0,0,0,0,0 /)

Any help?

Fortran has a stronger concept of bool/logical compared to C, where non-zero values evaluate true. In Fortran you cannot use an integer as the if condition.

Either you should change the array type to logical, or you should change your expressions to scalar == 1.

In SUBROUTINE for_preproc(), line 4077, it has this:

DO c = c,Cend
…
enddo

Does it make sense that a do loop can have its iterator as its start value?

As stated in a previous thread Some explanation on do loop

The number of trips through the loop is computed before the loop begins.

so the code is legal, although I would avoid writing such code. The code

implicit none
integer :: i
i = 2
do i=i,4
   print*,i
end do
end

for example gives output

           2
           3
           4

Thanks.

Starting on line 2170, it has a do loop with no increment. Is this allowed? Advised? Ill-advised?

  ! - - -  - - - - - - - - - - - - -
  ! - - - PASS 3 Cline -> Fline - - -
  ! - - -  - - - - - - - - - - - - -
  DO
! Lots of code…
   END DO                    ! until Cline processed

It is an infinite loop and is allowed. Sometimes it is the right loop construct to use. It is more general than the do while loop. There is probably a line such as

if (condition) exit

within the loop.

Hello,
Back at it…
I can’t get c2f.f90 to work on Windows, gfortran, Simply Fortran (SF).
Lots of edits to remove warnings and errors, e.g.:
Made it a module with the intent or making it a function call.
Hardwired the input file: character(60)::ksfileQ = “C2F-Test.c”;
Added suffix Q to SF blued names: nameQ
Type referencing: v.nameQ to v%nameQ
Functions get result treatment: function L2IQ(logi) result(rv)
Tried to resolve some troubling nested elseif
Other stuff…
RUNQQ section becomes:

if (run_pp) then
    arg1 = 'F\' // ProgramName(1:Nsize) // '.C'
    arg2 = 'F\' // ProgramName(1:Nsize) // '.CPP'
    INQUIRE(FILE='CPP.EXE', EXIST=exists)

    if (exists) then
        ! Use GNU CPP if present
        CALL EXECUTE_COMMAND_LINE('CPP -C -P ' // arg1 // ' ' // arg2)
    else
        ! Assume DVF FPP is installed on system
        CALL EXECUTE_COMMAND_LINE('FPP /ansi /m /C ' // arg1 // ' ' // arg2)
    endif

    ! Open the preprocessed file for reading
    OPEN(11, FILE='F\' // ProgramName(1:Nsize) // '.CPP', action='READ')
    WRITE(*,*) 'Pre-processing completed'
else
    ! Open the original .C file if no pre-processing is done
    OPEN(11, FILE='F\' // ProgramName(1:Nsize) // '.C', action='READ')
endif

But this compile error is a problem:

Compiling .\C2F.F90
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
.\C2F.F90:2145:17:

 2073 | DO nid = 1,MID
      |              2   
......
 2145 | string = v%nameQ(nid+1)
      |                 1
Warning: Array reference at (1) out of bounds (501 > 500) in loop beginning at (2) [-Wdo-subscript]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
.\C2F.F90:2151:17:

 2073 | DO nid = 1,MID
      |              2   
......
 2151 | string = v%nameQ(nid+2)
      |                 1
Warning: Array reference at (1) out of bounds (502 > 500) in loop beginning at (2) [-Wdo-subscript]
- - - - - - - - - - - - - - -

!########################################################
Rigged to run. This code:


// xxx
#include <stdio.h>

`int main()`
{
 printf("Hello World");
 return 0;
}

Gave this:

File exists and is U
 The file is           85  bytes in size
 C\C2F-TEST.C                                                                                        
     {  &- 
     }  &- 
 0 Errors detected (c)

!########################################################
I’m not sure how this code ever worked with gfortran in Windows.
Any thoughts, help?
Kale