Live data from Hacker News

Building a Regex Engine in Fewer Than 40 Lines of Code

nickdrane.com

1–10 of 37 posts

Re: Building a Regex Engine in Fewer Than 40 Lines of Code

#3
post #2

This does not implement grouping "()" or alternatives "|". Hence, looping is only required on individual characters. This is a considerable simplification over full regexp.

Which means that this CAN NOT BE USED TO ACCEPT REGULAR LANGUAGES. The language (a|b)*, any number of as or bs in any order, can not be descibed using that parser.

"considerable simplification" sounds like it's kinda regex. It's not even basic regular expressions/regular languages.

Edit: The author reimplements code from https://www.cs.princeton.edu/courses/archive/spr09/cos333/be... which acknowledges that the implemented subset does not match all classes but they propose that the classes they can parse, are already useful. The author of the new article makes no such acknowledgement that their implementation just represents a subset of regular languages.

Of course, the regular languages themselves are also just a subset of regexp, which can match more languages due to constructs like references.

Re: Building a Regex Engine in Fewer Than 40 Lines of Code

#6
Nice, remind me of a custom Regex engine i built some years ago in JS while trying to build a fast & small lexer, it does not support ? but +, - and sub-rules, it is around 60 LOC if I remind, mostly built by "accident", it work with JSON as input and output a finite state automaton (compiled version ?)

https://github.com/grz0zrg/jsb/blob/master/lexer.js

Re: Building a Regex Engine in Fewer Than 40 Lines of Code

#8
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!

Re: Building a Regex Engine in Fewer Than 40 Lines of Code

#9
post #2

This 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

Post reply on HN