Scala parser combinators vs ANTLR/Java generated p

2019-02-02 09:39发布

问题:

I am writing an expression parser for an app written mostly in Scala. I have built AST objects in Scala, and now need to write the parser. I have heard of Scala's built-in parser combinators, and also of ANTLR3, and am wondering: which would provide better performance and ease of writing code? So far:

ANTLR pros

  1. Well-known
  2. Fast
  3. External DSL
  4. ANTLRWorks (great IDE for parser grammer debugging/testing)

ANTLR cons

  1. Java-based (Scala interop may be challenging, any experience?)
  2. Requires a large dependency at runtime

Parser combinator pros

  1. Part of Scala
  2. One less build step
  3. No need for a runtime dependency; e.g. already included in Scala's runtime library

Parser combinator cons

  1. Internal DSL (may mean slower execution?)
  2. No ANTLRWorks (provides nice parser testing and visualization features)

Any thoughts?

EDIT: This expression parser parses algebraic/calculus expressions. It will be used in the app Magnificalc for Android when it is finalized.

回答1:

Scala's parser combinators aren't very efficient. They weren't designed to be. They're good for doing small tasks with relatively small inputs.

So it really depends on your requirements. There shouldn't be any interop problems with ANTLR. Calling Scala from Java can get hairy, but calling Java from Scala almost always just works.



回答2:

I wouldn't worry about the performance limitations of parser combinators unless you were planning on parsing algebraic expressions that are a few pages long. The Programming Scala book does mention that a more efficient implementation of parser combinators is feasible. Maybe somebody will find the time and energy to write one.

I think with ANTLR you are talking about two extra build steps: ANTLR compiles to Java, and you need to compile both Scala and Java to bytecode, instead of just Scala.



回答3:

I have created external DSLs both with ANTLRv4 and Scalas parser combinators and I clearly prefer the parser combinators, because you get excellent editor support when designing the language and it's very easy to transform your parsing results to any AST case class data structure. Developing ANTLR grammars takes much more time, because, even with the ANTLRWorks editor support, developing grammars is very error-prone. The whole ANTLR workflow feels quite bloated to me compared to the parser combinators' one.



回答4:

I would be inclined to try to produce an external DSL using parser combinators. It shouldn't need to be an internal DSL. But I don't know that it would be better.

The best approach to figuring this out would be to take a simplified version of the grammar, try it both ways and evaluate the differences.