[英]ANTLR vs parboiled
What is the difference between ANTLR and parboiled for parsing in Java? ANTLR 和 parboiled 在 Java 中解析有什么区别?
Parboiled looks like a really cool tool. Parboiled 看起来是一个非常酷的工具。 It might be easier for beginners as it is just pure programming using a "parser combinator" idiom.
对于初学者来说可能更容易,因为它只是使用“解析器组合器”习语的纯编程。 I think that this would become very verbose and harder to read, though the Java grammar doesn't look too bad that I see.
我认为这会变得非常冗长且难以阅读,尽管 Java 语法在我看来并不算太糟糕。 I cannot comment on its AST construction but ANTLR 4 generates parse trees not a ASTs.
我无法评论它的 AST 构造,但 ANTLR 4 生成的解析树不是 AST。 It claims to have good error messages/recovery but that is suspect because it is based upon parser expression grammars, which can only detect errors once the entire input is been seen (worst case).
它声称具有良好的错误消息/恢复能力,但这是可疑的,因为它基于解析器表达式语法,只有在看到整个输入(最坏情况)后才能检测错误。 It also cannot identify ambiguities in your grammar (not conflicts, ambiguities).
它也无法识别语法中的歧义(不是冲突、歧义)。 Neither tool announces parsing conflicts.
这两个工具都不会宣布解析冲突。 ANTLR 4 handles direct left recursion for things like arithmetic expressions but in general neither tool can handle left recursion.
ANTLR 4 处理算术表达式之类的直接左递归,但通常这两种工具都不能处理左递归。 ANTLR requires that you use a library for its parser interpreter like parboiled but you must learn to use the tool if you want to have it generate parsers.
ANTLR 要求您为其解析器解释器使用一个库,例如 parboiled,但如果您想让它生成解析器,则必须学习使用该工具。 Currently, ANTLR 4 can generate parsers in Java, C#, JavaScript, Python 2, Python 3.
目前,ANTLR 4 可以在 Java、C#、JavaScript、Python 2、Python 3 中生成解析器。
Today, Parboiled is mainly scala-tool.今天,Parboiled 主要是 Scala 工具。 So, if you are using scala it may be better solution for most cases.
因此,如果您使用的是 Scala,它可能是大多数情况下更好的解决方案。
ANTLR should be much easier for beginners. ANTLR 对于初学者来说应该容易得多。 It's easier to start with.
开始更容易。
Parboiled is a scala library. Parboiled 是一个 Scala 库。 You will have syntax highlights and type check out of the box.
您将有语法高亮显示和类型检查开箱即用。 Parboiled1 works fine in most IDEs.
Parboiled1 在大多数 IDE 中都可以正常工作。 Parbiled2 doesn't (will be fixed soon in Idea).
Parbiled2 没有(很快会在 Idea 中修复)。 The library uses macro-expressions and the most IDEs are not comfortable with it.
该库使用宏表达式,大多数 IDE 不适应它。 That's why you will have everything red.
这就是为什么你会看到一切都是红色的。
But both are pretty easy to start with.但两者都非常容易开始。
In my opinion Parboiled is more scalable.在我看来,Parboiled 更具可扩展性。 Because you are writing scala code.
因为您正在编写 Scala 代码。 You can decompose your parser to multiple scala traits and mix them one with another.
您可以将解析器分解为多个 scala 特征,并将它们与另一个混合。 You may create DateTime parser, and mix it to LogEvent parser or $PROTOCOL_NAME parser.
您可以创建 DateTime 解析器,并将其混合到 LogEvent 解析器或 $PROTOCOL_NAME 解析器。 And reuse them easily.
并轻松重复使用它们。 For parboiled1 you can do some naughty things in runtime.
对于 parboiled1,您可以在运行时做一些顽皮的事情。 Well, it gives you power.
嗯,它给了你力量。 For some cases you can construct parsers on a fly.
在某些情况下,您可以即时构建解析器。 For example you have datetime format, defined as string.
例如,您有日期时间格式,定义为字符串。 You can read the format string and generate the appropriate parser for it.
您可以读取格式字符串并为其生成适当的解析器。 It is possible even for Parboiled2 (which does lot's of stuff during compile time).
即使对于 Parboiled2(它在编译时做了很多事情)也是可能的。 I don't know whether it's possible for ANTLR.
我不知道 ANTLR 是否可能。
I like the Parboiled approach to AST.我喜欢 AST 的 Parboiled 方法。 It expects you to define ADT .
它希望您定义ADT 。 So in ideal case you will have an immutable tree of case classes.
因此,在理想情况下,您将拥有一个不可变的案例类树。 you may add some 'dsl-like' stuff to your tree nodes.
您可以向树节点添加一些“类似 dsl”的内容。 For example you may define "\\" method to your node, which returns a child with specified name.
例如,您可以为节点定义“\\”方法,该方法返回具有指定名称的子节点。
case class Node(value: String) {
....
def \ (childName: String): Option[Node] =
this.children.find(child => child.name == childName)
}
And Then use it:然后使用它:
city \ "3rd street" \ "23"
This makes work with AST much easier.这使得使用 AST 变得更加容易。 I hope it helps.
我希望它有帮助。
Well if I have to compare as a developer who have recently used both frameworks as a newbie to parsing frameworks, then I have the below comparison.好吧,如果我必须作为最近使用这两个框架作为解析框架的新手的开发人员进行比较,那么我有以下比较。
ANTLR ![]() |
Parboiled![]() |
|
---|---|---|
1 ![]() |
It has better documentation in general, has its own website, there's a book (The Definitive ANTLR reference by Terrence Parr), have multiple examples available in git.![]() |
It has limited documentation, that is only available in git.![]() |
2 ![]() |
There are ANTLR plugins for different IDEs that allows to see the syntax diagram of rules, check parseTree for the inputs.![]() ![]() |
It does not have any plugins for IDEs.![]() |
3 ![]() |
It's a java framework, written in java.![]() |
It's a Scala library/framework and is good if we are writing the parser in Scala.![]() ![]() ![]() |
4 ![]() |
In Antlr we write the parsing rules or the grammars separately in .g4 files.![]() ![]() ![]() |
In Parboiled we have to write the parsing rules and grammar in the java itself.![]() |
5 ![]() |
In antlr we get the ParseTree (which is similar to AST) by passing the input to the generated *.java antlr classes.![]() |
In parboiled we have to use the Abstract Data Types and use the value stack to push & pop the values while writing the grammar to get the AST.![]() |
So, after using the two I find Antlr a bit easier to use and learn.所以,在使用了这两个之后,我发现 Antlr 更容易使用和学习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.