Ocaml grammar overview
This appendix contains all grammar rules from this manual, including those for the toplevel and the debugger.
Page navigation
Lexical conventions
Names
Type expressions
Constants
Patterns
Expressions
Type and exception definitions
Classes
Module types (module specifications)
Module expressions (module implementations)
Compilation units
Language extensions
Range patterns
Assertion checking
Lazy evaluation
Local modules
Recursive modules
Private types
Local opens
Record notations
Explicit polymorphic type annotations
Locally abstract types
First-class modules
Recovering the type of a module
Substituting inside a signature
Explicit overriding in class definitions
Overriding in open statements
Generalized algebraic datatypes
Syntax for Bigarray access
The toplevel system (ocaml)
The debugger (ocamldebug)
6.1 Lexical conventions
Identifiers
ident
::=
(
letter
∣
_
) {
letter
∣
0
…
9
∣
_
∣
'
}
capitalized-ident
::=
(
A
…
Z
) {
letter
∣
0
…
9
∣
_
∣
'
}
lowercase-ident
::=
(
a
…
z
∣
_
) {
letter
∣
0
…
9
∣
_
∣
'
}
letter
::=
A
…
Z
∣
a
…
z
Integer literals
integer-literal
::=
[
-
] (
0
…
9
) {
0
…
9
∣
_
}
∣
[
-
] (
0x
∣
0X
) (
0
…
9
∣
A
…
F
∣
a
…
f
) {
0
…
9
∣
A
…
F
∣
a
…
f
∣
_
}
∣
[
-
] (
0o
∣
0O
) (
0
…
7
) {
0
…
7
∣
_
}
∣
[
-
] (
0b
∣
0B
) (
0
…
1
) {
0
…
1
∣
_
}
Floating-point literals
float-literal
::=
[
-
] (
0
…
9
) {
0
…
9
∣
_
} [
.
{
0
…
9
∣
_
}] [(
e
∣
E
) [
+
∣
-
] (
0
…
9
) {
0
…
9
∣
_
}]
Character literals
char-literal
::=
'
regular-char
'
∣
'
escape-sequence
'
escape-sequence
::=
\
(
\
∣
"
∣
'
∣
n
∣
t
∣
b
∣
r
∣
space
)
∣
\
(
0
…
9
) (
0
…
9
) (
0
…
9
)
∣
\x
(
0
…
9
∣
A
…
F
∣
a
…
f
) (
0
…
9
∣
A
…
F
∣
a
…
f
)
String literals
string-literal
::=
"
{
string-character
}
"
string-character
::=
regular-string-char
∣
escape-sequence
∣
\
newline
{
space
∣
tab
}
Naming labels
label-name
::=
lowercase-ident
label
::=
~
label-name
:
optlabel
::=
?
label-name
:
Prefix and infix symbols
infix-symbol
::=
(
=
∣
<
∣
>
∣
@
∣
^
∣
|
∣
&
∣
+
∣
-
∣
*
∣
/
∣
$
∣
%
) {
operator-char
}
prefix-symbol
::=
!
{
operator-char
}
∣
(
?
∣
~
) {
operator-char
}
+
operator-char
::=
!
∣
$
∣
%
∣
&
∣
*
∣
+
∣
-
∣
.
∣
/
∣
:
∣
<
∣
=
∣
>
∣
?
∣
@
∣
^
∣
|
∣
~
Line number directives
linenum-directive
::=
#
{
0
…
9
}
+
∣
#
{
0
…
9
}
+
"
{
string-character
}
"
6.3 Names
Naming objects
value-name
::=
lowercase-ident
∣
(
operator-name
)
operator-name
::=
prefix-symbol
∣
infix-op
infix-op
::=
infix-symbol
∣
*
∣
+
∣
-
∣
-.
∣
=
∣
!=
∣
<
∣
>
∣
or
∣
||
∣
&
∣
&&
∣
:=
∣
mod
∣
land
∣
lor
∣
lxor
∣
lsl
∣
lsr
∣
asr
constr-name
::=
capitalized-ident
tag-name
::=
ident
capitalized-ident
typeconstr-name
::=
lowercase-ident
field-name
::=
lowercase-ident
module-name
::=
capitalized-ident
modtype-name
::=
ident
class-name
::=
lowercase-ident
inst-var-name
::=
lowercase-ident
method-name
::=
lowercase-ident
Referring to named objects
value-path
::=
[
module-path
.
]
value-name
constr
::=
[
module-path
.
]
constr-name
typeconstr
::=
[
extended-module-path
.
]
typeconstr-name
field
::=
[
module-path
.
]
field-name
modtype-path
::=
[
extended-module-path
.
]
modtype-name
class-path
::=
[
module-path
.
]
class-name
classtype-path
::=
[
extended-module-path
.
]
class-name
module-path
::=
module-name
{
.
module-name
}
extended-module-path
::=
extended-module-name
{
.
extended-module-name
}
extended-module-name
::=
module-name
{
(
extended-module-path
)
}
6.4 Type expressions
typexpr
::=
'
ident
∣
_
∣
(
typexpr
)
∣
[[
?
]
label-name
:
]
typexpr
->
typexpr
∣
typexpr
{
*
typexpr
}
+
∣
typeconstr
∣
typexpr
typeconstr
∣
(
typexpr
{
,
typexpr
}
)
typeconstr
∣
typexpr
as
'
ident
∣
polymorphic-variant-type
∣
<
[
..
]
>
∣
<
method-type
{
;
method-type
} [
;
∣
;
..
]
>
∣
#
class-path
∣
typexpr
#
class-path
∣
(
typexpr
{
,
typexpr
}
)
#
class-path
extensions for first class modules:
∣
(module
package-type
)
poly-typexpr
::=
typexpr
∣
{
'
ident
}
+
.
typexpr
method-type
::=
method-name
:
poly-typexpr
Polymorphic variant types
polymorphic-variant-type
::=
[
tag-spec
-first
{
|
tag-spec
}
]
∣
[>
[
tag-spec
] {
|
tag-spec
}
]
∣
[<
[
|
]
tag-spec-full
{
|
tag-spec-full
} [
>
{
`
tag-name
}
+
]
]
tag-spec-first
::=
`
tag-name
[
of
typexpr
]
∣
[
typexpr
]
|
tag-spec
tag-spec
::=
`
tag-name
[
of
typexpr
]
∣
typexpr
tag-spec-full
::=
`
tag-name
[
of
[
&
]
typexpr
{
&
typexpr
} ]
∣
typexpr
6.5 Constants
constant
::=
integer-literal
∣
float-literal
∣
char-literal
∣
string-literal
∣
constr
∣
false
∣
true
∣
()
∣
begin
end
∣
[]
∣
[||]
∣
`
tag-name
extensions for more integer literals:
∣
int32-literal
∣
int64-literal
∣
nativeint-literal
6.6 Patterns
pattern
::=
value-name
∣
_
∣
constant
∣
pattern
as
value-name
∣
(
pattern
)
∣
(
pattern
:
typexpr
)
∣
pattern
|
pattern
∣
constr
pattern
∣
`
tag-name
pattern
∣
#
typeconstr
-name
∣
pattern
{
,
pattern
}
+
∣
{
field
=
pattern
{
;
field
=
pattern
} [
;
]
}
∣
[
pattern
{
;
pattern
} [
;
]
]
∣
pattern
::
pattern
∣
[|
pattern
{
;
pattern
} [
;
]
|]
extensions for range patterns:
∣
char-literal
..
char-literal
extensions for lazy evaluation:
∣
lazy
pattern
extensions for record notations:
∣
{
field
[
=
pattern
] {
;
field
[
=
pattern
] } [
;
_
] [
;
]
}
extensions for first class modules:
∣
(module
module-name
[
:
package-type
]
)
6.7 Expressions
expr
::=
value-path
∣
constant
∣
(
expr
)
∣
begin
expr
end
∣
(
expr
:
typexpr
)
∣
expr
{
,
expr
}
+
∣
constr
expr
∣
`
tag-name
expr
∣
expr
::
expr
∣
[
expr
{
;
expr
} [
;
]
]
∣
[|
expr
{
;
expr
} [
;
]
|]
∣
{
field
=
expr
{
;
field
=
expr
} [
;
]
}
∣
{
expr
with
field
=
expr
{
;
field
=
expr
} [
;
]
}
∣
expr
{
argument
}
+
∣
prefix-symbol
expr
∣
-
expr
∣
-.
expr
∣
expr
infix-op
expr
∣
expr
.
field
∣
expr
.
field
<-
expr
∣
expr
.(
expr
)
∣
expr
.(
expr
)
<-
expr
∣
expr
.[
expr
]
∣
expr
.[
expr
]
<-
expr
∣
if
expr
then
expr
[
else
expr
]
∣
while
expr
do
expr
done
∣
for
ident
value-name
=
expr
(
to
∣
downto
)
expr
do
expr
done
∣
expr
;
expr
∣
match
expr
with
pattern-matching
∣
function
pattern-matching
∣
fun
multiple-matching
∣
try
expr
with
pattern-matching
∣
let
[
rec
]
let-binding
{
and
let-binding
}
in
expr
∣
new
class-path
∣
object
class-body
end
∣
expr
#
method-name
∣
(
expr
:>
typexpr
)
∣
(
expr
:
typexpr
:>
typexpr
)
extensions for assertion checking:
∣
assert
expr
extensions for method bodies:
∣
inst-var-name
∣
inst-var-name
<-
expr
∣
{<
[
inst-var-name
=
expr
{
;
inst-var-name
=
expr
} [
;
] ]
>}
extensions for lazy evaluation:
∣
lazy
expr
extensions for local modules:
∣
let
module
module-name
{
(
module-name
:
module-type
)
} [
:
module-type
]
=
module-expr
in
expr
extensions for local opens:
∣
let
open
module-path
in
expr
∣
module-path
.(
expr
)
extensions for record notations:
∣
{
field
[
=
expr
] {
;
field
[
=
expr
] } [
;
]
}
∣
{
expr
with
field
[
=
expr
] {
;
field
[
=
expr
] } [
;
]
}
extensions for first class modules:
∣
(module
module-expr
[
:
package-type
]
)
extensions for overriding in open statements:
∣
let
open!
module-path
in
expr
extensions for bigarray access:
∣
expr
.{
expr
{
,
expr
}
}
∣
expr
.{
expr
{
,
expr
}
}
<-
expr
argument
::=
expr
∣
~
label-name
∣
~
label-name
:
expr
∣
?
label-name
∣
?
label-name
:
expr
pattern-matching
::=
[
|
]
pattern
[
when
expr
]
->
expr
{
|
pattern
[
when
expr
]
->
expr
}
multiple-matching
::=
{
parameter
}
+
[
when
expr
]
->
expr
let-binding
::=
pattern
=
expr
∣
value-name
{
parameter
} [
:
typexpr
]
[
:>
typexpr
]
=
expr
extensions for explicit polymorphic type annotations:
∣
value-name
:
poly-typexpr
=
expr
extensions for locally abstract types:
∣
value-name
:
type
{
typeconstr-name
}
+
.
typexpr
=
expr
parameter
::=
pattern
∣
~
label-name
∣
~
(
label-name
[
:
typexpr
]
)
∣
~
label-name
:
pattern
∣
?
label-name
∣
?
(
label-name
[
:
typexpr
] [
=
expr
]
)
∣
?
label-name
:
pattern
∣
?
label-name
:
(
pattern
[
:
typexpr
] [
=
expr
]
)
extensions for locally abstract types:
∣
(
type
typeconstr-name
)
6.8 Type and exception definitions
6.8.1 Type definitions
type-definition
::=
type
typedef
{
and
typedef
}
typedef
::=
[
type-params
]
typeconstr-name
type-information
type-information
::=
[
type-equation
] [
type-representation
] {
type-constraint
}
type-equation
::=
=
typexpr
extensions for private type abbreviations:
∣
=
private
typexpr
extensions for private row types:
∣
=
private
typexpr
type-representation
::=
=
[
|
]
constr-decl
{
|
constr-decl
}
∣
=
{
field-decl
{
;
field-decl
}
[
;
]
}
extensions for private variant and record types:
∣
=
private
[
|
]
constr-decl
{
|
constr-decl
}
∣
=
private
{
field-decl
{
;
field-decl
} [
;
]
}
type-params
::=
type-param
∣
(
type-param
{
,
type-param
}
)
type-param
::=
[
variance
]
'
ident
extensions for generalized algebraic datatypes:
∣
[
variance
]
_
variance
::=
+
∣
-
constr-decl
::=
(
constr-name
∣
()
) [
of
typexpr
{
*
typexpr
} ]
extensions for generalized algebraic datatypes:
∣
constr-name
:
[
typexpr
{
*
typexpr
}
->
]
typexpr
field-decl
::=
[
mutable
]
field-name
:
poly-typexpr
type-constraint
::=
constraint
'
ident
=
typexpr
6.8.2 Exception definitions
exception-definition
::=
exception
constr-name
[
of
typexpr
{
*
typexpr
} ]
∣
exception
constr-name
=
constr
6.9 Classes
6.9.1 Class types
class-type
::=
{[[
?
]
label-name
:
]
typexpr
->
}
class-body-type
class-body-type
::=
object
[
(
typexpr
)
] {
class-field-spec
}
end
∣
[
[
typexpr
{
,
typexpr
}
]
]
classtype-path
class-field-spec
::=
inherit
class-body-type
∣
val
[
mutable
] [
virtual
]
inst-var-name
:
typexpr
∣
val
virtual
mutable
inst-var-name
:
typexpr
∣
method
[
private
] [
virtual
]
method-name
:
poly-typexpr
∣
method
virtual
private
method-name
:
poly-typexpr
∣
constraint
typexpr
=
typexpr
6.9.2 Class expressions
class-expr
::=
class-path
∣
[
typexpr
{
,
typexpr
}
]
class-path
∣
(
class-expr
)
∣
(
class-expr
:
class-type
)
∣
class-expr
{
argument
}
+
∣
fun
{
parameter
}
+
->
class-expr
∣
let
[
rec
]
let-binding
{
and
let-binding
}
in
class-expr
∣
object
class-body
end
class-field
::=
inherit
class-expr
[
as
value-name
lowercase-ident
]
∣
val
[
mutable
]
inst-var-name
[
:
typexpr
]
=
expr
∣
val
[
mutable
]
virtual
inst-var-name
:
typexpr
∣
val
virtual
mutable
inst-var-name
:
typexpr
∣
method
[
private
]
method-name
{
parameter
} [
:
typexpr
]
=
expr
∣
method
[
private
]
method-name
:
poly-typexpr
=
expr
∣
method
[
private
]
virtual
method-name
:
poly-typexpr
∣
method
virtual
private
method-name
:
poly-typexpr
∣
constraint
typexpr
=
typexpr
∣
initializer
expr
extensions for locally abstract types:
∣
method
[
private
]
method-name
:
type
{
typeconstr-name
}
+
.
typexpr
=
expr
∣
method!
[
private
]
method-name
:
type
{
typeconstr-name
}
+
.
typexpr
=
expr
extensions for explicit overriding in class definitions:
∣
inherit!
class-expr
[
as
lowercase-ident
]
∣
val!
[
mutable
]
inst-var-name
[
:
typexpr
]
=
expr
∣
method!
[
private
]
method-name
{
parameter
} [
:
typexpr
]
=
expr
∣
method!
[
private
]
method-name
:
poly-typexpr
=
expr
Class body
class-body
::=
[
(
pattern
[
:
typexpr
]
)
] {
class-field
}
Method definition
expr
::=
…
∣
inst-var-name
<-
expr
∣
{<
[
inst-var-name
=
expr
{
;
inst-var-name
=
expr
} [
;
] ]
>}
6.9.3 Class definitions
class-definition
::=
class
class-binding
{
and
class-binding
}
class-binding
::=
[
virtual
] [
[
type-parameters
]
]
class-name
{
parameter
} [
:
class-type
]
=
class-expr
type-parameters
::=
'
ident
{
,
'
ident
}
6.9.4 Class specifications
class-specification
::=
class
class-spec
{
and
class-spec
}
class-spec
::=
[
virtual
] [
[
type-parameters
]
]
class-name
:
class-type
6.9.5 Class type definitions
classtype-definition
::=
class
type
classtype-def
{
and
classtype-def
}
classtype-def
::=
[
virtual
] [
[
type-parameters
]
]
class-name
=
class-body-type
6.10 Module types (module specifications)
module-type
::=
modtype-path
∣
sig
{
specification
[
;;
] }
end
∣
functor
(
module-name
:
module-type
)
->
module-type
∣
module-type
with
mod-constraint
{
and
mod-constraint
}
∣
(
module-type
)
extensions for recovering the type of a module:
∣
module
type
of
module-expr
mod-constraint
::=
type
[
type-params
]
typeconstr
type-equation
∣
module
module-path
=
extended-module-path
extensions for substituting inside a signature:
∣
type
[
type-params
]
typeconstr-name
:=
[
type-params
]
typeconstr-name
typexpr
∣
module
module-name
:=
extended-module-path
specification
::=
val
value-name
:
typexpr
∣
external
value-name
:
typexpr
=
external-declaration
∣
type-definition
∣
exception
constr-decl
∣
class-specification
∣
classtype-definition
∣
module
module-name
:
module-type
∣
module
module-name
{
(
module-name
:
module-type
)
}
:
module-type
∣
module
type
modtype-name
∣
module
type
modtype-name
=
module-type
∣
open
module-path
∣
include
module-type
extensions for recursive modules:
∣
module
rec
module-name
:
module-type
{
and
module-name
:
module-type
}
extensions for overriding in open statements:
∣
open!
module-path
6.11 Module expressions (module implementations)
module-expr
::=
module-path
∣
struct
[
module-items
]
end
∣
functor
(
module-name
:
module-type
)
->
module-expr
∣
module-expr
(
module-expr
)
∣
(
module-expr
)
∣
(
module-expr
:
module-type
)
extensions for first class modules:
∣
(val
expr
[
:
package-type
]
)
module-items
::=
[
;;
]
(
definition
∣
expr
) { [
;;
]
definition
∣
;;
expr
} [
;;
]
definition
::=
let
[
rec
]
let-binding
{
and
let-binding
}
∣
external
value-name
:
typexpr
=
external-declaration
∣
type-definition
∣
exception-definition
∣
class-definition
∣
classtype-definition
∣
module
module-name
{
(
module-name
:
module-type
)
} [
:
module-type
]
=
module-expr
∣
module
type
modtype-name
=
module-type
∣
open
module-path
∣
include
module-expr
extensions for recursive modules:
∣
module
rec
module-name
:
module-type
=
module-expr
{
and
module-name
:
module-type
=
module-expr
}
extensions for overriding in open statements:
∣
open!
module-path
6.12 Compilation units
unit-interface
::=
{
specification
[
;;
] }
unit-implementation
::=
[
module-items
]
Chapter 7 Language extensions
7.1 Integer literals for types
int32
,
int64
and
nativeint
constant
::=
...
∣
int32-literal
∣
int64-literal
∣
nativeint-literal
int32-literal
::=
integer-literal
l
int64-literal
::=
integer-literal
L
nativeint-literal
::=
integer-literal
n
7.4 Range patterns
pattern
::=
...
∣
char-literal
..
char-literal
7.5 Assertion checking
expr
::=
...
∣
assert
expr
7.6 Lazy evaluation
expr
::=
...
∣
lazy
expr
pattern
::=
...
∣
lazy
pattern
7.7 Local modules
expr
::=
...
∣
let
module
module-name
{
(
module-name
:
module-type
)
} [
:
module-type
]
=
module-expr
in
expr
7.8 Recursive modules
definition
::=
...
∣
module
rec
module-name
:
module-type
=
module-expr
{
and
module-name
:
module-type
=
module-expr
}
specification
::=
...
∣
module
rec
module-name
:
module-type
{
and
module-name
:
module-type
}
7.9 Private types
7.9.1 Private variant and record types
type-representation
::=
...
∣
=
private
[
|
]
constr-decl
{
|
constr-decl
}
∣
=
private
{
field-decl
{
;
field-decl
} [
;
]
}
7.9.2 Private type abbreviations
type-equation
::=
...
∣
=
private
typexpr
7.9.3 Private row types
type-equation
::=
...
∣
=
private
typexpr
7.10 Local opens
expr
::=
...
∣
let
open
module-path
in
expr
∣
module-path
.(
expr
)
7.11 Record notations
pattern
::=
...
∣
{
field
[
=
pattern
] {
;
field
[
=
pattern
] } [
;
_
] [
;
]
}
expr
::=
...
∣
{
field
[
=
expr
] {
;
field
[
=
expr
] } [
;
]
}
∣
{
expr
with
field
[
=
expr
] {
;
field
[
=
expr
] } [
;
]
}
7.12 Explicit polymorphic type annotations
let-binding
::=
...
∣
value-name
:
poly-typexpr
=
expr
7.13 Locally abstract types
parameter
::=
...
∣
(
type
typeconstr-name
)
let-binding
::=
...
∣
value-name
:
type
{
typeconstr-name
}
+
.
typexpr
=
expr
class-field
::=
...
∣
method
[
private
]
method-name
:
type
{
typeconstr-name
}
+
.
typexpr
=
expr
∣
method!
[
private
]
method-name
:
type
{
typeconstr-name
}
+
.
typexpr
=
expr
7.14 First-class modules
typexpr
::=
...
∣
(module
package-type
)
module-expr
::=
...
∣
(val
expr
[
:
package-type
]
)
expr
::=
...
∣
(module
module-expr
[
:
package-type
]
)
pattern
::=
...
∣
(module
module-name
[
:
package-type
]
)
package-type
::=
modtype-path
∣
modtype-path
with
package-constraint
{
and
package-constraint
}
package-constraint
::=
type
typeconstr
=
typexpr
7.15 Recovering the type of a module
module-type
::=
...
∣
module
type
of
module-expr
7.16 Substituting inside a signature
mod-constraint
::=
...
∣
type
[
type-params
]
typeconstr-name
:=
[
type-params
]
typeconstr-name
typexpr
∣
module
module-name
:=
extended-module-path
7.17 Explicit overriding in class definitions
class-field
::=
...
∣
inherit!
class-expr
[
as
lowercase-ident
]
∣
val!
[
mutable
]
inst-var-name
[
:
typexpr
]
=
expr
∣
method!
[
private
]
method-name
{
parameter
} [
:
typexpr
]
=
expr
∣
method!
[
private
]
method-name
:
poly-typexpr
=
expr
7.18 Overriding in open statements
definition
::=
...
∣
open!
module-path
specification
::=
...
∣
open!
module-path
expr
::=
...
∣
let
open!
module-path
in
expr
7.19 Generalized algebraic datatypes
constr-decl
::=
...
∣
constr-name
:
[
typexpr
{
*
typexpr
}
->
]
typexpr
type-param
::=
...
∣
[
variance
]
_
7.20 Syntax for Bigarray access
expr
::=
...
∣
expr
.{
expr
{
,
expr
}
}
∣
expr
.{
expr
{
,
expr
}
}
<-
expr
Chapter 9 The toplevel system (ocaml)
toplevel-input
::=
{
definition
}
+
;;
∣
expr
;;
∣
#
ident
[
directive-argument
]
;;
directive-argument
::=
string-literal
∣
integer-literal
∣
value-path
∣
true
∣
false
Chapter 16 The debugger (ocamldebug)
16.7 Examining variable values
simple-expr
::=
lowercase-ident
∣
{
capitalized-ident
.
}
lowercase-ident
∣
*
∣
$
integer
∣
simple-expr
.
lowercase-ident
∣
simple-expr
.(
integer
)
∣
simple-expr
.[
integer
]
∣
!
simple-expr
∣
(
simple-expr
)