2.5. Summary of Forms

This section shows the forms of the higher-level components of a Fortran program. The notation used in most of the forms is the same as that used to show the syntax forms in all the remaining sections of this manual. The complete Backus-Naur form (BNF), as given in the Fortran standard, is included in the Fortran Language Reference Manual, Volume 3.

2.5.1. Program Units

Fortran defines program_unit as follows:

Table 2-1.

 

executable_program

is

program_unit
  [ program_unit ]...

 

program_unit

is

main_program

 

 

or

external_subprogram

 

 

or

module

 

 

or

block_data

 

main_program

is

[program_stmt ]
  [ specification_part ]
  [ execution_part ]
  [ internal_subprogram_part ]
end_program_stmt

 

external_subprogram

is

function_subprogram

 

 

or

subroutine_subprogram

 

function_subprogram

is

function_stmt
  [ specification_part ]
  [ execution_part ]
  [ internal_subprogram_part ]
end_function_stmt

 

subroutine_subprogram

is

subroutine_stmt
  [ specification_part ]
  [ execution_part ]
  [ internal_subprogram_part ]
end_subroutine_stmt

 

module

is

module_stmt
  [ specification_part ]
  [ module_subprogram_part ]
end_module_stmt

 

block_data

is

block_data_stmt
  [ specification_part ]
end_block_data_stmt

 

specification_part

is

[use_stmt ]...
  [ import_stmt ]
  [ implicit_part ]
  [ declaration_construct ]...

 

implicit_part

is

[implicit_part_stmt ]...
  implicit_stmt

 

implicit_part_stmt

is

implicit_stmt

 

 

or

parameter_stmt

 

 

or

format_stmt

 

 

or

entry_stmt

 

declaration_construct

is

type_alias_stmt

 

 

or

derived_type_def

 

 

or

interface_block

 

 

or

type_declaration_stmt

 

 

or

specification_stmt

 

 

or

parameter_stmt

 

 

or

enum_alias_def

 

 

or

format_stmt

 

 

or

entry_stmt

 

 

or

stmt_function_stmt

 

execution_part

is

executable_construct
  [ execution_part_construct ]...

 

execution_part_construct

is

executable_construct

 

 

or

format_stmt

 

 

or

data_stmt

 

 

or

entry_stmt

 

internal_subprogram_part

is

contains_stmt
  internal_subprogram
  [ internal_subprogram ]...

 

internal_subprogram

is

function_subprogram

 

 

or

subroutine_subprogram

 

module_subprogram_part

is

contains_stmt
  module_subprogram
  [ module_subprogram ]...

 

module_subprogram

is

function_subprogram

 

 

or

subroutine_subprogram

 

specification_stmt

is

access_stmt

 

 

or

allocatable_stmt

 

 

or

bind_stmt

 

 

or

common_stmt

 

 

or

data_stmt

 

 

or

dimension_stmt

 

 

or

equivalence_stmt

 

 

or

external_stmt

 

 

or

intent_stmt

 

 

or

intrinsic_stmt

 

 

or

namelist_stmt

 

 

or

optional_stmt

 

 

or

pointer_stmt

 

 

or

protected_stmt

 

 

or

save_stmt

 

 

or

static_stmt

 

 

or

target_stmt

 

 

or

value_stmt

 

executable_construct

is

action_stmt

 

 

or

associate_construct

 

 

or

case_construct

 

 

or

do_construct

 

 

or

forall_construct

 

 

or

if_construct

 

 

or

where_construct

 

action_stmt

is

allocate_stmt

 

 

or

arithmetic_if_stmt

 

 

or

assign_stmt

 

 

or

assigned_goto_stmt

 

 

or

assignment_stmt

 

 

or

backspace_stmt

 

 

or

call_stmt

 

 

or

close_stmt

 

 

or

computed_goto_stmt

 

 

or

continue_stmt

 

 

or

cycle_stmt

 

 

or

deallocate_stmt

 

 

or

endfile_stmt

 

 

or

end_function_stmt

 

 

or

end_program_stmt

 

 

or

end_subroutine_stmt

 

 

or

exit_stmt

 

 

or

forall_stmt

 

 

or

goto_stmt

 

 

or

if_stmt

 

 

or

inquire_stmt

 

 

or

nullify_stmt

 

 

or

open_stmt

 

 

or

pause_stmt

 

 

or

pointer_assignment_stmt

 

 

or

print_stmt

 

 

or

read_stmt

 

 

or

return_stmt

 

 

or

rewind_stmt

 

 

or

stop_stmt

 

 

or

where_stmt

 

 

or

write_stmt

2.5.2. Main Program

Typically, a main program takes the following form:

[ PROGRAM program_name]
   [ specification_part ]
   [ execution_part ]
   [ CONTAINS internal_subprogram
[ internal_subprogram ] ...]
   END [ PROGRAM [ program_name] ]

2.5.3. External Subprogram

Typically, an external subprogram takes one of the following forms:

function_stmt
   [ specification_part ]
   [ execution_part ]
   [ CONTAINS internal_subprogram
[ internal_subprogram ] ... ]
   end_function_stmt

subroutine_stmt
   [ specification_part ]
   [ execution_part ]
   [ CONTAINS
internal_subprogram
[ internal_subprogram ] ... ]
   end_subroutine stmt

2.5.4. Module

Typically, a module takes the following form:

MODULE module_name
   [ specification_part ]
   [ CONTAINS module_subprogram
[ module_subprogram ] ... ]
   END [ MODULE [ module_name ]]

2.5.5. Block Data

Typically, a block data program unit takes the following form:

BLOCK DATA [ block_data_name ] [ specification_part ]
   END [ BLOCK DATA [ block_data_name ] ]

2.5.6. Internal Subprogram

Typically, an internal subprogram takes one of the following forms:

function_stmt [ specification_part ] [ execution_part ] end_function_stmt

subroutine_stmt [ specification_part ] [ executable_part ] end_subroutine stmt

2.5.7. Procedure Headings

The FUNCTION and SUBROUTINE statements are defined as follows:

[ prefix ] FUNCTION function_name ([ dummy_argument_list ]) 
   [ RESULT (result_name) ]

[ prefix ] SUBROUTINE  subroutine_name [ ([ dummy_argument_list ]) ]

2.5.8. Procedure Endings

The END FUNCTION and END SUBROUTINE statements are defined as follows:

END [ FUNCTION [ function_name ] ]

END [ SUBROUTINE [ subroutine_name ] ]

2.5.9. Specification Constructs

A specification part generally can contain any of the following. Some program units or subprograms may not allow some of these specification constructs. See the descriptions for each program unit or subprogram description for restrictions.

2.5.10. Derived-type Definition

Typically, derived-type definitions take the following form:

TYPE [ [, access_spec] :: ] type_name
   [ PRIVATE ]
   [ SEQUENCE ]
   type_spec [ [, component_attr_spec_list ] :: ]
     component_decl_list ...
   [ type_spec [ [, component_attr_spec_list ] :: ]
  component_decl_list ... ] ...
   END TYPE [ type_name ]

2.5.11. Interface Block

Typically, an interface block takes the following form:

INTERFACE [ generic_spec ]
   [ interface_body ] ...
   [[MODULE] PROCEDURE procedure_name_list ] ...
   END INTERFACE

2.5.12. Specification Statements

This section lists the general forms for specification statements. The BNF used here is an abbreviated format and is used only in this section. The specific formats are described in later sections of this manual. The specification statements are as follows:

ALLOCATABLE [ :: ] allocatable_array_list

AUTOMATIC automatic_list ...

BIND ( C, [ bind_spec_list] )  [ :: ] bind_entity_list

COMMON [ / [ common_block_name ] / ] common_block_object_list

DATA  data_statement_object_list / data_statement_value_list  /

DIMENSION array_dimension_list

EQUIVALENCE equivalence_set_list

EXTERNAL external_name_list

FORMAT ([ format_item_list ])

IMPLICIT implicit_spec

INTENT (intent_spec) [ :: ] dummy_argument_name_list

INTRINSIC intrinsic_procedure_name_list

NAMELIST / namelist_group_name / namelist_group_object_list

OPTIONAL [ :: ] optional_object_list

PARAMETER (named_constant_definition_list)

POINTER [ :: ] pointer_list

PROTECTED [ [ :: ] module_entity_name_list ]

PUBLIC [ [ :: ] module_entity_name_list ]

PRIVATE [ [ :: ] module_entity_name_list ]

SAVE [ [ :: ] saved_object_list ]

STATIC [ [ :: ] static_object_list ]

TARGET [ :: ] target_list

USE [[,module-nature] :: ] module-name [, rename-list]

USE [[,module-nature] :: ] module-name , ONLY : [only-list]

type_spec [ [, attr_spec ] ... :: ] object_declaration_list

VALUE [ :: ] dummy-arg-name-list

VOLATILE [::] entity_decl_list

Note: The Fortran standard does not specify the AUTOMATIC, or STATIC statement. BIND, module_nature, PROTECTED, VALUE, and VOLATILE are Fortran 2003 features.

2.5.13. Type Declaration Statements

Typically, the type declaration statements take the following form:

TYPE (type_name)

TYPE (type_alias_name)

LOGICAL [ ([ KIND= ] kind_parameter) ]

CHARACTER*char_length

CHARACTER (KIND= kind_parameter [, LEN= length_parameter ])

CHARACTER
   ([ LEN= ] length_parameter [, [KIND= ] kind_parameter ])

CHARACTER [ ([ LEN= ] length_parameter) ]

COMPLEX [ ([ KIND= ] kind_parameter) ]

DOUBLE PRECISION

REAL [ ([ KIND= ] kind_parameter) ]

INTEGER [ ([ KIND= ] kind_parameter) ]

2.5.14. Attribute Specifications

Section 2.5.12, introduced attr_spec. Typically, attribute specifications take the following form:

ALLOCATABLE

AUTOMATIC

BIND (C [ , bind_spec_list ]

DIMENSION (array_spec)

EXTERNAL

INTENT (intent_spec)

INTRINSIC

OPTIONAL

PARAMETER

POINTER

PRIVATE

PROTECTED

PUBLIC

SAVE

STATIC

TARGET

VALUE

VOLATILE

Note: The Fortran standard does not specify the AUTOMATIC, or STATIC attribute. The BIND, PROTECTED, VALUE, and VOLATILE attributes are Fortran 2003 features.

2.5.15. Execution Part

An execution part can contain the following:

2.5.16. Action Statements

Typically, action statements take the following form:

ALLOCATE (allocation_list  [ , STAT= scalar_integer_variable ])

ASSIGN label TO scalar_integer_variable

BACKSPACE external_file_unit

BACKSPACE (position_spec_list)

BUFFER IN (id, mode) (start_loc, end_loc )

BUFFER OUT (id, mode) (start_loc, end_loc )

CALL subroutine_name [ ([ actual_argument_spec_list ]) ]

CLOSE (close_spec_list)

CONTINUE

CYCLE [ do_construct_name ]

DEALLOCATE (name_list [ , STAT=  scalar_integer_variable ])

ENDFILE external_file_unit

ENDFILE (position_spec_list)

EXIT [ do_construct_name ]

FORALL forall_header forall_assignment_stmt

GO TO label

GO TO (label_list) [ , ] scalar_integer_expression

GO TO scalar_integer_variable [ [ , ] (label_list) ]

IF (scalar_logical_expression) action_statement

IF (scalar_numeric_expression ) label, label, label

INQUIRE (inquire_spec_list) [ output_item_list ]

NULLIFY (pointer_object_list)

OPEN (connect_spec_list)

PAUSE [ access_code ]

PRINT format [, output_item_list ]

READ (io_control_spec_list) [ input_item_list ]

READ format [, input_item_list ]

RETURN [ scalar_integer_expression ]

REWIND external_file_unit

REWIND (position_spec_list)

STOP [ access_code ]

WHERE (array_logical_expression) array_assignment_statement

WRITE (io_control_spec_list) [ output_item_list ]

pointer_variable => target_expression

variable = expression

2.5.17. CASE Construct

Typically, CASE constructs take the following form:

SELECT CASE (case_expr)
   [ CASE case_selector
   block ] ...
   [ CASE DEFAULT
   block ]
   END SELECT

2.5.18. DO Construct

Typically, DO constructs take the following form:

DO [ label ]
    block
   end_do

DO [ label ] [ , ] do_variable = scalar_numeric_expr,
   scalar_numeric_expr [  , scalar_numeric_expr ]
    block
   end_do

DO [ label ] [ , ] WHILE ( scalar_logical_expr)
    block
   end_do

2.5.19. IF Construct

Typically, IF constructs take the following form:

IF (scalar_logical_expr) THEN
      block
   [ ELSE IF (scalar_logical_expr) THEN
   block ] ...
   [ ELSE block ]
   END IF

2.5.20. FORALL Construct

Typically, FORALL constructs take the following form:

FORALL forall_header
   [ assignment_stmt ] |
   [ forall_assignment_stmt ] |
   [ forall_construct ] |
   [ forall_stmt ] |
   [ pointer_assignment_stmt ] |
   [ where_construct ] |
   [ where_stmt ]
   END FORALL

2.5.21. WHERE Construct

Typically, WHERE constructs take the following form:

[where_construct_name: ] WHERE (mask_expr)
      [ where_body_construct ] ...
   [ ELSEWHERE (mask_expr) [  where_construct_name ]
   [ where_body_construct ] ...  ]
   [ ELSEWHERE [ where_body_construct ] ...  ]
   END WHERE [where_construct_name]