Peaceful
Peaceful

Reputation: 5450

Identify if-endif and do-enddo constructs in long fortran code

I am trying to modify a very big Fortran legacy code (fortran77) but since the author of the code did not care to write comments and also didn't use proper indentation etc, I am having a huge difficulty in modifying the code. What I need is some way (a package or a command line tool) which will enable me to identify the end of a given construct like if-then or do-enddo if I show it where it starts. For example:

if(x .eq. 0)then
  if (y .eq. 0)then
    print*, y

  endif
endif

Suppose I want to see where does the first if statement ends, then this tool should show me last line and so on.

I would be quite grateful for any help. Thanks in advance.

Upvotes: 2

Views: 778

Answers (3)

jlokimlin
jlokimlin

Reputation: 593

Is there any particular reason why you're restricting yourself to Fortran 90; it's nearly 3 decades old. Modern Fortran allows identifiers for control structures, theexit is no longer confined to the do loops, and goto is finally deprecated by utilizing the Fortran 2008 block construct.

alpha: block
    bravo: do i = 1, num_in_set
        charlie: if (x == a(i)) then
            delta: select case (i)
                case (FIRST)
                    call do_something(x)
                case (SECOND)
                    call do_something_else(x)
                case (THIRD)
                    cycle bravo
                case default
                    exit alpha
            end select delta
        else if (x == a(i+1)) then
            x = foo(a)
        else
            x = bar(a)
            exit alpha
        end if charlie
        call finally_do_something(x)
    end do bravo
end block alpha

Upvotes: -1

Holmz
Holmz

Reputation: 724

In fort there is the labelling construct as follows (It may be a standard in F90):

 Cat_Loop:
&DO I = 1, N
    !<stuff>
  Dog_Loop: DO K = 1, M, 9  !Because Dogs are canines!
    !<stuff>
  Lizard_Loop:
&  DO J = 1, M
    !<stuff>

    ENNDO Lizard_Loop
  ENNDO Dog_Loop
ENDDO Cat_Loop

SELECTED CASE is sometimes helpful where an IF statement usually sits. This is another 90 standard:

SELECTED CASE (Dog)
  CASE(-9)
    <Stuff>
  !ENDCASE
  CASE(1)
    <Stuff>
  !ENDCASE
  CASE(2)
    <Stuff>
  !ENDCASE
  CASE(9)
    <Stuff>
  ENDCASE
  DEFAULT
    WRITE(*,*)' Why am I here with Dog=',Dog
  ENDCASE
END SELECT

The very first thing I would do is to capture some output for known inputs, and build some way to 'unit test' it. And periodically ensure you are sticking to the path that it was perviously at.

Upvotes: 0

Jauch
Jauch

Reputation: 1518

Here are two otions to do what you want:

http://www.polyhedron.com/pf-plusfort0html

https://sourceforge.net/projects/findent/files/

The other is to do (by yourself) a small code to indent FORTRAN files. It is not that difficult :)

Upvotes: 2

Related Questions