The specification of the syntax of Standard ML in the Definition of Standard ML (Revised) is spread over multiple chapters, which makes it hard to understand. Furthermore, the grammar allows many non-sensical forms that are of little use to programmers.

The grammar given below collects together all of the various forms into a single collected syntax. Furthermore, the grammar has been refined to make it more regular and to rule out some of the more ridiculous terms allowed by the Definition. Thus, the language defined by this grammar is a subset of that given in the Definition, but it should be more useful to the SML programmer.

The Collected Syntax of Standard ML

file

::=

progdec+

 

progdec

::=

topdec

|

exp ;

 

topdec

::=

sigdec ;?

|

strdec ;?

|

fundec ;?

 

sigdec

::=

signature sigbind ( and sigbind )*

 

sigbind

::=

sigid = sigexp

 

sigexp

::=

sig specs end

|

sigid

|

sigexp wheretype

 

wheretype

::=

where type wheretypespec ( and wheretypespec )*

 

wheretypespec

::=

tyvarseq tycon = ty

 

tyvarseq

::=

empty

|

tyvar

|

( tyvar ( , tyvar )* )

 

spec

::=

val valdesc ( and valdesc )*

|

type typdesc ( and typdesc )*

|

eqtype typdesc ( and typdesc )*

|

datatype datdesc ( and datdesc )*

|

datatype tycon = datatype longtycon

|

exception exdesc ( and exdesc )*

|

structure strdesc ( and strdesc )*

|

include sigexp

|

include sigid+

|

spec sharingspec

 

sharingspec

::=

sharing type longtycon ( = longtycon )+

|

sharing longstrid ( = longstrid )+

 

specs

::=

( spec ;? )*

 

valdesc

::=

vid : ty

 

typdesc

::=

tyvarseq tycon

 

datdesc

::=

tyvarseq tycon = condesc ( | condesc )*

 

condesc

::=

conid ( of ty )?

 

exdesc

::=

conid ( of ty )?

 

strdesc

::=

strid : sigexp

 

strdec

::=

dec

|

structure strbind ( and strbind )*

|

local strdecs in strdecs end

 

strdecs

::=

( strdec ; )*

 

strbind

::=

strid sigconstraint? = strexp

 

sigconstraint

::=

: sigexp

|

:> sigexp

 

strexp

::=

struct strdecs end

|

longstrid

|

strexp sigconstraint

|

funid ( functorarg )

|

let strdecs in strexp end

 

functorarg

::=

strdecs

|

strexp

 

fundec

::=

functor funbind ( and funbind )*

 

funbind

::=

funid ( functorparam ) sigconstraint? = strexp

 

functorparam

::=

strid : sigexp

|

specs

 

dec

::=

val tyvarseq valbind ( and valbind )*

|

val rec? fnbind ( and fnbind )*

|

fun tyvarseq fvalbind ( and fvalbind )*

|

type typbind ( and typbind )*

|

datatype datbind ( and datbind )* withtype?

|

datatype tycon = datatype longtycon

|

abstype datbind withtype? with decs end

|

exception exbind ( and exbind )*

|

local decs in decs end

|

open longstrid+

|

infix precedence? valid+

|

infixr precedence? valid+

|

nofix valid+

 

decs

::=

( dec ;? )*

 

valbind

::=

pat = exp

 

fnbind

::=

pat = fnexp

 

fvalbind

::=

fvalrule ( | fvalrule )*

 

fvalrule

::=

op? vid atpat+ ( : ty )? = exp

|

( atpat vid atpat ) atpat+ ( : ty )? = exp

|

( atpat vid atpat ) ( : ty )? = exp

|

atpat vid atpat ( : ty )? = exp

 

typbind

::=

tyvarseq tycon = ty

 

datbind

::=

tyvarseq tycon = conbind ( | conbind )*

 

conbind

::=

op? vid ( of ty )?

 

withtype

::=

withtype typbind ( and typbind )*

 

exbind

::=

conbind

|

vid = longvid

 

ty

::=

tuplety ( -> ty )?

 

tuplety

::=

tyapp ( * tyapp )*

 

tyapp

::=

tyarg+ longtycon

|

atty

 

tyarg

::=

atty

|

( ty ( , ty )* )

 

atty

::=

tyvar

|

longtycon

|

( ty )

|

{ tyrow? }

 

tyrow

::=

lab : ty ( , tyrow )?

 

pat

::=

op? vid ( : ty )? as pat

|

infpat : ty

 

infpat

::=

conpat ( vid conpat )*

 

conpat

::=

op? longvid atpat

|

atpat

 

atpat

::=

_

|

exactlit

|

op? longvid

|

{ patrow? }

|

( patseq? )

|

[ patseq? ]

 

patrow

::=

...

|

fieldpat ( , patrow )?

 

fieldpat

::=

vid ( : ty )? ( as pat )?

|

lab = pat

 

patseq

::=

pat ( , pat )*

 

exp

::=

fnexp

|

case exp of match

|

while exp do exp

|

if exp then exp else exp

|

raise exp

|

handleexp

 

fnexp

::=

fn match

 

handleexp

::=

orexp ( handle match )*

 

orexp

::=

andexp ( orelse andexp )*

 

andexp

::=

tyexp ( andalso tyexp )*

 

tyexp

::=

infexp ( : ty )*

 

infexp

::=

appexp ( vid appexp )*

 

appexp

::=

atexp+

 

atexp

::=

lit

|

op? longvid

|

# lab

|

{ exprow? }

|

( expseq? )

|

[ expseq? ]

|

( exp ( ; exp )+ )

|

let decs in exp ( ; exp )* end

 

exprow

::=

fieldexp ( , fieldexp )*

 

expseq

::=

exp ( , exp )*

 

match

::=

mrule ( | mrule )*

 

mrule

::=

pat => exp

 

exactlit

::=

chrlit

|

strlit

|

wordlit

|

intlit

 

lit

::=

exactlit

|

reallit