Walrat
Walrat is a sophisticated parser generator written in Ruby. It can be used to dynamically generate (at runtime):
- integrated lexers
- "packrat" (memoizing) parsers cable of recognizing Parsing Expression Grammars (PEG), including grammars that use left-recursion
- Abstract Syntax Tree builders that can build trees of arbitrary complexity
Walrat was originally written in 2007 as part of the Walrus object-oriented templating system. In 2010 it was extracted into a separate gem for easier reuse in other projects. The Walrus grammar is an excellent example of some of the more advanced parsing techniques that can be achieved using Walrat, including:
- dynamically define grammars and parsers at runtime using a simple DSL
- standard PEG constructs such as ordered choice, concatenation, repetition, predicates
- string and regular-expression based "parslets"
- convenient and customizable skipping behavior (in this case, whitespace skipping)
- left-recursion
- left-associative and right-associative productions
- dynamic AST node synthesis
- addition of custom behavior to AST nodes (in this case, compilation behavior) through custom Ruby code
- multiline comments, including nested multiline comments
- "island" parsers for processing "Here documents" and include files
System requirements
Walrat currently officially supports Ruby 1.8, although Ruby 1.9 compatibility work is basically complete. JRuby is not yet officially supported, although many complex grammars (such as the Walrus grammar) have already been successfully tested. See this blog post comparing the performance characteristics of the different Ruby implementations and versions.
Installation
$ sudo gem install walrat
Further reading
Walrus (and therefore Walrat) was originally written an 2007 and as it was my first non-trivial project in Ruby I made a number of blog posts during development:
- Test-Driven Development
- Writing a parser generator in Ruby
- Progress notes
- Abstract Syntax Trees
- Putting the 'packrat' into PEG: adding memoizing to my Ruby parser generator
- Updated packrat performance numbers
- Parser generator update
- Another performance update: parity
- Here documents
- Goodbye parity, hello superiority
- Payoff time