Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

Parslet – A Simple PEG Parser Framework for Ruby

By Peter Cooper / January 12, 2011

Screen shot 2011-01-12 at 2.32.49 AM.pngParslet is a new "simple parser framework" for Ruby built by Kaspar Schiess. It follows the PEG (parsing expression grammar) style of parsing and its primary goals are to be simple, testable, and to have extensive and powerful error reporting features (something that Kaspar states Treetop sorely lacks).

If you've already used Treetop, you might wonder what the hoopla is about. The key difference is that Parslet does not generate code to represent the parsing grammars you create as Treetop does - it works directly from rules defined using standard Ruby syntax (Treetop has its own Ruby-esque grammar language). Parslet's error messages are also significantly nicer than Treetop's when you inevitably run into trouble with your grammar as they're generated by Parslet itself and don't spring from Treetop's internally generated code.

ast.png

So if you need to build a parser (or think you might), check out Parslet - it has a great homepage with some useful documentation. Installation is simple (gem install parslet) and Kaspar has put together a "getting started" tutorial that walks through building a basic grammar for a teeny-tiny programming language. It concludes with a complete example of a programming language interpreter in a mere 49 lines.

Comments

  1. Pingback: Tweets that mention Parslet – A Simple PEG Parser Framework for Ruby -- Topsy.com

  2. Avdi Grimm says:

    So I was going to come here and say "oh dear god not another one", but then I saw "error reporting" and got interested. It's not just Kaspar stating that Treetop sorely lacks error reporting; I'd go so far (having used Treetop in production) to say that its lack of decent error reporting makes it unsuitable for real-world use. Any parser generator that takes errors seriously is worthy of real consideration.

  3. Jason Garber says:

    I wonder what its speed is like. Besides error reporting, that was the main thing I ran into with Treetop.

  4. Jason Garber says:

    "Once you start naming things, you’ll notice that what you don’t name, disappears. Parslet assumes that what you don’t name is unimportant."

    Finally!

    Transformations--also brilliant.

    Glad Parslet came along.

  5. Giles Bowkett says:

    There's also one called Citrus.

  6. Alex says:

    Awesome! Long live Parslet

Other Posts to Enjoy

Twitter Mentions