compile-time error due to single-digit statement label (in fixed-format Fortran)

206 views
Skip to first unread message

Sourav Mukherjee

unread,
Apr 16, 2014, 8:50:32 AM4/16/14
to gg...@googlegroups.com
Hi,

I came upon some peculiar Fortran syntax which is accepted by gfortran but not by g95. The error occurs when there is a single-digit statement label at column 1 in a line, the line is empty except the label, and the following line "continues" the not-yet-started statement of the previous line. Please see the example below for better understanding (file name is error1.F )

      go to 1
1
     . print*,'hi'
      end

! ######## Error message on compiling this file with g95 (gfortran issues no error) #########
! $ g95 error1.F
! In file error1.F:1
!
!       go to 1
!            1
! Error: Syntax error in GOTO statement at (1)



The error goes away with any of the following modifications to the above file:
1. The statement label '1' is shifted to any of the columns 2 to 5.
2. The statement label still begins at column 1 but is made a two digit number (even 01 works).
3. The statement in the following line which is meant to be labeled by the label is moved to the same line as the label (which is the usually syntax we come across).

Also notable is the error message issued by g95 which indicates an error NOT at the line containing the label, but in the preceding statement or in the statement in the continuing line.

I have attached example programs with error messages issued by g95 appended in those files which fail to compile with g95. Please note that gfortran successfully compiles all these programs. (Programs which are compiled by both g95 and gfortran are named ok1.F, ok2.F etc, programs which are not compiled by g95 are named error1.F, error2.F etc.)

I have two questions:
1. Is it valid Fortran syntax (i.e. authorized by the Fortran 77 standard) to have an empty line with only a label, followed by a continuation line containing the statement which is "meant" to be labeled by the "dangling" label in a previous line.
2. Is there a problem in the g95 parser since it is unable to to parse this syntax.

Thanks,
Sourav




line_label.zip

ziggy76

unread,
Apr 16, 2014, 12:23:38 PM4/16/14
to gg...@googlegroups.com


On Wednesday, April 16, 2014 2:50:32 PM UTC+2, Sourav wrote:

1. Is it valid Fortran syntax (i.e. authorized by the Fortran 77 standard) to have an empty line with only a label, followed by a continuation line containing the statement which is "meant" to be labeled by the "dangling" label in a previous line.


The fortran 77 standard says :
3.4  Statement_Labels

          Statement  labels  provide  a  means  of  referring  to
          individual  statements.   Any statement may be labeled,
          but  only  labeled  executable  statements  and  FORMAT
          statements  may  be referred to by the use of statement
          labels.  The form of a statement label is a sequence of
          one  to five digits, one of which must be nonzero.  The
          statement label may be placed  anywhere  in  columns  1
          through  5  of  the initial line of the statement.  The
          same statement label must not be given to more than one
          statement  in a program unit.  Blanks and leading zeros
          are not significant in distinguishing between statement
          labels.
So, I thing the goto statement can't refer to a line containing no executable statements. I think that a CONTINUE statement is compulsory
in such a situation.
For me, you can have a line with only a label but you can't refer to it with a goto.
Be careful that sometimes a character in the first column is interpretated as a comment (not the standard).
A character in the column 6 is a continuation line.


Best regards.

Sourav Mukherjee

unread,
Apr 17, 2014, 1:33:05 AM4/17/14
to gg...@googlegroups.com
Hi ziggy76,

Thanks for your response.


> So, I thing the goto statement can't refer to a line containing no executable statements. I think that a CONTINUE statement is compulsory in such a situation.

The problem persists even if I replace the PRINT statement with a CONTINUE statement. See below:


$ cat error1_modified1.F
      go to 1
1
     . continue
       print*,'hi'
      end
$ g95 error1.F
In file error1.F:1

      go to 1
           1

Error: Syntax error in GOTO statement at (1)

(BTW, PRINT is an executable statement, according to the F77 standard.)

The point is that the program successfully compiles with any of the 3 modifications mentioned in my previous email. For e.g. by replacing '1' in lines 1 and 2 with something like '10'. For that matter, even the following compiles:

$ cat error1_modified2.F
      go to 1
01
     . print*,'hi'
      end



My question is why does g95 exhibit the bias for a single-digit statement label at column 1? And if it doesn't want to accept an 'empty' single-digit-at-column-1 labeled line, it should just say so, and not indicate the error elsewhere!

Regards,
Sourav



--
You received this message because you are subscribed to the Google Groups "gg95" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gg95+uns...@googlegroups.com.
To post to this group, send email to gg...@googlegroups.com.
Visit this group at http://groups.google.com/group/gg95.
For more options, visit https://groups.google.com/d/optout.

ziggy76

unread,
Apr 17, 2014, 11:31:39 AM4/17/14
to gg...@googlegroups.com

My question is why does g95 exhibit the bias for a single-digit statement label at column 1? And if it doesn't want to accept an 'empty' single-digit-at-column-1 labeled line, it should just say so, and not indicate the error elsewhere!


In my opinion, you can put a label on an empty line but you can't refer to it. So it's the goto satatement which is an error, not the label.
You are supposed to put the continue statement on the same line as the label :

1     continue
...
       goto 1


Best regards
Reply all
Reply to author
Forward
0 new messages