Path: | README.md |
Last Update: | Mon Jan 09 01:37:57 +0000 2012 |
Support
Support for Treetop is provided through the mailing list you can join or browse here: groups.google.com/group/treetop-dev
Tutorial
Languages can be split into two components, their syntax and their semantics. It‘s your understanding of English syntax that tells you the stream of words "Sleep furiously green ideas colorless" is not a valid sentence. Semantics is deeper. Even if we rearrange the above sentence to be "Colorless green ideas sleep furiously", which is syntactically correct, it remains nonsensical on a semantic level. With Treetop, you‘ll be dealing with languages that are much simpler than English, but these basic concepts apply. Your programs will need to address both the syntax and the semantics of the languages they interpret.
Treetop equips you with powerful tools for each of these two aspects of interpreter writing. You‘ll describe the syntax of your language with a *parsing expression grammar*. From this description, Treetop will generate a Ruby parser that transforms streams of characters written into your language into *abstract syntax trees* representing their structure. You‘ll then describe the semantics of your language in Ruby by defining methods on the syntax trees the parser generates.
Parsing Expression Grammars, The Basics
The first step in using Treetop is defining a grammar in a file with the `.treetop` extension. Here‘s a grammar that‘s useless because it‘s empty:
# my_grammar.treetop grammar MyGrammar end
Next, you start filling your grammar with rules. Each rule associates a name with a parsing expression, like the following:
# my_grammar.treetop # You can use a .tt extension instead if you wish grammar MyGrammar rule hello 'hello chomsky' end end
The first rule becomes the root of the grammar, causing its expression to be matched when a parser for the grammar is fed a string. The above grammar can now be used in a Ruby program. Notice how a string matching the first rule parses successfully, but a second nonmatching string does not.
# use_grammar.rb require 'rubygems' require 'treetop' Treetop.load 'my_grammar' # or just: # require 'my_grammar' # This works because Polyglot hooks "require" to find and load Treetop files parser = MyGrammarParser.new puts parser.parse('hello chomsky') # => Treetop::Runtime::SyntaxNode puts parser.parse('silly generativists!') # => nil
Users of *regular expressions* will find parsing expressions familiar. They share the same basic purpose, matching strings against patterns. However, parsing expressions can recognize a broader category of languages than their less expressive brethren. Before we get into demonstrating that, lets cover some basics. At first parsing expressions won‘t seem much different. Trust that they are.
Terminal Symbols