September 12, 2007

Mazy(i) plain-BNF 修订稿

这次把 Bug 基本上都修掉了,语法完备化的同时去掉了一些容易引起混淆的运算符。毕竟 Mazy means Math & Lazy, not Mess or Busy.

comment :: ;;.*(?=\n)
Number :: \d+(\.\d+)?(_[+-]?\d+)?
Identifier :: [\$]\w*[\?']?
program ::
  sentence
  sentence \n program
sentence ::
  functionDefinition
  patternMatching
functionDefinition ::
  Identifier pattern = Experssion
  Identifier pattern = \n block .
block ::
  Experssion
  program \n Experssion
  Experssion ; whereBlock
  program \n Experssion ; whereBlock
  clause
  program \n clause
  clause ; whereBlock
  program \n clause ; whereBlock
clause ::
  subClause
  subClause \n Expression
subClause ::
  Expression , Expression
  Expression , Expression \n subClause
whereBlock ::
  patternMatching
  patternMatching \n whereBlock
patternMatching :: pattern = Experssion
Expression ::
  Number
  Identifier
  Lambda
  Compound
  Operation
  Comprehension
  List
  ( Expression )
Lambda :: [ pattern -> block ]
Compound :: [ block ]
Operation ::
  Expression Expression
  prefix Expression
  Expression midfix Expression
prefix :: oneof
  + - ~ #
midfix :: oneof
  midSymbol
  % = ~= < > >= <= & | . .. : ++ <-
midSymbol :: oneof
  + - * / ^ ** //
Comprehension :: ( Expression => sequence )
List ::
  ( )
  ( Expression , )
  ( sequence )
sequence ::
  Expression , Expression
  Expression , sequence
pattern ::
  _
  Number
  Identifier
  patternOperation
  patternList
  ( pattern )
patternOperation ::
  pattern : patternList
  pattern midSymbol Number
  Number midSymbol pattern
patternList ::
  ( )
  ( pattern , )
  ( patternSequence )
patternSequence ::
  pattern , pattern
  pattern , patternSequence

最重要的变更还是新增了 block 抽象语法,没想到的时候是很痛苦的。
为语法分析增加的额外产生式也有了,这样语法和语义基本上就定下来了,下面开始写词法分析器——相当好写,输出的数据结构上用点技巧(平衡符号的开始符号指出平衡点),连带 AST 生成器一并好写。
Ray 语录:做语言要做到什么地步才叫绝?想再加1条产生式时,烦于必须再加一系列产生式;想再减1条产生式时,苦于必须再减一系列产生式——这就是所谓的“精益求精”。

2 comments:

氷の鋭 said...

最终版本已经有了,几乎没什么改变。正则表达式换成了独立于实现的版本:
comment :: ;;.*\n
Number :: [0-9]+(\.[0-9]+)?(e[+-]?[0-9]+)?
Identifier :: [a-zA-Z$][a-zA-Z0-9_]*[?']?
另外抽取出来两条抽象语法以避免混淆,对语义没有任何影响。事实上,如果知道 Mazy(s) 的语法就不会混淆了~:)
最后一个改变是:右递归全部换成左递归...

Anonymous said...

I should email my girlfriend about your post.