我试图阅读SLS,但它有一些奇怪的类似BNF的符号。任何人都可以澄清这种表示法。例如,"类型"一章包含以下内容:
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}
像::=
和|
这样的符号对我来说很清楚,但是[]
和{}
有什么区别.我也找不到id
、[nl]
、Refinment
、Type
等内容的描述。
你是对的,SLS 中使用的符号称为 EBNF - 扩展的巴克斯-瑙尔形式。它是由Pascal的创造者Niklaus Wirth开发的,如果我没记错的话,他是Odersky教授博士研究的导师。所有 Scala 语法都在 SLS 的末尾(第 159 页)中描述,在那里你可以找到 Scala 中使用的Type
、Refinment
、nl
和其他东西。
至于 EBNF 它本身,这里是它语法的完整表:
Usage Notation
definition =
concatenation ,
termination ;
alternation |
option [ ... ]
repetition { ... }
grouping ( ... )
terminal string " ... "
terminal string ' ... '
comment (* ... *)
special sequence ? ... ?
exception -
SLS中的符号略有修改,即使用::=
代替简单的=
和用于连接的空间而不是,