I've written a JS regexp parser and engine. It did not fit in 40 lines. The most obnoxious part is backreferences. The atom \3 is a backreference if the whole regexp contains at least 3 capture groups; otherwise it is an octal (!) escape for char code 3. But you don't know how many capture groups there are until you're done parsing. This is why JS regexp parsers sometimes must make two passes!
Building a Regex Engine in Fewer Than 40 Lines of Code
11–20 of 37 posts
Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#12Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#13I'd almost admire how convenient it was that the language that was used ships regexes already.
Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#14I think it's the same implementation described here: http://www.cs.princeton.edu/courses/archive/spr09/cos333/bea... (not 100% sure as I lost my copy of Beautiful Code :()
EDIT: I see the author links to the article at the beginning of the post. Still, I missed this on my first reading, so I think posting the link here is still worthwhile. Especially because the translation to JS kind of misses the point - the beauty of the Rob's implementation comes from recursion and pointers and JS lacks the latter.
Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#15Once you add captures and maybe even backreferences, you get a whole new world of weird :-)
Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#16In the "Beautiful Code" book there is a chapter, I think from Rob Pike, which presents a bare-bones regex implementation in C. It doesn't implement alternatives or grouping if I remember correctly, but the implementation is breathtakingly beautiful and not any longer than this one. I think it's the same implementation described here: http://www.cs.princeton.edu/courses/archive/spr09/cos333/bea... (not 100% sure as I…
I stumbled upon an article the other day where Rob Pike implements a rudimentary regular expression engine in c. I converted his code to Javascript and added test specs so that someone can self-guide themselves through the creation of the regex engine. The specs and solution can be found in this GitHub repository.
Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#17https://swtch.com/~rsc/regexp/regexp1.html (previously discussed at https://news.ycombinator.com/item?id=466845)
Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#18In the "Beautiful Code" book there is a chapter, I think from Rob Pike, which presents a bare-bones regex implementation in C. It doesn't implement alternatives or grouping if I remember correctly, but the implementation is breathtakingly beautiful and not any longer than this one. I think it's the same implementation described here: http://www.cs.princeton.edu/courses/archive/spr09/cos333/bea... (not 100% sure as I…
Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#19For that you need to be able to build an equivalent to the regular language expression
(x|y)*Re: Building a Regex Engine in Fewer Than 40 Lines of Code
#20This does not implement grouping "()" or alternatives "|". Hence, looping is only required on individual characters. This is a considerable simplification over full regexp.
I really hated that Lua's "pattern" [1] is never a regular expression nor a regular language. So annoying that it will be better not to have it. [1] https://www.lua.org/manual/5.3/manual.html#6.4.1