Yes! Our program understands! But how can we be sure? In pedagogy (the science of learning), the difference between “knowing” and “understanding” (or between “understanding” and “knowing”, depending on which book you read) is the ability to apply, or to transform, what you have learned, rather than simply parroting it back. So can a Treetop parser transform what it claims to have understood?

Make the following modification to your parse_email_list.rb Ruby program:

if result=parser.parse(what_you_said)
  puts "I say yes! I understand!"
  puts "You said the email name #{result.email_name}"
else

Now run it and supply the same input that it understood last time ("Jena L. Dovie"). Now you get a result something like this:

You said the email name #<Treetop::Runtime::SyntaxNode:0x1005aa798>

Which, if you know about Ruby, means that you got an instance of the Ruby object Treetop::Runtime::SyntaxNode. In fact, your result is simply a tree of Syntax Node objects, one for each rule or rule component. You can see this by changing the puts line to read :

puts "You said the email address #{result.inspect}"

Here are some useful methods available to a Syntax Node:

Method Description
terminal? True if the node doesn't have children.
non_terminal? True if the node does have children.
text_value The text the node has matched.
elements An array of all the children.

So, clearly, text_value is the method we want. Modify your puts line as follows:

if result=parser.parse(what_you_said)
  puts "I say yes! I understand!"
  puts "You said the email name #{result.email_name.text_value}."
else

Try it again, with the same input—it works!

It should be simple then, to add another rule to parse our email address. We know an encapsulated email address is an open-angle-bracket (<) followed by the actual email address (which is any character but the close-angle-bracket and the pattern must match at least one character) followed by the close-angle-bracket(>).

Do this on your own, so you can gain confidence and so you can also gain practice in deciphering Treetop error messages. When you are finished, your program should be able to understand

"Jena L. Dovie" <jdovie_qs@agora.bungi.com>

And be able to print out:

I say yes! I understand!
You said the email name Jena L. Dovie and the email address jdovie_qs@agora.bungi.com.

Got it? Happy? Good. But what about email addresses that don’t have any email names? How can we parse those? On to the next tutorial…

Previous | Next