Question

I've tried to read SLS, but it has some strange BNF-like notation. Can any one clarify this notation. For example the Types chapter has the following:

Type                 ::= FunctionArgTypes ‘=>’ Type
                       | InfixType [ExistentialClause]
FunctionArgTypes     ::= InfixType
                       | ‘(’ [ ParamType {‘,’ ParamType } ] ‘)’
ExistentialClause    ::= ‘forSome’ ‘{’ ExistentialDcl {semi ExistentialDcl} ‘}’ 
ExistentialDcl       ::= ‘type’ TypeDcl
                       | ‘val’ ValDcl
InfixType            ::= CompoundType {id [nl] CompoundType}
CompoundType         ::= AnnotType {‘with’ AnnotType} [Refinement]
                       | Refinement
AnnotType            ::= SimpleType {Annotation} 
SimpleType           ::= SimpleType TypeArgs
                       | SimpleType ‘#’ id | StableId
                       | Path ‘.’ ‘type’
                       | ‘(’ Types ’)’
TypeArgs             ::= ‘[’ Types ‘]’ 
Types                ::= Type {‘,’ Type}

Symbols like ::= and | are clear to me, but what the difference between [] and {}. Also i couldn't find description for things like id, [nl], Refinment, Type.

Was it helpful?

Solution

You are right, the notation used in SLS is called EBNF - Extended Backus–Naur Form. It was developed by Niklaus Wirth, the creator of Pascal, and if i'm not mistaken, he was a supervisor of prof. Odersky in his Ph. D research. All Scala syntax is described in the end of SLS (page 159), there you can find Type, Refinment, nl and other things used in Scala.

As for EBNF it self, here the complete table of it's syntax:

Usage            Notation
definition       =
concatenation    ,
termination      ;
alternation      |
option           [ ... ]
repetition       { ... }
grouping         ( ... )
terminal string  " ... "
terminal string  ' ... '
comment          (* ... *)
special sequence ? ... ?
exception        -

The notation in SLS is slightly modified, i.e ::= is used instead of a simple = and space used for concatenation instead of ,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top