一个类似 DSL 的小型 lisp,被编译成 C/C++代码 -- Antlr 是一个不错的选择吗?
创建类似 DSL 的 Lisp 语法 - 非常小的特定应用 - 但非常快 - 用 C 生成代码,Antlr 是一个不错的选择吗?
由于多种原因,它必须非常快,并且它会在内部调用很多 C++ API,因此我不能用 C/C++ 以外的语言编写它。
我最后一次在编译器中做一些事情是大约 5 年前在学校的 flex/bison 中。
然而这一次,我必须为此任务编写生产就绪的可维护代码。我研究 Antlr 有一段时间了,看起来不错。但我有以下担忧:-
- 对 C/C++ 目标的支持好吗?
- Antlr 适合构建像 DSL 这样的 Lisp 语法吗?
- 总的来说,对于这个特定的应用程序,它会比使用 flex/bison 更好吗?
另外,如果你能让我知道我应该真正擅长 Antlr 的哪些主题/部分,那就太好了。
谢谢
Creating a Lisp syntax like DSL - very small specific application - but very fast - code generation in C, Antlr a good choice?
It is necessary for many reasons that it be very fast and it will internally call a lot of C++ APIs, hence I cannot write it in a language other than C/C++.
The last time I did something in compilers was in school around 5 years ago in flex/bison.
This time however, I have to write production ready maintainable code for this task. I've been looking into Antlr for quite a while and it seems good. But I've the following concerns:-
- Is support for C/C++ target good?
- Is Antlr suitable for building a Lisp syntax like DSL?
- Overall, will it be better than using flex/bison for this particular application?
Also, if you could let me know what topics/parts of Antlr I should be really good at then it would be great.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你的语法很复杂时,ANTLR 是很好的选择。
LISP 语法在设计上并不复杂。一切几乎都是由两个语法规则处理的:
它是如此简单的词法分析器和解析器,您可以轻松地将其实现为 递归下降解析器(C或
以任何其他语言;较旧的 LISP 在 LISP 中实现了这一点,因此它们可以直接在 LISP 中实现 LISP 解释器和编译器)。
你可以使用 ANTLR 来实现这一点,但我认为它太过分了。如果您的语言实际上比您建议的更复杂,那么 ANTLR 可能是一个不错的选择。
构建一个好的 DSL 通常需要的不仅仅是解析。有关更多详细信息,请参阅我的文章“解析后的生活”(来自 Bio)。
ANTLR is good when your syntax is complex.
LISP syntax is not complex, by design. Everything is pretty much handled by two grammar rules:
Its such a simple lexer and parser that you can easily implement this as a recursive descent parser (in C or
in any other language; older LISPs implement this in LISP so they can implement LISP interpreters and compilers directly in LISP).
You can use ANTLR for this, but I think its overkill. If your language is actually more complex than you suggest, then ANTLR may be a good choice.
Building a good DSL usually requires a LOT more than just parsing. See my essay on "Life After Parsing" (via bio) for more details.