Broke dependency on facets and trying to coexist with Rails.
[treetop.git] / doc / site / index.html
blobfaa77e4f365935ceca0fe091403dac4d4ec68d77
1 <html><head><link type="text/css" href="./screen.css" rel="stylesheet" />
2 <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
3 </script>
4 <script type="text/javascript">
5 _uacct = "UA-3418876-1";
6 urchinTracker();
7 </script>
8 </head><body><div id="top"><div id="main_navigation"><ul><li><a href="syntactic_recognition.html">Documentation</a></li><li><a href="contribute.html">Contribute</a></li><li>Home</li></ul></div></div><div id="middle"><div id="content"><p class="intro_text">
10 Treetop is a language for describing languages. Combining the elegance of Ruby with cutting-edge <em>parsing expression grammars</em>, it helps you analyze syntax with revolutionarily ease.
12 </p>
14 <pre><code>sudo gem install treetop
15 </code></pre>
17 <h1>Intuitive Grammar Specifications</h1>
19 <p>Parsing expression grammars (PEGs) are simple to write and easy to maintain. They are a simple but powerful generalization of regular expressions that are easier to work with than the LALR or LR-1 grammars of traditional parser generators. There's no need for a tokenization phase, and <em>lookahead assertions</em> can be used for a limited degree of context-sensitivity. Here's an extremely simple Treetop grammar that matches a subset of arithmetic, respecting operator precedence:</p>
21 <pre><code>grammar Arithmetic
22 rule additive
23 multitive '+' additive / multitive
24 end
26 rule multitive
27 primary '*' multitive / primary
28 end
30 rule primary
31 '(' additive ')' / number
32 end
34 rule number
35 [1-9] [0-9]*
36 end
37 end
38 </code></pre>
40 <h1>Syntax-Oriented Programming</h1>
42 <p>Rather than implementing semantic actions that construct parse trees, Treetop lets you define methods on trees that it constructs for you automatically. You can define these methods directly within the grammar...</p>
44 <pre><code>grammar Arithmetic
45 rule additive
46 multitive '+' additive {
47 def value
48 multitive.value + additive.value
49 end
52 multitive
53 end
55 # other rules below ...
56 end
57 </code></pre>
59 <p>...or associate rules with classes of nodes you wish your parsers to instantiate upon matching a rule.</p>
61 <pre><code>grammar Arithmetic
62 rule additive
63 multitive '+' additive &lt;AdditiveNode&gt;
65 multitive
66 end
68 # other rules below ...
69 end
70 </code></pre>
72 <h1>Reusable, Composable Language Descriptions</h1>
74 <p>Because PEGs are closed under composition, Treetop grammars can be treated like Ruby modules. You can mix them into one another and override rules with access to the <code>super</code> keyword. You can break large grammars down into coherent units or make your language's syntax modular. This is especially useful if you want other programmers to be able to reuse your work.</p>
76 <pre><code>grammar RubyWithEmbeddedSQL
77 include SQL
79 rule string
80 quote sql_expression quote / super
81 end
82 end
83 </code></pre>
85 <h1>Acknowledgements</h1>
87 <p><a href="http://pivotallabs.com"><img id="pivotal_logo" src="./images/pivotal.gif"></a></p>
89 <p>First, thank you to my employer Rob Mee of <a href="http://pivotallabs.com"/>Pivotal Labs</a> for funding a substantial portion of Treetop's development. He gets it.</p>
91 <p>I'd also like to thank:</p>
93 <ul>
94 <li>Damon McCormick for several hours of pair programming.</li>
95 <li>Nick Kallen for lots of well-considered feedback and a few afternoons of programming.</li>
96 <li>Brian Takita for a night of pair programming.</li>
97 <li>Eliot Miranda for urging me rewrite as a compiler right away rather than putting it off.</li>
98 <li>Ryan Davis and Eric Hodel for hurting my code.</li>
99 <li>Dav Yaginuma for kicking me into action on my idea.</li>
100 <li>Bryan Ford for his seminal work on Packrat Parsers.</li>
101 <li>The editors of Lambda the Ultimate, where I discovered parsing expression grammars.</li>
102 </ul></div></div><div id="bottom"></div></body></html>