# Collected Modula-2 Concrete Syntax

- Programs, Program Modules, and Separate Modules
- Definitions and Declarations
- Data Types
- Blocks
- Statements
- Variable Designators
- Expressions
- Parameter Compatibility and Argument Binding

## Programs, Program Modules, and Separate Modules

- Programs and Compilation Modules
- Program Modules
- Definition Modules
- Implementation Modules
- Import Lists
- Export Lists
- Protected Modules

### Programs and Compilation Modules

compilation module=
program module | definition module | implementation module ;

### Program Modules

program module=
"MODULE", module identifier, [ interrupt protection ], ";",
import lists,
module block, module identifier, "." ;

module identifier= identifier ;

### Definition Modules

definition module=
"DEFINITION", "MODULE", module identifier, ";",
import lists,
definitions,
"END", module identifier, "." ;

### Implementation Modules

- Sourced Implementation Modules

#### Sourced Implementation Modules

implementation module=
"IMPLEMENTATION", "MODULE", module identifier, [ interrupt protection ], ";",
import lists,
module block, module identifier, "." ;

### Import Lists

import lists= { import list } ;

import list= simple import | unqualified import ;

simple import= "IMPORT", identifier list, ";" ;

unqualified import= "FROM", module identifier, "IMPORT", identifier list, ";" ;

### Export Lists

export list= unqualified export | qualified export ;

unqualified export= "EXPORT", identifier list, ";" ;

qualified export= "EXPORT", "QUALIFIED", identifier list, ";" ;

### Protected Modules

interrupt protection= "[", protection expression, "]" ;

protection expression= constant expression ;

## Definitions and Declarations

- Qualified Identifiers
- Definitions
- Type Definitions
- Procedure Headings
- Declarations
- Constant Declarations
- Type Declarations
- Variable Declarations
- Procedure Declarations
- Local Module Declarations

### Qualified Identifiers

qualified identifier= { qualifying identifier, "." }, identifier ;

qualifying identifier= module identifier ;

### Definitions

definitions= { definition } ;

definition=
"CONST", { constant declaration, ";" } |
"TYPE", { type definition, ";" } |
"VAR", { variable declaration, ";" } |
procedure heading, ";" ;

### Type Definitions

type definition= type declaration | opaque type definition ;

opaque type definition= identifier ;

### Procedure Headings

procedure heading= proper procedure heading | function procedure heading ;

- Proper Procedure Headings
- Function Procedure Headings
- Formal Parameters

#### Proper Procedure Headings

proper procedure heading=
"PROCEDURE", procedure identifier, [ formal parameters ] ;

formal parameters= "(", [ formal parameter list ], ")" ;

formal parameter list= formal parameter, { ";", formal parameter } ;

#### Function Procedure Headings

function procedure heading=
"PROCEDURE", procedure identifier, formal parameters, ":", function result type ;

function result type= type identifier ;

#### Formal Parameters

formal parameter=
value parameter specification | variable parameter specification ;

value parameter specification= identifier list, ":", formal type ;

variable parameter specification= "VAR", identifier list, ":", formal type ;

### Declarations

declarations= { declaration } ;

declaration=
"CONST", { constant declaration, ";" } |
"TYPE", { type declaration, ";" } |
"VAR", { variable declaration, ";" } |
procedure declaration, ";" |
local module declaration, ";" ;

### Constant Declarations

constant declaration= identifier, "=", constant expression ;

### Type Declarations

type declaration= identifier, "=", type denoter ;

### Variable Declarations

variable declaration= variable identifier list, ":", type denoter ;

variable identifier list=
identifier, [ machine address ], { ",", identifier, [ machine address ] } ;

machine address= "[", value of address type, "]" ;

value of address type= constant expression ;

### Procedure Declarations

procedure declaration=
proper procedure declaration | function procedure declaration ;

proper procedure declaration=
proper procedure heading, ";",
( proper procedure block, procedure identifier | "FORWARD" ) ;

procedure identifier= identifier ;

function procedure declaration=
function procedure heading, ";",
( function procedure block, procedure identifier | "FORWARD" ) ;

### Local Module Declarations

local module declaration=
"MODULE", module identifier, [ interrupt protection ], ";",
import lists, [ export list ],
module block, module identifier ;

## Data Types

- Type Denoters and Ordinal Type Denoters
- Type Identifiers
- New Types
- Enumeration Types
- Subrange Types
- Set Types
- Packedset Types
- Pointer Types
- Procedure Types
- Array Types
- Record Types

### Type Denoters and Ordinal Type Denoters

type denoter= type identifier | new type ;

ordinal type denoter= ordinal type identifier | new ordinal type ;

### Type Identifiers

type identifier= qualified identifier ;

ordinal type identifier= type identifier ;

### New Types

new type=
new ordinal type | set type |
packedset type | pointer type |
procedure type | array type |
record type ;

new ordinal type= enumeration type | subrange type ;

### Enumeration Types

enumeration type= "(", identifier list, ")" ;

identifier list= identifier, { ",", identifier } ;

### Subrange Types

subrange type=
[0 pt] [ range type ], "[", constant expression, "..", constant expression, "]" ;

range type= ordinal type identifier ;

### Set Types

set type= "SET", "OF", base type ;

base type= ordinal type denoter ;

### Packedset Types

packedset type= "PACKEDSET", "OF", base type ;

### Pointer Types

pointer type= "POINTER", "TO", bound type ;

bound type= type denoter ;

### Procedure Types

procedure type= proper procedure type | function procedure type ;

proper procedure type=
"PROCEDURE", [ "(", [ formal parameter type list ], ")" ] ;

function procedure type=
"PROCEDURE", "(", [ formal parameter type list ], ")", ":", function result type ;

- Formal Parameter Type Lists
- Formal Types

#### Formal Parameter Type Lists

formal parameter type list=
formal parameter type, { ",", formal parameter type } ;

formal parameter type=
variable formal type | value formal type ;

variable formal type= "VAR", formal type ;

value formal type= formal type ;

#### Formal Types

formal type= type identifier | open array formal type ;

open array formal type= "ARRAY", "OF", open array component type ;

open array component type= formal type ;

### Array Types

array type= "ARRAY", index type, { ",", index type }, "OF", component type ;

index type= ordinal type denoter ;

component type= type denoter ;

### Record Types

record type= "RECORD", field list, "END" ;

field list= fields, { ";", fields } ;

fields= [ fixed fields | variant fields ] ;

- Fixed Fields
- Variant Fields

#### Fixed Fields

fixed fields= identifier list, ":", field type ;

field type= type denoter ;

#### Variant Fields

variant fields= "CASE", tag field, "OF", variant list, "END" ;

tag field= [ tag identifier ], ":", tag type ;

tag identifier= identifier ;

tag type= ordinal type identifier ;

variant list= variant, { "|", variant }, [ variant else part ] ;

variant else part= "ELSE", field list ;

variant= [ variant label list, ":", field list ] ;

variant label list= variant label, { ",", variant label } ;

variant label= constant expression, [ "..", constant expression ] ;

## Blocks

- Proper Procedure Blocks
- Function Procedure Blocks
- Module Blocks
- Block Bodies and Exception Handling

### Proper Procedure Blocks

proper procedure block= declarations, [ procedure body ], "END" ;

procedure body= "BEGIN", block body ;

### Function Procedure Blocks

function procedure block= declarations, function body, "END" ;

function body= "BEGIN", block body ;

### Module Blocks

module block= declarations, [ module body ], "END" ;

module body= initialization body, [ finalization body ] ;

initialization body= "BEGIN", block body ;

finalization body= "FINALLY", block body ;

### Block Bodies and Exception Handling

block body= normal part, [ "EXCEPT", exceptional part ] ;

normal part= statement sequence ;

exceptional part= statement sequence ;

## Statements

statement=
empty statement | assignment statement | procedure call |
return statement | retry statement | with statement |
if statement | case statement | while statement |
repeat statement | loop statement | exit statement |
for statement ;

- Statement Sequences
- Empty Statements
- Assignment Statements
- Procedure Calls
- Return Statements
- Retry Statements
- With Statements
- If Statements
- Case Statements
- While Statements
- Repeat Statements
- Loop Statements
- Exit Statements
- For Statements

### Statement Sequences

statement sequence= statement, { ";", statement } ;

### Empty Statements

empty statement= ;

### Assignment Statements

assignment statement= variable designator, ":=", expression ;

### Procedure Calls

procedure call= procedure designator, [ actual parameters ] ;

procedure designator= value designator ;

### Return Statements

return statement= simple return statement | function return statement ;

simple return statement= "RETURN" ;

function return statement= "RETURN", expression ;

### Retry Statements

retry statement= "RETRY" ;

### With Statements

with statement= "WITH", record designator, "DO", statement sequence, "END" ;

record designator= variable designator | value designator ;

### If Statements

if statement= guarded statements, [ if else part ], "END" ;

guarded statements=
"IF", boolean expression, "THEN", statement sequence,
{ "ELSIF", boolean expression, "THEN", statement sequence } ;

if else part= "ELSE", statement sequence ;

boolean expression= expression ;

### Case Statements

case statement= "CASE", case selector, "OF", case list, "END" ;

case selector= ordinal expression ;

case list= case alternative, { "|", case alternative }, [ case else part ] ;

case else part= "ELSE", statement sequence ;

- Case Alternatives

#### Case Alternatives

case alternative= [ case label list, ":", statement sequence ] ;

case label list= case label, { ",", case label } ;

case label= constant expression, [ "..", constant expression ] ;

### While Statements

while statement=
"WHILE", boolean expression, "DO", statement sequence, "END" ;

### Repeat Statements

repeat statement=
"REPEAT", statement sequence, "UNTIL", boolean expression ;

### Loop Statements

loop statement= "LOOP", statement sequence, "END" ;

### Exit Statements

exit statement= "EXIT" ;

### For Statements

for statement=
"FOR", control variable identifier, ":=", initial value,
"TO", final value, [ "BY", step size ], "DO",
statement sequence, "END" ;

control variable identifier= identifier ;

initial value= ordinal expression ;

final value= ordinal expression ;

step size= constant expression ;

## Variable Designators

variable designator=
entire designator | indexed designator |
selected designator | dereferenced designator ;

- Entire Designators
- Indexed Designators
- Selected Designators
- Dereferenced Designators

### Entire Designators

entire designator= qualified identifier ;

### Indexed Designators

indexed designator=
array variable designator, "[", index expression, { ",", index expression }, "]" ;

array variable designator= variable designator ;

index expression= ordinal expression ;

### Selected Designators

selected designator= record variable designator, ".", field identifier ;

record variable designator= variable designator ;

field identifier= identifier ;

### Dereferenced Designators

dereferenced designator= pointer variable designator, " " ;

pointer variable designator= variable designator ;

## Expressions

expression= simple expression, [ relational operator, simple expression ] ;

simple expression= [ sign ], term, { term operator, term } ;

term= factor, { factor operator, factor } ;

factor=
"(", expression, ")" |
logical negation operator, factor |
value designator | function call |
value constructor | constant literal ;

- Ordinal Expressions
- Infix Expressions and Operations
- Value Designators
- Value Constructors
- Constant Literals
- Constant Expressions

### Ordinal Expressions

ordinal expression= expression ;

### Infix Expressions and Operations

relational operator=
equals operator | inequality operator |
less than operator | greater than operator |
less than or equal operator | subset operator |
greater than or equal operator | superset operator |
set membership operator ;

term operator=
plus operator | set union operator |
minus operator | set difference operator |
logical disjunction operator | string catenate symbol ;

factor operator=
multiplication operator | set intersection operator |
division operator | symmetric set difference operator |
rem operator | div operator |
mod operator | logical conjunction operator ;

### Value Designators

value designator=
entire value | indexed value |
selected value | dereferenced value ;

- Entire Values
- Indexed Values
- Selected Values
- Dereferenced Values
- Function Calls

#### Entire Values

entire value= qualified identifier ;

#### Indexed Values

indexed value= array value, "[", index expression, { ",", index expression }, "]" ;

array value= value designator ;

#### Selected Values

selected value= record value, ".", field identifier ;

record value= value designator ;

#### Dereferenced Values

dereferenced value= pointer value, dereferencing operator ;

pointer value= value designator ;

#### Function Calls

function call= function designator, actual parameters ;

function designator= value designator ;

### Value Constructors

value constructor= array constructor | record constructor | set constructor ;

- Array Constructors
- Record Constructors
- Set Constructors

#### Array Constructors

array constructor= array type identifier, array constructed value ;

array type identifier= type identifier ;

array constructed value=
"{", repeated structure component, { ",", repeated structure component }, "}" ;

repeated structure component=
structure component, [ "BY", repetition factor ] ;

repetition factor= constant expression ;

structure component=
expression | array constructed value |
record constructed value | set constructed value ;

#### Record Constructors

record constructor= record type identifier, record constructed value ;

record type identifier= type identifier ;

record constructed value=
"{", [ structure component, { ",", structure component } ], "}" ;

#### Set Constructors

set constructor= set type identifier, set constructed value ;

set type identifier= type identifier ;

set constructed value= "{", [ member, { ",", member } ], "}" ;

member= interval | singleton ;

interval= ordinal expression, "..", ordinal expression ;

singleton= ordinal expression ;

### Constant Literals

constant literal= whole number literal | real literal | string literal ;

### Constant Expressions

constant expression= expression ;

## Parameter Compatibility and Argument Binding

- Actual Parameters

### Actual Parameters

actual parameters= "(", [ actual parameter list ], ")" ;

actual parameter list= actual parameter, { ",", actual parameter } ;

actual parameter= variable designator | expression | type parameter ;

- Type Parameters

#### Type Parameters

type parameter= type identifier ;