index.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <html><head><link href="./screen.css" rel="stylesheet" type="text/css" />
  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="main_content"><p class="intro_text">
  9. 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 revolutionary ease.
  10. </p>
  11. <pre><code>sudo gem install treetop
  12. </code></pre>
  13. <h1>Intuitive Grammar Specifications</h1>
  14. <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>
  15. <pre><code>grammar Arithmetic
  16. rule additive
  17. multitive ( '+' multitive )*
  18. end
  19. rule multitive
  20. primary ( [*/%] primary )*
  21. end
  22. rule primary
  23. '(' additive ')' / number
  24. end
  25. rule number
  26. '-'? [1-9] [0-9]*
  27. end
  28. end
  29. </code></pre>
  30. <h1>Syntax-Oriented Programming</h1>
  31. <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>
  32. <pre><code>grammar Arithmetic
  33. rule additive
  34. multitive a:( '+' multitive )* {
  35. def value
  36. a.elements.inject(multitive.value) { |sum, e|
  37. sum+e.multitive.value
  38. }
  39. end
  40. }
  41. end
  42. # other rules below ...
  43. end
  44. </code></pre>
  45. <p>...or associate rules with classes of nodes you wish your parsers to instantiate upon matching a rule.</p>
  46. <pre><code>grammar Arithmetic
  47. rule additive
  48. multitive ('+' multitive)* &lt;AdditiveNode&gt;
  49. end
  50. # other rules below ...
  51. end
  52. </code></pre>
  53. <h1>Reusable, Composable Language Descriptions</h1>
  54. <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>
  55. <pre><code>grammar RubyWithEmbeddedSQL
  56. include SQL
  57. rule string
  58. quote sql_expression quote / super
  59. end
  60. end
  61. </code></pre>
  62. <h1>Acknowledgements</h1>
  63. <p><a href="http://pivotallabs.com"><img id="pivotal_logo" src="./images/pivotal.gif"></a></p>
  64. <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>
  65. <p>I'd also like to thank:</p>
  66. <ul>
  67. <li>Damon McCormick for several hours of pair programming.</li>
  68. <li>Nick Kallen for lots of well-considered feedback and a few afternoons of programming.</li>
  69. <li>Brian Takita for a night of pair programming.</li>
  70. <li>Eliot Miranda for urging me rewrite as a compiler right away rather than putting it off.</li>
  71. <li>Ryan Davis and Eric Hodel for hurting my code.</li>
  72. <li>Dav Yaginuma for kicking me into action on my idea.</li>
  73. <li>Bryan Ford for his seminal work on Packrat Parsers.</li>
  74. <li>The editors of Lambda the Ultimate, where I discovered parsing expression grammars.</li>
  75. </ul>
  76. </div></div><div id="bottom"></div></body></html>