|
OCaml Syntax
This document gives a complete syntax for the OCaml programming language. The syntax is based largely on information in the OCaml reference document (at [link] http://caml.inria.fr/ocaml/htmlman/). The syntax here is written in a form intended to ease comprehension. To that end the syntax rules here are presented in a different order than the order in the reference document, and some non-terminals are given different names (for example Type is used here rather than TypeExpression as used in the reference document).
The syntax is expressed in a simple, line-oriented metalanguage that is intended to be largely self-explanatory. If the metalanguage seems confusing, you may wish to consult its reference document (at [link] http://scotty.usc.edu/_DTW_/ocaml/metalanguage.html).
Although a lot of effort has been put in to writing this syntax, it still has some rough edges and may be incorrect or misleading in some places (hopefully these are minor). We would appreciate any comments or observations about correctness, completeness or readability. These comments may be sent via email to:
Tim Rentsch <tocr@shamko.com>
The syntax will be corrected and/or explanatory comments will be added as your comments come in.
Syntax Outline
The syntax below is grouped into sections. Here is a high-level outline.
- Compilation Units, Module Structure
- Classes
- Types
- Expressions and Patterns
- Names, Labels, Operators, Literals, Identifiers
- White space and Comments
Here is a section level outline.
- Top Level
- Specifications
- Definitions
- Extension - Recursive Modules
- Class Specifications
- Class Type Definitions
- Class Definitions
- Type Definitions
- Types
- Variant Types
- Let Bindings, Parameters
- Expressions
- Single and Multiple Pattern Matches
- Patterns
- External Declarations
- Compound Names
- Nonterminals and lexical units related to LabelName
- Simple Names
- Operators
- Literals
- Identifiers
- Lexical Units
The general order of progression is "larger units first", or a mostly top-down ordering. This ordering is also observed within individual rules; in Expression, for example, more loosely binding forms (which produce larger units) are listed earlier, and more tightly binding forms (which produce smaller units) are listed later.
There is a minor heading for each of the sections listed above, with most having a citation (sometimes more than one) that looks like this: (section 6.12, page 130). These citations refer to portions of the reference document, and are given for convenience.
Last revised (again) January 10, 2005
OCaml Syntax
Top Level (section 6.12, page 130)
CompilationUnit ::=
InterfaceCompilationUnit
ImplementationCompilationUnit
InterfaceCompilationUnit ::=
Specification[;;]*
ImplementationCompilationUnit ::=
Definition[;;]*
Specifications (section 6.10, page 123)
Specification ::=
open ModulePath
include ModuleType
module type ModuleTypeName = ModuleType
module type ModuleTypeName
module ModuleName FunctorParameter* : ModuleType
class type ClassTypeDefinitions
class ClassSpecifications
type TypeDefinitions
exception ConstructorDeclarator
external ValueName : Type = ExternalDeclaration
val ValueName : Type
ModuleType ::=
ModuleType with ModuleConstraints
( ModuleType )
functor FunctorParameter -> ModuleType
sig Specification[;;]* end
ModuleTypePath
ModuleConstraints ::=
ModuleConstraints and ModuleConstraint
ModuleConstraint
ModuleConstraint ::=
type TypeParameter(s) TypeConstructorPath = Type
module ModulePath = ExtendedModulePath
FunctorParameter ::=
( ModuleName : ModuleType )
Definitions (section 6.11, page 127)
Definition ::=
open ModulePath
include Module
external ValueName : Type = ExternalDeclaration
module type ModuleTypeName = ModuleType
module ModuleName FunctorParameter* : ModuleType = Module
module ModuleName FunctorParameter* = Module
class type ClassTypeDefinitions
class ClassDefinitions
type TypeDefinitions
exception ExceptionDefinition
let [rec] LetBindings
Module ::=
Module ( Module )
( Module : ModuleType )
( Module )
functor FunctorParameter -> Module
struct Definition[;;]* end
ModulePath
ExceptionDefinition ::=
ExceptionName of Type
ExceptionName = ConstructorPath
ExceptionName
Extension - Recursive Modules (section 7.9, page 134)
Specification ...::=
module rec ModuleSpecifications
ModuleSpecifications ::=
ModuleSpecifications and ModuleSpecification
ModuleSpecification
ModuleSpecification ::=
ModuleName : ModuleType
Definition ...::=
module rec ModuleDefinitions
ModuleDefinitions ::=
ModuleDefinitions and ModuleDefinition
ModuleDefinition
ModuleDefinition ::=
ModuleName : ModuleType = Module
Class Specifications (section 6.9.4, page 122)
ClassSpecifications ::=
ClassSpecifications and ClassSpecification
ClassSpecification
ClassSpecification ::=
ParameterizedClassName = ClassType
ParameterizedClassName ::=
[virtual] ClassTypeParameter(s) ClassName
ClassTypeParameter(s) ::=
[ 'Identifier,... ]
Empty
Class Type Definitions (section 6.9.6, page 123; section 6.9.1, page 117)
ClassTypeDefinitions ::=
ClassTypeDefinitions and ClassTypeDefinition
ClassTypeDefinition
ClassTypeDefinition ::=
ParameterizedClassName = ClassBodyType
ClassType ::=
[[?]LabelName:] Type -> ClassType
ClassBodyType
ClassBodyType ::=
[ Type,... ] ClassPath
object ( Type ) ClassFieldSpecification* end
object ClassFieldSpecification* end
ClassPath
ClassFieldSpecification ::=
inherit ClassType
constraint Type = Type
method [private] [virtual] MethodName : MethodType
val [mutable] InstanceVariableName : Type
Class Definitions (section 6.9.3, page 122; section 6.9.2, page 119)
ClassDefinitions ::=
ClassDefinitions and ClassDefinition
ClassDefinition
ClassDefinition ::=
ParameterizedClassName Parameter* [:ClassType] = Class
Class ::=
Class Argument+
( Class : ClassType )
( Class )
fun Parameter+ -> Class
let [rec] LetBindings in Class
object Pattern [:Type] ClassField* end
object ClassField* end
[ Type,... ] ClassPath
ClassPath
ClassField ::=
inherit Class as ValueName
inherit Class
constraint Type = Type
method [private] virtual MethodName : MethodType
method [private] MethodName : MethodType = Expression
method [private] MethodName Parameter+ [:Type] = Expression
method [private] MethodName = Expression
val [mutable] InstanceVariableName [:Type] = Expression
initializer Expression
Type Definitions (section 6.8, page 115)
TypeDefinitions ::=
TypeDefinitions and TypeDefinition
TypeDefinition
TypeDefinition ::=
TypeParameter(s) TypeConstructorName [TypeInformation]
TypeParameter(s) ::=
( TypeParameter,... )
TypeParameter
Empty
TypeParameter ::=
+ 'Identifier
- 'Identifier
'Identifier
TypeInformation ::=
[TypeEquation] [TypeRepresentation] TypeConstraint*
TypeEquation ::=
= Type
TypeRepresentation ::=
= { FieldDeclarator;... }
= ConstructorDeclarator|...
FieldDeclarator ::=
[mutable] FieldName : MethodType
ConstructorDeclarator ::=
ConstructorName of Type
ConstructorName
TypeConstraint ::=
constraint 'Identifier = Type
Types (section 6.4, page 98)
Type ::=
Type as 'Identifier
Type(s) TypeConstructorPath
[ VariantType ]
<[..]>
< MethodNameAndType;... [;..] >
[[?]LabelName:] Type -> Type
TupleType
Type(s) # ClassPath
( Type )
'Identifier
_
Type(s) ::=
( Type,... )
Type
Empty
MethodNameAndType ::=
MethodName : MethodType
MethodType ::=
'Identifier+ . Type
Type
TupleType ::=
TupleType * Type
Type * Type
Variant types (section 6.4, page 99)
VariantType ::=
< [|] FullTagSpecification|... > `TagName+
< [|] FullTagSpecification|...
[|] TagSpecification|...
> TagSpecification|...
>
FullTagSpecification ::=
`TagName of Type & Type&...
`TagName & Type&...
TagSpecification
TagSpecification ::=
`TagName of Type
`TagName
Type
Let Bindings, Parameters (section 6.7, page 106)
LetBindings ::=
LetBindings and LetBinding
LetBinding
LetBinding ::=
Pattern = Expression
ValueName Parameter* [:Type] = Expression
Parameter ::=
? ( LabelName [:Type] [=Expression] )
~ ( LabelName [:Type] )
?LabelName: ( Pattern [:Type] [=Expression] )
?LabelName: Pattern
~LabelName: Pattern
?LabelName
~LabelName
Pattern
Expressions (section 6.7, page 105)
Expression ::=
let [rec] LetBindings in Expression
function PatternMatching
fun MultipleParameterMatching
match Expression with PatternMatching
try Expression with PatternMatching
begin Expression end
while Expression do Expression done
for Identifier = Expression to Expression do Expression done
for Identifier = Expression downto Expression do Expression done
Expression ; Expression
if Expression then Expression else Expression
if Expression then Expression
AssignableExpression <- Expression
InstanceVariableName <- Expression
Expression := Expression
Expression , Expression,...
Expression DisjunctiveInfixOperator Expression
Expression ConjunctiveInfixOperator Expression
Expression RelationalOrOtherInfixOperator Expression
Expression (@|^)FamilyOperator Expression
Expression :: Expression
Expression (+|-)FamilyOperator Expression
Expression (*|/|%)FamilyOperator Expression
Expression (**)FamilyOperator Expression
Expression # MethodName
(-|-.) Expression
assert Expression
lazy Expression
Expression Argument+
ConstructorPath Expression
`TagName Expression
PrefixSymbol Expression
AssignableExpression
{< InstanceVariableValueDefinition;... >}
{ Expression with FieldValueDefinition;... }
{ FieldValueDefinition;... }
[| Expression;... |]
[ Expression;... ]
( Expression [:Type] :> Type )
( Expression [:Type] )
new ClassPath
InstanceVariableName
ValuePath
Constant
AssignableExpression ::=
Expression .[ Expression ]
Expression .( Expression )
Expression . FieldPath
FieldValueDefinition ::=
FieldPath = Expression
InstanceVariableValueDefinition ::=
InstanceVariableName = Expression
Argument ::=
?LabelName: Expression
~LabelName: Expression
?LabelName
~LabelName
Expression
Single and Multiple Pattern Matchings (section 6.7, page 106)
PatternMatching ::=
[|] SingleExpressionMatching|...
SingleExpressionMatching ::=
Pattern [MatchPredicate] -> Expression
MultipleParameterMatching ::=
Parameter+ [MatchPredicate] -> Expression
MatchPredicate ::=
when Expression
Patterns (section 6.6, page 101)
Pattern ::=
Pattern as ValueName
Pattern | Pattern
Pattern , Pattern,...
ConstructorPath Pattern
`TagName Pattern
# TypeConstructorName
{ NamedFieldPattern;... }
[| Pattern;... |]
[ Pattern;... ]
Pattern :: Pattern
( Pattern [:Type] )
ValueName
_
Constant
NamedFieldPattern ::=
FieldPath = Pattern
Constant ::=
IntegerLiteral
FloatingPointLiteral
CharacterLiteral
StringLiteral
ConstructorPath
`TagName
External Declarations (section 18.1, page ???)
ExternalDeclaration ::=
OutsideSymbolReference+
OutsideSymbolReference+ ::=
OutsideSymbolReference+ OutsideSymbolReference
OutsideSymbolReference
OutsideSymbolReference ::=
" C_LanguageIdentifier "
C_LanguageIdentifier ::=
C_LanguageIdentifier Letter
C_LanguageIdentifier Digit
C_LanguageIdentifier _
Letter
_
Compound Names (section 6.3, page 97)
ModuleTypePath ::=
ExtendedModulePath . ModuleTypeName
ModuleTypeName
TypeConstructorPath ::=
ExtendedModulePath . TypeConstructorName
TypeConstructorName
ExtendedModulePath ::=
ExtendedModulePath ( ExtendedModulePath )
ExtendedModulePath . ModuleName
ModuleName
ConstructorPath ::=
ModulePath . CapitalizedIdentifier
ConstructorName
ClassPath ::=
ModulePath . ClassName
ClassName
FieldPath ::=
ModulePath . FieldName
FieldName
ValuePath ::=
ModulePath . ValueName
ValueName
ModulePath ::=
ModulePath . ModuleName
ModuleName
`TagName ::=
` TagName
Nonterminals and lexical units related to LabelName
[?]LabelName: ::=
?LabelName:
LabelName :
?LabelName: ::=
?LabelName :
~LabelName: ::=
~LabelName :
?LabelName ::=
? LabelName
~LabelName ::=
~ LabelName
Simple Names (section 6.3, page 96)
ValueName ::=
LowercaseIdentifier
( OperatorName )
OperatorName ::=
PrefixSymbol
InfixOperator
ConstructorName ::=
CapitalizedIdentifier
false
true
[| |]
[ ]
( )
::
ExceptionName ::=
CapitalizedIdentifier
LabelName ::=
LowercaseIdentifier
TagName ::=
CapitalizedIdentifier
TypeConstructorName ::=
LowercaseIdentifier
FieldName ::=
LowercaseIdentifier
ModuleName ::=
CapitalizedIdentifier
ModuleTypeName ::=
Identifier
ClassName ::=
LowercaseIdentifier
InstanceVariableName ::=
LowercaseIdentifier
MethodName ::=
LowercaseIdentifier
Operators (section 6.3, page 96; section 6.1, page 92)
InfixOperator ::=
DisjunctiveInfixOperator
ConjunctiveInfixOperator
RelationalOrOtherInfixOperator
(@|^)FamilyOperator
(+|-)FamilyOperator
(*|/|%)FamilyOperator
(**)FamilyOperator
DisjunctiveInfixOperator ::=
||
or
ConjunctiveInfixOperator ::=
&&
&
RelationalOrOtherInfixOperator ::=
BitwiseInfixOperator
OtherInfixSymbol
BitwiseInfixOperator ::=
land
lor
lxor
lsl
lsr
asr
(@|^)FamilyOperator ::=
@ OperatorCharacter*
^ OperatorCharacter*
(+|-)FamilyOperator ::=
+ OperatorCharacter*
- OperatorCharacter*
(*|/|%)FamilyOperator ::=
* OperatorCharacter*
/ OperatorCharacter*
% OperatorCharacter*
mod
(**)FamilyOperator ::=
* * OperatorCharacter*
(-|-.) ::=
-.
-
OtherInfixSymbol ::=
OtherInfixSymbolLeadCharacter OperatorCharacter*
OtherInfixSymbolLeadCharacter ::= { =<>|&$ }
PrefixSymbol ::=
(!|?|~) OperatorCharacter*
(!|?|~) ::= { !?~ }
OperatorCharacter ::= { !$%&*+-./:<=>?@^|~ }
Literals (section 6.1, pages 90-91)
StringLiteral ::=
" CharacterRepresentative* "
CharacterLiteral ::=
' CharacterRepresentative '
CharacterRepresentative ::=
CharacterEscapeSequence
RegularCharacter
CharacterEscapeSequence ::=
\ x HexadecimalDigit HexadecimalDigit
\ Digit Digit Digit
\ \
\ "
\ '
\ n
\ t
\ b
\ r
RegularCharacter ::=
Letter
Digit
RegularPunctuator
?_any_other_regular_characters_?
RegularPunctuator ::= { ,./;[]-=`<>?:{}!@#$%^&*()_+|~ }
FloatingPointLiteral ::=
[-] UnprefixedFloatingPointLiteral
UnprefixedFloatingPointLiteral ::=
DecimalLiteral FractionalPart ExponentPart
DecimalLiteral FractionalPart
DecimalLiteral ExponentPart
FractionalPart ::=
FractionalPart Digit
FractionalPart _
.
ExponentPart ::=
ExponentLetter + DecimalLiteral
ExponentLetter - DecimalLiteral
ExponentLetter DecimalLiteral
ExponentLetter ::= { eE }
IntegerLiteral ::=
[-] UnprefixedIntegerLiteral
UnprefixedIntegerLiteral ::=
DecimalLiteral
HexadecimalLiteral
OctalLiteral
BinaryLiteral
DecimalLiteral ::=
DecimalLiteral Digit
DecimalLiteral _
Digit
HexadecimalLiteral ::=
HexadecimalLiteral HexadecimalDigit
HexadecimalLiteral _
0x HexadecimalDigit
0X HexadecimalDigit
OctalLiteral ::=
OctalLiteral OctalDigit
OctalLiteral _
0o OctalDigit
0O OctalDigit
BinaryLiteral ::=
BinaryLiteral BinaryDigit
BinaryLiteral _
0b BinaryDigit
0B BinaryDigit
Digit ::=
DecimalDigit
HexadecimalDigit ::= { 0123456789abcdefABCDEF }
DecimalDigit ::= { 0123456789 }
OctalDigit ::= { 01234567 }
BinaryDigit ::= { 01 }
Identifiers
'Identifier ::=
' Identifier
Identifier ::=
CapitalizedIdentifier
LowercaseIdentifier
UnderscoreIdentifier
CapitalizedIdentifier ::=
UppercaseLetter IdentifierCharacter*
LowercaseIdentifier ::=
LowercaseLetter IdentifierCharacter*
UnderscoreIdentifier ::=
_ IdentifierCharacter*
IdentifierCharacter ::=
Letter
Digit
_
'
Letter ::=
UppercaseLetter
LowercaseLetter
UppercaseLetter ::= { ABCDEFGHIJKLMNOPQRSTUVWXYZ }
LowercaseLetter ::= { abcdefghijklmnopqrstuvwxyz }
Lexical Units
Lexical_units ...::=
InfixOperator
PrefixSymbol
?LabelName:
~LabelName:
?LabelName
~LabelName
StringLiteral
CharacterLiteral
FloatingPointLiteral
IntegerLiteral
CapitalizedIdentifier
LowercaseIdentifier
UnderscoreIdentifier
The ... before the ::= means there are other things that also are 'Lexical_units'.
Conventional Rules
The "conventional rules" are grouped by type.
Concatenation
Specification[;;] ::=
Specification [;;]
Definition[;;] ::=
Definition [;;]
Zero-or-more repetition
Specification[;;]* ::=
Specification[;;]+
Empty
Definition[;;]* ::=
Definition[;;]+
Empty
FunctorParameter* ::=
FunctorParameter+
Empty
ClassField* ::=
ClassField+
Empty
ClassFieldSpecification* ::=
ClassFieldSpecification+
Empty
Parameter* ::=
Parameter+
Empty
TypeConstraint* ::=
TypeConstraint+
Empty
CharacterRepresentative* ::=
CharacterRepresentative+
Empty
IdentifierCharacter* ::=
IdentifierCharacter+
Empty
OperatorCharacter* ::=
OperatorCharacter+
Empty
One-or-more repetition
Specification[;;]+ ::=
Specification[;;]+ Specification[;;]
Specification[;;]
Definition[;;]+ ::=
Definition[;;]+ Definition[;;]
Definition[;;]
FunctorParameter+ ::=
FunctorParameter+ FunctorParameter
FunctorParameter
ClassField+ ::=
ClassField+ ClassField
ClassField
ClassFieldSpecification+ ::=
ClassFieldSpecification+ ClassFieldSpecification
ClassFieldSpecification
Parameter+ ::=
Parameter+ Parameter
Parameter
Argument+ ::=
Argument+ Argument
Argument
TypeConstraint+ ::=
TypeConstraint+ TypeConstraint
TypeConstraint
'Identifier+ ::=
'Identifier+ 'Identifier
'Identifier
CharacterRepresentative+ ::=
CharacterRepresentative+ CharacterRepresentative
IdentifierCharacter
IdentifierCharacter+ ::=
IdentifierCharacter+ IdentifierCharacter
IdentifierCharacter
`TagName+ ::=
`TagName+ `TagName
`TagName
OperatorCharacter+ ::=
OperatorCharacter+ OperatorCharacter
OperatorCharacter
Terminal-symbol-separated sequences
'Identifier,... ::=
'Identifier,... , 'Identifier
'Identifier
Expression,... ::=
Expression,... , Expression
Expression
Expression;... ::=
Expression;... ; Expression
Expression
FieldValueDefinition;... ::=
FieldValueDefinition;... ; FieldValueDefinition
FieldValueDefinition
InstanceVariableValueDefinition;... ::=
InstanceVariableValueDefinition;... ; InstanceVariableValueDefinition
InstanceVariableValueDefinition
SingleExpressionMatching|... ::=
SingleExpressionMatching|... | SingleExpressionMatching
SingleExpressionMatching
Pattern,... ::=
Pattern,... , Pattern
Pattern
Pattern;... ::=
Pattern;... ; Pattern
Pattern
NamedFieldPattern;... ::=
NamedFieldPattern;... ; NamedFieldPattern
NamedFieldPattern
TypeParameter,... ::=
TypeParameter,... , TypeParameter
TypeParameter
ConstructorDeclarator|... ::=
ConstructorDeclarator|... | ConstructorDeclarator
ConstructorDeclarator
FieldDeclarator;... ::=
FieldDeclarator;... ; FieldDeclarator
FieldDeclarator
Type,... ::=
Type,... , Type
Type
MethodNameAndType;... ::=
MethodNameAndType;... ; MethodNameAndType
MethodNameAndType
FullTagSpecification|... ::=
FullTagSpecification|... | FullTagSpecification
FullTagSpecification
TagSpecification|... ::=
TagSpecification|... | TagSpecification
TagSpecification
Type&... ::=
Type&... & Type
Type
Optional items
[[?]LabelName:] ::=
[?]LabelName:
Empty
[:ClassType] ::=
: ClassType
Empty
[:Type] ::=
: Type
Empty
[=Expression] ::=
= Expression
Empty
[MatchPredicate] ::=
MatchPredicate
Empty
[TypeInformation] ::=
TypeInformation
Empty
[TypeEquation] ::=
TypeEquation
Empty
[TypeRepresentation] ::=
TypeRepresentation
Empty
[mutable] ::=
mutable
Empty
[private] ::=
private
Empty
[rec] ::=
rec
Empty
[virtual] ::=
virtual
Empty
[;;] ::=
; ;
Empty
[;..] ::=
; ..
Empty
[|] ::=
|
Empty
[-] ::=
-
Empty
Empty
Empty ::=
The rule for the Empty non-terminal having no alternatives means that "nothing" is an acceptable input for generating this non-terminal.
Reference Document Syntax Remaining
The rules here are not part of the grammar but included to show parts of the reference document that seem redundant or erroneous.
Section 6.1, page 91 (implied by the text)
LexicalUnits ...::=
Label
OptionalLabel
The following rules appear to be obsolete, they were the result of a mistake in the grammar of the reference document.
Label ::=
~ LowercaseIdentifier :
OptionalLabel ::=
? LowercaseIdentifier :
Anything else?
Last revised January 8, 2005
Last revised January 10, 2005
|