SlideShare a Scribd company logo
1 of 39
Download to read offline
Efficient Regular Expressions that produce Parse Trees
Aaron Karper Niko Schwarz
University of Bern

January 7, 2014

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

1 / 38
Regular expressions so far

Regular expressions

https? : // (([a − z] + .) + ([a − z]+)) ((/[a − z0 − 9]+)/?)
domain

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

path segments

January 7, 2014

2 / 38
Regular expressions so far

Regular expressions

https? : // (([a − z] + .) + ([a − z]+)) ((/[a − z0 − 9]+)/?)
domain

path segments

http : // www . reddit . com / r / computerscience / comments / 1sg 69d /
domain domain domain

Aaron Karper, Niko Schwarz (UniBe)

path

path

Regex Parse Trees

path

path

January 7, 2014

2 / 38
Regular expressions so far

Regular expressions are greedy by default:
(a+)(a?) on "aaa" → "aaa" in group 0 and "" in group 1.

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

3 / 38
Regular expressions so far

Regular expressions so far

Posix gives only one match.
Regular languages are recognized, but parsing with combinatorical parsers
takes O(n3 ).
Backtracking implementations (Java, python, perl, . . . ) are exponentially
slow in the worst case.

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

4 / 38
Benchmarks

Parsing with https?://(([a-z]+.)+([a-z]+))((/[a-z0-9]+)/?)
0

3
2
4
http:// www. reddit. com /r /computerscience /comments /1sg69d
1

Figure : Posix
0

1
3
4 4
4
4
2
2
2
http:// www. reddit. com /r /computerscience /comments /1sg69d

Figure : Our approach

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

5 / 38
Benchmarks

Benchmarks

Matching ((a+b)+c)+ against
(a200 bc)2000 .
Tool

Time

JParsec
java.util.regex
Ours

Extract all class names from our project
with complex regular expression1 .

4,498
1,992
5,332

Tool

Time

java.util.regex
Ours

11,319
8,047

1 (.*?([a-z]+.)*([A-Z][a-zA-Z]*))*.*?
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

6 / 38
Benchmarks

Optimizations of the algorithm

Benchmarks – Optimizations of the algorithm

Typically most time is spent in long repetitions, we optimize for that case by:
Lazily compile deterministic FA.
Avoiding to recreate state if seen similar state.
Use compressed representation if in static repetition.

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

7 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+

Parse
(a?(a)b)+
over
”aabab”
01234

1

2

1
2

a a b a b
0 1 2 3 4
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

8 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

[[], [], [], []]

q5

q3

q4

q7

q8

-

q6

q9

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

9 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

[[], [], [], []]

[[0], [], [], []]

q5

q6

q4

q7

q8

-

q9

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

10 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

[[], [], [], []]

[[0], [], [], []]

[[0], [], [], []]

q5

q6

-

q4

q7

q8

q9

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

11 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

q4

[[], [], [], []]

[[0], [], [], []]

[[0], [], [], []]

[[0], [], [0], []]

q5

q6

q7

q8

-

q9

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

12 / 38
Benchmarks

NFA interpretation

Threads

State:

q

Copy of thread is modified.
Copy of array of histories makes
reading a character O(m2 )

Histories:
h1 h2 h3 h4 h5 h6

Aaron Karper, Niko Schwarz (UniBe)

Need faster persistent data
structure to get O(m log m).

Regex Parse Trees

January 7, 2014

13 / 38
Benchmarks

NFA interpretation

Optimized thread forking
Set entry 2 to 20:
1

2

9

3

4

6

5

Aaron Karper, Niko Schwarz (UniBe)

7

10

8

Regex Parse Trees

11

13

12

January 7, 2014

14 / 38
Benchmarks

NFA interpretation

Optimized thread forking
Set entry 2 to 20:
1

2

20

3

4

1

9

6

5

Aaron Karper, Niko Schwarz (UniBe)

7

10

8

Regex Parse Trees

11

13

12

January 7, 2014

15 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

q4

[[], [], [], []]

[[0], [], [], []]

[[0], [], [], []]

[[0], [], [0], []]

q5

q6

q7

q8

-

q9

For each character read, threads start hungry and must eat immediately.
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

16 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

[[], [], [], []]

[[0], [], [], []]

[[0], [], [], []]

q5

q6

-

q4

q7

q8

[[0], [], [0], []]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

17 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

[[], [], [], []]

[[0], [], [], []]

[[0], [], [], []]

q5

q6

[[0], [], [0], []]

-

q4

[[0], [], [0], [0]]

q7

q8

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

18 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

[[], [], [], []]

[[0], [], [], []]

q7

q5

q8

-

q6

[[0], [], [0], []]

q4

[[0], [], [0], [0]]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

19 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q2

q1

q3

q5

q8

q6

[[0], [], [0], []]

[[0], [], [1], []]

q7

-

q4

[[0], [], [], []]

[[0], [], [0], [0]]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

20 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q2

q1

q3

q4

-

q5

q6

[[0], [], [1], []]

q7

q8

[[0], [], [0], [0]]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

21 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q2

q1

q3

q4

-

q5

q6

[[0], [], [1], []]

q7

q8

[[0], [], [0], [0]]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

22 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q2

q1

q3

q4

q7

q8

-

q5

q6

[[0], [], [1], []]

[[0], [], [1], [1]]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

23 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q2

q1

q3

q4

q7

q8

-

q5

q6
[[0], [], [1], [1]]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

24 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

q4

[[0], [2], [1], [1]]

[[0,2], [2], [1], [1]]

[[0,2], [2], [1], [1]]

[[0,2], [2], [1,3], [1]]

q5

q6

q7

q8

[[0], [], [1], [1]]

[[0], [2], [1], [1]]

-

q9
[[0], [2], [1], [1]]

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

25 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

q3

q4

[[0], [2], [1], [1]]

[[0,2], [2], [1], [1]]

[[0,2], [2], [1], [1]]

[[0,2], [2], [1,3], [1]]

q5

q6

q7

q8

[[0], [], [1], [1]]

[[0], [2], [1], [1]]

-

q9
[[0], [2], [1], [1]]

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

26 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q2

q1

q3

q5

q8

q6

[[0,2], [2], [1,3], [1]]

[[0,2], [2], [1,4], [1]]

q7

-

q4

[[0,2], [2], [1], [1]]

[[0,2], [2], [1,3], [1,3]]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

27 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q2

q1

q3

q5

q8

q6

[[0,2], [2], [1,3], [1]]

[[0,2], [2], [1,4], [1]]

q7

-

q4

[[0,2], [2], [1], [1]]

[[0,2], [2], [1,3], [1,3]]

q9

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

28 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q1

q2

[[0,2], [2,4], [1,3], [1,3]]

[[0,2,5], [2,4], [1,3], [1,3]]

q3

q5

q6

-

q4

[[0,2,5], [2,4], [1,3], [1,3]]

[[0,2,5], [2,4,5], [1,3], [1,3]]

q7

q8

[[0,2], [2], [1,3], [1,3]]

[[0,2], [2,4], [1,3], [1,3]]

q9
[[0,2], [2,4], [1,3], [1,3]]

For each character read, threads start hungry and must eat immediately.
Only a hungry thread can eat
Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

29 / 38
Benchmarks

NFA interpretation

Example: (a?(a)b)+
Reading "aabab"
01234

q9
[[0,2], [2,4], [1,3], [1,3]]

1

2

1
2

a a b a b
0 1 2 3 4

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

30 / 38
Download

https://github.com/nes1983/tree-regex

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

31 / 38
NFA construction
S1

S

-

Optional
S?

S2
Alternation
S1|S2

S

S

-

Capture group
(S)

Star operation
S*?

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

32 / 38
Backtracking’s nightmare

(a + a+) + b
against
”an b”
will backtrack Θ(2n ) times.

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

33 / 38
Backtracking’s nightmare

Extract the first cell in a CSV that starts with "P"1 :
∧(.∗?, ) + (P.∗?),
failing against
”1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13”
is exponential.

1 From

http://www.regular-expressions.info/catastrophic.html

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

34 / 38
Thread execution order matters

.*(a?)
start

q1

τ1 ↑

q3

a

q4

τ1 ↓

q5

any

q2

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

35 / 38
Priority matters

(a)|(a)
q2

a

q4
τ1 ↓

τ1 ↑

start

q1

q6
τ2 ↑

τ2 ↓
q3

Aaron Karper, Niko Schwarz (UniBe)

a

q5

Regex Parse Trees

January 7, 2014

36 / 38
Optimization Pipeline

1

Convert to nondeterministic FA

2

Interpret nondeterministic FA, building deterministic FA lazily.

3

Find similar/mappable states to avoid creating infinite DFA.

4

Run on DFA if possible

5

Compactify DFA if creation of new states wasn’t necessary for a while.

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

37 / 38
NFA interpretation

Aaron Karper, Niko Schwarz (UniBe)

Regex Parse Trees

January 7, 2014

38 / 38

More Related Content

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Recently uploaded (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Regular expression that produce parse trees

  • 1. Efficient Regular Expressions that produce Parse Trees Aaron Karper Niko Schwarz University of Bern January 7, 2014 Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 1 / 38
  • 2. Regular expressions so far Regular expressions https? : // (([a − z] + .) + ([a − z]+)) ((/[a − z0 − 9]+)/?) domain Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees path segments January 7, 2014 2 / 38
  • 3. Regular expressions so far Regular expressions https? : // (([a − z] + .) + ([a − z]+)) ((/[a − z0 − 9]+)/?) domain path segments http : // www . reddit . com / r / computerscience / comments / 1sg 69d / domain domain domain Aaron Karper, Niko Schwarz (UniBe) path path Regex Parse Trees path path January 7, 2014 2 / 38
  • 4. Regular expressions so far Regular expressions are greedy by default: (a+)(a?) on "aaa" → "aaa" in group 0 and "" in group 1. Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 3 / 38
  • 5. Regular expressions so far Regular expressions so far Posix gives only one match. Regular languages are recognized, but parsing with combinatorical parsers takes O(n3 ). Backtracking implementations (Java, python, perl, . . . ) are exponentially slow in the worst case. Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 4 / 38
  • 6. Benchmarks Parsing with https?://(([a-z]+.)+([a-z]+))((/[a-z0-9]+)/?) 0 3 2 4 http:// www. reddit. com /r /computerscience /comments /1sg69d 1 Figure : Posix 0 1 3 4 4 4 4 2 2 2 http:// www. reddit. com /r /computerscience /comments /1sg69d Figure : Our approach Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 5 / 38
  • 7. Benchmarks Benchmarks Matching ((a+b)+c)+ against (a200 bc)2000 . Tool Time JParsec java.util.regex Ours Extract all class names from our project with complex regular expression1 . 4,498 1,992 5,332 Tool Time java.util.regex Ours 11,319 8,047 1 (.*?([a-z]+.)*([A-Z][a-zA-Z]*))*.*? Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 6 / 38
  • 8. Benchmarks Optimizations of the algorithm Benchmarks – Optimizations of the algorithm Typically most time is spent in long repetitions, we optimize for that case by: Lazily compile deterministic FA. Avoiding to recreate state if seen similar state. Use compressed representation if in static repetition. Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 7 / 38
  • 9. Benchmarks NFA interpretation Example: (a?(a)b)+ Parse (a?(a)b)+ over ”aabab” 01234 1 2 1 2 a a b a b 0 1 2 3 4 Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 8 / 38
  • 10. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 [[], [], [], []] q5 q3 q4 q7 q8 - q6 q9 Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 9 / 38
  • 11. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 [[], [], [], []] [[0], [], [], []] q5 q6 q4 q7 q8 - q9 Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 10 / 38
  • 12. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 [[], [], [], []] [[0], [], [], []] [[0], [], [], []] q5 q6 - q4 q7 q8 q9 Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 11 / 38
  • 13. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 q4 [[], [], [], []] [[0], [], [], []] [[0], [], [], []] [[0], [], [0], []] q5 q6 q7 q8 - q9 Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 12 / 38
  • 14. Benchmarks NFA interpretation Threads State: q Copy of thread is modified. Copy of array of histories makes reading a character O(m2 ) Histories: h1 h2 h3 h4 h5 h6 Aaron Karper, Niko Schwarz (UniBe) Need faster persistent data structure to get O(m log m). Regex Parse Trees January 7, 2014 13 / 38
  • 15. Benchmarks NFA interpretation Optimized thread forking Set entry 2 to 20: 1 2 9 3 4 6 5 Aaron Karper, Niko Schwarz (UniBe) 7 10 8 Regex Parse Trees 11 13 12 January 7, 2014 14 / 38
  • 16. Benchmarks NFA interpretation Optimized thread forking Set entry 2 to 20: 1 2 20 3 4 1 9 6 5 Aaron Karper, Niko Schwarz (UniBe) 7 10 8 Regex Parse Trees 11 13 12 January 7, 2014 15 / 38
  • 17. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 q4 [[], [], [], []] [[0], [], [], []] [[0], [], [], []] [[0], [], [0], []] q5 q6 q7 q8 - q9 For each character read, threads start hungry and must eat immediately. Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 16 / 38
  • 18. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 [[], [], [], []] [[0], [], [], []] [[0], [], [], []] q5 q6 - q4 q7 q8 [[0], [], [0], []] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 17 / 38
  • 19. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 [[], [], [], []] [[0], [], [], []] [[0], [], [], []] q5 q6 [[0], [], [0], []] - q4 [[0], [], [0], [0]] q7 q8 q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 18 / 38
  • 20. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 [[], [], [], []] [[0], [], [], []] q7 q5 q8 - q6 [[0], [], [0], []] q4 [[0], [], [0], [0]] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 19 / 38
  • 21. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q2 q1 q3 q5 q8 q6 [[0], [], [0], []] [[0], [], [1], []] q7 - q4 [[0], [], [], []] [[0], [], [0], [0]] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 20 / 38
  • 22. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q2 q1 q3 q4 - q5 q6 [[0], [], [1], []] q7 q8 [[0], [], [0], [0]] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 21 / 38
  • 23. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q2 q1 q3 q4 - q5 q6 [[0], [], [1], []] q7 q8 [[0], [], [0], [0]] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 22 / 38
  • 24. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q2 q1 q3 q4 q7 q8 - q5 q6 [[0], [], [1], []] [[0], [], [1], [1]] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 23 / 38
  • 25. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q2 q1 q3 q4 q7 q8 - q5 q6 [[0], [], [1], [1]] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 24 / 38
  • 26. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 q4 [[0], [2], [1], [1]] [[0,2], [2], [1], [1]] [[0,2], [2], [1], [1]] [[0,2], [2], [1,3], [1]] q5 q6 q7 q8 [[0], [], [1], [1]] [[0], [2], [1], [1]] - q9 [[0], [2], [1], [1]] For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 25 / 38
  • 27. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 q3 q4 [[0], [2], [1], [1]] [[0,2], [2], [1], [1]] [[0,2], [2], [1], [1]] [[0,2], [2], [1,3], [1]] q5 q6 q7 q8 [[0], [], [1], [1]] [[0], [2], [1], [1]] - q9 [[0], [2], [1], [1]] For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 26 / 38
  • 28. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q2 q1 q3 q5 q8 q6 [[0,2], [2], [1,3], [1]] [[0,2], [2], [1,4], [1]] q7 - q4 [[0,2], [2], [1], [1]] [[0,2], [2], [1,3], [1,3]] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 27 / 38
  • 29. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q2 q1 q3 q5 q8 q6 [[0,2], [2], [1,3], [1]] [[0,2], [2], [1,4], [1]] q7 - q4 [[0,2], [2], [1], [1]] [[0,2], [2], [1,3], [1,3]] q9 For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 28 / 38
  • 30. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q1 q2 [[0,2], [2,4], [1,3], [1,3]] [[0,2,5], [2,4], [1,3], [1,3]] q3 q5 q6 - q4 [[0,2,5], [2,4], [1,3], [1,3]] [[0,2,5], [2,4,5], [1,3], [1,3]] q7 q8 [[0,2], [2], [1,3], [1,3]] [[0,2], [2,4], [1,3], [1,3]] q9 [[0,2], [2,4], [1,3], [1,3]] For each character read, threads start hungry and must eat immediately. Only a hungry thread can eat Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 29 / 38
  • 31. Benchmarks NFA interpretation Example: (a?(a)b)+ Reading "aabab" 01234 q9 [[0,2], [2,4], [1,3], [1,3]] 1 2 1 2 a a b a b 0 1 2 3 4 Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 30 / 38
  • 32. Download https://github.com/nes1983/tree-regex Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 31 / 38
  • 33. NFA construction S1 S - Optional S? S2 Alternation S1|S2 S S - Capture group (S) Star operation S*? Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 32 / 38
  • 34. Backtracking’s nightmare (a + a+) + b against ”an b” will backtrack Θ(2n ) times. Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 33 / 38
  • 35. Backtracking’s nightmare Extract the first cell in a CSV that starts with "P"1 : ∧(.∗?, ) + (P.∗?), failing against ”1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13” is exponential. 1 From http://www.regular-expressions.info/catastrophic.html Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 34 / 38
  • 36. Thread execution order matters .*(a?) start q1 τ1 ↑ q3 a q4 τ1 ↓ q5 any q2 Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 35 / 38
  • 37. Priority matters (a)|(a) q2 a q4 τ1 ↓ τ1 ↑ start q1 q6 τ2 ↑ τ2 ↓ q3 Aaron Karper, Niko Schwarz (UniBe) a q5 Regex Parse Trees January 7, 2014 36 / 38
  • 38. Optimization Pipeline 1 Convert to nondeterministic FA 2 Interpret nondeterministic FA, building deterministic FA lazily. 3 Find similar/mappable states to avoid creating infinite DFA. 4 Run on DFA if possible 5 Compactify DFA if creation of new states wasn’t necessary for a while. Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 37 / 38
  • 39. NFA interpretation Aaron Karper, Niko Schwarz (UniBe) Regex Parse Trees January 7, 2014 38 / 38