Friday, October 22, 2010

Ruby: Parsing Expression Grammars

Ruby's syntactic flexibility often makes it a convenient choice for a DSL (Domain-Specific Language). Treetop is a nice way to specify a PEG (Parsing Expression Grammar) in Ruby. Here is an example.

In the example, note that the SexpParser class is defined magically via Treetop.Load(). (See Instantiating and using parsers.) I do not like such magic, but it is a common thing amongst Ruby coders.

No comments:

Post a Comment