ANTLR 和 JavaCC 哪个更好?
关注点是文档/可学习性、Eclipse 集成、工具、社区支持和性能(大致按此顺序)。
Concerns are documentation/learnability, eclipse integration, tooling, community support and performance (in roughly that order).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您不应排除以下几种替代方案:
总的来说,我的印象是代码生成器的时代已经结束了。 如果我是你,我会使用 Scala 的解析器组合器工具包。 基本上,任何支持 Scala 的 IDE 也“支持”这个解析器组合器框架。 性能很好,AFAICT。
顺便说一句,ANTLR 作为 Eclipse 插件有相当不错的 IDE 支持(但也许 IntelliJ 中也有一些东西 - 我不记得了。)所以,如果您选择在外部定义词法分析器和解析器的经典方法你的语言,那么 ANTLR 应该是你的选择,我想。 它在 Java 开发人员中拥有最大的关注度,有工具支持,并且有 ANTLR 作者的一本很棒的书。 我认为其他任何工具包都无法做到这一点。
There are a couple of alternatives you shouldn't rule out:
In general, I get the impression that the years of the code generators are over. If I would be you, I would use Scala's parser combinator toolkit. Basically, any IDE supporting Scala, also 'supports' this parser combinator framework. Performance is good, AFAICT.
By the way, ANTLR has quite decent IDE support, as an Eclipse plugin (but perhaps there's also something in IntelliJ - I don't remember.) So, if you would opt for the classic approach of defining your lexical analyzer and parser outside of your language, then ANTLR should be your choice, I think. It has the biggest mindshare among Java developers, there is tool support, and there is a great book by the author of ANTLR. I don't think any of the other toolkits can claim that.
初步估计,在实践中对您来说真正重要的是符号对于您的眼睛来说有多方便和直观。
话虽如此,我用 ANTLR 和 JavaCC 完成了项目,发现 ANTLR 对于大多数事情来说都是非常重量级的。
To a first approximation, what's really going to matter to you in practice is how convenient and intuitive the notation is to your eyes.
Having said that, I'd done projects with ANTLR and JavaCC, and found ANTLR to be awfully heavyweight for most things.
对于您提到的问题,我建议 JavaCC 是更好的选择。 对于 Java 开发人员来说,学习起来更快更容易(语法与普通 Java 极其相似),文档很全面,并且 Eclipse 集成也足够。
With respect to the concerns you mentioned, I'd suggest that JavaCC was a better choice. It's quicker and easier for a Java developer to learn (the syntax is extremely similar to ordinary Java), the documentation is comprehensive, and the Eclipse integration is adequate.
ANTLR 的功能更全面:它是一个开箱即用的编译器 - 词法分析、解析、AST、树转换和代码生成。
对于 JavaCC 来说,它更像是一个解析器生成器,而不是编译器编译器。 AST 支持是通过另一个名为 JJTree 的库提供的。
ANTLR is more fully featured: it is a much more out the box compiler compiler - lexing, parsing, AST, tree transformations and code generation.
For JavaCC, it is much more a Parser generator than a compiler compiler. AST support is provided through another lib called JJTree.
ANTLR 相对于 JavaCC 的一个具体优势是它具有除 Java 之外的其他语言的生成器。 这可能会使您的语言移植到其他地方变得更加容易。
A concrete advantage ANTLR has over JavaCC is that it has generators for languages other than Java. This might make porting your language to other places much easier.
我上面第二个是詹姆斯。
根据我个人的经验,您可以使用 ANTLR 做更多的事情,包括在规则之间传递参数以及通过所有子规则,这在制作复杂的解析器(例如 C# 的解析器)时有很大帮助。 另外,规则重写也很经典。 它可以帮助您轻松格式化您理想的 AST。
不过,它确实很重。 对于一个简单的项目,您可能永远不会使用这些功能。 Javacc 更酷。
I second jamesh above.
From my personal experience, you can do a lot more things with ANTLR, including passing parameter between rules and through all the sub-rules, which helps a lot while making a complex parser, like the parser for C#. Also, the rule re-writing is a classic too. It helps you to format your ideal AST easily.
However, it's really heavy. For a simple project, you probably will never use these functionalities. Javacc is cooler for it.
我有一段时间没有使用解析器生成器了,但几年前当我对它们感兴趣时,我记得喜欢 SableCC 最好的。 它实现了一些关于面向对象解析器生成的有趣想法,这些想法可能已被替代方案采纳,也可能未采纳。
I haven't used parser generators in a while, but several years back when I was interested in them, I remember liking SableCC best. It implemented some interesting ideas with respect to object oriented parser generation that may or may not have been picked up by the alternatives.
我用 SableCC 编写了一个 CAS 语言的编译器,例如 Maple 或 MuPAD 来转换它
Maxima(用于 CAS-Capacity)和 LaTeX(用于显示)的单一语言。 SableCC 的 AST 是
严格面向对象,并且很容易扩展它以生成不同的语言。 如果你
想要将一种语言编译成多种其他语言,请尝试一下。
I wrote a compiler for CAS-language like Maple or MuPAD with SableCC to convert this
single language to Maxima (for CAS-Capacity) and LaTeX (to display). SableCC's AST is
strict object-oriented and it's easy to extends it to generate diffence languages. If you
want to compile a language into more than one others languages, just give it a try.