SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Perl 6 rules
Radek Kotowicz
2nd Polish Perl Workshop,
Poznań, May 2014
What drives complexity of Perl 5 regex?
• Basic regular expressions (describing regular
languages) are easy to match: O(|T|) /
O(|r|*|T|)
• Backreferences:
– Exponential time O(2^|T|)
– NP-complete problem (3SAT reduction)
– implicate the use of backtracking algorithms
…
• Look-behind assertions add even more
complexity (input is not consumed): O(2|T|+|r|)
• Experimental features of code assertions
introduced undecidability:
a=~/<?{do 1 while 2;}>/
Apocalypse 5
Perl 5 REGEXes were:
• too compact and 'cute‘
• had too much reliance on too few
metacharacters
• little support for named captures
• little support for grammars
• poor integration with the 'real' language
What’s new in Perl 6 rules?
• Named captures (backported to 5.10)
• Simpler notation for non-capturing groups
• White spaces ignored unless backspaced
• Simplified code assertions (experimental in
5.18)
• New character classes and set operations on
char classes
Now the big things
• Rules are part of the ‘real’ language (same
lexer/parser)
• Support for Parsing Expression Grammars (PEGs)
• Backtracking/matching control
• Code assertions can fail or succeed unlike in Perl
5
$perl -e "'abrakadabra'=~/(ab)(?{0}).*(?{print 'OK'})/“
OK
$perl6 -e "'abrakadabra'~~/(ab)<?{0}>.*<?{say 'OK'}>/“
PEGs
• Similar to CFG but unambiguous in terms of parse
trees
• CFG rules explain how to produce words – PEGs
explain how to parse them:
– CGF for {an bn : n >=1}:
• S -> 'a' S 'b‘
• S -> ε
– PEG
• S ← 'a' S? 'b'
• PEG: negative/positive look-ahead assertions
…
• With PEGs it’s possible to describe some non-
context-free grammars {an bn cn : n >=1}
• Recognizing PEG expressions can be done in
linear time if there’s enough memory
• Rakudo comes with a JSON parser
implementation written in Perl 6 rules!
• Naturaly, Perl6 grammar is also expressed in
PEG
Exploratory parsing
grammar BookGrammar {
rule TOP {<book>+}
regex book {<author><whitespace><title> | <title><whitespace><author>}
regex author { (<name>|<initial>) [<whitespace>[<name>|<initial>]]?
<whitespace> <surename> }
rule name {(<word>)<?{ %firstNames{$0}:exists}>}
rule surename {<word>}
rule title { '"'<word>[<whitespace><word>]*'"'}
token word { <alpha>+ }
token initial { <alpha>.? }
token whitespace {s+}
}
…
sub MAIN() {
my $match = BookGrammar.parse('Joseph Conrad "Lord Jim"');
say $match;
}
…
Joseph Conrad "Lord Jim""
book => "Joseph Conrad "Lord Jim""
author => "Joseph Conrad"
0 => "Joseph"
name => "Joseph"
0 => "Joseph"
word => "Joseph"
alpha => "J"
alpha => "o"
alpha => "s"
alpha => "e"
alpha => "p"
alpha => "h"
whitespace => " "
surename => "Conrad"
word => "Conrad"
alpha => "C"
alpha => "o"
alpha => "n"
alpha => "r"
alpha => "a"
alpha => "d"
whitespace => " "
title => ""Lord Jim""
word => "Lord"
alpha => "L"
alpha => "o"
alpha => "r"
alpha => "d"
whitespace => " "
word => "Jim"
alpha => "J"
alpha => "i"
alpha => "m"
use Grammar::Tracer
C:UsersI079489projectsplpw2014>c:rakudobinperl6.exe parse_books.pl
←[1mTOP←[0m
| ←[1mbook←[0m
| | ←[1mauthor←[0m
| | | ←[1mname←[0m
| | | | ←[1mword←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "J"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "o"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "s"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "e"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "p"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "h"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;41mFAIL←[0m
| | | | * ←[37;42mMATCH←[0m←[37m "Joseph"←[0m
| | | * ←[37;42mMATCH←[0m←[37m "Joseph"←[0m
| | | ←[1mwhitespace←[0m
| | | * ←[37;42mMATCH←[0m←[37m " "←[0m
| | | ←[1mname←[0m
| | | | ←[1mword←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "C"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "o"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "n"←[0m
| | | | | ←[1malpha←[0m
| | | | | * ←[37;42mMATCH←[0m←[37m "r"←[0m
That’s it
Thank you!

Weitere ähnliche Inhalte

Andere mochten auch

SMBE 2015: Rapid Identification of Phylogenetically Informative Data from Nex...
SMBE 2015: Rapid Identification of Phylogenetically Informative Data from Nex...SMBE 2015: Rapid Identification of Phylogenetically Informative Data from Nex...
SMBE 2015: Rapid Identification of Phylogenetically Informative Data from Nex...Rachel Schwartz
 
Copy of che guevara project
Copy of che guevara projectCopy of che guevara project
Copy of che guevara projectjenryder
 
ファッション論総論
ファッション論総論ファッション論総論
ファッション論総論展弘 大野
 
SOLID Tasarım Prensipleri
SOLID Tasarım PrensipleriSOLID Tasarım Prensipleri
SOLID Tasarım PrensipleriKoray Peker
 
Cyberdyne systems (2)
Cyberdyne systems (2)Cyberdyne systems (2)
Cyberdyne systems (2)Bryan Moss
 

Andere mochten auch (8)

SMBE 2015: Rapid Identification of Phylogenetically Informative Data from Nex...
SMBE 2015: Rapid Identification of Phylogenetically Informative Data from Nex...SMBE 2015: Rapid Identification of Phylogenetically Informative Data from Nex...
SMBE 2015: Rapid Identification of Phylogenetically Informative Data from Nex...
 
Rakhi description
Rakhi descriptionRakhi description
Rakhi description
 
Copy of che guevara project
Copy of che guevara projectCopy of che guevara project
Copy of che guevara project
 
ファッション論総論
ファッション論総論ファッション論総論
ファッション論総論
 
SOLID Tasarım Prensipleri
SOLID Tasarım PrensipleriSOLID Tasarım Prensipleri
SOLID Tasarım Prensipleri
 
Yertl v2 granada
Yertl v2 granadaYertl v2 granada
Yertl v2 granada
 
Cyberdyne systems (2)
Cyberdyne systems (2)Cyberdyne systems (2)
Cyberdyne systems (2)
 
Nobles
NoblesNobles
Nobles
 

Ähnlich wie Perl 5 & 6 regex complexity

Use PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language ParserUse PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language ParserYodalee
 
Optimize perl5 code for perfomance freaks
Optimize perl5 code for perfomance freaksOptimize perl5 code for perfomance freaks
Optimize perl5 code for perfomance freakskarupanerura
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeProf. Wim Van Criekinge
 
Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Ramamohan Chokkam
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
APMG juni 2014 - Regular Expression
APMG juni 2014 - Regular ExpressionAPMG juni 2014 - Regular Expression
APMG juni 2014 - Regular ExpressionByte
 
A3 sec -_regular_expressions
A3 sec -_regular_expressionsA3 sec -_regular_expressions
A3 sec -_regular_expressionsa3sec
 
Brogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitBrogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitRon Reiter
 
How to check valid Email? Find using regex.
How to check valid Email? Find using regex.How to check valid Email? Find using regex.
How to check valid Email? Find using regex.Poznań Ruby User Group
 
Perly Parsing with Regexp::Grammars
Perly Parsing with Regexp::GrammarsPerly Parsing with Regexp::Grammars
Perly Parsing with Regexp::GrammarsWorkhorse Computing
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsEran Zimbler
 
Cool Things in Perl 6
Cool Things in Perl 6Cool Things in Perl 6
Cool Things in Perl 6brian d foy
 
What's new in Perl 5.10?
What's new in Perl 5.10?What's new in Perl 5.10?
What's new in Perl 5.10?acme
 
20130530-PEGjs
20130530-PEGjs20130530-PEGjs
20130530-PEGjszuqqhi 2
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perldaoswald
 
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...openCypher
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?lichtkind
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 

Ähnlich wie Perl 5 & 6 regex complexity (20)

Use PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language ParserUse PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language Parser
 
Optimize perl5 code for perfomance freaks
Optimize perl5 code for perfomance freaksOptimize perl5 code for perfomance freaks
Optimize perl5 code for perfomance freaks
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
 
Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
APMG juni 2014 - Regular Expression
APMG juni 2014 - Regular ExpressionAPMG juni 2014 - Regular Expression
APMG juni 2014 - Regular Expression
 
A3 sec -_regular_expressions
A3 sec -_regular_expressionsA3 sec -_regular_expressions
A3 sec -_regular_expressions
 
Brogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitBrogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and Git
 
How to check valid Email? Find using regex.
How to check valid Email? Find using regex.How to check valid Email? Find using regex.
How to check valid Email? Find using regex.
 
Perly Parsing with Regexp::Grammars
Perly Parsing with Regexp::GrammarsPerly Parsing with Regexp::Grammars
Perly Parsing with Regexp::Grammars
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Cool Things in Perl 6
Cool Things in Perl 6Cool Things in Perl 6
Cool Things in Perl 6
 
What's new in Perl 5.10?
What's new in Perl 5.10?What's new in Perl 5.10?
What's new in Perl 5.10?
 
20130530-PEGjs
20130530-PEGjs20130530-PEGjs
20130530-PEGjs
 
Whatsnew in-perl
Whatsnew in-perlWhatsnew in-perl
Whatsnew in-perl
 
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?
 
Perl 101
Perl 101Perl 101
Perl 101
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 

Kürzlich hochgeladen

Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Kürzlich hochgeladen (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

Perl 5 & 6 regex complexity

  • 1. Perl 6 rules Radek Kotowicz 2nd Polish Perl Workshop, Poznań, May 2014
  • 2.
  • 3.
  • 4. What drives complexity of Perl 5 regex? • Basic regular expressions (describing regular languages) are easy to match: O(|T|) / O(|r|*|T|) • Backreferences: – Exponential time O(2^|T|) – NP-complete problem (3SAT reduction) – implicate the use of backtracking algorithms
  • 5. … • Look-behind assertions add even more complexity (input is not consumed): O(2|T|+|r|) • Experimental features of code assertions introduced undecidability: a=~/<?{do 1 while 2;}>/
  • 6. Apocalypse 5 Perl 5 REGEXes were: • too compact and 'cute‘ • had too much reliance on too few metacharacters • little support for named captures • little support for grammars • poor integration with the 'real' language
  • 7. What’s new in Perl 6 rules? • Named captures (backported to 5.10) • Simpler notation for non-capturing groups • White spaces ignored unless backspaced • Simplified code assertions (experimental in 5.18) • New character classes and set operations on char classes
  • 8. Now the big things • Rules are part of the ‘real’ language (same lexer/parser) • Support for Parsing Expression Grammars (PEGs) • Backtracking/matching control • Code assertions can fail or succeed unlike in Perl 5 $perl -e "'abrakadabra'=~/(ab)(?{0}).*(?{print 'OK'})/“ OK $perl6 -e "'abrakadabra'~~/(ab)<?{0}>.*<?{say 'OK'}>/“
  • 9. PEGs • Similar to CFG but unambiguous in terms of parse trees • CFG rules explain how to produce words – PEGs explain how to parse them: – CGF for {an bn : n >=1}: • S -> 'a' S 'b‘ • S -> ε – PEG • S ← 'a' S? 'b' • PEG: negative/positive look-ahead assertions
  • 10. … • With PEGs it’s possible to describe some non- context-free grammars {an bn cn : n >=1} • Recognizing PEG expressions can be done in linear time if there’s enough memory • Rakudo comes with a JSON parser implementation written in Perl 6 rules! • Naturaly, Perl6 grammar is also expressed in PEG
  • 11. Exploratory parsing grammar BookGrammar { rule TOP {<book>+} regex book {<author><whitespace><title> | <title><whitespace><author>} regex author { (<name>|<initial>) [<whitespace>[<name>|<initial>]]? <whitespace> <surename> } rule name {(<word>)<?{ %firstNames{$0}:exists}>} rule surename {<word>} rule title { '"'<word>[<whitespace><word>]*'"'} token word { <alpha>+ } token initial { <alpha>.? } token whitespace {s+} }
  • 12. … sub MAIN() { my $match = BookGrammar.parse('Joseph Conrad "Lord Jim"'); say $match; }
  • 13. … Joseph Conrad "Lord Jim"" book => "Joseph Conrad "Lord Jim"" author => "Joseph Conrad" 0 => "Joseph" name => "Joseph" 0 => "Joseph" word => "Joseph" alpha => "J" alpha => "o" alpha => "s" alpha => "e" alpha => "p" alpha => "h" whitespace => " " surename => "Conrad" word => "Conrad" alpha => "C" alpha => "o" alpha => "n" alpha => "r" alpha => "a" alpha => "d" whitespace => " " title => ""Lord Jim"" word => "Lord" alpha => "L" alpha => "o" alpha => "r" alpha => "d" whitespace => " " word => "Jim" alpha => "J" alpha => "i" alpha => "m"
  • 14. use Grammar::Tracer C:UsersI079489projectsplpw2014>c:rakudobinperl6.exe parse_books.pl ←[1mTOP←[0m | ←[1mbook←[0m | | ←[1mauthor←[0m | | | ←[1mname←[0m | | | | ←[1mword←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "J"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "o"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "s"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "e"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "p"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "h"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;41mFAIL←[0m | | | | * ←[37;42mMATCH←[0m←[37m "Joseph"←[0m | | | * ←[37;42mMATCH←[0m←[37m "Joseph"←[0m | | | ←[1mwhitespace←[0m | | | * ←[37;42mMATCH←[0m←[37m " "←[0m | | | ←[1mname←[0m | | | | ←[1mword←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "C"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "o"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "n"←[0m | | | | | ←[1malpha←[0m | | | | | * ←[37;42mMATCH←[0m←[37m "r"←[0m