Let us know how we can contact you.
We'll respond shortly.
By Pivotal Labs • September 15, 2007
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.
Sorry about that, Ajax submit button + no feed back + slow server response = 3 extra posts
Nathan, what screen capturing tool did you use for the mov?
That’s pretty sweet, nice job man.
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.
Inspiring. I want to go parse something!
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”
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.
'keyword' (!'keyword' other_word)+ 'keyword'
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.
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?
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.
I’m not Nathan, and its a late response, but case insensitivity seems to be handled simply by, e.g.:
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.
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.
Your Information (Name required. Email address will not be displayed with comment.)
Mail (will not be published) (required)
* Copy This Password *
* Type Or Paste Password Here *