SlideShare a Scribd company logo
Parboiled2
explained
Covered
 Why Parboiled2
 Library basics
 Perfomance optimizations
 Best Practices
 Migration
Features
 PEG
 No lexer required
 Flexible typesfe EDSL
 Compile-time optimizations
 Decent error reporting
 scala.js support
When regex fail
Parse arbitrary HTML with regexes
is like asking Paris Hilton to write an
operating system (c)
When regex fail
Performance (regex)
Parsing
Warmup
620.38
621.95
Parboiled2
Regex
Data is taken from here:
http://bit.ly/1XHAJaA
Lower is better
Performance (json)
Parboiled1
Parboiled2
Argonaut
Json4SNative
Json4SJackson
85.64
13.17
7.01
8.06
4.09
Data is taken from here:
http://myltsev.name/ScalaDays2014/#/
Lower is better
Performance (json)
Parser combinators
Parboiled1
Parboiled2
Argonaut
Json4SNative
Json4SJackson
2385.78
85.64
13.17
7.01
8.06
4.09
Data is taken from here:
https://groups.google.com/forum/#!topic/parboiled-user/bGtdGvllGgU
Lower is better
Alternatives
● Grappa [java]
● ANTLR
● Regexps
● Parser-combinators
● Language Workbenches (xtext, MPS)
<dependency>
<groupId>org.parboiled</groupId>
<artifactId>parboiled_2.11</artifactId>
<version>2.1.0</version>
</dependency>
import org.parboiled2._
class MyParser (val input: ParserInput)
extends Parser {
// Your grammar
}
Rule DSL
Basic match
def CaseDoesntMatter = rule {
ignoreCase("string")
}
def MyCharRule = rule { 'a' }
def MyStringRule = rule { "string" }
def MyCharRule = rule { ch('a') }
def MyStringRule = rule { str("string") }
Basic match
def CaseDoesntMatter: Rule0 =
rule { ignoreCase("string") }
def MyCharRule: Rule0 =
rule {'a'}
def MyStringRule: Rule0 =
rule { "string" }
Syntactic predicates
● ANY – matches any character except EOI
● EOI – virtual chararter represents the end of
input
val EOI = 'uFFFF'
You must define EOI at the end of the
main/root rule
Syntactic predicates
● anyOf – at least one of the defined chars
● noneOf – everything except those chars
def Digit = rule {
anyOf("1234567890")
}
def Visible = rule {
noneOf(" nt")
}
Character ranges
def Digit = rule { '0' - '9' }
def AlphaLower = rule { 'a' - 'z' }
Good, but not flexible
(the main issue of parboiled1)
● Sometimes you don't need ANY character
● You have a range of characters
Character predicates
There is set of predifined char predicates:
● CharPredicate.All
● CharPredicate.Digit
● CharPredicate.Digit19
● CharPredicate.HexDigit
Of course you can defien your own
def AllButQuotes = rule {
CharPredicate.Visible -- """ -- "'"
}
def ValidIdentifier = rule {
CharPredicate.AlphaNum ++ "_"
}
CharPredicate from (_.isSpaceChar)
Character predicates
def ArithmeticOperation = rule {
anyOf("+-*/^")
}
def WhiteSpaceChar = rule {
noneOf(" tn")
}
anyOf/noneOf
def cows =
rule { 1000 times "cow" }
def PRI =
rule { 1 to 3 times Digit }
N times
def OptWs = rule {
zeroOrMore(Whitespace) // Whitespace.*
}
def UInt = rule {
oneOrMore(Digit) // Whitespace.+
}
def CommaSeparatedNumbers = rule {
oneOrMore(UInt).separatedBy(",")
}
0+/1+
import CharPredicate.Digit
// "yyyy-mm-dd"
def SimplifiedRuleForDate = rule {
Year ~ "-" ~ Month ~ "-" ~ Day
}
def Year = rule {
Digit ~ Digit ~ Digit ~ Digit
}
def Month = rule { Digit ~ Digit }
def Day = rule { Digit ~ Digit }
Sequence
// zeroOrOne
def Newline = rule {
optional('r') ~ 'n'
}
def Newline = rule {
'r'.? ~ 'n'
}
Optional
def Signum = rule { '+' | '-' }
def bcd = rule {
'b' ~ 'c' | 'b' ~ 'd'
}
Ordered choice
// why order matters
def Operator = rule {
"+=" | "-=" | "*=" | "++" |
"--" | "+" | "-" | "*" | "/" ...
}
def Operators = rule {
("+" ~ ("=" | "+").?) |
("-" ~ ("=" | "-").?) | ...
}
Order matters
Running the parser
class MyParser(val input: ParserInput)
extends Parser {
def MyStringRule: Rule0 = rule {
ignoreCase("match") ~ EOI
}
}
Running the parser
val p1 = new MyParser("match")
val p2 = new MyParser("much")
p1.MyStringRule.run() // Success
p2.MyStringRule.run() // Failure
Different delivery schemes are also available
Running the parser
val p1 = new MyParser("match")
val p2 = new MyParser("much")
p1.MyStringRule.run() // Success
p2.MyStringRule.run() // Failure
Different delivery schemes are also available
BKV
server.name = "webserver"
server {
port = "8080"
address = "192.168.88.88"
settings {
greeting_message = "Hello!n It's me!"
}
}
Performance
Unroll n.times for n <=4
// Slower
rule { 4 times Digit }
// Faster
rule { Digit ~ Digit ~ Digit ~ Digit }
Faster stack operations
// Much faster
def Digit4 = rule {
Digit ~ Digit ~ Digit ~ Digit ~
push(
#(charAt(-4))*1000 +
#(charAt(-3))*100 +
#(charAt(-2))*10 +
#(lastChar)
)
}
Do not recreate CharPredicate
class MyParser(val input: ParserInput)
extends Parser {
val Uppercase =
CharPredicate.from(_.isUpper)
…
}
Use predicates
def foo = rule {
capture(zeroOrMore(noneOf("n")))
}
def foo = rule {
capture(zeroOrMore(!'n')) //loop here
}
def foo = rule {
capture(zeroOrMore( !'n' ~ ANY ))
}
Best Practices
Best Practices
● Unit tests
● Small rules
● Decomposition
● Case objects instead of strings
Push case objects
def LogLevel = rule {
capture("info" | "warning" | "error")
}
def LogLevel = rule {
“info” ~ push(LogLevel.Info)
| “warning" ~ push(LogLevel.Warning)
| “error" ~ push(LogLevel.Error)
}
Simple syntax for object capture
case class Text(s: String)
def charsAST: Rule1[AST] = rule {
capture(Chars) ~> ((s: String) => Text(s))
}
def charsAST = rule {
capture(Chars) ~> Text
}
Named rules
def Header: Rule1[Header] =
rule("I am header") { ... }
def Header: Rule1[Header] = namedRule("header")
{...}
def UserName = rule {
Prefix ~ oneOrMore(NameChar).named("username")
}
Migration
Migration
● Separate classpath
org.parboiled vs org.parboiled2
● Grammar is hard to break
● Compotition: trait → abstract class
● Removing primitives library
Drawbacks
Drawbacks
● PEG (absence of lexer)
● No support for left recursive grammars
● No error recovery mechanism
● No IDE support
● No support for indentation based grammars
● Awful non informative error messages
Parboiled explained
Q/A

More Related Content

KEY
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
PPTX
Compare mysql5.1.50 mysql5.5.8
PPTX
Mysql5.1 character set testing
PPTX
Mysql handle socket
PDF
TestR: generating unit tests for R internals
PDF
PyCon KR 2019 sprint - RustPython by example
PDF
RestMQ - HTTP/Redis based Message Queue
PDF
Tuga IT 2017 - Redis
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Compare mysql5.1.50 mysql5.5.8
Mysql5.1 character set testing
Mysql handle socket
TestR: generating unit tests for R internals
PyCon KR 2019 sprint - RustPython by example
RestMQ - HTTP/Redis based Message Queue
Tuga IT 2017 - Redis

What's hot (20)

PDF
R/C++ talk at earl 2014
PDF
Job Queue in Golang
PDF
The Ring programming language version 1.5.3 book - Part 87 of 184
PPTX
Interface record comparator
PDF
Source Plugins
PDF
Async Microservices with Twitter's Finagle
PDF
The Ring programming language version 1.5.3 book - Part 34 of 184
PDF
The Ring programming language version 1.5.1 book - Part 33 of 180
PPTX
Sharding and Load Balancing in Scala - Twitter's Finagle
PDF
Finch + Finagle OAuth2
PDF
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
PDF
The Ring programming language version 1.10 book - Part 44 of 212
PDF
The Ring programming language version 1.7 book - Part 38 of 196
PDF
用 Go 語言打造多台機器 Scale 架構
PDF
Wprowadzenie do technologi Big Data i Apache Hadoop
ODP
Using Logstash, elasticsearch & kibana
PDF
Javascript compilation execution
PDF
Rop and it's friends
PPTX
Javascript Execution Context Flow
PPTX
Deep Dumpster Diving
R/C++ talk at earl 2014
Job Queue in Golang
The Ring programming language version 1.5.3 book - Part 87 of 184
Interface record comparator
Source Plugins
Async Microservices with Twitter's Finagle
The Ring programming language version 1.5.3 book - Part 34 of 184
The Ring programming language version 1.5.1 book - Part 33 of 180
Sharding and Load Balancing in Scala - Twitter's Finagle
Finch + Finagle OAuth2
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
The Ring programming language version 1.10 book - Part 44 of 212
The Ring programming language version 1.7 book - Part 38 of 196
用 Go 語言打造多台機器 Scale 架構
Wprowadzenie do technologi Big Data i Apache Hadoop
Using Logstash, elasticsearch & kibana
Javascript compilation execution
Rop and it's friends
Javascript Execution Context Flow
Deep Dumpster Diving
Ad

Viewers also liked (17)

PPT
Lupe Fiasco
PPTX
Tugas bahasa indonesia
PDF
Manco_Employment Cer
PDF
poster_Dortmund
PDF
Az RTL Group 2015-ös eredményei
PDF
Как завязать платок, шарф, парео
PPT
PEMBELAJARAN TEKHNOLOGI INFORMASI
DOCX
Practicum - Ammachi Plavu Neyyattinkara
PDF
Componentes del juego
PPT
The Vine and the Wine in Art
PDF
CG_Brand Positioning Campaign
PDF
Barnäktenskap
PDF
Beyond Scala Lens
PPTX
Ciclo de las politicas publicas
ODP
Scala lens: An introduction
Lupe Fiasco
Tugas bahasa indonesia
Manco_Employment Cer
poster_Dortmund
Az RTL Group 2015-ös eredményei
Как завязать платок, шарф, парео
PEMBELAJARAN TEKHNOLOGI INFORMASI
Practicum - Ammachi Plavu Neyyattinkara
Componentes del juego
The Vine and the Wine in Art
CG_Brand Positioning Campaign
Barnäktenskap
Beyond Scala Lens
Ciclo de las politicas publicas
Scala lens: An introduction
Ad

Similar to Parboiled explained (20)

PDF
Solr @ Etsy - Apache Lucene Eurocon
PDF
Writing Perl 6 Rx
PDF
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
ZIP
Lisp Macros in 20 Minutes (Featuring Clojure)
PPTX
Groovy
PDF
Alexey Tsoy Meta Programming in C++ 16.11.17
PPTX
What is new in Java 8
PDF
Scala in Places API
PDF
Logstash-Elasticsearch-Kibana
PPTX
Php & my sql
PDF
Hacking parse.y (RubyKansai38)
PDF
Design Summit - Rails 4 Migration - Aaron Patterson
PDF
Rust ⇋ JavaScript
PDF
PHP Conference Asia 2016
PDF
PPTX
PHP PPT FILE
PPTX
Beyond java8
PDF
How to not write a boring test in Golang
PDF
User Defined Aggregation in Apache Spark: A Love Story
PDF
User Defined Aggregation in Apache Spark: A Love Story
Solr @ Etsy - Apache Lucene Eurocon
Writing Perl 6 Rx
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
Lisp Macros in 20 Minutes (Featuring Clojure)
Groovy
Alexey Tsoy Meta Programming in C++ 16.11.17
What is new in Java 8
Scala in Places API
Logstash-Elasticsearch-Kibana
Php & my sql
Hacking parse.y (RubyKansai38)
Design Summit - Rails 4 Migration - Aaron Patterson
Rust ⇋ JavaScript
PHP Conference Asia 2016
PHP PPT FILE
Beyond java8
How to not write a boring test in Golang
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPT
Introduction Database Management System for Course Database
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administraation Chapter 3
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Understanding Forklifts - TECH EHS Solution
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
history of c programming in notes for students .pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
Softaken Excel to vCard Converter Software.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Introduction Database Management System for Course Database
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administraation Chapter 3
Odoo POS Development Services by CandidRoot Solutions
PTS Company Brochure 2025 (1).pdf.......
Online Work Permit System for Fast Permit Processing
Understanding Forklifts - TECH EHS Solution
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
How to Migrate SBCGlobal Email to Yahoo Easily
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Wondershare Filmora 15 Crack With Activation Key [2025

Parboiled explained