0
VISHNUG O PA L                        EASY                        COMPILERS                        WITH RUBY AND TREETOPSa...
•       Vishnu Gopal               •    Started as a Software Engineer, SlideShare Inc.               •    Human-Computer ...
•     College startup in 2006           •     NASSCOM Global Leadership Award, 2011           •     VAS & core network sol...
DRAGON           BOOK           •     Aho, Sethi & Ullman           •     1009 pages of wisdomSaturday, 28 May 2011
•     Too complicated.           •     Meant for Computer ScientistsSaturday, 28 May 2011
•     Designed for a programmer           •     Not too hard to grok           •     Iterative development           •    ...
COMPILERS?               HOW ARE PARSERS AND COMPILERS USEFUL?Saturday, 28 May 2011
A compiler converts a high-level language into a form that                        computers can understand.               ...
PARSINGSaturday, 28 May 2011
PARSING 2Saturday, 28 May 2011
•     Convert this regular expression into a FSM.           •     Run a block of text against this FSM and return true or ...
•     When you write a regular expression, you do not worry about all of                 this. You do not care about how i...
•     A search query language like Google’s:                 Cricket -Tendulkar           •     A SQL-like frontend for yo...
•     Yacc & LEX           •     Bison & FLEX           •     Boost Proto           NOT MY IDEASaturday, 28 May 2011
A RUBY WAYSaturday, 28 May 2011
TREETWEETSaturday, 28 May 2011
•     BNF Form: a computer-sciency way to define language keywords,                 syntax and order of precedence of oper...
TT BNFSaturday, 28 May 2011
TT BNF 2Saturday, 28 May 2011
Inc                              orre                                     ct!           TT BNF 3Saturday, 28 May 2011
TT BNF 4Saturday, 28 May 2011
Expression                         Not Term                    Not Term                        Term                 “NOT” ...
BNF TO TREETOPSaturday, 28 May 2011
GET IT WORKING!Saturday, 28 May 2011
A compiler converts a high-level language into a form that                        computers can understand.               ...
•     Syntax Oriented Programming           •     Matching rules can have Ruby methods inline or mixed in as a            ...
TREETWEET                                             LANGUAGE .treetop                                                gen...
COMPILE 1Saturday, 28 May 2011
COMPILE 2Saturday, 28 May 2011
COMPILE 3Saturday, 28 May 2011
COMPILE 4Saturday, 28 May 2011
COMPILE 5Saturday, 28 May 2011
•     What we’ve done is implement a cross-compiler.           •     The cross-compiler compiles the TreeTweet DSL down to...
•     github.com/vishnugopal           •     Not released yet, soon!           SOURCE CODESaturday, 28 May 2011
codejam.mobme.in           QUESTIONS?Saturday, 28 May 2011
Upcoming SlideShare
Loading in...5
×

Easy Compilers With Ruby and Treetop

5,158

Published on

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

Published in: Technology
2 Comments
5 Likes
Statistics
Notes
No Downloads
Views
Total Views
5,158
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
39
Comments
2
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "Easy Compilers With Ruby and Treetop"

  1. 1. VISHNUG O PA L EASY COMPILERS WITH RUBY AND TREETOPSaturday, 28 May 2011
  2. 2. • 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. ABOUT ME vishnugopal.com @vishnugopalSaturday, 28 May 2011
  3. 3. • 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 WIRELESS mobme.in @mobmewirelessSaturday, 28 May 2011
  4. 4. DRAGON BOOK • Aho, Sethi & Ullman • 1009 pages of wisdomSaturday, 28 May 2011
  5. 5. • Too complicated. • Meant for Computer ScientistsSaturday, 28 May 2011
  6. 6. • Designed for a programmer • Not too hard to grok • Iterative development • Friendly • Fun IN RUBYSaturday, 28 May 2011
  7. 7. COMPILERS? HOW ARE PARSERS AND COMPILERS USEFUL?Saturday, 28 May 2011
  8. 8. 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. WHAT?Saturday, 28 May 2011
  9. 9. PARSINGSaturday, 28 May 2011
  10. 10. PARSING 2Saturday, 28 May 2011
  11. 11. • Convert this regular expression into a FSM. • Run a block of text against this FSM and return true or false. COMPILINGSaturday, 28 May 2011
  12. 12. • 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. ABSTRACTIONSaturday, 28 May 2011
  13. 13. • 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 POSSIBILITIESSaturday, 28 May 2011
  14. 14. • Yacc & LEX • Bison & FLEX • Boost Proto NOT MY IDEASaturday, 28 May 2011
  15. 15. A RUBY WAYSaturday, 28 May 2011
  16. 16. TREETWEETSaturday, 28 May 2011
  17. 17. • 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 BACKUS NAURSaturday, 28 May 2011
  18. 18. TT BNFSaturday, 28 May 2011
  19. 19. TT BNF 2Saturday, 28 May 2011
  20. 20. Inc orre ct! TT BNF 3Saturday, 28 May 2011
  21. 21. TT BNF 4Saturday, 28 May 2011
  22. 22. Expression Not Term Not Term Term “NOT” Term target = tweet.text binary_operator = contains argument = “India” TT BNF EXAMPLESaturday, 28 May 2011
  23. 23. BNF TO TREETOPSaturday, 28 May 2011
  24. 24. GET IT WORKING!Saturday, 28 May 2011
  25. 25. 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. AGAIN?Saturday, 28 May 2011
  26. 26. • 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! SYNTAX FIRSTSaturday, 28 May 2011
  27. 27. TREETWEET LANGUAGE .treetop generates TREETWEET TWITTER LANGUAGE FIREHOSE TREETWEET PARSER is parsed by generates produces TWEET RUBY CODE RUBY OBJECT performs match BOOLEAN RESULT OVERVIEWSaturday, 28 May 2011
  28. 28. COMPILE 1Saturday, 28 May 2011
  29. 29. COMPILE 2Saturday, 28 May 2011
  30. 30. COMPILE 3Saturday, 28 May 2011
  31. 31. COMPILE 4Saturday, 28 May 2011
  32. 32. COMPILE 5Saturday, 28 May 2011
  33. 33. • 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. RECAPSaturday, 28 May 2011
  34. 34. • github.com/vishnugopal • Not released yet, soon! SOURCE CODESaturday, 28 May 2011
  35. 35. codejam.mobme.in QUESTIONS?Saturday, 28 May 2011
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×