1

In my fortran code, I'm attempting to include a DO loop, but I'm ending up with a "DO loop or BLOCK IF not closed." It appears if any other DO loops after the INCLUDE statement are opened it treats them as nested loops, indicating to me the included opening DO line is interpreted correctly, but not the END DO statement. I've reduced the included code to bare-bones to make sure it's the loop and not the statements in the loop giving a problem. The control variable is declared before the DO loop.

DO A = 1,3
END DO

Does Fortran77 not allow for DO loops in INCLUDE files?

I'm using gfortran for my compiler if it changes much.

Edit: grammar

Edit2: I'm using GCC 4.6.2. Now to note, if this makes a difference, gfortran is being run from a makefile made by PSCAD. I can provide info on that if it's pertinent.

Here's code that is experiencing this issue:

TEST.F:

SUBROUTINE TESTFX() 
INTEGER A 
INCLUDE '../HDR.INC' 
END

HDR.INC:

DO A = 1,3 
END DO

Edit3: Edited typos in code and removed RETURN from subroutine.

agentp
  • 6,849
  • 2
  • 19
  • 37
Will S
  • 311
  • 2
  • 9
  • I guess those stray quote marks after end and end do are unintentional. and the include is actually on its own line ? I'd do a clean up edit, but maybe tat stuff is your problem. – agentp Aug 05 '13 at 18:21
  • What is the complete and exact text of the error message you get? Like george, I assume the formatting and quote marks are not in your original source. – Steve Lionel Aug 05 '13 at 18:41
  • "Error on line 6 of TEST.f: DO loop or BLOCK IF not closed" is the error. Also, I made an assumption PSCAD had been using gfortran, which was incorrect. PSCAD actually is compiling using f2c, which I'm not sure if f2c is using g77, which is in the same directory as the f2c being called by the makefile. This ends up being the compile statement: "f2c.exe -r8 -w -Nn5000 -NL400 -Nx400 -I"C:\PROGRA~1\PSCAD42\emtdc\gnu\inc" -I"C:\PROGRA~1\PSCAD42\emtdc\gnu\windows" -g TEST.f" from the makefile. – Will S Aug 05 '13 at 18:59
  • make sure if you are using fixed form source that the include file is properly in fixed form ( having the "include" line indented does "not" indent the contents of the file. – agentp Aug 05 '13 at 19:05
  • Yep, that's square as well. Everything starts on column 7 in my F file and include file. – Will S Aug 05 '13 at 19:07
  • This is sounding like an f2c issue -- I added the f2c tag. Maybe looking at the resulting c code could be enlightening. – agentp Aug 05 '13 at 19:19
  • F2c isn't creating any c code from what I can tell. Any way I can force it to spit out some code? – Will S Aug 05 '13 at 19:48
  • 1
    This is almost certainly an f2c parsing issue - I didn't know anyone still used that. It is probably not correctly dealing with DO loops in include files. Get a better Fortran compiler (one from this century would help.) – Steve Lionel Aug 05 '13 at 21:03
  • If you replace the include statement with the body of the include file, does it work? If it does, then it is possible that f2c does not recognize include. – cup Oct 10 '13 at 04:27

2 Answers2

1

Fortran 77 doesn't have INCLUDE at all. That first made its appearance in a Fortran standard in Fortran 90. That said, INCLUDE was available as an extension in pretty much all Fortran 77 compilers and the behavior was the same - it is essentially the same as inserting the included file in the source file where the INCLUDE appears. There are no restrictions on what can be there, though I have seen compilers struggle with issues related to block constructs that straddle an include file boundary.

Perhaps if you included both the source file and the include file text and told us exactly which version of gfortran you're using, a more definitive response could be provided.

Steve Lionel
  • 6,972
  • 18
  • 31
  • I'm using GCC 4.6.2. Now to note, if this makes a difference, gfortran is being run from a makefile made by PSCAD. I can provide info on that if it's pertinent. Here's code that is experiencing this issue: TEST.F: ' SUBROUTINE TESTFX() INTEGER A INCLUDE '../HDR.INC' RETURN END' HDR.INC: ' DO A = 1,3 END DO' – Will S Aug 05 '13 at 17:28
  • Sorry, tried to add code to the comment reply. I've updated my question with some example code. – Will S Aug 05 '13 at 17:34
0

Your INCLUDE must be placed on a new line, it is not part of the statement declaring A. Why do you have apostrophes at your ENDs? They cannot be there. The RETURN statement before the END is totally superfluous also. Try:

TEST.F:

SUBROUTINE TESTFX() 

INTEGER A

INCLUDE '../HDR.INC' 

END SUBROUTINE

HDR.INC:

DO A = 1,3 
END DO
  • Sorry bout the apostrophes and newline. Fixed post. Was a product of the copy-paste. The code you've provided is exactly as I have it save for the return statement and end subroutine statement. I tried to run removing the return statement and changing "END" to "END SUBROUTINE" but still having the same problem and the compiler is reporting a syntax error with the "END SUBROUTINE" line. – Will S Aug 05 '13 at 18:43