Monday, January 24, 2011

A Simple PEG Parser Framework for Ruby - Parslet

Parslet 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.

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.

Source: http://www.rubyinside.com

0 comments:

Post a Comment