A Fortran statement is made of lexical tokens: names (which include keywords), constants, operators, labels and the delimiters and separators. Upper and lower case are the same except in character literals. Where the tokens may stand on a line is the business of the fixed and free source forms.
Names
- A name starts with a letter and goes on with letters, digits and underscores. Longest allowed: 6 characters in FORTRAN 77 (ANSI X3.9-1978), 31 in Fortran 90 and 95, 63 in Fortran 2003 and later
- The standard has no
$in names; DEC, Intel and GNU Fortran (-fdollar-ok) accept one after the first character, and the viewer reads it as part of the name - A name may be used for a variable, constant, procedure, program unit, derived type, component, construct, generic, namelist or common block. The same name cannot be two things in one scope
Keywords are not reserved
Fortran has no reserved words. The same word is a keyword where a keyword may stand and a name everywhere else. All of these are legal statements:
integer :: if, then, end, real, program
if = 1
then = 2
end = if + then
if (if == 1) then
real = 3.5
end if
if is the keyword.The viewer colours a word as a keyword only where one can stand: the first word of a statement that is not an assignment, the words that follow it in the
same statement (end if, go to, double precision), attributes before ::, control specifiers such as iostat=,
and the like. A variable called end, value, type, print or data stays plain, and so does a component such as a%end.
The full list of keywords by statement is on Statements and control flow.
Literal constants
Every constant of the intrinsic types has a form below. Each may carry a kind parameter: _ then a digit string or the name of an integer
constant (1_8, 2.0_dp, .true._c_bool). The kind parameter was added in Fortran 90.
Integer
42
-7
100000_int64 ! kind named by a constant
1_8 ! kind written as a number (kind 8, usually 64 bits)
A string of digits with an optional sign (the sign belongs to the expression, not the constant), and an optional _kind. Kind numbers are the compiler’s;
the portable way is selected_int_kind or the constants int8, int16, int32 and int64 of iso_fortran_env.
Real
1.5 .5 5. 1e10 1.5e-3 2d0 1.0_dp 1.5e3_dp 3q-2 1.d0
- a significand with a decimal point (
1.5,.5,5.), or an integer digit string followed by an exponent (1e10) - an exponent is the letter
EorDand an optionally signed digit string, in either case.Emeans the default real kind, or the kind given after the constant (1.5e3_dp) Dmakes a double precision real (2d0); a kind parameter cannot follow aDexponentQas an exponent letter (3q-2) is an extension for quad precision, accepted by GNU Fortran, Intel Fortran and others- the viewer does not split a real from a following operator:
1.eq.2is the integer1, the operator.eq.and2
Complex
(1.0, 2.0) (0, -1) (re_part, 1.5_dp)
Two real or integer constants in parentheses. From Fortran 2003 either part may also be a named constant. The viewer colours each part as its own constant.
Logical
.TRUE. and .FALSE., in any case, with an optional kind: .true._1. The viewer shows them as literals.
Character
'It''s' ! apostrophe doubled: It's
"say ""hello""" ! quote doubled: say "hello"
ascii_'text' ! kind named by a constant
1_'text' ! kind written as a number
'' ! length zero
- between apostrophes or between quotes; the delimiter is written twice to put it in the text. The other delimiter needs no doubling
- blanks count and a literal can be of length zero. There are no escape sequences:
\nis two characters unless the compiler is told otherwise (GNU Fortran-fbackslash); usenew_line('a')orachar(10) - an optional kind comes first (
kind_'text') - a literal may be continued over lines (free form, fixed form), and the viewer colours the pieces as one string
Binary, octal and hexadecimal (BOZ)
b'10110' B"1010" o'777' O"17" z'FF' Z"1f"
x'1F' ! accepted by compilers as another spelling of Z
'1F'x '777'o '101'b 'ff'z ! older DEC suffix form
A letter B, O or Z followed by a digit string in apostrophes or quotes. A BOZ constant has no type. In Fortran 2003 it could be used only in a data statement;
Fortran 2008 allowed it as an argument of int, real, dble and cmplx and in data, and later standards allow it in more intrinsics (the bit procedures, for example).
The suffix form 'FF'X of DEC and VAX Fortran is not standard but common in old code; the viewer colours both forms as numbers, and only when the digits are valid for the base.
Hollerith constants
DATA MSG /4HABCD/
CALL SHOW(5HHELLO)
100 FORMAT (1X, 7HMESSAGE, I5)
A count, the letter H and exactly that many characters (5HHELLO). Hollerith was the character type of FORTRAN 66, where it could fill integer
and real variables; FORTRAN 77 kept it only inside format statements, as the H edit descriptor, and Fortran 95 deleted that too. Compilers still accept it, in data, as arguments and in formats. The viewer takes
nH as a Hollerith constant only after (, ,, / or = (or * outside a declaration), where a name could not follow, so real*8 h is no Hollerith.
Other constants
- Structure constructors (
point(1.0, 2.0)) and array constructors ([1, 2, 3]and the older(/ 1, 2, 3 /)) build values; they are expressions, not tokens (see Expressions and arrays) - The viewer colours a type name before
::in a constructor ([integer :: 1, 2]) as a type - Unsigned integers (a
usuffix and theunsignedtype) are proposed for a later standard and are an experiment in GNU Fortran (-funsigned); they are not in the Fortran 2023 standard
Operators
| Kind | Operators |
|---|---|
| Arithmetic | ** * / + - |
| Character | // (concatenation) |
| Relational | .EQ. .NE. .LT. .LE. .GT. .GE., and since Fortran 90 == /= < <= > >= |
| Logical | .NOT. .AND. .OR. .EQV. .NEQV.; .XOR. is an extension for .NEQV. |
| Defined | any .name. of letters (.cross., .in.), made with an interface operator(.name.) |
| Assignment | =, pointer assignment =>; interface assignment(=) defines more |
The viewer colours the word operators (.AND., .eq. and any user .name.) as keywords and .TRUE. and .FALSE. as literals. Symbols are left plain. Precedence is on
Expressions and arrays.
Delimiters and separators
| Token | Meaning |
|---|---|
( ) | grouping, subscripts, argument lists, type parameters |
[ ], (/ /) | array constructors ([ ] from Fortran 2003); [ ] also hold image subscripts (a[2], Fortran 2008) |
, | list separator |
:, :: | range and section separator; separator between attributes and entities in declarations |
% | component selection (p%x). VAX Fortran used . for this, as an extension |
; | statement separator |
=> | pointer assignment, use renaming, procedure binding, associate association |
* | list-directed or standard unit, assumed size, assumed length, repetition count in data and namelist |
& | continuation (free form only) |
! | comment (free form, and fixed form after column 6) |
? | conditional expression, with :, in Fortran 2023 |
Other symbols appear only in directives and extensions: %val, %ref, %loc (argument passing in VAX and GNU Fortran, coloured as functions), <<< >>> (CUDA Fortran kernel launch).
Labels and construct names
A statement label is up to five digits. The viewer colours a label as a number. Construct names (outer: do …) and the names after end are plain.
How the viewer colours the tokens
| Colour class | What it marks |
|---|---|
| Comment | ! to the end of the line; C, c, *, D in column 1 of fixed form; columns 73 to 80 of fixed form |
| String | character literals of both quotes, with kind prefix and continuation; Hollerith constants; a quoted string in a format |
| Number | integer, real, complex parts and BOZ constants, with kind suffixes; labels; widths in format |
| Keyword | statement keywords in their places, attributes, control specifiers, .op. word operators, operator, assignment, format edit descriptors |
| Type | integer, real, complex, logical, character, double precision, byte, type(…), class(…), derived type names after type, extends and class is, and the kind constants of iso_fortran_env, iso_c_binding and ieee_* |
| Literal | .true., .false., and constants such as input_unit, iostat_end, c_null_ptr |
| Directive | !$omp, !$acc, !dir$, !dec$, !gcc$ lines, # preprocessor lines, ?? coco lines, $ metacommands |
| Function | names of procedures after call, function, subroutine and entry; every intrinsic function followed by (; functions defined in the same file; %val |