Previous Up
Ocaml grammar overview

This appendix contains the grammar rules for the Ocaml programming language (extracted from Chapter 6 and 7), the toplevel system (from Chapter 9) and the debugger (see Chapter 16). I also included statistics about the ocaml meta symbols.


6.1 Lexical conventions

Identifiers

ident ::= (letter| _) { 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| _ }

Extensions for int32, int64 and nativeint Integer literals

int32-literal ::= integer-literal l
int64-literal ::= integer-literal L
nativeint-literal ::= integer-literal n

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)
  | \ (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-char-str
  | escape-sequence

Naming labels

label ::= ~ (a ... z) { letter| 0...9| _| ' } :
optlabel ::= ? (a ... z) { letter| 0...9| _| ' } :

Prefix and infix symbols

infix-symbol ::= (= | < | > | @ | ^ | | | & | + | - | * | / | $ | %) { operator-char }
prefix-symbol ::= (! | ? | ~) { 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
label-name ::= lowercase-ident
tag-name ::= 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 ::= value-name
  | module-path .  value-name
constr ::= constr-name
  | module-path .  constr-name
typeconstr ::= typeconstr-name
  | extended-module-path .  typeconstr-name
field ::= field-name
  | module-path .  field-name
module-path ::= module-name
  | module-path .  module-name
extended-module-path ::= module-name
  | extended-module-path .  module-name
  | extended-module-path (  extended-module-path )
modtype-path ::= modtype-name
  | extended-module-path .  modtype-name
class-path ::= class-name
  | module-path .  class-name

6.4 Type expressions

typexpr ::= ' ident
  | _
  | ( typexpr )
  | [[?]label-name:]  typexpr ->  typexpr
  | typexpr  { * typexpr }+
  | typeconstr
  | typexpr  typeconstr
  | ( typexpr  { , typexpr } )  typeconstr
  | typexpr as '  ident
  | variant-type
  | < [..] >
  | < method-type  { ; method-type }  [; ..] >
  | # class-path
  | typexpr #  class-path
  | ( typexpr  { , typexpr } ) #  class-path
poly-typexpr ::= typexpr
  | { ' ident }+ .  typexpr
method-type ::= method-name :  poly-typexpr

Variant types

variant-type ::= [ [ | ] tag-spec  { | tag-spec } ]
  | [> [ tag-spec ]  { | tag-spec } ]
  | [< [ | ] tag-spec-full  { | tag-spec-full }  [ > { `tag-name }+ ] ]
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
  | []
  | ()
  | `tag-name

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 } |]

6.7 Expressions

expr ::= value-path
  | constant
  | ( expr )
  | begin expr end
  | ( expr :  typexpr )
  | expr ,  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  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 =  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 [( pattern  [: typexpr] )]  { class-field } end
  | expr #  method-name
  | inst-var-name
  | inst-var-name <-  expr
  | ( expr :>  typexpr )
  | ( expr :  typexpr :>  typexpr )
  | {< inst-var-name =  expr  { ; inst-var-name =  expr } >}
  | assert expr
  | lazy 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] =  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] )

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
type-representation ::= = constr-decl  { | constr-decl }
  | = { field-decl  { ; field-decl } }
 
     Extensions for private types:
  | = private constr-decl  { | constr-decl }
  | = private { field-decl  { ; field-decl } }
 
type-params ::= type-param
  | ( type-param  { , type-param } )
type-param ::= ' ident
  | + ' ident
  | - ' ident
constr-decl ::= constr-name
  | constr-name of  typexpr
field-decl ::= field-name :  poly-typexpr
  | mutable field-name :  poly-typexpr
type-constraint ::= constraint ' ident =  typexpr

Exception definitions

exception-definition ::= exception constr-name  [of typexpr]
  | exception constr-name =  constr

6.9 Classes

6.9.1 Cass types

class-type ::=  
  | class-body-type
  | [[?]label-name:]  typexpr ->  class-type
class-body-type ::= object [( typexpr )]  {class-field-spec} end
  | class-path
  | [ typexpr  {, typexpr} ]  class-path
class-field-spec ::= inherit class-type
  | val [mutable] inst-var-name :  typexpr
  | method [private] method-name :  poly-typexpr
  | method [private] virtual 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 [( pattern  [: typexpr] )]  { class-field } end
class-field ::= inherit class-expr  [as value-name]
  | val [mutable] inst-var-name  [: typexpr] =  expr
  | method [private] method-name  {parameter}  [: typexpr] =  expr
  | method [private] method-name :  poly-typexpr =  expr
  | method [private] virtual method-name :  poly-typexpr
  | constraint typexpr =  typexpr
  | initializer expr

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 specification

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 )
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 }
 
mod-constraint ::= type [type-parameters]  typeconstr =  typexpr
  | module module-path =  extended-module-path

6.11 Module expressions (module implementations)

module-expr ::= module-path
  | struct { definition  [;;] } end
  | functor ( module-name :  module-type ) ->  module-expr
  | module-expr (  module-expr )
  | ( module-expr )
  | ( module-expr :  module-type )
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 }

6.12 Compilation units

unit-interface ::= { specification  [;;] }
unit-implementation ::= { definition  [;;] }


9. The toplevel system (ocaml)

toplevel-input ::= { toplevel-phrase } ;;
toplevel-phrase ::= toplevel-definition
  | expr
  | # ident  directive-argument
toplevel-definition ::= let [rec] let-binding  { and let-binding }
  | external value-name :  typexpr =  external-declaration
  | type-definition
  | exception-definition
  | module module-name  [ : module-type ] =  module-expr
  | module type modtype-name =  module-type
  | open module-path
directive-argument ::= nothing
  | string-literal
  | integer-literal
  | value-path


16. The debugger (ocamldebug)

16.7 Examining variable values

debug-expr ::= lowercase-ident
  | { capitalized-ident . }  lowercase-ident
  | *
  | $ integer
  | debug-expr .  lowercase-ident
  | debug-expr .(  integer )
  | debug-expr .[  integer ]
  | ! debug-expr
  | ( debug-expr )


Ocaml Grammar Meta Symbol Chart

There are 87 grammar meta symbols in the whole documentation.

meta symbolusage count
expr79
typexpr52
pattern26
module-type20
module-name16
label-name13
ident12
module-path10
value-name10
module-expr10
inst-var-name9
class-path8
let-binding8
field8
method-name7
poly-typexpr7
class-expr7
constr-name6
modtype-name6
extended-module-path6
tag-name6
debug-expr5
class-type5
parameter5
class-name5
integer-literal5
constr-decl5
typeconstr-name4
type-parameters4
tag-spec4
letter4
field-decl4
field-name4
typeconstr4
constr4
specification3
definition3
type-definition3
type-param3
pattern-matching3
class-body-type2
typedef2
method-type2
argument2
infix-op2
class-field2
classtype-definition2
constant2
tag-spec-full2
value-path2
classtype-def2
string-character2
class-binding2
escape-sequence2
exception-definition2
type-representation2
mod-constraint2
class-spec2
operator-char2
prefix-symbol2
string-literal2
type-information1
directive-argument1
variant-type1
class-definition1
char-literal1
class-specification1
type-params1
class-field-spec1
infix-symbol1
float-literal1
toplevel-phrase1
modtype-path1
type-equation1
multiple-matching1
type-constraint1
operator-name1
toplevel-definition1
unit-implementation0
int32-literal0
nativeint-literal0
optlabel0
unit-interface0
linenum-directive0
label0
toplevel-input0
int64-literal0


Previous Up