easy compilers with ruby and treetop

35
VISHNU GOPAL EASY COMPILERS WITH RUBY AND TREETOP Saturday, 28 May 2011

Upload: vishnu

Post on 06-May-2015

5.094 views

Category:

Technology


1 download

DESCRIPTION

My talk at RubyConf India 2011. A short one that describes how to use treetop to write easy compilers.

TRANSCRIPT

Page 1: Easy Compilers With Ruby and Treetop

VISHNU GOPAL

EASY COMPILERSWITH RUBY AND TREETOP

Saturday, 28 May 2011

Page 2: Easy Compilers With Ruby and Treetop

ABOUT ME

• Vishnu Gopal

• Started as a Software Engineer, SlideShare Inc.

• Human-Computer Interaction from UCLIC, London

• CTO, MobME Wireless.

• Working with Ruby for over 5 years.

vishnugopal.com

@vishnugopal

Saturday, 28 May 2011

Page 3: Easy Compilers With Ruby and Treetop

MOBME WIRELESS

• College startup in 2006

• NASSCOM Global Leadership Award, 2011

• VAS & core network solutions for mobile operators

• Over 40 engineers working in Ruby

& 2 speakers at RubyConf India

mobme.in

@mobmewireless

Saturday, 28 May 2011

Page 4: Easy Compilers With Ruby and Treetop

DRAGON BOOK

• Aho, Sethi & Ullman

• 1009 pages of wisdom

Saturday, 28 May 2011

Page 5: Easy Compilers With Ruby and Treetop

• Too complicated.

• Meant for Computer Scientists

Saturday, 28 May 2011

Page 6: Easy Compilers With Ruby and Treetop

IN RUBY

• Designed for a programmer

• Not too hard to grok

• Iterative development

• Friendly

• Fun

Saturday, 28 May 2011

Page 7: Easy Compilers With Ruby and Treetop

COMPILERS?HOW ARE PARSERS AND COMPILERS USEFUL?

Saturday, 28 May 2011

Page 8: Easy Compilers With Ruby and Treetop

WHAT?

A compiler converts a high-level language into a form that

computers can understand.

A parser structures a high-level language into an intermediate

form that compilers can then use.

Saturday, 28 May 2011

Page 9: Easy Compilers With Ruby and Treetop

PARSINGSaturday, 28 May 2011

Page 10: Easy Compilers With Ruby and Treetop

PARSING 2Saturday, 28 May 2011

Page 11: Easy Compilers With Ruby and Treetop

COMPILING

• Convert this regular expression into a FSM.

• Run a block of text against this FSM and return true or false.

Saturday, 28 May 2011

Page 12: Easy Compilers With Ruby and Treetop

ABSTRACTION

• When you write a regular expression, you do not worry about all of

this. You do not care about how it’s parsed, compiled or matched.

• A regular expression matches strings, but what if you want to do

other things with your own mini language?

• A compiler construction library will help you do that.

Saturday, 28 May 2011

Page 13: Easy Compilers With Ruby and Treetop

POSSIBILITIES

• A search query language like Google’s:

Cricket -Tendulkar

• A SQL-like frontend for your favourite NoSQL backend:

SELECT * FROM LIST redis.rubyconf

• A small DSL to match twitter entries.

tweet.text contains “India” AND tweet.text has hyperlink

Saturday, 28 May 2011

Page 14: Easy Compilers With Ruby and Treetop

NOT MY IDEA

• Yacc & LEX

• Bison & FLEX

• Boost Proto

Saturday, 28 May 2011

Page 15: Easy Compilers With Ruby and Treetop

A RUBY WAY

Saturday, 28 May 2011

Page 16: Easy Compilers With Ruby and Treetop

TREETWEET

Saturday, 28 May 2011

Page 17: Easy Compilers With Ruby and Treetop

BACKUS NAUR

• BNF Form: a computer-sciency way to define language keywords,

syntax and order of precedence of operators.

• Or, a formal definition that you can feed to a parser

Saturday, 28 May 2011

Page 18: Easy Compilers With Ruby and Treetop

TT BNF

Saturday, 28 May 2011

Page 19: Easy Compilers With Ruby and Treetop

TT BNF 2

Saturday, 28 May 2011

Page 20: Easy Compilers With Ruby and Treetop

TT BNF 3

Incorrect!

Saturday, 28 May 2011

Page 21: Easy Compilers With Ruby and Treetop

TT BNF 4

Saturday, 28 May 2011

Page 22: Easy Compilers With Ruby and Treetop

TT BNF EXAMPLE

Expression

Not TermNot Term

TermTerm

target = tweet.text

binary_operator = contains

argument = “India”

“NOT”

Saturday, 28 May 2011

Page 23: Easy Compilers With Ruby and Treetop

BNF TO TREETOP

Saturday, 28 May 2011

Page 24: Easy Compilers With Ruby and Treetop

GET IT WORKING!

Saturday, 28 May 2011

Page 25: Easy Compilers With Ruby and Treetop

AGAIN?

A compiler converts a high-level language into a form that

computers can understand.

A parser structures a high-level language into an intermediate

form that compilers can then use.

Saturday, 28 May 2011

Page 26: Easy Compilers With Ruby and Treetop

SYNTAX FIRST

• Syntax Oriented Programming

• Matching rules can have Ruby methods inline or mixed in as a

module.

• These methods are available to the parser.

• Implement a “compile” method for e.g. to compile parsed objects

into an executable form.

• Let us compile TreeTweet into our favourite language!

Saturday, 28 May 2011

Page 27: Easy Compilers With Ruby and Treetop

TREETWEET LANGUAGE

TREETWEET PARSER

RUBY CODE TWEET RUBY OBJECT

BOOLEAN RESULT

TWITTER FIREHOSE

performs match

TREETWEET LANGUAGE .treetop

generates

is parsed by

generates produces

OVERVIEWSaturday, 28 May 2011

Page 28: Easy Compilers With Ruby and Treetop

COMPILE 1

Saturday, 28 May 2011

Page 29: Easy Compilers With Ruby and Treetop

COMPILE 2

Saturday, 28 May 2011

Page 30: Easy Compilers With Ruby and Treetop

COMPILE 3

Saturday, 28 May 2011

Page 31: Easy Compilers With Ruby and Treetop

COMPILE 4

Saturday, 28 May 2011

Page 32: Easy Compilers With Ruby and Treetop

COMPILE 5

Saturday, 28 May 2011

Page 33: Easy Compilers With Ruby and Treetop

RECAP

• What we’ve done is implement a cross-compiler.

• The cross-compiler compiles the TreeTweet DSL down to Ruby.

• It could however, be easily modified to compile it down to say Java,

or even C if you need performance.

• It’s small and concise. The treetop grammar is less than 50sloc. The

compiler less than 400sloc.

• It’s really easy to modify.

Saturday, 28 May 2011

Page 34: Easy Compilers With Ruby and Treetop

SOURCE CODE

• github.com/vishnugopal

• Not released yet, soon!

Saturday, 28 May 2011

Page 35: Easy Compilers With Ruby and Treetop

QUESTIONS?

codejam.mobme.in

Saturday, 28 May 2011