SlideShare a Scribd company logo
Ragel & Ruby
Ry Dahl (ry@tinyclouds.org)
Euruko, November 2007
What is Ragel?
 A parser generator
 Created by Adrian Thurston (Queens
University)
 Ragel compiles to a host language: C, D,
Java, Ruby.
 Compiled code has no dependencies on to
Ragel
What is Ragel useful for?
 Parsing protocols and data formats.
 EG: HTTP, XML, JSON, CSS
 More than a single regular expression
 Speed
 Less than a full LALR parser.
You should use it when you need…
Ruby projects using Ragel
 Mongrel (HTTP request parsing)
 Ruby JSON
 SuperRedCloth (Textile markup)
 Hpricot (HTML parser)
Example: Parsing /_(ab|cd)*_/
%%{
machine ex2;
action foo { f += 1 }
action bar { b += 1 }
x = "ab" %foo;
y = "cd" %bar;
main := "_" (x|y)* "_";
}%%
Actions and Non-Determinism
 Actions can be executed midstream.
%action_name leaving action
$action_name all transition action
>action_name starting action
@action_name finishing action
 Non-Determinism can be controlled by setting
priorities
Actions have the ability to
 Change the state (node)
 Move the data pointer
 Exit the parser
 Anything really…
Actions are written in the host language but can
include special Ragel functions (fcall, fgoto, …)
Interfacing Ruby & Ragel
 Compile into Ruby
 SLOW
 Even if you’re willing to take the speed hit,
might be easier to use adhoc code instead of
Ragel.
 Useful for prototyping and testing a Ragel
machine.
 Write C extension
• FAST
Writing a Ruby-Ragel C extension
 Most important task is to extract string
 Mark the beginning with >mark_action
 At the end save to a VALUE variable with
%save_action
 In the save action use
VALUE rb_str_new(const char* str, long length);
Writing a Ruby-Ragel C extension
An example from Hpricot
/* Adapted from hpricot_scan.rl */
VALUE hpricot_scan(VALUE self, VALUE data)
{
int cs = 0; # current state
char *p = RSTRING_PTR(data); # data pointer
char *pe = p + RSTRING_LEN(data); # data end pointer
VALUE tag = Qnil;
char *mark_tag = 0;
%% write init;
%% write exec;
/*...*/
}
Writing a Ruby-Ragel C extension
An example from Hpricot
%%{
# Adapted from hpricot_scan.rl
machine hpricot_common;
action _tag { mark_tag = p; }
action tag { tag = rb_str_new(mark_tag, p-mark_tag); }
NameChar = [-A-Za-z0-9._:?] ;
Name = [A-Za-z_:] NameChar* ;
NameCap = Name >_tag %tag;
StartTag = "<" NameCap ">";
EndTag = "</" NameCap ">";
# ...
}%%
What if the host language is Ruby?
action _tag { mark_tag = p }
action tag { tag = data[mark_tag..p-1] }
Writing a Ruby-Ragel C extension
 Sometimes desirable to have a Ruby
independent interface
 Not much more work
 Clean interface
 Zed Shaw’s http11 is a good example. He uses
 A struct with callbacks for each token
encountered.
 The struct contains void* pointer for the Ruby
data structure that is being built up during the parsing
typedef void (*element_cb)(void *data, const char *at,
size_t length);
typedef struct http_parser {
int cs;
size_t body_start;
int content_len;
size_t nread;
size_t mark;
size_t field_start;
size_t field_len;
size_t query_start;
void *data;
element_cb request_method;
element_cb request_uri;
element_cb fragment;
element_cb request_path;
element_cb query_string;
element_cb http_version;
element_cb header_done;
} http_parser;
What to take away from this
Ragel is a parser generator being used in
a few Ruby projects
You should read the code of Zed Shaw
and why the lucky stiff.

More Related Content

PDF
HyperLogLog in Hive - How to count sheep efficiently?
bzamecnik
 
PDF
Node.js testing
Nathachai Thongniran
 
PDF
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Data Con LA
 
DOC
An example of bubble sort written in c
Sumedha
 
DOCX
Practica54
Mûzâmîl ßâlõch
 
PPTX
Beyond Lists - Functional Kats Conf Dublin 2015
Phillip Trelford
 
PPTX
Lessons learned from functional programming
BryceLohr
 
PDF
Sperasoft‬ talks j point 2015
Sperasoft
 
HyperLogLog in Hive - How to count sheep efficiently?
bzamecnik
 
Node.js testing
Nathachai Thongniran
 
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Data Con LA
 
An example of bubble sort written in c
Sumedha
 
Beyond Lists - Functional Kats Conf Dublin 2015
Phillip Trelford
 
Lessons learned from functional programming
BryceLohr
 
Sperasoft‬ talks j point 2015
Sperasoft
 

What's hot (13)

PDF
Debugger Principle Overview & GDB Tricks
dutor
 
DOC
Techtest10 schema refresh
gopalkuppuswamy
 
PDF
Data struture lab
krishnamurthy Murthy.Tt
 
PDF
Demystifying Prototypes
Dmitry Baranovskiy
 
PPT
Python Coding Examples for Drive Time Analysis
Wisconsin Land Information Association
 
PPT
Schema Design by Chad Tindel, Solution Architect, 10gen
MongoDB
 
PPTX
ADVANCED FEATURES OF C++
NITHYA KUMAR
 
PDF
2018 cosup-delete unused python code safely - english
Jen Yee Hong
 
ODP
Apache sirona
Olivier Lamy
 
PPTX
Tail Recursion in data structure
Rumman Ansari
 
PPTX
Storm is coming
Grzegorz Kolpuc
 
PDF
201801 CSE240 Lecture 11
Javier Gonzalez-Sanchez
 
DOCX
ng6b8.docx
Jeff Smith
 
Debugger Principle Overview & GDB Tricks
dutor
 
Techtest10 schema refresh
gopalkuppuswamy
 
Data struture lab
krishnamurthy Murthy.Tt
 
Demystifying Prototypes
Dmitry Baranovskiy
 
Python Coding Examples for Drive Time Analysis
Wisconsin Land Information Association
 
Schema Design by Chad Tindel, Solution Architect, 10gen
MongoDB
 
ADVANCED FEATURES OF C++
NITHYA KUMAR
 
2018 cosup-delete unused python code safely - english
Jen Yee Hong
 
Apache sirona
Olivier Lamy
 
Tail Recursion in data structure
Rumman Ansari
 
Storm is coming
Grzegorz Kolpuc
 
201801 CSE240 Lecture 11
Javier Gonzalez-Sanchez
 
ng6b8.docx
Jeff Smith
 
Ad

Similar to Ragel talk (20)

PDF
Playfulness at Work
Erin Dees
 
PDF
Lets build-ruby-app-server: Vineet tyagi
ThoughtWorks
 
PPTX
HTTP::Parser::XS - writing a fast & secure XS module
Kazuho Oku
 
PDF
RubyConf 2015 - Stately State Machines with Ragel - Ian Duggan
ijcd
 
PPTX
Introduction to Ruby Native Extensions and Foreign Function Interface
Oleksii Sukhovii
 
ODP
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
rivierarb
 
PDF
Rubinius - A Tool of the Future
evanphx
 
PDF
Ruby Xml Mapping
Marc Seeger
 
KEY
Rails development environment talk
Reuven Lerner
 
PDF
Ruxmon feb 2013 what happened to rails
snyff
 
ZIP
Ruby Kaigi 2008 LT
Motohiro Takayama
 
PPT
Intro To Ror
myuser
 
ODP
Test
PaulMWatson
 
PDF
Ruby Metaprogramming - OSCON 2008
Brian Sam-Bodden
 
PDF
Rest
Brian Kaney
 
KEY
Picking gem ruby for penetration testers
Paolo Perego
 
PDF
Ruby confhighlights
Claire Tran
 
PPT
Learn Ruby Programming in Amc Square Learning
ASIT Education
 
KEY
I can haz HTTP - Consuming and producing HTTP APIs in the Ruby ecosystem
Sidu Ponnappa
 
PDF
How to Begin to Develop Ruby Core
Hiroshi SHIBATA
 
Playfulness at Work
Erin Dees
 
Lets build-ruby-app-server: Vineet tyagi
ThoughtWorks
 
HTTP::Parser::XS - writing a fast & secure XS module
Kazuho Oku
 
RubyConf 2015 - Stately State Machines with Ragel - Ian Duggan
ijcd
 
Introduction to Ruby Native Extensions and Foreign Function Interface
Oleksii Sukhovii
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
rivierarb
 
Rubinius - A Tool of the Future
evanphx
 
Ruby Xml Mapping
Marc Seeger
 
Rails development environment talk
Reuven Lerner
 
Ruxmon feb 2013 what happened to rails
snyff
 
Ruby Kaigi 2008 LT
Motohiro Takayama
 
Intro To Ror
myuser
 
Ruby Metaprogramming - OSCON 2008
Brian Sam-Bodden
 
Picking gem ruby for penetration testers
Paolo Perego
 
Ruby confhighlights
Claire Tran
 
Learn Ruby Programming in Amc Square Learning
ASIT Education
 
I can haz HTTP - Consuming and producing HTTP APIs in the Ruby ecosystem
Sidu Ponnappa
 
How to Begin to Develop Ruby Core
Hiroshi SHIBATA
 
Ad

More from elliando dias (20)

PDF
Clojurescript slides
elliando dias
 
PDF
Why you should be excited about ClojureScript
elliando dias
 
PDF
Functional Programming with Immutable Data Structures
elliando dias
 
PPT
Nomenclatura e peças de container
elliando dias
 
PDF
Geometria Projetiva
elliando dias
 
PDF
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
PDF
Javascript Libraries
elliando dias
 
PDF
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
PDF
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
PDF
Introdução ao Arduino
elliando dias
 
PDF
Minicurso arduino
elliando dias
 
PDF
Incanter Data Sorcery
elliando dias
 
PDF
Rango
elliando dias
 
PDF
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
PDF
The Digital Revolution: Machines that makes
elliando dias
 
PDF
Hadoop + Clojure
elliando dias
 
PDF
Hadoop - Simple. Scalable.
elliando dias
 
PDF
Hadoop and Hive Development at Facebook
elliando dias
 
PDF
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 
PDF
From Lisp to Clojure/Incanter and RAn Introduction
elliando dias
 
Clojurescript slides
elliando dias
 
Why you should be excited about ClojureScript
elliando dias
 
Functional Programming with Immutable Data Structures
elliando dias
 
Nomenclatura e peças de container
elliando dias
 
Geometria Projetiva
elliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Javascript Libraries
elliando dias
 
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
Introdução ao Arduino
elliando dias
 
Minicurso arduino
elliando dias
 
Incanter Data Sorcery
elliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
The Digital Revolution: Machines that makes
elliando dias
 
Hadoop + Clojure
elliando dias
 
Hadoop - Simple. Scalable.
elliando dias
 
Hadoop and Hive Development at Facebook
elliando dias
 
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 
From Lisp to Clojure/Incanter and RAn Introduction
elliando dias
 

Recently uploaded (20)

PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Software Development Methodologies in 2025
KodekX
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 

Ragel talk

  • 1. Ragel & Ruby Ry Dahl ([email protected]) Euruko, November 2007
  • 2. What is Ragel?  A parser generator  Created by Adrian Thurston (Queens University)  Ragel compiles to a host language: C, D, Java, Ruby.  Compiled code has no dependencies on to Ragel
  • 3. What is Ragel useful for?  Parsing protocols and data formats.  EG: HTTP, XML, JSON, CSS  More than a single regular expression  Speed  Less than a full LALR parser. You should use it when you need…
  • 4. Ruby projects using Ragel  Mongrel (HTTP request parsing)  Ruby JSON  SuperRedCloth (Textile markup)  Hpricot (HTML parser)
  • 5. Example: Parsing /_(ab|cd)*_/ %%{ machine ex2; action foo { f += 1 } action bar { b += 1 } x = "ab" %foo; y = "cd" %bar; main := "_" (x|y)* "_"; }%%
  • 6. Actions and Non-Determinism  Actions can be executed midstream. %action_name leaving action $action_name all transition action >action_name starting action @action_name finishing action  Non-Determinism can be controlled by setting priorities
  • 7. Actions have the ability to  Change the state (node)  Move the data pointer  Exit the parser  Anything really… Actions are written in the host language but can include special Ragel functions (fcall, fgoto, …)
  • 8. Interfacing Ruby & Ragel  Compile into Ruby  SLOW  Even if you’re willing to take the speed hit, might be easier to use adhoc code instead of Ragel.  Useful for prototyping and testing a Ragel machine.  Write C extension • FAST
  • 9. Writing a Ruby-Ragel C extension  Most important task is to extract string  Mark the beginning with >mark_action  At the end save to a VALUE variable with %save_action  In the save action use VALUE rb_str_new(const char* str, long length);
  • 10. Writing a Ruby-Ragel C extension An example from Hpricot /* Adapted from hpricot_scan.rl */ VALUE hpricot_scan(VALUE self, VALUE data) { int cs = 0; # current state char *p = RSTRING_PTR(data); # data pointer char *pe = p + RSTRING_LEN(data); # data end pointer VALUE tag = Qnil; char *mark_tag = 0; %% write init; %% write exec; /*...*/ }
  • 11. Writing a Ruby-Ragel C extension An example from Hpricot %%{ # Adapted from hpricot_scan.rl machine hpricot_common; action _tag { mark_tag = p; } action tag { tag = rb_str_new(mark_tag, p-mark_tag); } NameChar = [-A-Za-z0-9._:?] ; Name = [A-Za-z_:] NameChar* ; NameCap = Name >_tag %tag; StartTag = "<" NameCap ">"; EndTag = "</" NameCap ">"; # ... }%%
  • 12. What if the host language is Ruby? action _tag { mark_tag = p } action tag { tag = data[mark_tag..p-1] }
  • 13. Writing a Ruby-Ragel C extension  Sometimes desirable to have a Ruby independent interface  Not much more work  Clean interface  Zed Shaw’s http11 is a good example. He uses  A struct with callbacks for each token encountered.  The struct contains void* pointer for the Ruby data structure that is being built up during the parsing
  • 14. typedef void (*element_cb)(void *data, const char *at, size_t length); typedef struct http_parser { int cs; size_t body_start; int content_len; size_t nread; size_t mark; size_t field_start; size_t field_len; size_t query_start; void *data; element_cb request_method; element_cb request_uri; element_cb fragment; element_cb request_path; element_cb query_string; element_cb http_version; element_cb header_done; } http_parser;
  • 15. What to take away from this Ragel is a parser generator being used in a few Ruby projects You should read the code of Zed Shaw and why the lucky stiff.