Syntax error in a Fortran FORMAT statement - fortran

I am using the gfortran compiler and I am unable to find the syntax error in the line. Can anyone tell me the syntax error in the given line?
7 FORMAT(1X, 'a=',F8.8, 1X,'b=',F8.8, 2X, 'REAL=',F8.5, 2X, 'IMAG=',F8.5, 2X, 'k1=',F8.8, 2X, 'LDF=',F8.8)

I think your problem stems from the fact that the format statement is too long. I just ran a short f77 file and I put a format statement in the file that was longer than 72 spaces. This generated an error message and said the format statement was too long. I suggest you split that line with 2nd half being on the next line. When you do that put a character in column 6 to indicate to compiler that its a continuation line. Now your format statement should look like this:
7 FORMAT(1X, 'a=',F8.8, 1X,'b=',F8.8, 2X, 'REAL=',F8.5, 2X,
> 'IMAG=',F8.5, 2X, 'k1=',F8.8, 2X, 'LDF=',F8.8)
Notice the '>' character must be in column 6..
If your editor doesn't indicate column number then type a line in your code that looks like this:
C123456789012345678901234567890123456789012345678901234567890
you can use this to determine columns. Also notice that the C must be in column 1 because that indicates that the line is a comment and the compiler won't try to compile it.

Related

Syntax error in compiling an old Fortran code [duplicate]

This question already has answers here:
Syntax error in a Fortran FORMAT statement
(1 answer)
Fortran :: Syntax error in OPEN statement at (1)
(1 answer)
Syntax error in argument list of a subroutine
(1 answer)
Closed 1 year ago.
I am using someone's Fortran scripts, I have changed a little bit in that. Now I am having a syntax error while executing it. I am new to Fortran so not able to figure it out.
The code is as follows:
integer i_canorg
integer i
integer n
integer canorg(n)
real r1demdoms(n)
real r1supdoms(n)
real r1supdomcan(n)
real r1rivout(n)
real r1envflw(n)
if(canorg(i).ne.0)then
i0l_canorg=canorg(i)
call calc_supcan1(i_canorg,r1demdoms(i),r1supdoms(i),r1rivout(i_canorg),r1envflw(i_canorg),r1supdomcan(i))
end if
The code is very long, I additionally added the if command and i_canorg parameter.
I am getting the following error while running the code:
348 | call calc_supcan1(i0lcanorg,r1demdoms(i),r1supdoms(i),r1rivout(i_canorg),r1envflw(i_canorg),r1supdomcan(i))
| 1
Error: Syntax error in argument list at (1)
What can be the reason behind this syntax error?
The reason the program is not compiling is that the line is too long, and unfortunately your compiler's error is not helpful for figuring that out. The dead giveaway is that, including the six spaces at the beginning of the line in the error message, the 1 in the error message is at column 73. Your compiler is interpreting the source code in fixed source form, which has some very particular requirements: see section 3.3.2 of N1191 for complete details.
Your compiler should have an option to specify that your source code should be interpreted in free source form. Based on your error message, it looks like you are most likely using gfortran, which has the command-line option -ffree-line-length-[n]. Check the documentation and set it to an appropriate value for your source code.
If you wish to continue to use fixed source form, then the line has to be broken into multiple lines with a continuation character in the sixth column of each continued line. The character can be anything except a space ( ) or a zero (0), and is ignored if it is in a comment line (a line beginning with the letter C or an asterisk (*)). This is one possible way of fixing the line:
****** <- note the six special columns at the beginning of the line
call calc_supcan1(i_canorg,r1demdoms(i),r1supdoms(i),
> r1rivout(i_canorg),r1envflw(i_canorg),r1supdomcan(i))

problem with define dimensions of variables fortran 77

I found this one old fortran 77 program printed in one old book, and i typed in the program but it isnt running. When I give the dimensions of variables, appear the next:
DIMENSION XN(20),YN(20),W(20),NUMAJ(20),NUMAAJ(20),SSW(20)
1,NEJI(5,20),QX(20,20),QY(20,20),IACTE(20),N1(20),N2(20),X(20)
2,Y(20),DD(20),TT(20),NUMAJA(20)
And the error code:
Error: Non-numeric character in statement label at (1)
Error: Unclassifiable statement at (1)
Thanks a lot,
Comparing the original and edited version shows still incorrect indenting: fixed format reserves columns 1-5 for labels, 6 for the continuation character and starting from 7 for the statements. If column 6 is not a space the line continues the previous line.
In the 3 line example above the characters "1" and "2" are the continuation characters of line 2 and 3, so they have to be exactly in column 6. The first line has no label or continuation character so it must start in column 7. The comment from #melpomene was incomplete regarding continuation character and based on the old version where one space less was used (the edited version has one space too much).

Intel Compiler returns "Found Format_element when Expected end of statement"

As shown in the following code that I use to practice fixed-form Fortran (because the code I am trying to learn is fixed form Fortran), the 4th format statement and the reading from file 1 are newly added. The code worked as expected prior to adding these statements, but now the intel ifort compiler with no additional flags will not compile the 4th format statement and returns the following error:
fortPractice.for(18): error #5082: Syntax error, found FORMAT_ELEMENT 'format' when expecting one of: <END-OF-STATEMENT> ;
4 format(i6,2x)
--------------^
fortPractice.for(26): error #6052: This label has not been defined as a FORMAT label.
write(2,3)
----------------------------------^
The code is:
c This is a script for practicing Fortran codes
program fortPractice
implicit none
integer :: x(0:5),y(2:7)
integer :: nph(1:6)
real :: z(4:9)
integer :: i
OPEN(unit=1,file='test.txt',status='old')
read(1,*) nph
close(unit=1)
open(unit = 2, file = 'output.txt')
2 format(i3,2x,i3,2x,2e11.2)
3 format(1x,78('*'))
4 format(i6,2x)
do i = 0,5
x(i) = i;
y(i+2) = i+2;
z(i+4) = x(i)**2 + y(i+2)**2
z(i+4) = sqrt(z(i+4)) + 10000
write(2,2) x(i),y(i+2),z(i+4)
write(2,3)
enddo
write(2,*) nph
close(unit = 2)
endprogram fortPractice
Output is a file created by the program. test.txt contains just a row of numbers: 1 2 3 4 5 6
When I comment or remove the 4th format statement then the code compiles and runs as expected. As I am just learning how fixed form Fortran works, I am just interested in why the 4th format statement won't compile.
Edit:
I have replaced all the tabs with spaces, and the program with some more modification shown below can now compile, but the program does not produce the output file, likely encountered some run-time error:
Edit 2:
Nevermind, I forgot to change the file identifiers.
Thanks everyone!
Now we have enough information to solve the problem. In the troublesome source line, the label 4 is preceded by a tab. Tabs in Fortran source are not standard, but Intel Fortran (and many other compilers) support something called "tab source form". The way it works in ifort is as follows:
If the line starts with a tab and then a nonzero digit, the digit is treated as if it were in column 6
If the line starts with a tab and then some other character, then the character is treated as if it were in column 7
If the line starts with a numeric statement label and then a tab, the next character is treated as if it were in column 7
Otherwise, a tab is treated as a blank (this last varies among compilers)
In your case, a tab preceded the 4 so it was taken to be a continuation of the previous line, resulting in an error. Either don't use tabs, or understand how the compiler treats them. Editors that automatically insert tabs will just give you trouble.

Error: Unexpected end of format string in format string at (1) [duplicate]

Getting this error while trying to compile a copied code from a Fortran 77 program.
code:
900 FORMAT(1H0,2X,'ABSOLUTE GRID LIMITS FOR DATA RETENTION FOR RADAR',I3,' XMIN-XMAX ',2F8.3,' YMIN-YMAX ',2F8.3,' ZMAX ',F8.3, /3X,'WITH AZIMUTH LIMITS OF',2F8.2, 3X,'AND RANGE LIMITS OF',2F10.3,/)
compiler error:
messy21.f90:529.132:
N FOR RADAR',I3,' XMIN-XMAX ',2F8.3,' YMIN-YMAX ',2F8.3,' ZMAX ',F8.3, /3X,(1)
Error: Unexpected end of format string in format string at (1)
I am not sure what the error means.
My guess (on the basis of error position in the line, 132) would be: starting from Fortran 90 we use free source form (free-form source input). Each line may contain up to 132 character. And if your statement is even bigger you can use up to 39 (255 in current Fortran 2003 standard) continuation lines. Fortran 77 used fixed source form which is just another story.
Use so-called continuation mark (&) to divide your very long FORMAT statement, i.e.
900 FORMAT(1H0,2X,'ABSOLUTE GRID LIMITS FOR DATA RETENTION FOR RADAR',I3,' XMIN-XMAX ',2F8.3,' YMIN-YMAX ',2F8.3,' ZMAX ',F8.3, &
/3X,'WITH AZIMUTH LIMITS OF',2F8.2, 3X,'AND RANGE LIMITS OF',2F10.3,/)
Read some Fortran 90/95/2003 book or associated section of Fortran standard. For example, in Fortran 2003 Standard (Final Committee Draft, PDF, 5MB) section "3.3 Source form" contains relevant information.
Your line is too long.
In free form files (.f90) you can only use 132 character lines. You can break your line and continue on the next line. Put & character
at the end of the line before continuing on the next line.
In fixed form Fortran (.f .for) you can only use 72 character lines. You can break your line and continue on the next line. Put any character to the fifth column on the present line.
There are compiler options which can loosen these restrictions.

Proper Fortran compiler to execute a program

I have little knowledge of the Fortran language. I have come across some programs written in the 90s (see attached snapshot showing just a portion of a long script).
I'd like to know what kind of compiler is appropriate to execute such codes? I have installed gfortran-4.2.3 on my mac. I'm also not sure about the indentation in the attached code: if C (comment) is at column 1, does the main code start at column 9 or 7? what about the position of numbers placed in between referred by GO TO?
This is not Fortran 90. This is Fortran 77.
That said, gfortran is able to compile this code. Make sure that the file extension for the file is .f so that gfortran realises it's fixed-form.
The C needs to be in the first column, the numbers that you reference are labels, they can be between column 1 and 5. The asterisk at line 198 is a continuation character, meaning that this should be treated as part of the previous line. It must be in column 6. Everything else needs to be between columns 7 and 72 (inclusive)
Oh, and the 3-digit numbers at the very beginning are line numbers, and must not be in the source code.
Edited to add: Since you have to type it all again anyway, you might as well make it free-form:
Replace the C in the first column with !, and change the way continuation lines are marked: Turn this:
write (*, *) "This is some long text,
* which doesn't fit into a line"
Into this:
write(*, *) "This is some long text, " // &
"which doesn't fit into a line"
Everything else can stay like it is. (You can now use proper indentation, too!)
New Edit
So you've pasted the code that you wrote and the error messages, so I'm replying to that now.
In Fixed Form, any character past column 72 is ignored. You have a few lines with long strings, where the terminating quotation mark is in that ignored region.
Make sure that no line exceeds the 72nd row.