NAG Fortran Compiler, Release 7.2: Fortran 95 Expressions

Navigation: Previous Up Next

## 8.2 Fortran 95 Expressions

This section contains a quick reference guide to expression syntax and semantics in Fortran 95. For more details see Metcalf, Reid and Cohen “Fortran 95/2003 Explained” or the Fortran 95 standard “IS 1539-1:1997”.

- Syntax element definition is represented by ‘::=’.
- Names in italics (e.g., ‘expr’) are syntax element names.
- Items in ‘[ ]’ are optional.
- Ellipsis ‘...’ indicates optional repetition of the preceding item.
- ‘{’ and ‘}’ are for grouping only.
- Items separated by ‘|’ are a “one-of-many” selection.

### General Form

The general form of an expression expr is as follows:

expr ::= [ unary-operator ] operand [ binary-operator operand ]...

operand ::= | literal-constant | object | array-constructor |
| structure-constructor | function-reference | `(` expr `)`

binary-operator ::= | `+` | `-` | `*` | `/` | `**` | `==` | `/=` | `<` | `>` | `<=` | `>=` | // |
| `.AND.` | `.OR.` | `.EQV.` | `.NEQV.` | user-operator

unary-operator ::= `+` | `-` | `.NOT.` | user-operator

user-operator ::= `.`letter[letter...]`.`

The operators `==`, `/=`, `<`, `>`, `<=` and `>=` may also be represented by `.EQ.`, `.NE.`, `.LT.`, `.GT.`, `.LE.` and `.GE.` respectively.

### Operators

Binary operators are associated left-to-right except for exponentiation, which associates right-to-left. The precedence of each operator is shown by the following table, from highest to lowest:

Operators | Binary/Unary
user-operator | Unary
`**` | Binary
`*` `/` | Binary
`+` `-` | Unary
`+` `-` | Binary
`//` | Binary
`==` `/=` `<` `>` `<=` `>=` | Binary
`.NOT.` | Unary
`.AND.` | Binary
`.OR.` | Binary
`.EQV.` `.NEQV.` | Binary
user-operator | Binary

### Literal Constants

literal-constant ::= | integer-literal | real-literal | double-precision-literal |
| logical-literal | complex-literal | character-literal

integer-literal ::= digit-string [ `_` kind-specifier ]

digit-string ::= { 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 }...

kind-specifier ::= digit-string | name

Note: name must be an `INTEGER PARAMETER` name.

Examples:

          42
          17_1
          32767_int16

real-literal ::= | mantissa [ `E` exponent ] [ `_` kind-specifier ] |
| digit-string `E` exponent [ `_` kind-specifier ]

mantissa ::= | digit-string `.` [ digit-string ] |
| `.` digit-string

exponent ::= [ `+` | `-` ] digit-string

Examples:

          4.2
          .7_1
          327E+67_real64

double-precision-literal ::= { mantissa | digit-string } `D` exponent

Examples:

          4d2
          1.7D-10

logical-literal ::= { `.TRUE.` | `.FALSE.` } [ `_` kind-specifier ]

complex-literal ::= `(` int-or-real-literal `,` int-or-real-literal `)`

int-or-real-literal ::= integer-literal | real-literal

Examples:

          (42,17_1)
          (0,1d0)

character-literal ::= [ kind-specifier `_` ] { `'` [char...] `'` | `"` [char...] `"` }

Note: char is any character other than the quoting character, or the quoting character occurring twice (e.g., `''''` is the same as `"'"`).

Examples:

          '42'
          "Say 'Hello'."

### Constants

constant ::= literal-constant | name

Named constants are declared by the `PARAMETER` statement or by a type-declaration statement with the `PARAMETER` attribute

### Objects

object ::= object-ref [ `%` object-ref ]... [ `(` substring-range `)` ]

object-ref ::= name [ section-subscript-list ]

section-subscript-list ::= `(` section-subscript [ `,` section-subscript ]... `)`

section-subscript ::= expr | triplet

triplet ::= [ expr ] `:` [ expr ] [ `:` expr ]

substring-range ::= [ expr ] `:` [ expr ]

name ::= letter [ letter | digit | `_` ] ...

letter ::= | `A` | `B` | `C` | `D` | `E` | `F` | `G` | `H` | `I` | `J` | `K` | `L` | `M` | `N` | `O` | `P` | `Q` | `R` | `S` | `T` | `U` | `V` | `W` |
| `X` | `Y` | `Z`

variable ::= object

constant-subobject ::= object | character-literal `(` substring-range `)`

Note: For a variable, the name in the initial object-ref must be that of a variable.
For a constant-subobject, the name in the initial object-ref must be that of a constant (i.e., a `PARAMETER`).

An object is scalar if and only if all its object-ref's are scalar. An object-ref with non-zero rank (i.e., an array) can only be followed by scalar non-pointer object-refs.

An array element is a scalar object whose final object-ref contains a section-subscript-list where each section-subscript is a scalar expression. For example

          ARRAY(I,J)
          SCALAR%ARRAY(I,J)

A structure component is an object with more than one object-ref whose final object-ref has no section-subscript-list and all preceding object-refs are scalar. For example

          SCALAR%SCALAR
          ARRAY(I,J)%SCALAR

A substring is a scalar object with a substring-range. For example

          SCALAR(I:J)
          ARRAY(I,J)(K:L)

A whole array is an array object with only one object-ref.

An array section is an array object where a non-final object-ref has non-zero rank, or the final object-ref has a section-subscript-list and one or more of the section-subscripts has non-zero rank or is a triplet. For example

          ARRAY(2:M)
          ARRAY%SCALAR

### Array Constructor

array-constructor ::= `(/` ac-item [ `,` ac-item ] ... `/)`

ac-item ::= expr | ac-implied-do

ac-implied-do ::= `(` expr [ `,` expr ] ... `,` name `=` expr `,` expr [ `,` expr ] `)`

Note: name is an integer variable name, but its value is not affected by the execution of the array constructor. All expressions in an array constructor must have the same type and type parameters; in particular, for characters, they must have the same length.

### Structure Constructor

structure-constructor ::= name `(` expr [ `,` expr ] ... `)`

Note: name must be the name of a derived type. Each expression must be assignment-compatible with the corresponding component of the derived type.

### Function Reference

function-reference ::= name `(` [ expr [ `,` expr ] ... ] `)`

Note: name must be the name of a function.