OCaml Grammar

Top level symbols:

1 Lexical
ident ::=
letter ( letter | 0...9 | ' )*
lowercase-ident ::=
lowercase-letter ( letter | 0...9 | ' )*
capitalized-ident ::=
uppercase-letter ( letter | 0...9 | ' )*
letter ::=
|
uppercase-letter ::=
A...Z
lowercase-letter ::=
a...z | _
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 | _ )*
float-literal ::=
( - )? 0...9 ( 0...9 | _ )* ( . ( 0...9 | _ )* )? ( ( E | e ) ( + | - )? 0...9 ( 0...9 | _ )* )?
char-literal ::=
' regular-char '
|
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-literal ::=
string-character ::=
regular-char-str
|
label ::=
optlabel ::=
infix-symbol ::=
( = | < | > | @ | ^ | | | & | + | - | * | / | $ | % ) ( operator-char )*
prefix-symbol ::=
( ! | ? | ~ ) ( operator-char )*
operator-char ::=
! | $ | % | & | * | + | - | . | / | : | < | = | > | ? | @ | ^ | | | ~
linenum-directive ::=
# ( 0...9 )+
|
# ( 0...9 )+ " ( string-character )* "
keyword1 ::=
and | as | assert | asr | begin | class | constraint | do | done | downto | else | end | exception | external | false | for | fun | function | functor | if | in | include | inherit | initializer | land | lazy | let | lor | lsl | lsr | lxor | match | method | mod | module | mutable | new | object | of | open | or | private | rec | sig | struct | then | to | true | try | type | val | virtual | when | while | with
keyword2 ::=
!= | # | & | && | ' | ( | ) | * | + | , | - | -. | -> | . | .. | : | :: | := | :> | ; | ;; | < | <- | = | > | >] | >} | ? | ?? | [ | [< | [> | [| | ] | _ | ` | { | {< | | | |] | } | ~
Camlp4keyword ::=
parser | << | <: | >> | $ | $$ | $:
2 Names
2.1 Naming objects
value-name ::=
|
operator-name ::=
prefix-symbol | infix-op
infix-op ::=
infix-symbol | * | = | or | & | := | mod | land | lor | lxor | lsl | lsr | asr
constr-name ::=
|
false
|
true
|
[]
|
()
|
::
exception-name ::=
label-name ::=
lowercase-ident
tag-name ::=
typeconstr-name ::=
lowercase-ident
field-name ::=
lowercase-ident
module-name ::=
modtype-name ::=
ident
class-name ::=
lowercase-ident
inst-var-name ::=
lowercase-ident
method-name ::=
lowercase-ident
2.2 Referring to named objects
value-path ::=
|
constr ::=
|
typeconstr ::=
|
field ::=
|
module-path ::=
|
extended-module-path ::=
|
|
modtype-path ::=
|
class-path ::=
|
3 Type expressions
typexpr ::=
|
_
|
( typexpr )
|
( ( ? )? label-name : )? typexpr -> typexpr
|
|
|
|
|
|
< ( .. )? >
|
< method-type ( ; method-type )* ( ; .. )? >
|
|
|
( typexpr ( , typexpr )* ) # class-path
poly-typexpr ::=
|
( ' ident )+ . typexpr
method-type ::=
3.1 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 )?
|
tag-spec-full ::=
` tag-name ( of typexpr )? ( & typexpr )*
|
4 Constants
constant ::=
|
|
|
|
|
5 Patterns
pattern ::=
|
_
|
|
|
( pattern )
|
|
|
|
|
|
|
{ field = pattern ( ; field = pattern )* }
|
[ pattern ( ; pattern )* ]
|
|
[| pattern ( ; pattern )* |]
6 Expressions
expr ::=
|
|
( expr )
|
begin expr end
|
( expr : typexpr )
|
expr , expr ( , expr )*
|
|
|
|
[ expr ( ; expr )* ]
|
[| expr ( ; expr )* |]
|
{ field = expr ( ; field = expr )* }
|
{ expr with field = expr ( ; field = expr )* }
|
|
|
|
expr . field ( <- 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
|
|
|
|
|
|
let ( rec )? let-binding ( and let-binding )* in expr
|
|
|
|
( expr ( : typexpr )? :> typexpr )
|
{< ( inst-var-name = expr ( ; inst-var-name = expr )* )? >}
|
assert expr
|
lazy expr
argument ::=
|
~ label-name ( : expr )?
|
? label-name ( : expr )?
pattern-matching ::=
( | )? pattern ( when expr )? -> expr ( | pattern ( when expr )? -> expr )*
multiple-matching ::=
( parameter )+ ( when expr )? -> expr
let-binding ::=
|
parameter ::=
|
|
~ ( label-name ( : typexpr )? )
|
|
? ( label-name ( : typexpr )? ( = expr )? )
|
? label-name : ( pattern ( : typexpr )? ( = expr )? )
7 Type and exception definitions
7.1 Type definitions
type-definition ::=
type typedef ( and typedef )*
typedef ::=
type-information ::=
type-equation ::=
type-representation ::=
|
= { field-decl ( ; field-decl )* }
type-params ::=
|
type-param ::=
|
+ ' ident
|
- ' ident
constr-decl ::=
|
field-decl ::=
|
type-constraint ::=
constraint ' ident = typexpr
7.2 Exception definitions
exception-definition ::=
exception exception-name ( of typexpr )?
|
exception exception-name = constr
8 Classes
8.1 Class types
class-type ::=
|
( ( ? )? label )? typexpr -> class-type
class-body-type ::=
object ( ( typexpr ) )? ( class-field-spec )* end
|
|
class-field-spec ::=
inherit class-type
|
val ( mutable )? inst-var-name : typexpr
|
method ( private )? ( virtual )? method-name : poly-typexpr
|
constraint typexpr = typexpr
8.2 Class expressions
class-expr ::=
|
|
|
|
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
8.3 Class definitions
class-definition ::=
class-binding ::=
( virtual )? ( [ type-parameters ] )? class-name ( parameter )* ( : class-type )? = class-expr
type-parameters ::=
' ident ( , ' ident )*
8.4 Class specification
class-specification ::=
class class-spec ( and class-spec )*
class-spec ::=
( virtual )? ( [ type-parameters ] )? class-name : class-type
8.5 Class type definitions
classtype-definition ::=
class type classtype-def ( and classtype-def )*
classtype-def ::=
( virtual )? ( [ type-parameters ] )? class-name = class-body-type
9 Module types (module specifications)
module-type ::=
|
sig ( specification ( ;; )? )* end
|
|
|
specification ::=
|
external value-name : typexpr = C-function-name
|
|
exception constr-decl
|
|
|
|
module type modtype-name ( = module-type )?
|
|
include module-type
mod-constraint ::=
|
10 Module expressions (module implementations)
module-expr ::=
|
struct ( definition ( ;; )? )* end
|
|
|
definition ::=
let ( rec )? let-binding ( and let-binding )*
|
external value-name : typexpr = C-function-name
|
|
|
|
|
|
module type modtype-name = module-type
|
|
include module-expr
11 Compilation units
unit-interface ::=
( specification ( ;; )? )*
unit-implementation ::=
( definition ( ;; )? )*


Notes:
a) Compiled based on information from The Objective Caml language manual.
b) OCaml is developed at INRIA Rocquencourt, projet Cristal.

Copyright 2003 Peter Hercek, All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can receive a copy of the GNU General Public License here.

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~