SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances

Debugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB TricksDebugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB Tricks
dutor
 
Techtest10 schema refresh
Techtest10 schema refreshTechtest10 schema refresh
Techtest10 schema refresh
gopalkuppuswamy
 

Tendances (13)

Debugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB TricksDebugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB Tricks
 
Techtest10 schema refresh
Techtest10 schema refreshTechtest10 schema refresh
Techtest10 schema refresh
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
Demystifying Prototypes
Demystifying PrototypesDemystifying Prototypes
Demystifying Prototypes
 
Python Coding Examples for Drive Time Analysis
Python Coding Examples for Drive Time AnalysisPython Coding Examples for Drive Time Analysis
Python Coding Examples for Drive Time Analysis
 
Schema Design by Chad Tindel, Solution Architect, 10gen
Schema Design  by Chad Tindel, Solution Architect, 10genSchema Design  by Chad Tindel, Solution Architect, 10gen
Schema Design by Chad Tindel, Solution Architect, 10gen
 
ADVANCED FEATURES OF C++
ADVANCED FEATURES OF C++ADVANCED FEATURES OF C++
ADVANCED FEATURES OF C++
 
2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english
 
Apache sirona
Apache sironaApache sirona
Apache sirona
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
Storm is coming
Storm is comingStorm is coming
Storm is coming
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 
ng6b8.docx
ng6b8.docxng6b8.docx
ng6b8.docx
 

Similaire à Ragel talk

Web aplikāciju izstrāde ar Ruby on Rails un Oracle DB
Web aplikāciju izstrāde ar Ruby on Rails un Oracle DBWeb aplikāciju izstrāde ar Ruby on Rails un Oracle DB
Web aplikāciju izstrāde ar Ruby on Rails un Oracle DB
Raimonds Simanovskis
 
Jscript Fundamentals
Jscript FundamentalsJscript Fundamentals
Jscript Fundamentals
rspaike
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrāde
Raimonds Simanovskis
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packages
Ajay Ohri
 
Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
Sages
 

Similaire à Ragel talk (20)

Web aplikāciju izstrāde ar Ruby on Rails un Oracle DB
Web aplikāciju izstrāde ar Ruby on Rails un Oracle DBWeb aplikāciju izstrāde ar Ruby on Rails un Oracle DB
Web aplikāciju izstrāde ar Ruby on Rails un Oracle DB
 
Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout
 
Jscript Fundamentals
Jscript FundamentalsJscript Fundamentals
Jscript Fundamentals
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrāde
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
Lrz kurse: r as superglue
Lrz kurse: r as superglueLrz kurse: r as superglue
Lrz kurse: r as superglue
 
Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_maps
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for R
 
CGI.ppt
CGI.pptCGI.ppt
CGI.ppt
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
No more struggles with Apache Spark workloads in production
No more struggles with Apache Spark workloads in productionNo more struggles with Apache Spark workloads in production
No more struggles with Apache Spark workloads in production
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packages
 
7 functions
7  functions7  functions
7 functions
 
Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
 
Ruby on Rails Intro
Ruby on Rails IntroRuby on Rails Intro
Ruby on Rails Intro
 
Microsoft R - Data Science at Scale
Microsoft R - Data Science at ScaleMicrosoft R - Data Science at Scale
Microsoft R - Data Science at Scale
 

Plus de elliando dias

Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
elliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
elliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
elliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
elliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
elliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
elliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introduction
elliando dias
 

Plus de elliando dias (20)

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

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Ragel talk

  • 1. Ragel & Ruby Ry Dahl (ry@tinyclouds.org) 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.