Skip to content

Commit 4b9faeb

Browse files
committed
feat: add notes on Brzozowski derivatives
1 parent 8e36144 commit 4b9faeb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: you can take the derivative of a regular expression?!
3+
link: https://jvns.ca/blog/2016/04/24/how-regular-expressions-go-fast/
4+
date: 2016-04-24
5+
---
6+
7+
> But we can describe some of the ideas in words. Let's say we have a regular expression which is supposed to match the strings ('a', 'abab', 'aaaabb', 'bbc').
8+
>
9+
> The derivative of those strings with respect to `'a'` is ('', 'bab', 'aaabb') – you strip off the `a` from every string.
10+
>
11+
> It turns out (which is not totally obvious to me, but I think if you go through all the cases it's not too bad) that you can take the derivative of any regular expression in a pretty straightforward way – like if you have `(a+)|(b+)`, the derivative with respect to `a` is `(a+)`. These derivatives turn out to combine in nice ways which is I guess why they are called derivatives.
12+
13+
Generally, I think this means you can fashion a RegExp matching library out of a simple recursive descent parser:
14+
15+
1. take the first letter of the string
16+
2. take the derivative of the regular expression with respect to that letter
17+
3. recur with the rest of the string and the derivative of the original RegExp
18+
19+
This post later lead me to reading up on [Brzozowski derivatives][wiki], which in turn pointed me towards [Parsing with Derivatives][pwd-11].
20+
21+
<!-- links -->
22+
23+
[wiki]: https://en.wikipedia.org/wiki/Brzozowski_derivative
24+
[pwd-11]: https://dl.acm.org/doi/abs/10.1145/2034574.2034801

0 commit comments

Comments
 (0)