浏览模式: 普通 | 列表
1

从coco/r 迁移到 antlr 心得体会

[ 2007-03-28 21:18:47 | 作者: progame ]
终于将sql broker从coco/r迁移到antlr了,并且把单元测试都跑得通过了,总结下来,转换工作量不是太大,我加起来用了2个工作日左右,语法文件大小从原来的1200多行到现在的1500多行,首先看一个例子的不同语法定义

coco/r :
InsertValue<out InsertValue node>  =
    (. node = new InsertValue(); .)
    ("DEFAULT" (. ExceptLevel(2); node.usedefault = true; .)
    | Expression<out node.expression> )
.

antlr:
insertvalue
  returns [InsertValue node]
  { node = new InsertValue(); }
  :
  (DEFAULT { ExceptLevel(2); node.usedefault = true; }
  | {Expression exp = null;} exp = expression {node.expression = exp;} )
;

一些区别:
1、antlr中keyword必须定义在lexer中,而且不能发生LL冲突,否则需要放入tokens节,这点coco/r要方便很多
2、因为antlr的[]是参数定义,所以coco/r中的表示zero or one的 '[]'就不能用了,必须'()?'
3、{}在antlr中为代码段,等同coco/r的(..),所以不能再用它表示zero or more,而应该使用'()*'
4、coco/r 可以用t.val得到最后一个token的value,而antlr中可以使用LT(0),依此类推,还可以使用LT(1), LT(-1)...,另外就是可通过定义符号来取值
5、antlr的buildast应该比较有用,我可以通过它构建初步的AST,再根据这个AST生成我自己的AST,但因为原来coco/r是直接通过嵌入代码构造好了最终的AST,所以就没有用antlr的这个功能,况且要重复在TreeParser中定义一次类似的语法也挺烦人的
1