This chapter describes the language elements that a Fortran statement can contain. Language elements consist of lexical tokens, which include names, keywords, operators, and statement labels. Rules for forming lexical tokens from the characters in the Fortran character set are also presented.
The Fortran character set, which includes the 26 uppercase letters of the alphabet, the corresponding 26 lowercase letters, and several special characters.
The numerical digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
The underscore character (_).
Table 3-1, shows the special characters of the character sets.
Decimal point or period
The 52 letters define the syntax class letter.
In most cases, the compiler is not case sensitive; lowercase letters are considered the same as uppercase letters. In the following cases, however, lowercase and uppercase letters are considered to have different data values:
Within a character constant
Within a quotation mark, apostrophe, or H edit descriptor
Within a Hollerith constant (nonstandard)
|Note: Hollerith data is not included in the Fortran standard.|
The compiler considers the following two statements to be equivalent:
PRINT *, N Print *, n
The digits 0 through 9 define the syntax class digit. The digits are assumed to be decimal numbers when used to describe a numeric value, except in binary, octal, and hexadecimal (BOZ) literal constants or input/output (I/O) records corresponding to B, O, or Z edit descriptors.
For example, in the following DATA statement, the digits of the first constant are decimal digits, those of the second constant are binary digits, and those of the third are hexadecimal digits:
DATA X, I, J / 4.89, B'1011', Z'BAC91' /
The underscore can be used to make names more readable. For example, in the identifier NUMBER_OF_CARS, each underscore is used to separate the obvious English words. It is a significant character in any name. An underscore cannot be used as the first character of a name, but it can be the last character. An underscore is also used to separate the kind value from the actual value of a literal constant (for example, 123_2).
There are 22 special characters used for operators like multiply and add. They are also used as separators or delimiters in Fortran statements. Separators and delimiters make the form of a statement unambiguous.
Fortran's treatment of uppercase and lowercase letters can lead to portability problems when calling subprograms written in other languages. The problem occurs because the standard does not specify the case of letters used for external names. The following program fragment illustrates the problem:
EXTERNAL FOO . . . CALL FOO . . . END
The compiler converts external names into lowercase and append an underscore, so the compiler would use foo_ as the external name. If the subprogram were written in C, which is case sensitive, and if foo were written in lowercase, the external name used in C would then be different from the name produced by the compiler.
The NAME compiler directive allows you to specify a case-sensitive external name in a Fortran program. You can use this directive, for example, when writing calls to C routines. For more information on the NAME directive, see the MIPSpro Fortran 90 Commands and Directives Reference Manual.
A statement is constructed from low-level syntax. The low-level syntax describes the basic language elements, called lexical tokens, in a statement. A lexical token is the smallest meaningful unit of a statement and can consist of 1 or more characters. Tokens are names, keywords, literal constants (except for complex literal constants), labels, operator symbols, comma, =, =>, :, ::, ;, %, and delimiters. A literal of type complex consists of several tokens. Examples of operator symbols are + and //.
/ ... / ( ... ) (/ ... /)
In the following statements, the slashes distinguish the value list from the object list in a DATA statement, the parentheses are delimiters that mark the beginning and end of the argument list in the CALL statement, and the pairs (/ and /) mark the beginning and end of the elements of an array constructor:
DATA X, Y / 1.0, -10.2/ CALL PRINT_LIST(LIST, SIZE) VECTOR = (/ 10, 20, 30, 40 /)
DO is a statement keyword that identifies the DO statement. Other keywords identify parts of a statement such as ONLY in a USE statement or WHILE in one of the forms of a DO construct, as follows:
DO WHILE( .NOT. FOUND )
Others specify options in the statement such as IN, OUT, or INOUT in the INTENT statement.
There are three statements in Fortran that have no statement keyword. They are the assignment statement, the pointer assignment statement, and the statement function.
Some sequences of capital letters in the formal syntax rules are not statement keywords. For example, EQ, in the lexical token .EQ., and EN, as an edit descriptor, are not statement keywords.
A dummy argument keyword, a different sort of keyword, is discussed in the MIPSpro Fortran Language Reference Manual, Volume 2.
Variables, named constants, program units, common blocks, procedures, arguments, constructs, derived types (types for structures), namelist groups, structure components, dummy arguments, and function results are among the elements in a program that have a name. Fortran permits up to 31 characters in a name.
letter [ alphanumeric_character ]...
|Note: The @ sign is not supported by the compiler.|
A name must begin with a letter and can consist of letters, digits, and underscores. The compiler allows you to use the underscore (_), and dollar sign ($) in a name, but none of these can be the first character of a name. Use $ is not recommended, however, because it is intended for internal use.
The following are examples of names:
A CAR_STOCK_NUMBER A__BUTTERFLY Z_28 TEMP_
|Note: The Fortran standard does not allow the dollar sign ($) character in a name. The at sign (@) is not included in the standard character set.|
A constant is a syntactic notation for a value. The value can be of any intrinsic type; that is, it can be a numeric (integer, real, or complex) value, a character value, or a logical value. A constant is defined as follows:
digit [ digit... ] B
O" digit [ digit... ] "
O' digit [ digit... ] '
" digit [ digit... ] "O
' digit [ digit... ] 'O
X' hex_digit [ hex_digit... ]'
X" hex_digit [ hex_digit... ] "
' hex_digit [ hex_digit... ] 'X
" hex_digit [ hex_digit... ] "X
Z' hex_digit [ hex_digit... ] '
Z" hex_digit [ hex_digit... ] "
B' digit [ digit... ] '
B" digit [ digit... ] "
The following notes pertain to the preceding format:
digit must have one of the values 0 through 7 in octal_typeless_constant
digit must have a value of 0 or 1 in binary_typeless_constant
The B, O, X, and Z characters can be in uppercase or lowercase.
1.23 400 ( 0.0, 1.0 ) "ABC" B'0110110' .TRUE.
A value that has a name is called a named constant and can be of any type, including a derived type. A named constant can also be array-valued. In the following statements, X_AXIS and MY_SPOUSE are examples of named constants:
REAL, DIMENSION(2), PARAMETER :: X_AXIS = (/ 0.0, 1.0 /) TYPE(PERSON), PARAMETER :: MY_SPOUSE = PERSON( 39, 'PAT' )
Note that the entity on the right of the equal sign is not itself a constant but a constant expression. The forms for defining named constants are described in more detail in Section 5.5.1 in Chapter 5.
Multiplication of numeric values
Concatenation of character values
Comparison for equality (same as .EQ.)
The abbreviations .A., .O., .N., and .X. are synonyms for .AND., .OR., .NOT., and .XOR., respectively. If the abbreviated operator is overloaded in an interface block as a defined operator, the abbreviated form of the intrinsic operator cannot be used in any scope in which the defined operator is accessible.
|Note: The Fortran standard does not specify the .A., .O., or .N., abbreviations for the logical operators, nor does it specify the .XOR. operator or its .X. abbreviation. The Fortran standard does not specify the .LG. or <> operators.
You can define operators in addition to the intrinsic operators. User-defined operators begin with a period (.), followed by a sequence of up to 31 letters, and end with a period (.), except that the letter sequence must not be the same as any intrinsic operator defined by the Fortran standard or the logical constants .FALSE. or .TRUE.
A label can be used to identify a statement. A label consists of 1 to 5 decimal digits, one of which must be nonzero. If a statement has a label, it is uniquely identified and the label can be used in DO constructs, CALL statements, branching statements, and I/O statements.
Leading zeros in a label are not significant. In other words, the labels 020 and 20 are considered to be the same label. A statement label is local to a scoping unit. This means that a program unit can contain more than one definition of the same label as long as the labels are defined in different scoping units. For example, a main program and a contained internal subprogram can both define the same statement label. The cases in which duplicate labels can be used in the same program unit are explained as part of the general treatment of the scope of entities in the MIPSpro Fortran Language Reference Manual, Volume 2.
The following are examples of statements with labels:
100 CONTINUE 21 X = X + 1.2 101 FORMAT (1X, 2F10.2)
Fortran syntax does not permit a statement with no content. This is sometimes referred to as a blank statement. Such a statement is always treated as a comment; therefore, if such a statement is created, it must not be labeled. For example, each of the following lines is nonstandard Fortran. They generate an error message from the compiler:
A Fortran program consists of statements, comments, and INCLUDE lines. This collection of statements, comments, and INCLUDE lines is called source text. A statement consists of one or more lines of source text and is constructed from low-level syntax.
The lines within a program unit (except comment lines) and the order of the lines are, in general, significant.
Because all program units terminate with their own END statement, lines following such an END statement are never part of the preceding program unit; they are part of the program unit that follows. END statements can be continued.
|Note: The Fortran standard does not describe END statement continuation.|
There are two source forms for writing source text: free source form and fixed source form.
|Note: The Fortran standard has declared fixed source form to be obsolescent. The preferred alternative is free source form.|
Fixed source form is the default for Fortran source files with a .f or .F suffix. Free source form is the default source for Fortran source files with a .f90 or .F90 suffix. The compiler allows you to use the FIXED and FREE compiler directives to switch from one source form to the other within a program unit. The f90(1) command line allows you to override the source form implied by the input file suffix. See the MIPSpro Fortran 90 Commands and Directives Reference Manual, for information on the f90(1) command line and on the FIXED and FREE compiler directives. Section 3.4, describes a way to write Fortran statements so that the source text is acceptable to both free and fixed source forms.
|Note: The Fortran standard does not describe compiler directives or command lines.|
Characters that form the value of a character literal constant, a Hollerith constant, or a character string edit descriptor (quotation mark, apostrophe, or H edit descriptor) are in a character context. The characters in a character context do not include the delimiters used to indicate the beginning and end of the character constant or string. Also, the ampersands (&) in free source form, which are used to indicate that a character string is being continued and used to indicate the beginning of the character string on the continued line, are never part of the character string value and thus are not in character context.
|Note: The Fortran standard does not describe Hollerith constants.|
The rules that apply to characters in a character context are different from the rules that apply to characters in other contexts. For example, blanks are always significant in a character context but are never significant in other parts of a program written using fixed source form. The following code fragment illustrates this:
CHAR = CHAR1 // "Mary K. Williams" ! The blanks within the character string ! (within the quotation marks) are significant. ! The next two statements are equivalent ! in fixed source form. DO2I=1,N DO 2 I = 1, N
Comments can contain any printable character.
In free source form, the only restriction on statement positioning within a line is that the line itself cannot contain more than 132 characters. A blank character is significant and may be required to separate lexical tokens.
Blank characters are significant everywhere, but a sequence of blank characters outside a character context is treated as a single blank character. They can be used freely between tokens and delimiters to improve the readability of the source text. For example, the following two statements are interpreted identically:
SUM=SUM+A(I) SUM = SUM + A (I)
Each line can contain from 0 through 132 characters.
The exclamation mark (!), not in character context, indicates the beginning of a comment that ends with the end of the line. A line can consist of nothing but a comment. Comments, including the !, are ignored and do not alter the interpretation of statements in any way.
The compiler supports compiler directives. Compiler directives are lines inserted into source code that specify actions to be performed by the compiler. Other compilers may treat compiler directive lines as comments. For more information on compiler directives, see the MIPSpro Fortran 90 Commands and Directives Reference Manual.
A line whose first nonblank character is an exclamation mark is called a comment line.
The following is an example of a comment line:
! Begin the next iteration.
The following is an example of a statement with a trailing comment:
ITER = ITER + 1 ! Begin the next iteration.
Zero or more blanks.
A comment and the end of the line. If the line following this line is not a comment line; it is a continuation line.
The following is an example of a continued line and a continuation line:
FORCE = G * MASS1 * & ! This is a continued line. MASS2 / R**2 ! This is a continuation line.
|Note: If you are using free source form, the Fortran standard allows no more than 39 continuation lines within a statement.|
Comment lines cannot be continued. That is, the ampersand as the last character in a comment is part of the comment and does not indicate continuation.
A line with only blank characters or with no characters is treated as a comment line.
More than one statement or partial statement can appear on a line. The statement separator is the semicolon (;), provided it is not in a character context. Multiple successive semicolons on a line with or without blanks intervening are considered as a single separator. The end of a line is also a statement separator, but a semicolon at the end of a line that is not part of a comment is considered as a single separator. Essentially, a null statement is a legal Fortran statement. The following statements show use of the semicolon:
! The semicolon is a statement separator. X = 1.0; Y = 2.0 ! However, the semicolon below, at the end of a ! line, is not treated as a separator and is ! ignored. Z = 3.0; ! Also, consecutive semicolons are treated as one ! semicolon, even if blanks intervene. Z = 3.0; ; W = 4.0
A label can appear before a statement, provided that it is not part of another statement, but it must be separated from the statement by at least one blank. Examples:
10 FORMAT(10X,2I5) ! 10 is a label IF (X == 0.0) 200 Y = SQRT(X) ! Label 200 is ! not allowed.
Any printable character can be used in character literal constants and character string edit descriptors.
The ampersand (&) is used as the continuation symbol in free source form. If it is the last nonblank character in a line after any comments are deleted and it is not in a character context, the statement is continued on the next line that does not begin with a comment. If the first nonblank character on the continuing line is an ampersand, the statement continues after the ampersand; otherwise, the statement continues with the first position of the line. The ampersand or ampersands used as the continuation symbols are not considered part of the statement. For example, the following statement takes two lines (one continuation line) because it is too long to fit on one line:
STOKES_LAW_VELOCITY = 2 * GRAVITY * RADIUS ** 2 * & (DENSITY_1 - DENSITY_2) / (9 * COEFF_OF_VISCOSITY)
The leading blanks on the continued line are included in the statement and are allowed in this case because they are between lexical tokens.
The double ampersand convention must be used to continue a name, a character constant, or a lexical token consisting of more than 1 character split across lines. The following statement is the same statement as in the previous example:
STOKES_LAW_VELOCITY = 2 * GRAVITY * RADIUS ** 2 * (DEN& &SITY_1 - DENSITY_2) / (9 * COEFF_OF_VISCOSITY)
However, splitting names across lines makes the code difficult to read and is not recommended.
Ampersands can be included in a character constant. Only the last ampersand on the line is the continuation symbol, as illustrated in the following example:
LAWYERS = "Jones & Clay & & &Davis"
The value of this constant is Jones & Clay & Davis. The first two ampersands are in a character context; they are part of the value of the character string.
END statements cannot be continued.
Blanks in free source form cannot appear within tokens, such as names or symbols consisting of more than 1 character, but blanks can be used freely in format specifications. For example, blanks cannot appear between the characters of multicharacter operators such as ** and .NE.. Format specifications are an exception because blanks can appear within edit descriptors such as BN, SS, or TR.
The compiler treats tabs and single blanks as equivalent in free source form, except in character literal strings.
|Note: The Fortran standard does not allow tabs in Fortran source files.|
A blank must be used to separate a statement keyword, name, constant, or label from an adjacent name, constant, or label. For example, the blanks in the following statements are required:
INTEGER SIZE PRINT 10,N DO I=1,N
Adjacent keywords require a blank separator in some cases (for example, CASE DEFAULT). In other cases, two adjacent keywords can be written either with or without intervening blanks (for example, BLOCK DATA).
Blank separators are optional in the following keywords:
END BLOCK DATA
Blank separators are mandatory in the following keywords:
Blanks are not mandatory in all cases, but blank separators between statement keywords make the source text more readable and clarify the statements. Generally, if common rules of English text are followed, everything will be correct. For example, blank separators in the following statements make them quite readable, and the blanks in DOUBLE PRECISION and END FUNCTION are optional:
RECURSIVE FUNCTION F(X) DOUBLE PRECISION X END FUNCTION F
The following is a sample program in free source form. Note that the numbers and the line at the top are not part of the program; the vertical bars to the left of the program are also not part of the program. These graphics are included to show the columns this program uses in free source form.
123456789....... ----------------------------------------------------- |PROGRAM LEFT_RIGHT | REAL X(5), Y(5) | ! Print arrays X and Y | PRINT 100, X, Y | 100 FORMAT (F10.1, F10.2, F10.3, F10.4, & | F10.5) | . . . |END
|Note: The Fortran standard has declared fixed source form to be obsolescent. The preferred alternative is free source form.|
By default, statements or parts of statements must be written between positions 7 and 72, inclusive. The f90(1) command line includes options that allow you to specify different line lengths. For information on specifying different line lengths, see the MIPSpro Fortran 90 Commands and Directives Reference Manual.
Regardless of the command line specification, character positions 1 through 6 are reserved for special purposes.
|Note: The Fortran standard does not allow a compiler to recognize characters beyond column 72.|
D O 10 I = 1, L O O P E N D DO 10 I = 1, LOOPEND
A C or * in position 1 identifies a comment. In this case, the entire line is a comment and is called a comment line. A ! in any position except position 6 and not in a character context indicates that a comment follows to the end of the line. Comments are not significant.
A line with only blank characters or with no characters is treated as a comment line.
Multiple statements on a line are separated by one or more semicolons. Semicolons can occur at the end of a line, and these are ignored.
Any character (including ! and ;) other than blank or 0 in position 6 indicates that the line is a continuation of the previous line. Such a line is called a continuation line. The text on the continuation line begins in position 7. There can be no more than 99 continuation lines for one statement in fixed source form. The first line of a continued statement is called the initial line.
|Note: If you are using fixed source form, the Fortran standard allows no more than 19 continuation lines within a statement.|
E &N &D
|Note: The Fortran standard states that an END statement must not be continued.|
The characters END can appear as the only characters on the initial line of a statement, as follows:
|Note: The Fortran standard states that END cannot be an initial line of a statement other than an END statement.|
Any character from the supported compiler character set (including graphic and control characters) can be used in character literal constants and character edit descriptors. Although the Fortran standard permits a processor to limit the use of control characters (such as the newline) to such character contexts, the compiler imposes no such limitation.
If a tab is the first character on a line, the next character determines how the line is interpreted. A nonzero digit indicates a continuation line. Otherwise, this line is the initial line of a statement.
A statement label, if present, must precede the first tab.
The compiler treats the tab character in a statement the same way it treats a blank character.
A tab character is not converted to spaces, so the exact visual placement of tabbed statements depends on the utility you use to edit or display text.
|Note: : A tab counts as 1 character even though it may expand to more than one space in the listing or editor. Thus, statements that include tabs may appear to have data beyond column 72 (or 80) in the editor or listing.|
The compiler allows a D or d character in column one. This notation directs the compiler to replace the D or d with a blank and treat the rest of the line as a source statement. This can be used, for example, for debugging purposes if the rest of the line contains a PRINT statement.
This functionality is controlled through the -d_lines option on the compiler. For more information on these options, see your f90(1) man page.
|Note: The Fortran standard does not describe using D or d characters in column 1.|
The following is a sample program in fixed source form. Note that the numbers and the line at the top are not part of the program; the vertical bars to the left of the program are also not part of the program. These graphics are included to show the columns this program uses in fixed source form.
123456789..... --------------------------------------------- | PROGRAM LEFT_RIGHT | REAL X(5), Y(5) |C Print arrays X and Y | PRINT 100, X, Y | 100 FORMAT (F10.1, F10.2, F10.3, F10.4, | 1 F10.5) | . . . | END
For portability in many cases, such as an included file, it is desirable to use a form of the source code that is valid and equivalent for either free source form or fixed source form. Such a source form can be written by obeying the following rules and restrictions:
Limit labels to positions 1 through 5, and statements to positions 7 through 72. These are the limits required in fixed source form.
Treat blanks as significant. Because blanks are ignored in fixed source form, using the rules of free source form will not impact the requirements of fixed source form.
Use the exclamation mark (!) for a comment, but do not place it in position 6, which indicates continuation in fixed source form. Do not use the C or * forms for a comment.
To continue statements, use the ampersand in both position 73 of the line to be continued and in position 6 of the continuation. Positions 74 to 80 must remain blank or contain only a comment. Positions 1 through 5 must be blank. The first ampersand continues the line after position 72 in free source form and is ignored in fixed source form. The second ampersand indicates continuation in fixed source form and in free source form indicates that the text for the continuation of the previous line begins after the ampersand.
The compiler allows you to switch between fixed and free source forms within a file or include file by using the FIXED and FREE compiler directives. All compiler directives should begin in column 1. For more information on compiler directives, see the MIPSpro Fortran 90 Commands and Directives Reference Manual.
The following is a sample program that is acceptable in either fixed or free source form. Note that the numbers and the line at the top are not part of the program; the vertical bars to the left of the program are also not part of the program. These graphics are included to show the columns this program uses in fixed source form.
123456789..... 73 -------------------------------------------.....---- | PROGRAM LEFT_RIGHT | REAL X(5), Y(5) |! Print arrays X and Y | PRINT 100, X, Y | 100 FORMAT (F10.1, F10.2, F10.3, F10.4, & | & F10.5) | . . . | END
Source text can be imported from another file and included within a program file during processing. An INCLUDE line consists of the keyword INCLUDE followed by a character literal constant. The following is an example of an INCLUDE line:
The specified text is substituted for the INCLUDE line during compilation and is treated as if it were part of the original program source text. The compiler allows you to specify search path names on the f90(1) command line for locating files to be included. For more information on INCLUDE lines, see the MIPSpro Fortran 90 Commands and Directives Reference Manual.
The INCLUDE line provides a convenient way to include source text that is the same in several program units. For example, the specification of interface blocks or objects in common blocks may constitute a file that is referenced in the INCLUDE line.
The format for an INCLUDE line is as follows:
The character_literal_constant used cannot have a kind parameter that is a named constant.
The INCLUDE line is a directive (but not a compiler directive) to the compiler; it is not a Fortran statement.
The INCLUDE line is placed where the included text is to appear in the program.
The INCLUDE line must appear on one line with no other text except possibly a trailing comment. There can be no statement label.
The INCLUDE lines can be nested. That is, a second INCLUDE line may appear within the text to be included. The Fortran anstandard does not specify the permitted level of nesting, and the compiler imposes no limit. The text inclusion cannot be recursive at any level. For example, included text A cannot include text B, which includes text A.
A file intended to be referenced in an INCLUDE line cannot begin or end with an incomplete statement.
An example of a program unit with an INCLUDE line follows:
PROGRAM MATH REAL, DIMENSION(10,5,79) :: X, ZT! Some arithmetic INCLUDE 'FOURIER'! More arithmetic . . . END
The source text in the file FOURIER in effect replaces the INCLUDE line.