Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A small PEG based parser library. See the Hacking page in the Wiki as well.
Ruby HTML CSS Shell

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.



A small library that implements a PEG grammar. PEG means Parsing Expression
Grammars [1]. These are a different kind of grammars that recognize almost the
same languages as your conventional LR parser, except that they are easier to
work with, since they haven't been conceived for generation, but for
recognition of languages. You can read the founding paper of the field by
Bryan Ford here [2].

Other Ruby projects that work on the same topic are:

My goal here was to see how a parser/parser generator should be constructed to
allow clean AST construction and good error handling. It seems to me that most
often, parser generators only handle the success-case and forget about
debugging and error generation.

More specifically, this library is motivated by one of my compiler projects. I
started out using 'treetop' (see the link above), but found it unusable. It
was lacking in

  * error reporting: Hard to see where a grammar fails. 
  * stability of generated trees: Intermediary trees were dictated by the 
    grammar. It was hard to define invariants in that system - what was 
    convenient when writing the grammar often wasn't in subsequent stages. 
  * clarity of parser code: The parser code is generated and is very hard
    to read. Add that to the first point to understand my pain. 
So parslet tries to be different. It doesn't generate the parser, but instead
defines it in a DSL which is very close to what you find in [2]. A successful
parse then generates a parser tree consisting entirely of hashes and arrays
and strings (read: instable). This parser tree can then be converted to a real
AST (read: stable) using a pattern matcher that is also part of this library.

Error reporting is another area where parslet excels: It is able to print not
only the error you are used to seeing ('Parse failed because of REASON at line
1 and char 2'), but also prints what led to that failure in the form of a
tree (#error_tree method).



  require 'parslet'
  include Parslet

  # Constructs a parser using a Parser Expression Grammar like DSL: 
  parser =  str('"') >> 
              str('\\') >> any |
              str('"').absnt? >> any
            ) >> 
  # Parse the string and capture parts of the interpretation (:string above)        
  tree = parser.parse(%Q{
    "This is a \\"String\\" in which you can escape stuff"

  tree # => {:string=>"This is a \\\"String\\\" in which you can escape stuff"}

  # Here's how you can grab results from that tree, two methods: 

  # 1) => simple(:x)).each_match(tree) do |dictionary|
    puts "String contents (method 1): #{dictionary[:x]}"

  # 2)
  transform = do
    rule(:string => simple(:x)) { 
      puts "String contents (method 2): #{x}" }


This library should work with both ruby 1.8 and ruby 1.9.


My gigantous thanks go to the following cool guys and gals that help make this

Florian Hanke (


On the road to 1.0; improving documentation, packaging and upgrading to rspec2.

(c) 2010 Kaspar Schiess
Something went wrong with that request. Please try again.