Nathan SoboNathan Sobo
Treetop 1.0.1
edit Posted by Nathan Sobo on Saturday September 15, 2007 at 12:25AM

I've been working on a parsing framework for Ruby since January called Treetop. I just released version 1.0.1 and recorded this screencast demonstrating its use.

Comments

  1. DJ Davis DJ Davis on September 15, 2007 at 02:11AM

    Sorry about that, Ajax submit button + no feed back + slow server response = 3 extra posts

  2. Zach Dennis Zach Dennis on September 15, 2007 at 04:07AM

    http://www.pivotalblabs.com/files/treetop-arithmetic-example.mov

    Nathan, what screen capturing tool did you use for the mov?

  3. steve d steve d on September 15, 2007 at 05:15PM

    That's pretty sweet, nice job man.

  4. Nathan Sobo Nathan Sobo on September 15, 2007 at 05:48PM

    I used Snapz Pro X. Sorry for the link confusion I thought Typo would create one automatically and the movie took so long to upload that I let it finish after I left for the day.

  5. Ross Hale Ross Hale on September 16, 2007 at 04:05AM

    Awesome!

  6. Alex C Alex C on September 16, 2007 at 02:09PM

    Inspiring. I want to go parse something!

  7. matt matt on September 19, 2007 at 03:52AM

    Been using the treetop gem and it is excellent. I hit a problem though, I need to be able to take phrases and pick out n number of words between my syntax/keywords. I can easily take a sentence 'keyword keyword ruby" and parse out ruby, but for example I want the value 'thanks ruby' from the following sentance. "keyword keyword thanks ruby keyword"

  8. Nathan Sobo Nathan Sobo on September 25, 2007 at 02:20AM

    The problem you're talking about is an excellent candidate for the application of a very powerful feature of parsing expression grammars that I didn't cover in this screencast: lookahead assertions. There's an example of their use in metagrammar.treetop, the grammar for treetop grammar files. I can briefly address their application to your example problem here.

    Say I want to match the word "keyword" followed by n words that are not "keyword" followed by another "keyword". You encode this in the parsing expression language as follows.

    <code>rule example
      'keyword' (!'keyword' other_word)+ 'keyword'
    end
    </code>

    The important part of the above code sample is the bang-prefixed 'keyword'that guards the repeated other_word nonterminal. Basically, this is saying: look ahead at the next input and make sure it does not match 'keyword'. As long as it doesn't, proceed with the parse without consuming any input, trying to match other_word. As soon as input matching 'keyword' is encountered, the repeated expression does not match, and this input is matched by the final 'keyword' terminal.

    Anyway, hope this helps. Check out other resources for parsing expressions out there on the internet for more information on all this stuff. I implement them in a pretty standard way. I'll be getting more documentation online gradually leading up to RailsConf.

  9. Case insensitivity? Case insensitivity? on November 17, 2007 at 01:59AM

    Very cool this treetop. I've been looking for something like this for some time - please, please, please do not abandon it as so many similar Ruby projects have been abondoned.

    That said, I'm wondering how do you handle case insensitivity? for example, I'm parsing a language in which all the keywords are case insensitive (VHDL) so 'if' is the same as 'IF' - how is that handled in your implementation of PEG?

  10. will will on December 18, 2007 at 05:40PM

    Nathan, the video seems to be unavailable by now, could you upload it somewhere, I saw it a few weeks ago, but now I tried to show it to a friend of mine, the link didn't work.

  11. chris chris on January 21, 2008 at 05:43PM

    I'm not Nathan, and its a late response, but case insensitivity seems to be handled simply by, e.g.:

    rule if
        [Ii] [Ff]
    end
    
  12. Mushtaq Mushtaq on November 26, 2008 at 07:46AM

    Hi Nathan,

    I saw your Treetop screen cast at RubyConf 2007, it was an excellent presentation.

    In one of our projects we have to implement a DSL which will parse the plain text in to rails active record statements and execute them. For example say we have a text box where we will enter “get me all the timesheets for project XYZ” this statement should get parsed and get us timesheets for XYZ project from the database(both timesheet and project are database tables). And one more thing is that the text what we enter in text box shouldn’t have any predefined order for example we can also enter “show me all members who have not submitted timesheets for X month”.

    We are thinking of using Treetop parser to parse the text and convert it in to active record statements, shall we use treetop to implement this feature or should we implement our own parser , your suggestion would be of great help.

  13. Ari Ari on January 01, 2009 at 10:46PM

    Thank you for this excellent library... it's been a joy to use so far.

    I was looking for some example of what to actually do with the finished tree after you've parsed your text -- how do I traverse the tree to actually do something with it?

    For example, I need to know how many child elements each node in the tree contains. Is there a built-in way to navigate the finished tree? I haven't seen this addressed anywhere.

    Thanks...

Add a Comment (MarkDown available)