treetop austinrb

Post on 20-Jun-2015

203 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

TREETOP: Making Parsers Fun

Monday October 7th @ Capital Factory

Say what? Why do I need a Parser?Argentina.blft # real world example Big Lever Gears Format

propertyUrlPrefix = "propiedad";landingPageUrlPrefix = "alquileres-vacaciones";propertyReviewsPrefix = "reviews";propertyReviewsWritePrefix = "reviews/write";propertyReviewsConfirmPrefix = "reviews/confirm";propertyReviewsResponsePrefix = "reviews/response";

This could work…

But…Locale = { es_AR;}BrandId = 68;BrandConfig{ OmnitureAccount { Dev = "homeawayardev"; } linguaEnabled = false;}

Nesting

Enum

Number

String

Boolean

Ok… maybe not

# this just extracts the values, we haven’t even # begun to set the correct type or handle the # nesting/([a-zA-Z0-9])+=([a-zA-Z]+[^;]*|'"'[^"]*'"'|[0-9]+|(true|false))/

Ok, so we need a Parser

.tt (Treetop) Grammar Files

Using your grammars inRuby

$tt foo.treetop bar.treetop$tt foo.treetop -o foogrammar.rb

Using Parsers in Ruby

Let’s try this again

What about nesting?

But It gets better!

Use Ruby in your .tt files!

Alternate Method

Bringing it all together

Bringing it all together Part II

Gotchas• First rule must match the document

On Parsing Expression Grammars

Parsing expression grammars (PEGs) are an alternative to context free grammars for formally specifying syntax, and packrat parsers are parsers for PEGs that operate in guaranteed linear time through the use of memoization.

• Linear time, fast!• Memory hog, storage proportional to the total input size• Not suitable for natural language processing

Further Reading

Parsing Expression Grammars• http://en.wikipedia.org/wiki/Parsing_expression_grammar• http://bford.info/packrat/

Treetop• http://treetop.rubyforge.org/• http://github.com/nathansobo/treetop/tree/master• https://groups.google.com/forum/#!forum/treetop-dev

Thank You!

Patrick Ritchie@pritchie

top related