Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Treetop – Powerful But Easy Ruby Parser Library

By Peter Cooper / January 25, 2008

paren_language_output.png

Treetop is a very well put together Ruby library for building Ruby-powered parsers. Treetop makes it possible to rapidly put together parsers for your own mini languages (a basic "arithmetic" parser is the main demonstration used on the Treetop site) and relies on parsing expression grammars to make it a pretty easy process.

Back in 2006 I posted about my own Ruby recursive descent parser on Ruby Inside, but Treetop goes a lot further in making it developer friendly and more powerful overall, even going as far as making blending grammars together as easy as mixing Ruby modules! Another key difference between a raw recursive descent parser and Treetop is that Treetop actually generates Ruby code that works on your particular grammar, somewhat like yacc or Ragel.

This is a really exciting project if you're into parsing or need to build a parser, so check it out. The official site has lots of useful documentation (although not many particularly deep examples, but they are bound to come in time). If you end up using Treetop to produce something, make sure to let me know!

Comments

  1. RD says:

    Have not gone through the whole documentation, but a quick question is it something which can help us write code reviews and if it is so then is there any project working on code review?

  2. Peter Cooper says:

    I probably should add something saying that this isn't something that parses Ruby, but a "parser" library written in Ruby. So, no, unless you wrote a whole spec for Ruby in Treetop's syntax, I don't think code review would be a use case for this. That would be more a job for ParseTree and ruby2ruby.

  3. Rick Bradley says:

    I saw this presented at RubyConf 2007 and have to say I was thoroughly impressed. Very slick and well worth looking at.

  4. Rick Bradley says:

    Sorry -- I meant to include the link to the rubyconf presentation video:

    http://rubyconf2007.confreaks.com/d1t1p5_treetop.html

  5. Geoffrey Grosenbach says:

    I recorded and interview with Mr. Sobo last week in San Francisco. I hope to post it to the Rails Podcast within the next few weeks.

  6. CptOatmeal says:

    I've been using this fairly heavily since RubyConf to write a full grammar for IDL (the data processing language, not the interface description one.) There are a few rough edges, but I've been able to develop a fully functional grammar in a remarkably short span of time. Treetop is seriously useful.

  7. Peter Cooper says:

    Just testing something.. sorry!

Other Posts to Enjoy

Twitter Mentions