Treetop Introductory Tutorial

Part 3 of 10: What Number am I Thinking Of?

Now we are going to use our grammar in a Ruby program. Our program is a kind of "What Number am I Thinking Of" game. The user will type some input, and our program will let the user know whether or not it matches our grammar or not. Of course, it only matches our grammar if it is a double-quote!

First we need to save our grammar so Treetop can compile it. (I told you this was not my ideal compiler-compiler...) Save your program as double_quote.treetop. Now create a new Ruby program and enter the following code:

#!/usr/bin/env ruby
# Talk To Me -- tell me something and I'll tell you if I understand it
 
require 'rubygems'
require 'treetop'
puts 'Loaded Treetop with no problems...'

Save this code as talk_to_me.rb and run it. If you get error messages, then you haven't installed properly. Hopefully the error messages will be instructive enough. You can always try gem list --local to see if all the required gems are installed.

Now add the following code to the end of your program (yes, these are baby steps! Don't worry, we'll pick up speed soon enough):

#!/usr/bin/env ruby
# Talk To Me -- tell me something and I'll tell you if I understand it
 
require 'rubygems'
require 'treetop'
puts 'Loaded Treetop with no problems...'
 
Treetop.load 'double_quote'
puts 'Loaded double quote grammar with no problems...'

This is the code that actually compiles your grammar. If you get error messages now, then your problem is most likely you didn't enter the code from the previous tutorial properly. Or that your version of Treetop (gem list --local | grep "treetop" in unix-like systems) is too different from mine. I've tested this code with Treetop 1.4.4.

Once you have things working, you can fill in the rest of our Ruby program:

#!/usr/bin/env ruby
# Talk To Me -- tell me something and I'll tell you if I understand it
 
require 'rubygems'
require 'treetop'
puts 'Loaded Treetop with no problems...'
 
Treetop.load 'double_quote'
puts 'Loaded double quote grammar with no problems...'
 
parser = DoubleQuoteParser.new
 
print "You say: "; what_you_said = gets
what_you_said.strip! # remove the newline at the end
 
if parser.parse(what_you_said)
  puts "I say yes! I understand!"
else
  puts "I say no, I don't understand."
end

Notice that the Treetop loader has created a Ruby class DoubleQuoteParser out of our grammar! This class has a method parse which takes input and returns false if it doesn't understand it. (What it returns if it does understand it, we'll find out later.)

What we have done is get a line of text from the keyboard, strip it of the newline (carriage return) that comes with it, and then pass it to the parser using the parse method.

Now run the program. At the prompt, enter some data like Do you understand what I'm saying to you?. The program will respond I say no, I don't understand. because, that's true, the only thing it understands is a double-quote.

You now have a complete working integrated Treetop and Ruby program! How do you feel? Ready to try out something a little more complicated?...

Previous|Next