Parslet – A Simple PEG Parser Framework for Ruby

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.

Leave a Reply

Your email address will not be published. Required fields are marked *