从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中定义一次类似的语法也挺烦人的

评论Feed 评论Feed: /feed.asp?q=comment&id=647

浏览模式: 显示全部 | 评论: 3 | 引用: 0 | 排序 | 浏览: 1245
引用 william
[ 2007-03-28 22:04:48 ]
看到小P开始兴奋,我也开始兴奋了。 [lol] [lol] [lol]
引用 roland*
[ 2008-09-19 17:17:43 ]
您好!我最近在做表达式的解析器,需要把字符串转换为AST,由于许可的限制,只好选择了coco/r。但是因为刚刚接触这类技术,不知如何下手,尤其是不知如何才能得到AST,可以帮忙说一下具体的做法、步骤吗?或者帮忙写个简单的小例子?或者有哪些资料、网站,介绍一下呢?
希望能够得到您的帮助,roland1125@gmail.com是我的邮箱,不胜感激!
引用 progame
[ 2008-09-19 18:54:47 ]
自己google 我现在都忘光了

发表
表情图标
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
UBB代码
转换链接
表情图标
悄悄话
用户名:   密码:   (非注册用户不需要输入密码) 注册?
验证码(不区分大小写) * 请输入验证码