The Programming Language Oberon
Revision 22.9.2011
Niklaus Wirth
Make it as simple as possible, but not simpler.
Table of Contents
+4. Declarations and scope rules
+ + + + + + + +Appendix: The Syntax of Oberon
+1. Introduction
Oberon is a general-purpose programming language that evolved from Modula-2. Its principal new feature is the concept of type extension. It permits the construction of new data types on the basis of existing ones and to relate them.
+This report is not intended as a programmer's tutorial. It is intentionally kept concise. Its function is to serve as a reference for programmers, implementors, and manual writers. What remains unsaid is mostly left so intentionally, either because it is derivable from stated rules of the language, or because it would unnecessarily restrict the freedom of implementors.
+This document describes the language defined in 1988/90 as revised in 2007/11.
+2. Syntax
A language is an infinite set of sentences, namely the sentences well formed according to its syntax. In Oberon, these sentences are called compilation units. Each unit is a finite sequence of
To describe the syntax, an extended Backus-Naur Formalism called EBNF is used. Brackets [ and ] denote optionality of the enclosed sentential form, and braces { and } denote its repetition (possibly 0 times). Syntactic entities (non-terminal symbols) are denoted by English words expressing their intuitive meaning. Symbols of the language vocabulary (terminal symbols) are denoted by strings enclosed in quote marks or by words in capital letters.
+3. Vocabulary
The following lexical rules must be observed when composing symbols. Blanks and line breaks must not occur within symbols (except in comments, and blanks in strings). They are ignored unless they are essential to separate two consecutive symbols. Capital and lower-case letters are considered as being distinct.
+ ident = letter {letter | digit}.
Examples:
+ x scan Oberon GetSymbol firstLetter
A
number = integer | real.
integer = digit {digit} | digit {hexDigit} "H".
real = digit {digit} "." {digit} [ScaleFactor].
ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.
hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
Examples:
+ 1987
100H = 256
12.3
4.567E8 = 456700000
string = """ {character} """ | digit {hexdigit} "X" .
Examples:
+ "OBERON" "Don't worry!" 22X
+ := ARRAY IMPORT THEN
- ^ BEGIN IN TO
* = BY IS TRUE
/ # CASE MOD TYPE
~ < CONST MODULE UNTIL
& > DIV NIL VAR
. <= DO OF WHILE
, >= ELSE OR
; .. ELSIF POINTER
| : END PROCEDURE
( ) FALSE RECORD
[ ] FOR REPEAT
{ } IF RETURN
4. Declarations and scope rules
Every identifier occurring in a program must be introduced by a declaration, unless it is a predefined identifier. Declarations also serve to specify certain permanent properties of an object, such as whether it is a constant, a type, a variable, or a procedure.
+The identifier is then used to refer to the associated object. This is possible in those parts of a program only which are within the
1. If a type
2. Field identifiers of a record declaration (see 6.3) are valid in field designators only.
+In its declaration, an identifier in the global scope may be followed by an export mark (*) to indicate that it be
qualident = [ident "."] ident.
identdef = ident ["*"].
The following identifiers are predefined; their meaning is defined in section 6.1 (types) or 10.2 (procedures):
+ ABS ASR ASSERT BOOLEAN CHAR
CHR COPY DEC EXCL FLOOR
FLT INC INCL INTEGER LEN
LSL LONG LONGREAL NEW ODD
ORD PACK REAL ROR SET
SHORT UNPK
5. Constant declarations
A constant declaration associates an identifier with a constant value.
+ ConstantDeclaration = identdef "=" ConstExpression.
ConstExpression = expression.
A constant expression can be evaluated by a mere textual scan without actually executing the program. Its operands are constants (see Ch. 8). Examples of constant declarations are:
+ N = 100
limit = 2*N - 1
all = {0 .. WordSize-1}
name = "Oberon"
6. Type declarations
A data type determines the set of values which variables of that type may assume, and the operators that are applicable. A type declaration is used to associate an identifier with a type. The types define the structure of variables of this type and, by implication, the operators that are applicable to components. There are two different structures, namely arrays and records, with different component selectors.
+ TypeDeclaration = identdef "=" StrucType.
StrucType = ArrayType | RecordType | PointerType | ProcedureType.
type = qualident | StrucType.
Examples:
+ Table = ARRAY N OF REAL
Tree = POINTER TO Node
Node = RECORD
key: INTEGER;
left, right: Tree
END
CenterNode = RECORD (Node)
name: ARRAY 32 OF CHAR;
subnode: Tree
END
Function = PROCEDURE (x: INTEGER): INTEGER
6.1. Basic types
The following basic types are denoted by predeclared identifiers. The associated operators are defined in 8.2, and the predeclared function procedures in 10.2. The values of a given basic type are the following:
+ BOOLEAN the truth values TRUE and FALSE
CHAR the characters of a standard character set
INTEGER the integers
REAL real numbers
LONGREAL real numbers
SET the sets of integers between 0 and 31
The type LONGREAL is intended to represent real numbers with a higher number of digits than REAL. However, the two types may be identical.
+6.2. Array types
An array is a structure consisting of a fixed number of elements which are all of the same type, called the
ArrayType = ARRAY length {"," length} OF type.
length = ConstExpression.
A declaration of the form
+ ARRAY N0, N1, ... , Nk OF T
ARRAY N0 OF
ARRAY N1 OF
...
ARRAY Nk OF T
Examples of array types:
+ ARRAY N OF INTEGER
ARRAY 10, 20 OF REAL
6.3. Record types
A record type is a structure consisting of a fixed number of elements of possibly different types. The record type declaration specifies for each element, called
RecordType = RECORD ["(" BaseType ")"] [FieldListSequence] END.
BaseType = qualident.
FieldListSequence = FieldList {";" FieldList}.
FieldList = IdentList ":" type.
IdentList = identdef {"," identdef}.
If a record type is exported, field identifiers that are to be visible outside the declaring module must be marked. They are called
Record types are extensible, i.e. a record type can be defined as an extension of another record type. In the examples above,
Examples of record types:
+ RECORD day, month, year: INTEGER
END
RECORD
name, firstname: ARRAY 32 OF CHAR;
age: INTEGER;
salary: REAL
END
6.4. Pointer types
Variables of a pointer type
PointerType = POINTER TO type.
If
6.5. Procedure types
Variables of a procedure type
ProcedureType = PROCEDURE [FormalParameters].
7. Variable declarations
Variable declarations serve to introduce variables and associate them with identifiers that must be unique within the given scope. They also serve to associate fixed data types with the variables.
+ VariableDeclaration = IdentList ":" type.
Variables whose identifiers appear in the same list are all of the same type. Examples of variable declarations (refer to examples in Ch. 6):
+ i, j, k: INTEGER
x, y: REAL
p, q: BOOLEAN
s: SET
f: Function
a: ARRAY 100 OF REAL
w: ARRAY 16 OF
RECORD
ch: CHAR;
count: INTEGER
END
t: Tree
8. Expressions
Expressions are constructs denoting rules of computation whereby constants and current values of variables are combined to derive other values by the application of operators and function procedures. Expressions consist of operands and operators. Parentheses may be used to express specific associations of operators and operands.
+8.1. Operands
With the exception of sets and literal constants, i.e. numbers and strings, operands are denoted by
If A designates an array, then
The
1.
2.
designator = qualident {selector}.
selector = "." ident | "[" ExpList "]" | "^" | "(" qualident ")".
ExpList = expression {"," expression}.
If the designated object is a variable, then the designator refers to the variable's current value. If the object is a procedure, a designator without parameter list refers to that procedure. If it is followed by a (possibly empty) parameter list, the designator implies an activation of the procedure and stands for the value resulting from its execution. The (types of the) actual parameters must correspond to the formal parameters as specified in the procedure's declaration (see Ch. 10).
+Examples of designators (see examples in Ch. 7):
+ i (INTEGER)
a[i] (REAL)
w[3].ch (CHAR)
t.key (INTEGER)
t.left.right (Tree)
t(CenterNode).subnode (Tree)
8.2. Operators
The syntax of expressions distinguishes between four classes of operators with different precedences (binding strengths). The operator ~ has the highest precedence, followed by multiplication operators, addition operators, and relations. Operators of the same precedence associate from left to right. For example,
expression = SimpleExpression [relation SimpleExpression].
relation = "=" | "#" | "<" | "<=" | ">" | ">=" | IN | IS.
SimpleExpression = ["+"|"-"] term {AddOperator term}.
AddOperator = "+" | "-" | OR.
term = factor {MulOperator factor}.
MulOperator = "*" | "/" | DIV | MOD | "&" .
factor = number | string | NIL | TRUE | FALSE |
set | designator [ActualParameters] | "(" expression ")" | "~" factor.
set = "{" [element {"," element}] "}".
element = expression [".." expression].
ActualParameters = "(" [ExpList] ")" .
The available operators are listed in the following tables. In some instances, several different operations are designated by the same operator symbol. In these cases, the actual operation is identified by the type of the operands.
+ symbol result
OR logical disjunction
& logical conjunction
~ negation
These operators apply to BOOLEAN operands and yield a BOOLEAN result.
+ p OR q stands for "if p then TRUE, else q"
p & q stands for "if p then q, else FALSE"
~ p stands for "not p"
symbol result
+ sum
- difference
* product
/ quotient
DIV integer quotient
MOD modulus
The operators +, -, *, and / apply to operands of numeric types. Both operands must be of the same type, which is also the type of the result. When used as unary operators, - denotes sign inversion and + denotes the identity operation.
+The operators DIV and MOD apply to integer operands only. Let q = x DIV y, and r = x MOD y. Then quotient
x = q*y + r 0 <= r < y
symbol result
+ union
- difference
* intersection
/ symmetric set difference
When used with a single operand of type SET, the minus sign denotes the set complement.
+ symbol relation
= equal
# unequal
< less
<= less or equal
> greater
>= greater or equal
IN set membership
IS type test
Relations are Boolean. The ordering relations <, <=, >, >= apply to the numeric types, CHAR, and character arrays. The relations = and # also apply to the types BOOLEAN and SET, and to pointer and procedure types. The relations <= and >= denote inclusion when applied to sets.
+1. T is an extension of the declared type T0 of v, and if
+2. v is a variable parameter of record type or v is a pointer.
+Assuming, for instance, that T is an extension of T0 and that v is a designator declared of type T0, then the test
Examples of expressions (refer to examples in Ch. 7):
+ 1987 (INTEGER)
i DIV 3 (INTEGER)
~p OR q (BOOLEAN)
(i+j) * (i-j) (INTEGER)
s - {8, 9, 13} (SET)
a[i+j] * a[i-j] (REAL)
(0<=i) & (i<100) (BOOLEAN)
t.key = 0 (BOOLEAN)
k IN {i .. j-1} (BOOLEAN)
t IS CenterNode (BOOLEAN)
9. Statements
Statements denote actions. There are elementary and structured statements. Elementary statements are not composed of any parts that are themselves statements. They are the assignment and the procedure call. Structured statements are composed of parts that are themselves statements. They are used to express sequencing and conditional, selective, and repetitive execution. A statement may also be empty, in which case it denotes no action. The empty statement is included in order to relax punctuation rules in statement sequences.
+ statement = [assignment | ProcedureCall | IfStatement | CaseStatement |
WhileStatement | RepeatStatement | ForStatement].
9.1. Assignments
The assignment serves to replace the current value of a variable by a new value specified by an expression. The assignment operator is written as ":=" and pronounced as
assignment = designator ":=" expression.
If a value parameter is structured (of array or record type), no assignment to it or to its elements are permitted. Neither may assignments be made to imported variables.
+The type of the expression must be the same as that of the designator. The following exceptions hold:
+1. The constant NIL can be assigned to variables of any pointer or procedure type.
+2. Strings can be assigned to any array of characters, provided the number of characters in the string is not greater than that of the array. If it is less, a null character (0X) is appended. Singlecharacter strings can also be assigned to variables of type CHAR.
+3. In the case of records, the type of the source must be an extension of the type of the destination. Examples of assignments (see examples in Ch. 7):
+ i := 0
p := i = j
x := FLT(i + 1)
k := (i + j) DIV 2
f := log2
s := {2, 3, 5, 7, 11, 13}
a[i] := (x+y) * (x-y)
t.key := i
w[i+1].ch := "A"
9.2. Procedure calls
A procedure call serves to activate a procedure. The procedure call may contain a list of actual parameters which are substituted in place of their corresponding formal parameters defined in the procedure declaration (see Ch. 10). The correspondence is established by the positions of the parameters in the lists of actual and formal parameters respectively. There exist two kinds of parameters:
In the case of variable parameters, the actual parameter must be a designator denoting a variable. If it designates an element of a structured variable, the selector is evaluated when the formal/actual parameter substitution takes place, i.e. before the execution of the procedure. If the parameter is a value parameter, the corresponding actual parameter must be an expression. This expression is evaluated prior to the procedure activation, and the resulting value is assigned to the formal parameter which now constitutes a local variable (see also 10.1.).
+ ProcedureCall = designator [ActualParameters].
Examples of procedure calls:
+ ReadInt(i) (see Ch. 10)
WriteInt(2*j + 1, 6)
INC(w[k].count)
9.3. Statement sequences
Statement sequences denote the sequence of actions specified by the component statements which are separated by semicolons.
+ StatementSequence = statement {";" statement}.
9.4. If statements
IfStatement = IF expression THEN StatementSequence
{ELSIF expression THEN StatementSequence}
[ELSE StatementSequence]
END.
If statements specify the conditional execution of guarded statements. The Boolean expression preceding a statement is called its
Example:
+ IF (ch >= "A") & (ch <= "Z") THEN ReadIdentifier
ELSIF (ch >= "0") & (ch <= "9") THEN ReadNumber
ELSIF ch = 22X THEN ReadString
END
9.5. Case statements
Case statements specify the selection and execution of a statement sequence according to the value of an expression. First the case expression is evaluated, then the statement sequence is executed whose case label list contains the obtained value. The case expression must be of type INTEGER or CHAR, and all labels must be integers or single-character strings, respectively.
+ CaseStatement = CASE expression OF case {"|" case} END.
case = [CaseLabelList ":" StatementSequence].
CaseLabelList = LabelRange {"," LabelRange}.
LabelRange = label [".." label].
label = integer | string | ident.
Example:
+ CASE k OF
0: x := x + y
| 1: x := x - y
| 2: x := x * y
| 3: x := x / y
END
9.6. While statements
While statements specify repetition. If any of the Boolean expressions (guards) yields TRUE, the corresponding statement sequence is executed. The expression evaluation and the statement execution are repeated until none of the Boolean expressions yields TRUE.
+ WhileStatement = WHILE expression DO StatementSequence
{ELSIF expression DO StatementSequence} END.
Examples:
+ WHILE j > 0 DO
j := j DIV 2; i := i+1
END
WHILE (t # NIL) & (t.key # i) DO
t := t.left
END
WHILE m > n DO m := m - n
ELSIF n > m DO n := n - m
END
9.7. Repeat Statements
A repeat statement specifies the repeated execution of a statement sequence until a condition is satisfied. The statement sequence is executed at least once.
+ RepeatStatement = REPEAT StatementSequence UNTIL expression.
9.8. For statements
A for statement specifies the repeated execution of a statement sequence for a given number of times, while a progression of values is assigned to an integer variable called the
ForStatement =
FOR ident ":=" expression TO expression [BY ConstExpression] DO
StatementSequence END .
The for statement
+ FOR v := beg TO end BY inc DO S END
v := beg; lim := end;
WHILE v <= lim DO S; v := v + inc END
v := beg; lim := end;
WHILE v >= lim DO S; v := v + inc END
The types of
10. Procedure declarations
Procedure declarations consist of a procedure heading and a procedure body. The heading specifies the procedure identifier, the formal parameters, and the result type (if any). The body contains declarations and statements. The procedure identifier is repeated at the end of the procedure declaration.
+There are two kinds of procedures, namely proper procedures and function procedures. The latter are activated by a function designator as a constituent of an expression, and yield a result that is an operand in the expression. Proper procedures are activated by a procedure call. A function procedure is distinguished in the declaration by indication of the type of its result following the parameter list. Its body must end with a RETURN clause which defines the result of the function procedure.
+All constants, variables, types, and procedures declared within a procedure body are local to the procedure. The values of local variables are undefined upon entry to the procedure. Since procedures may be declared as local objects too, procedure declarations may be nested.
+In addition to its formal parameters and locally declared objects, the objects declared in the environment of the procedure are also visible in the procedure (with the exception of variables and of those objects that have the same name as an object declared locally).
+The use of the procedure identifier in a call within its declaration implies recursive activation of the procedure.
+ ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident.
ProcedureHeading = PROCEDURE identdef [FormalParameters].
ProcedureBody = DeclarationSequence [BEGIN StatementSequence]
[RETURN expression] END.
DeclarationSequence = [CONST {ConstantDeclaration ";"}]
[TYPE {TypeDeclaration ";"}] [VAR {VariableDeclaration ";"}]
{ProcedureDeclaration ";"}.
10.1. Formal parameters
Formal parameters are identifiers which denote actual parameters specified in the procedure call. The correspondence between formal and actual parameters is established when the procedure is called. There are two kinds of parameters, namely
The kind of a parameter is indicated in the formal parameter list: Variable parameters are denoted by the symbol VAR and value parameters by the absence of a prefix.
+A function procedure without parameters must have an empty parameter list. It must be called by a function designator whose actual parameter list is empty too.
+Formal parameters are local to the procedure, i.e. their scope is the program text which constitutes the procedure declaration.
+ FormalParameters = "(" [FPSection {";" FPSection}] ")" [":" qualident].
FPSection = [VAR] ident {"," ident} ":" FormalType.
FormalType = {ARRAY OF} qualident.
The type of each formal parameter is specified in the parameter list. For variable parameters, it must be identical to the corresponding actual parameter's type, except in the case of a record, where it must be a base type of the corresponding actual parameter's type.
+If the formal parameter's type is specified as
+ ARRAY OF T
If a formal parameter specifies a procedure type, then the corresponding actual parameter must be either a procedure declared globally, or a variable (or parameter) of that procedure type. It cannot be a predefined procedure. The result type of a procedure can be neither a record nor an array.
+Examples of procedure declarations:
+ PROCEDURE ReadInt(VAR x: INTEGER);
VAR i : INTEGER; ch: CHAR;
BEGIN i := 0; Read(ch);
WHILE ("0" <= ch) & (ch <= "9") DO
i := 10*i + (ORD(ch)-ORD("0")); Read(ch)
END ;
x := i
END ReadInt
PROCEDURE WriteInt(x: INTEGER); (* 0 <= x < 10^5 *)
VAR i: INTEGER;
buf: ARRAY 5 OF INTEGER;
BEGIN i := 0;
REPEAT buf[i] := x MOD 10; x := x DIV 10; INC(i) UNTIL x = 0;
REPEAT DEC(i); Write(CHR(buf[i] + ORD("0"))) UNTIL i = 0
END WriteInt
PROCEDURE log2(x: INTEGER): INTEGER;
VAR y: INTEGER; (*assume x>0*)
BEGIN y := 0;
WHILE x > 1 DO x := x DIV 2; INC(y) END ;
RETURN y
END log2
10.2. Predefined procedures
The following table lists the predefined procedures. Some are generic procedures, i.e. they apply to several types of operands. v stands for a variable, x and n for expressions, and T for a type.
+
Name Argument type Result type Function
ABS(x) numeric type type of x absolute value
ODD(x) INTEGER BOOLEAN x MOD 2 = 1
LEN(v) v: array INTEGER the length of v
LSL(x, n) x, n: INTEGER type of x logical shift left, x * 2n
ASR(x, n) x, n: INTEGER type of x signed shift right, x DIV 2n
ROR(x, n) x. n: INTEGER type of x x rotated right by n bits
Name Argument type Result type Function
FLOOR(x) REAL, LONGREAL INTEGER largest integer <= x
FLT(x) INTEGER REAL identity
ORD(x) CHAR, BOOLEAN, SET INTEGER ordinal number of x
CHR(x) INTEGER CHAR character with ordinal number x
LONG(x) REAL LONGREAL x
SHORT(x) LONGREAL REAL x
Name Argument types Function
INC(v) INTEGER v := v + 1
INC(v, n) INTEGER v := v + n
DEC(v) INTEGER v := v - 1
DEC(v, n) INTEGER v := v - n
INCL(v, x) v: SET; x: INTEGER v := v + {x}
EXCL(v, x) v: SET; x: INTEGER v := v - {x}
COPY(x, v) x: character array, string v := x
v: character array
NEW(v) pointer type allocate v^
ASSERT(b) BOOLEAN abort, if ~b
ASSERT(b, n) BOOLEAN, INTEGER
PACK(x, y) REAL; INTEGER pack x and y into x
UNPK(x, y) REAL; INTEGER unpack x into x and y
Procedures INC and DEC may have an explicit increment or decrement. It must be a constant. Also for INCL and EXCL,
The parameter
11. Modules
A module is a collection of declarations of constants, types, variables, and procedures, and a sequence of statements for the purpose of assigning initial values to the variables. A module typically constitutes a text that is compilable as a unit.
+ module = MODULE ident ";" [ImportList ";"] DeclarationSequence
[BEGIN StatementSequence] END ident "." .
ImportList = IMPORT import {"," import} ";" .
Import = ident [":=" ident].
The import list specifies the modules of which the module is a client. If an identifier x is exported from a module M, and if M is listed in a module's import list, then x is referred to as M.x. If the form "M := M1" is used in the import list, an exported object x declared within M1 is referenced in the importing module as M.x .
+Identifiers that are to be visible in client modules, i.e. which are to be exported, must be marked by an asterisk (export mark) in their declaration. Variables cannot be exported, with the exception of those of scalar types in read-only mode.
+The statement sequence following the symbol BEGIN is executed when the module is added to a system (loaded). Individual (parameterless) procedures can thereafter be activated from the system, and these procedures serve as commands.
+Example:
+ MODULE Out; (*exported procedures: Write, WriteInt, WriteLn*)
IMPORT Texts, Oberon;
VAR W: Texts.Writer;
PROCEDURE Write*(ch: CHAR);
BEGIN Texts.Write(W, ch)
END ;
PROCEDURE WriteInt*(x, n: INTEGER);
VAR i: INTEGER; a: ARRAY 16 OF CHAR;
BEGIN i := 0;
IF x < 0 THEN Texts.Write(W, "-"); x := -x END ;
REPEAT a[i] := CHR(x MOD 10 + ORD("0")); x := x DIV 10; INC(i) UNTIL x = 0;
REPEAT Texts.Write(W, " "); DEC(n) UNTIL n <= i;
REPEAT DEC(i); Texts.Write(W, a[i]) UNTIL i = 0
END WriteInt;
PROCEDURE WriteLn*;
BEGIN Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf)
END WriteLn;
BEGIN Texts.OpenWriter(W)
END Out.
11.1 The Module SYSTEM
The optional module SYSTEM contains definitions that are necessary to program low-level operations referring directly to resources particular to a given computer and/or implementation. These include for example facilities for accessing devices that are controlled by the computer, and perhaps facilities to break the data type compatibility rules otherwise imposed by the language definition. It is strongly recommended to restrict their use to specific low-level modules, as such modules are inherently non-portable and not "type-safe". However, they are easily recognized due to the identifier SYSTEM appearing in their import lists. The subsequent definitions are generally applicable. However, individual implementations may include in their module SYSTEM additional definitions that are particular to the specific, underlying computer. In the following,
Name Argument types Result type Function
ADR(v) any INTEGER address of variable v
SIZE(T) any type INTEGER size in bytes
BIT(a, n) a, n: INTEGER BOOLEAN bit n of mem[a]
Name Argument types Function
GET(a, v) a: INTEGER; v: any basic type v := mem[a]
PUT(a, x) a: INTEGER; x: any basic type mem[a] := x
Appendix
The Syntax of Oberon
letter = "A" | "B" | Е | "Z" | "a" | "b" | Е | "z".
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".
ident = letter {letter | digit}.
qualident = [ident "."] ident.
identdef = ident ["*"].
integer = digit {digit} | digit {hexDigit} "H".
real = digit {digit} "." {digit} [ScaleFactor].
ScaleFactor = ("E" | "D") ["+" | "-"] digit {digit}.
number = integer | real.
string = """ {character} """ | digit {hexDigit} "X".
ConstantDeclaration = identdef "=" ConstExpression.
ConstExpression = expression.
TypeDeclaration = identdef "=" StrucType.
StrucType = ArrayType | RecordType | PointerType | ProcedureType.
type = qualident | StrucType.
ArrayType = ARRAY length {"," length} OF type.
length = ConstExpression.
RecordType = RECORD ["(" BaseType ")"] [FieldListSequence] END.
BaseType = qualident.
FieldListSequence = FieldList {";" FieldList}.
FieldList = IdentList ":" type.
IdentList = identdef {"," identdef}.
PointerType = POINTER TO type.
ProcedureType = PROCEDURE [FormalParameters].
VariableDeclaration = IdentList ":" type.
expression = SimpleExpression [relation SimpleExpression].
relation = "=" | "#" | "<" | "<=" | ">" | ">=" | IN | IS.
SimpleExpression = ["+" | "-"] term {AddOperator term}.
AddOperator = "+" | "-" | OR.
term = factor {MulOperator factor}.
MulOperator = "*" | "/" | DIV | MOD | "&".
factor = number | string | NIL | TRUE | FALSE |
set | designator [ActualParameters] | "(" expression ")" | "~" factor.
designator = qualident {selector}.
selector = "." ident | "[" ExpList "]" | "^" | "(" qualident ")".
set = "{" [element {"," element}] "}".
element = expression [".." expression].
ExpList = expression {"," expression}.
ActualParameters = "(" [ExpList] ")" .
statement = [assignment | ProcedureCall | IfStatement | CaseStatement |
WhileStatement | RepeatStatement | ForStatement].
assignment = designator ":=" expression.
ProcedureCall = designator [ActualParameters].
StatementSequence = statement {";" statement}.
IfStatement = IF expression THEN StatementSequence
{ELSIF expression THEN StatementSequence}
[ELSE StatementSequence] END.
CaseStatement = CASE expression OF case {"|" case} END.
case = [CaseLabelList ":" StatementSequence].
CaseLabelList = LabelRange {"," LabelRange}.
LabelRange = label [".." label].
label = integer | string | ident.
WhileStatement = WHILE expression DO StatementSequence
{ELSIF expression DO StatementSequence} END.
RepeatStatement = REPEAT StatementSequence UNTIL expression.
ForStatement = FOR ident ":=" expression TO expression [BY ConstExpression]
DO StatementSequence END.
ProcedureDeclaration = ProcedureHeading ";" ProcedureBody ident.
ProcedureHeading = PROCEDURE identdef [FormalParameters].
ProcedureBody = DeclarationSequence [BEGIN StatementSequence]
[RETURN expression] END.
DeclarationSequence = [CONST {ConstDeclaration ";"}]
[TYPE {TypeDeclaration ";"}]
[VAR {VariableDeclaration ";"}]
{ProcedureDeclaration ";"}.
FormalParameters = "(" [FPSection {";" FPSection}] ")" [":" qualident].
FPSection = [VAR] ident {"," ident} ":" FormalType.
FormalType = {ARRAY OF} qualident.
module = MODULE ident ";" [ImportList] DeclarationSequence
[BEGIN StatementSequence] END ident "." .
ImportList = IMPORT import {"," import} ";".
import = ident [":=" ident].