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.
Treetop 1.0.1
Saturday, 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
December 12, 2007 at 11:50 pm
http://www.pivotalblabs.com/files/treetop-arithmetic-example.mov
Nathan, what screen capturing tool did you use for the mov?
December 12, 2007 at 11:50 pm
That’s pretty sweet, nice job man.
December 12, 2007 at 11:50 pm
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.
December 12, 2007 at 11:50 pm
Awesome!
December 12, 2007 at 11:50 pm
Inspiring. I want to go parse something!
December 12, 2007 at 11:50 pm
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”
December 12, 2007 at 11:50 pm
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.
The important part of the above code sample is the bang-prefixed
'keyword'that guards the repeatedother_wordnonterminal. 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 matchother_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.
December 12, 2007 at 11:50 pm
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?
December 12, 2007 at 11:50 pm
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.
December 18, 2007 at 5:40 pm
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
January 21, 2008 at 5:43 pm
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.
November 26, 2008 at 7:46 am
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…
January 1, 2009 at 10:46 pm