Treetop is a language for describing languages. Combining the elegance of Ruby with cutting-edge parsing expression grammars, it helps you analyze syntax with revolutionary ease.
sudo gem install treetop #Intuitive Grammar Specifications 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 _lookahead assertions_ 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: grammar Arithmetic rule additive multitive ( '+' multitive )* end rule multitive primary ( [*/%] primary )* end rule primary '(' additive ')' / number end rule number '-'? [1-9] [0-9]* end end #Syntax-Oriented Programming 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... grammar Arithmetic rule additive multitive a:( '+' multitive )* { def value a.elements.inject(multitive.value) { |sum, e| sum+e.multitive.value } end } end # other rules below ... end ...or associate rules with classes of nodes you wish your parsers to instantiate upon matching a rule. grammar Arithmetic rule additive multitive ('+' multitive)*