我设计的 Mazy 语言第三版,意淫了一下,把 BNF 给磨出来了。28条 plain-BNF,即使算上语法分析的额外要求也不算多。
Digits :: [0-9]+
Identifier :: [a-zA-Z]+_*'*
Comment :: ".*?"
Program ::
Sentence
Sentence \n Program
Sentence ::
FunctionDefinition
PatternMatching
FunctionDefinition ::
Identifier Pattern = Experssion
Identifier Pattern = \n Clause .
Identifier Pattern = \n Clause ; WhereBlock .
Identifier Pattern = \n Program Clause ; WhereBlock .
WhereBlock ::
PatternMatching
PatternMatching \n WhereBlock
PatternMatching ::
Pattern = Experssion
Expression ::
Number
List
Identifier
Operation
Condition
Lambda
( Expression )
Number ::
Digits
Digits.Digits
Digits_Digits
Digits.Digits_Digits
List ::
( )
( Expression , )
( Sequence )
Comprehension
Comprehension :: ( Expression => Sequence )
Sequence ::
Expression
Expression , Sequence
Operation ::
Expression Expression
Prefix Expression
Expression Midfix Expression
Expression Postfix
Condition ::
[Clause]
[Expression ; WhereBlock]
[Clause ; WhereBlock]
Clause ::
SubClause
SubClause \n Expression
SubClause ::
Expression , Expression
Expression , Expression \n SubClause
Lambda ::
\ Pattern -> Expression .
\ Pattern -> Expression ; WhereBlock .
Prefix :: oneof
+ - ~ #
Midfix :: oneof
+ - * / % ^ ** // : ++ -- = ~= < > >= <= & | ! !! $ .. <-
Postfix :: oneof
++ -- !
Pattern ::
PatternAtom
PatternList
Pattern : PatternList
PatternPreOpt Pattern
Pattern PatternMidOpt PatternAtom
PatternAtom PatternMidOpt Pattern
Pattern PatternPostOpt
PatternList ::
( )
( PatternAtom , )
( PatternSequence )
PatternSequence ::
PatternAtom
PatternAtom \n PatternSequence
PatternAtom :: oneof
Identifier Number _
PatternPreOpt :: oneof
- ~
PatternMidOpt :: oneof
+ - * / ^ ** // :
PatternPostOpt :: oneof
++ --
新语言想设计成为基于模式匹配的语言,于是进行了模式匹配完备化的努力,最后搞出了一个带有推导功能的东西,这样函数就可以只接受一个参数,同时满足了参数之间“关系”表达的要求——终于找到可以超过 Haskell 的地方了 :)
加入了 Lambda 表达式,且带有作用域的语法都有了一个 WhereBlock,但功能其实相当于 Scheme 中的 let* 而不是 Haskell 中的 where;
列表相当于 tuple 和 list 二者的结合,列表领悟也有了;
其它一些纯属使语言更“漂亮”的小改动不值一提,感兴趣的自己看吧。