- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: New alternative to Scala parser combinators
Tue, 2010-08-17, 08:29
Rex,
thanks for your comments!
It would of course be no problem to add "alias methods" with the respective single char operators '*', '+' and '?' (just like the combinators offer them as well).
However, due to the Scala operator precedence rules, rule expressions would not be parsed quite as expected:
a ~ b* would be treated as (a ~ b)* and not as a ~ (b*), because the tilde (~) operator has higher associativity than all the classic PEG operators. This could be fixed by not using the tilde as sequence operator, but either '&', '^', '<' or '>' as realistic options (since these four have the right operator precedence rank between the pipe and the plus). The '&' operator is already taken by one of the syntactic predicates, leaving only '^', '<' or '>' as options. And neither of them appeared quite as appealing to me for the most frequent sequence operator as the tilde.
Also, it seems by judging from this comment in the combinator sources:
// the operator formerly known as +++, ++, &, but now, behold the venerable ~
// it's short, light (looks like whitespace), has few overloaded meaning (thanks to the recent change from ~ to unary_~)
// and we love it! (or do we like `,` better?)
that the Scala team has gone through quite some DSL refactoring itself over time, and the fact that they finally went with the tilde for sequencing was another argument for me to go with that choice too.
Maybe someone involved into that decision can shine some more light on the discussion behind this choice?
Cheers,
Mathias
---
mathias@parboiled.org
http://www.parboiled.org
On 16.08.2010, at 19:47, Rex Kerr wrote:
> Parboiled looks quite interesting. I did notice, however, that the DSL is a hybrid of "classic" PEG notation and "classic" Java method calls. Why, for example, is not "zeroOrMore" represented by "*"? As a complete novice with Parboiled, I found the mix in the Scala version rather hard to read.
>
> --Rex
>
> On Mon, Aug 16, 2010 at 12:50 PM, Mathias wrote:
> Hello Everybody,
>
> I would like to draw your attention to a new open-source PEG parsing library available to Scala developers:
> "parboiled for Scala" (http://parboiled.org).
>
> It let's you build PEG parsers with an internal Scala DSL very similar to Scalas own parser combinators, but, among others, offers the following advantages:
> - much better performance due to clear separation of parser rule creation and input analysis
> - better error reporting
> - good error recovery enabling parsing of the complete input even in the presence of syntax errors
>
> The parboiled PEG parsing engine is written in Java and is already being used for some time by quite a few Java projects. Originally it only offered an internal Java DSL for parser construction but recently received a "Scala facade" that allows parser building using a Scala DSL.
>
> Some links:
> Main parboiled website with sources, documentation, examples, etc: http://parboiled.org
> Simple calculator example: http://github.com/sirthias/parboiled/blob/master/src/examples/scala/org/...
> Complete JSON parser example: http://github.com/sirthias/parboiled/blob/master/src/examples/scala/org/...
> Java 6 parser example (in parboiled for Java DSL): http://github.com/sirthias/parboiled/blob/master/src/examples/java/org/p...
> Markdown processor (in parboiled for Java DSL): http://github.com/sirthias/pegdown/blob/master/src/org/pegdown/Parser.java
>
> I also just published a blog post introducing "parboiled for Scala" in more detail:
> http://www.decodified.com/parboiled/2010/08/16/parboiled_for_scala.html
>
> Any feedback is, of course, more than welcome!
>
> Cheers,
> Mathias
>
> ---
> mathias@parboiled.org
> http://www.parboiled.org
>
>