Ampersand in 5th column and at the end of the line

女生的网名这么多〃 提交于 2019-12-22 10:53:55

问题


I came across some code today that looks like this:

      subroutine sub(hello,world,this,routine,takes,a, &
     &   crazy,large,number,of,arguments,              &
     &   so,many,that,it,gets,split,across,four,       &
     &   lines)

It seems that the ampersands in the 5th column are in case the user wants to compile the code with a f77 compiler -- My question is whether this is legitimate (standard conforming) fortran 90 code. I suppose that it probably isn't standard conforming f77 code since the trailing ampersands aren't in column 73 either...but if they were, would this be a standard way to make free-format and fixed-format compilers happy? In other words, does the fortran 90 standard say that &'s in the middle of a line are simply ignored (or something to that effect)?


回答1:


The & is the Fortran 90 free-form continuation character. The compiler will behave as if the lines are concatenated by matching the location of the trailing & with the leading & on the next line. Since Fortran is happy to ignore spaces between tokens what you have posted is a way of writing a very long statement across a number of source file lines. (OP probably already knows all this, other readers may not.)

The snippet you show is definitely standard-conforming Fortran 90. The standard defines a trailing & to be the continuation character. If you want to split either a token (such as a variable name) or a 'character-context' (such as a string literal) across lines then the line onto which the continuation runs must start with an & and there must be no blanks inserted into the sequence of characters after the line-starting &. I think that what you have posted sneaks into standard-conformity on the grounds that the successive &s might be considered to be not introducing any whitespace into the existing whitespace.

However, the line-starting &s are not required in your snippet.

Your snippet is definitely not standard-conforming fixed-format FORTRAN77. However, most modern Fortran compilers will, unless you instruct them otherwise, take a relaxed view of this sort of 'not-standard-conforming' but 'obvious-what-is-meant' code and compile it quite happily. Unless, that is, you try something odd like adding free-format code into an existing fixed-format source file, that's just asking for the compiler to have a hissy fit.

EDIT

My original answer failed to answer OP's question relating to moving the &s into the 73rd column as a tactic for creating code to keep fixed- and free-form compilation happy. In FORTRAN77 continuation is marked by a character (any character) in column 6 of the continuing line. That version of the language did not require any character at the end of the line being continued. Again, I can't see anything wrong with putting an & in column 73. As OP pointed out in a comment, fixed-form Fortran ignores any characters in col 73-80, and punched cards (ahh, happy days) had only 80 columns.

But I can't see anything to recommend this approach either. I think that it is far preferable not to mix free- and fixed-form in the same source file. All the current Fortran compilers will compile both forms in the same compilation. But if you want to go ahead and mix the two, feel free to do so.




回答2:


Yes, this is conforming Fortran 90. If the first non-blank character on the next line is '&', the actual source text begins at the next non-blank character.




回答3:


High Performance Mark's answer is correct, however there's another important point about the fixed (F77) vs. free (allowed in F90 and later) format for continuations. First, it looks to me like the code provided has the & character in the 6th column. If it does, then it's almost F77-compatible; all that's required is to remove the trailing &'s at the end of the lines, or to move them to column 73. And as Hristo Iliev noted, the use of an & at both columns 73 and 6 allows for a format that conforms to multiple Fortran standards, which can be useful for INCLUDE statements when coding in an environment that doesn't stick to a single standard.



来源:https://stackoverflow.com/questions/10584561/ampersand-in-5th-column-and-at-the-end-of-the-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!