Treetop Introductory Tutorial

Part 2 of 10: As Long as You Speak Double-Quote

Our development style is what I call the Baby-Step style: start with something simple, make sure it works, and keep adding to it. And, of course, retest every step. The advantage of that style is that you can be pretty sure that, if something goes wrong, the error is in the last thing you added.

So we are going to start with something simple. We want to recognize just the very first character of our email list: the double-quotes.

Ensure you have Ruby and Treetop installed properly, as per the installation instructions. Then fire up your editor and write your first Treetop grammar definition:

# my first Treetop Grammar
# As long as you speak double-quote, I know what you mean.
#
 
grammar DoubleQuote
  rule double_quote
    '"'
  end
end

Before we go too much further, let's take a look at some of the characteristics of our Treetop grammar language.

# my first Treetop Grammar

Comments start with "#', just like Ruby.

grammar DoubleQuote
  ...
end

Our grammar is identified by the keyword grammer, has a CamelCase name, and ends with the word end, like Ruby classes and modules.

  rule double_quote
    ...
  end

Our grammar consists of rules. Each rule starts with the keyword rule, has a lower-case name, and ends with the word end, like Ruby methods.

  rule double_quote
    ...
  end

Our rule double_quote will match the single character ". That means, if it is given the character ", it is content. Anything else, and it gets this blank look on its face...

Previous|Next