Tree Top

Post on 06-Dec-2014

1.210 views 1 download

Tags:

description

Tree Top at RubyConfUy

Transcript of Tree Top

TreeTop (uy version)TreeTop (uy version)

++[>++[>++[>++[>++<-] <-] <-] <-] >>>><+++++[>++++++++++<-] >. <+++[>++++++<-] >+.

<+++++++[>++<-] >. <+[>-<-] >-. <+++[>---------------------------<-] >. <+++++++++++++++++++++++++++++++++++++++++[>++<-] >. <+++[>--<-] >. <+++++++[>++<-] >. <+++++++++++++[>------<-] >. <+++[>----<-] >. <+++++++++++

++++++++++++++++++++++++++++++[>++<-] >+. <++[>--<-] >. <+++++++++++++[>------<-] >-. <+++++++[>++++++++++++<-] >. <+++[>----<-] >. <++[>++++++++<-] >. <++++++++++

+[>--------<-] >. <+++++++[>++++++++++<-] >. <+++[>+++<-] >. <++[>+<-] >+. <+++++++++++++++++++++++++++++++++++++++++[>--<-] >. <+++++++++++[>++++++<-] >+. <+++

[>++++<-] >. <+[>-<-] >-. <+[>+<-] >.

++[>++[>++[>++[>++<-] <-] <-] <-] >>>><+++++[>++++++++++<-] >. <+++[>++++++<-] >+.

<+++++++[>++<-] >. <+[>-<-] >-. <+++[>---------------------------<-] >. <+++++++++++++++++++++++++++++++++++++++++[>++<-] >. <+++[>--<-] >. <+++++++[>++<-] >. <+++++++++++++[>------<-] >. <+++[>----<-] >. <+++++++++++

++++++++++++++++++++++++++++++[>++<-] >+. <++[>--<-] >. <+++++++++++++[>------<-] >-. <+++++++[>++++++++++++<-] >. <+++[>----<-] >. <++[>++++++++<-] >. <++++++++++

+[>--------<-] >. <+++++++[>++++++++++<-] >. <+++[>+++<-] >. <++[>+<-] >+. <+++++++++++++++++++++++++++++++++++++++++[>--<-] >. <+++++++++++[>++++++<-] >+. <+++

[>++++<-] >. <+[>-<-] >-. <+[>+<-] >.

The gloryThe glory

DSL (embedded)DSL (embedded)

Domain.specific do result.should be 2end

L?L?

In order to write a languageAs a Ruby programmer

I want to have an easy and powerful tool

Write a lang? WTF?Write a lang? WTF?

In fact, you do it all the time.Open files, parse stuff, turn it into strings,

numbers, arrays and link it to code.Like when you wrote your first csv parser.

(yeah, i know you did it...)

You just didn’t had the right tool.

BrainFuckBrainFuck

3000 slots

00 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

><+-.,[]

00 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

>00 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

<00 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

+11 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

-00 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

,00 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

,00 00 00 00 00 00 00 00 00

BB

BrainFuckBrainFuck

,6666 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

.6666 00 00 00 00 00 00 00 00

BB

BrainFuckBrainFuck

[]6666 00 00 00 00 00 00 00 00

BrainFuckBrainFuck

[>+]6666 11 11 11 11 11 11 11 11

(now some live coding...)

CFGCFGFormally defined by Noam

Chomsky in the 50s.(Yeah, that weird talking guy from the World Social Forum.

For your info he is a great linguist.)

LEX, Yacc

Idea: Generates a language

CFGCFG

‘if(‘ exp ‘)’ stmt ‘else’ stmt | ‘if(’ exp ‘)’ stmt

oror

CFGCFG

// Wich alert will be executed?

if(false) if(false) alert(“Don’t say ‘the second if alert’.”)else alert(“Say ‘the else alert’. Say it!!!”)

CFGCFGGotcha!!!

None.=> undefined

Generally, languages prefer to associate else to the nearest if. But this has to be made in a “meta” rule.

PEGPEGParsing Expression Grammars

Brian Ford paper 2004: http://pdos.csail.mit.edu/~baford/packrat/

Priorities, Not Ambiguities / (slash operator)

(a / a b) is different of (a b / a)Syntactic Predicates (& and !)

Regular Expression LikeNo left recursion.

Idea: Recognizes a language

PEGPEG

Result:- linear time parsing using memoization- one and only one valid parse tree- no dangling else cases- much easier to learn and useDrawbacks- still new- memory eater- not suitable for natural language processing

PEGPEG

‘if(‘ exp ‘)’ stmt ‘else’ stmt / ‘if(’ exp ‘)’ stmt

priority operatorpriority operator

ProjectsProjectsarithmetic + lambda calculus + sql

gherkin (cucumber)

mail (rails 3 standard)

0xCOFFEE (full lang compiled to LLVM)many more

html, css, json, odp, scheme, apache conf, trxl, lolcode, brainfuck,sloc, hairball (haml), vcard, hapag, dcf, lordbishop (names)

BeyondBeyond

Compile with LLVM(look at 0xCOFFEE)

Compile to Parrot?Implement Ruby?

Watch Citrus

Other langsOther langs

Rats! JavaPetitParser SmallTalk

ppeg, pyPEG, pyparsing PythonPerl6 rules Perl6 (native!)

Frisby, Leafy Haskell

CSV by the wayCSV by the way

grammar Csv rule file (line [\n])* line end rule line (value ',')* value end rule value [^,]+ endend

+++[>+++[>+++++++<-] <-] >><+++[>++++++<-] >. <+++[>++++++++++++<-] >. <++[>--------<-] >. <+++++

++[>++<-] >. <+[>+<-] >. <+++++[>--<-] >-. <+++[>++<-] >. <+[>-<-] >. <++[>++<-] >+. <+++++++++++++

[>----<-] >.

@joaomilho . mailee.merailer.im/chuck-norris