什么时候使用 ANTLR 这样的解析器比编写自己的解析代码更好?

发布于 2024-08-25 12:53:25 字数 200 浏览 6 评论 0原文

我需要解析一个简单的 DSL,如下所示:

funcA Type1 a (funcB Type1 b) ReturnType c

由于我没有语法解析工具的经验,我认为自己编写一个基本解析器(用 Java)会更快。

即使对于简单的 DSL,对我来说使用 ANTLR 之类的东西并构建正确的语法定义会更好吗?

I need to parse a simple DSL which looks like this:

funcA Type1 a (funcB Type1 b) ReturnType c

As I have no experience with grammar parsing tools, I thought it would be quicker to write a basic parser myself (in Java).

Would it be better, even for a simple DSL, for me to use something like ANTLR and construct a proper grammar definition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅冇旳回忆 2024-09-01 12:53:25

简单的答案:编写描述语法的规则比编写接受语法所描述的语言的代码更容易。

如果您需要解析的唯一内容看起来与上面所写的完全一样,那么我会说您可以手动编写它。

更一般地说,我想说大多数常规语言可以手动更快地解析(使用正则表达式)。

如果您正在解析具有大量规则和产生式的上下文无关语言,ANTLR(或其他解析器生成器)可以让生活变得更轻松。

另外,如果您有一种简单的语言,并且预计将来会变得更加复杂,那么将规则描述添加到 ANTLR 语法中比将它们构建到手动编码的解析器中会更容易。

Simple answer: when it is easier to write the rules describing your grammar than to write code that accepts the language described by your grammar.

If the only thing you need to parse looks exactly like what you've written above, then I would say you could just write it by hand.

More generally speaking, I would say that most regular languages could be parsed more quickly by hand (using a regular expression).

If you are parsing a context-free language with lots of rules and productions, ANTLR (or other parser generators) can make life much easier.

Also, if you have a simple language that you expect to grow more complicated in the future, it will be easier to add rule descriptions to an ANTLR grammar than to build them into a hand-coded parser.

来日方长 2024-09-01 12:53:25

语法往往会不断发展(需求也是如此)。 Home Brew 解析器很难维护,并导致重新发明轮子的例子。如果您认为可以用 java 编写一个快速解析器,那么您应该知道使用任何 lex/yacc/compiler-compiler 解决方案都会更快。词法分析器更容易编写,然后您会想要自己的规则优先语义,这不容易测试或维护。 ANTLR还提供了一个可视化AST的IDE,你能打败那个伙伴吗?额外的优点是能够使用字符串模板生成中间代码,这是完全不同的方面。

Grammars tend to evolve, (as do requirements). Home brew parsers are difficult to maintain and lead to re-inventing the wheel example. If you think you can write a quick parser in java, you should know that it would be quicker to use any of the lex/yacc/compiler-compiler solutions. Lexers are easier to write, then you would want your own rule precedence semantics which are not easy to test or maintain. ANTLR also provides an ide for visualising AST, can you beat that mate. Added advantage is the ability to generate intermediate code using string templates, which is a different aspect altogether.

桃扇骨 2024-09-01 12:53:25

当您想要开发和使用自定义语言时,最好使用现成的解析器(生成器),例如 ANTLR。当您的目标是编写解析器时,最好编写自己的解析器。

除非您有丰富的编写解析器的经验,并且可以比使用 ANTLR 更快地获得可用的解析器。但从你提出的问题来看,我推测该退出条款不适用。

It's better to use an off-the-shelf parser (generator) such as ANTLR when you want to develop and use a custom language. It's better to write your own parser when your objective is to write a parser.

UNLESS you have a lot of experience writing parsers and can get a working parser that way more quickly than using ANTLR. But I surmise from your asking the question that this get-out clause does not apply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文