This chapter describes the basic elements of the Fortran language, the format of Fortran records and the types of expressions and assignments accepted by pgf77 Fortran.
A Fortran module is either a SUBROUTINE, FUNCTION, BLOCK DATA or PROGRAM subprogram.
Fortran source consists of a sequence of modules which are to be compiled into object modules. Every module consists of statements and optionally comments beginning with the module statement, either a SUBROUTINE, FUNCTION, BLOCK DATA or PROGRAM statement, and finishing with an END statement.
In the absence of a module statement, the compiler will insert a PROGRAM statement.
Statements are either executable statements or specification statements. Each statement consists of a single line or source record, possibly followed by one or more continuation lines. Comments may appear anywhere in the source.
To append a comment to a Fortran statement line, follow the line with an exclamation mark, '!' followed by the comment on the same line. @
The letter "D" in column 1 designates the statement on that line to be a debugging statement. The compiler will treat the debugging statement as a comment unless the command line option -Mdlines is set during the compilation. In that case, the compiler acts as if the "D" were a blank and compiles the line according to the standard rules.
The rules defining the order in which statements appear in a program unit have been relaxed, as compared to the ANSI standard, as follows:
Table 1-1 shows the Fortran character set. Character variables and constants can use any ASCII character.
Character
|
Description
|
\v
|
vertical
tab
|
\a
|
alert
(bell)
|
\n
|
newline
|
\t
|
tab
|
\b
|
backspace
|
\f
|
formfeed
|
\r
|
carriage
return
|
\0
|
null
|
\'
|
apostrophe
(does not terminate a string)
|
\"
|
double
quotes (does not terminate a string)
|
\\
|
\
|
\x
|
x,
where x is any other character
|
\ddd
|
character
with the given octal representation.
|
A Fortran record may be formatted with tabs or by column formatting.
A Fortran record consists of a sequence of up to 73 ASCII characters, the last being <CR>. There is a fixed layout as shown in Table 1-3.
Position
|
Field
|
1-5
|
Label
field
|
6
|
Continuation
field
|
7-72
|
Statement
field
|
Characters beyond position 72 on a line are ignored.[*]
A tab formatted record consists of up to 72 ASCII characters. It is made up of a label field, an optional continuation indicator and a statement field. The label field is terminated by a tab character. The label cannot be more than 5 characters.
A continuation line is indicated by a tab character followed immediately by a digit. The statement field starts after a continuation indicator, when one is present. The 73rd and subsequent characters are ignored.*
The label field holds up to five characters. The characters C or * in the first character position of a label field indicate a comment line.
In addition, to C or *, either of the characters D or ! in the first character position of a label field also indicate a comment line. @
When a numeric field drawn from digits 0 to 9 is placed in the label field, the field is a label. A line with no label, and with five space characters or a <TAB> (the tab is an extention @ ) in the label field, is an unlabeled statement. Each label must be unique in its module. Continuation lines must not be labeled. Labels can only be jumped to when they are on executable statements.
The sixth character position, or the position after the tab, is the continuation field. This field is ignored in comment lines. It is invalid if the label field is not five spaces. A value of 0, <space> or <TAB> indicates the first line of a statement. Any other value indicates a subsequent (continuation) line to the preceding statement.
This consists of valid identifiers and symbols, possibly separated by <space> or <TAB> and terminated by <CR>.
Within the statement field tabs and spaces are ignored as are characters following a ! or beyond the 72nd character.[*] @
The sequence of consecutive compilation of source statements may be interrupted so that an extra source file can be included. This is carried out using the INCLUDE statement which takes the following form:
INCLUDE "filename"where filename is the name of the file to be included. Pairs of either single or double quotes are acceptable enclosing filename.
The INCLUDE file is compiled to replace the INCLUDE statement, and on completion of that source the file is closed and compilation continues with the statement following the INCLUDE.
INCLUDE files are especially recommended when the same COMMON blocks and the same COMMON block data mappings are used in several modules.
For example the following statement includes the file MYFILE.CMN.
INCLUDE "MYFILE.CMN"
Input source file format has been extended from Fortran 77 to allow the following extensions:
Fortran modules are made up of statements which consist of expressions and elements. An expression can be broken down to simpler expressions and eventually to its elements combined with operators. Hence the basic building block of a statement is an element. An element takes one of the following forms:
Symbolic names identify different entities in Fortran source. A symbolic name is a string of letters and digits, which must start with a letter and is terminated by a character not in the symbolic names set (for example a <space> or a <TAB> character). Underscore (_) characters may appear within symbolic names. Symbolic names may start with a dollar sign ($) or underscore (_) character (this is a pgf77 extension). Only the first thirty-one characters identify the symbol. Below are several symbolic names:
NUM CRA9 Y numericabcdefghijklmnopqrstuvwxyzThe last example is identified by its first 31 characters and is equivalent to:
numericabcdefghijklmnopqrstuvwxThe following are examples are invalid symbolic names.
8QThis is invalid because it begins with a number.
FIVE.4This is invalid because it contains a period which is an invalid character.
Symbolic names may be declared locally or globally.
Names of COMMON blocks, SUBROUTINEs and FUNCTIONs are global to those modules that reference them. They must refer to unique objects, not only during compilation, but also in the link stage.
The scope of names other than these is local to the module in which they occur, and any reference to the name in a different module will imply a new local declaration. This includes the arithmetic function statement.
Each data item, such as a variable or a constant, represents a particular value at any point during program execution. These elements may be combined together to form expressions, using binary or unary operators, so that the expression itself yields a value.
An expression is formed as:
expr binary-operator expror
unary-operator exprwhere an expr is formed as
expression or elementFor example,
A+B -C +DThese are simple expressions whose components are elements. Expressions fall into one of four classes: arithmetic, relational, logical or character.
Arithmetic expressions are formed from arithmetic elements and arithmetic operators. An arithmetic element may be:
Note that a value should be associated with a variable or array element before it is used in an expression. Arithmetic expressions are evaluated in an order determined by a precedence associated with each operator. Table 1-5 shows the precedence of each arithmetic operator.
This following example is resolved into the arithmetic expressions (A) + (B * C) rather than (A + B) * (C).
A + B * CNormal ranked precedence may be overcome using parentheses which force the item(s) enclosed to be evaluated first.
(A + B) * CThe compiler resolves this into the expressions (A + B) * (C).
Operator
|
Function
|
**
|
Exponentiation
|
*
|
Multiplication
|
/
|
Division
|
+
|
Addition
or unary plus
|
-
|
Subtraction
or unary minus
|
Operator
|
Precedence
|
**
|
First
|
* and /
|
Second
|
+
and -
|
Third
|
The type of an arithmetic expression is:
INTEGER
|
if
it contains only integer elements.
|
REAL
|
if
it contains only real and integer elements.
|
DOUBLE
PRECISION |
if
it contains only double precision, real and integer elements.
|
COMPLEX
|
if
any element is complex. Any element which needs conversion to complex will be
converted by taking the real part from the original value and setting the
imaginary part to zero.
|
DOUBLE
COMPLEX @
|
if any element is double complex. |
A relational expression is composed of two arithmetic expressions separated by a relational operator. The value of the expression is true or false (.TRUE. or .FALSE.) depending on the value of the expressions and the nature of the operator.
Table 1-6 shows the relational operators.
Operator
|
Relationship
|
.LT.
|
Less
than
|
.LE.
|
Less
than or equal to
|
.EQ.
|
Equal
to
|
.NE.
|
Not
equal to
|
.GT.
|
Greater
than
|
.GE.
|
Greater
than or equal to
|
In relational expressions the arithmetic elements are evaluated to obtain their values. The relationship is then evaluated to obtain the true or false result. Thus the relational expression:
TIME + MEAN .LT. LASTmeans if the sum of TIME and MEAN is less than the value of LAST, then the result is true, otherwise it is false.
A logical expression is composed of two relational or logical expressions separated by a logical operator. Each logical expression yields the value true or false (.TRUE. or .FALSE.) .
Table 1-7 shows the logical operators.
Operator
|
Relationship
|
.AND.
|
True
if both expressions are true.
|
.OR.
|
True
if either expression or both is true.
|
.NOT.
|
This
is a unary operator; it is true if the expression is false, otherwise it is
false.
|
.NEQV.
|
False
if both expressions have the same logical value
|
.XOR.
|
Same
as .NEQV.
|
.EQV.
|
True
if both expressions have the same logical value
|
In the following example, TEST will be .TRUE. if A is greater than B or I is not equal to J+17.
TEST = A .GT. B .OR. I .NE. J+17
An expression of type CHARACTER can consist of one or more printable characters. Its length is the number of characters in the string. Each character is numbered consecutively from left to right beginning with 1. For example:
'ab_&' 'A@HJi2' 'var[1,12]'
A character expression can be formed by concatenating two (or more) valid character expressions using the concatenation operator //.
Table 1-8 shows several examples of concatenation
Expression
|
Value
|
'ABC'//'YZ'
|
"ABCYZ"
|
'JOHN
'//'SMITH'
|
"JOHN SMITH"
|
'J
'//'JAMES '//'JOY'
|
"J JAMES JOY"
|
Arithmetic, relational and logical expressions may be identified to the compiler by the use of parentheses, as described in the section on arithmetic expressions. When no guidance is given to the compiler it will impose a set of precedence rules to identify each expression uniquely. Table 1-9 shows the operator precedence rules for expressions.
Operator
|
Evaluated
|
**
|
First
|
*
and /
|
Second
|
+
and -
|
Third
|
Relational
operators
|
Fourth
|
.NOT.
|
Fifth
|
.AND.
|
Sixth
|
.OR.
|
Seventh
|
.NEQV.
and .EQV.
|
Eighth
|
A*B+B**C .EQ. X+Y/Z .AND. .NOT. K-3.0 .GT. Tis equivalent to:
((((A*B)+(B**C)) .EQ. (X+(Y/Z))) .AND. (.NOT. ((K-3.0) .GT. T)))
A Fortran assignment statement can be any of the following:
The arithmetic assignment statement has the following form:
object = arithmetic-expressionwhere object is one of the following:
In the case of:
complex = real expressionthe implication is that the real part of the complex number becomes the result of the expression and the imaginary part becomes zero. The same applies if the expression is double precision, except that the expression will be coerced to real.
The following are examples of arithmetic assignment statements.
A=(P+Q)*(T/V) B=R**T**2
The logical assignment statement has the following form:
object = logical-expressionwhere object is one of the following:
In the following example, FLAG takes the logical value .TRUE. if P+Q is greater than R; otherwise FLAG has the logical value .FALSE.
FLAG=(P+Q) .GT. R
The form of a character assignment is
object = character expressionwhere object is one of the following:
None of the character positions being defined in object can be referenced in the character expression and only such characters as are necessary for the assignment to object need to be defined in the character expression. The character expression and object can have different lengths. When object is longer than the character expression trailing blanks are added to the object ; and if object is shorter than the character expression the righthand characters of the character expression are truncated as necessary.
In the following example, note that all the variables and arrays are assumed to be of type character.
FILE = 'BOOKS' PLOT(3:8) = 'PLANTS' TEXT(I,K+1)(2:B-1) = TITLE//X
The compiler, pgf77, recognizes three VAX/VMS compiler directives that affect the program listing process:
All of the directives must begin in column one.
[*] The pgf77 option -Mbackslash enables and disables this enhancement.
[*] Extended lines containing up to 132 characters are valid. For information on the extend option, refer to the description of -Mextend in the pgf77 User's Guide.
[*] Extended lines containing up to 132 characters are valid. For information on the extend option, refer to the description of -Mextend in the pgf77 User's Guide.