用于 DSL 实施的 ANTLR 或 SableCC?
有人同时使用语言实现和 能够比较它们,指出优点并 弱点?我正在寻找一个支持以下功能的 RAD 工具 AST Walker 代码生成。 SableCC 是 LALR,因此 支持“左递归”,而 ANTLR 是 LL(*)。 这对于典型语法或 DSL 重要吗?我需要 还可以执行一些特定领域的分析。 (目标 我的编译器的语言将是 OpenCL C)。因为这将是 对于学生项目来说,重要的是我不要失败 那么多时间在乏味的方面,那就是实施 语言的前端。
Has somebody used both for Language implementation and
is able to compare them, pointing out strengths and
weaknesses? I seek a RAD tool with support for
AST Walker Code generation. SableCC is LALR and thus
supports ´Left recursion´, whereas ANTLR is LL(*).
Is this important for typical grammars or DSLs? I need
to perform some domain-specific analysis as well. (The target
language of my compiler will be OpenCL C). As this will be
for a student project it is important that I do not lose
that much time on the tedious side, that is implementing
the Front-End of the language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于 ANTLR 我不能说太多,但也许可以提供一些关于 SableCC 的信息。
设计
它生成一个解析器,使用访问者模式将生成的代码和手写的代码完全分开,并集成了从具体语法树到抽象语法树的转换。因此,设计者可以在解析器成功解析输入后获得 AST,并且可以遍历树并对相应节点进行操作。
设计者可以先编写并调试他的语法,尝试优化从具体语法树到抽象语法树的转换。当他拥有扎实的 AST 后,他可以在单独的类中编写操作代码。因此,设计者只需编写一次语法,就可以为语法编写更多类型的操作,例如一次用于语法突出显示,一次用于语义分析和代码生成器。我已经在一个生产系统中完成了它。它运作得很好。
使用 ANTLR,设计者可以通过在语法中添加操作代码来构建 AST 树,生成 AST,然后以不同的方式重用它。但生成的代码和手写的代码之间并没有明确的区别。
另一方面可能是对IDE的支持。由于使用SableCC你已经分离了代码,你可以轻松使用IDE的自动完成功能。
语法
SableCC 是一个 LR(1) 解析器生成器,因此在我看来,为 ANTLR(一个 LL(k) 解析器生成器)编写语法更容易(没有技巧)。我认为(aber 不确定)SableCC 是唯一一个 LR(1) java 解析器生成器,它非常受欢迎。
输出解析器
ANTLR可以生成多种语言的解析器,而SableCC只能生成Java(主流)的解析器。有一些插件/适配器可以生成其他语言的解析器,但是根据作者(http://www.mare.ee/indrek/sablecc/)的说法,它们太旧了。 SableCC 4 可以生成更多,但它处于测试阶段,不推荐用于严肃的项目。
开发支持
ANTLR有一个IDE来编写语法。它是 ANTLRWorks,它可以可视化语法,在源代码中导航(例如跳转到标记或产生式的定义)。 SableCC 没有这样的工具。 VIM 有原始的语法突出显示脚本,而 Netbeans 的功能插件很差。
结论
IMO我认为对于大型项目,需要长期维护的SableCC比ANTLR更合适。
Martin Fowler 有关于 SableCC 的信息,您可以在这里找到。
http://martinfowler.com/bliki/HelloSablecc.html
I cannot say much about ANTLR, but maybe some information about SableCC.
Design
It generate a parser, which generated code and hand-written code are clean separated using Visitor pattern, and integrates the transform from Concrete Syntax Tree to Abstract Syntax Tree. As a result the designer can get a AST after the parser parses successful the input, and he can walk through the tree and make action on corresponding nodes.
The designer can first write and debug his grammar, try to optimize the transform from Concrete Syntax Tree to Abstract Syntax Tree. After he has a solid AST he can write action code in separated class. So the designer write grammar only once and can write more type of action for the grammar, for example once for Syntax Highlight, once for Semantic analysis and code generator. I have done it in a productive system. It works very well.
With ANTLR the designer can construct the AST tree by adding action code in grammar t generate the AST, then reuses it for different manner. But there a not a clean separation between generated code and hand-written code.
An other aspect maybe support of IDE. Since with SableCC you have separated code, you can easy use auto-complete function of IDE.
grammar
SableCC is a LR(1) parser generator, so it is IMO easier to write grammar for ANTLR, which is a LL(k) parser generator, (without trick). I think (aber not sure) SableCC is the only one LR(1) java parser generator, which is so popular.
output parser
ANTLR can generate parser in many languages, while SableCC can only generate parser in Java (mainstream). There some plugin / adapter to generate parser in other language, however according to the author (http://www.mare.ee/indrek/sablecc/) they are too old. SableCC 4 can generate more, but it is in beta, which is not recommend for serious project.
Development Support
ANTLR hat a IDE to write grammar. It is ANTLRWorks, which can visual grammar, navigate in source (like jump to definition of token or production). SableCC hat no such tools. There are primitive Syntax Highlight script for VIM and a poor feature plugin for Netbeans.
Conclusion
IMO I think for big project, required long term maintenance SableCC is more suitable than ANTLR.
Martin Fowler has a informative about SableCC, you can find it here.
http://martinfowler.com/bliki/HelloSablecc.html