Re:Re2c: A free and open-source lexer generator for C and C++
by burntsushi on Friday March 01, 2019 @09:25AM
(#19280832)
For any other state machine nuts such as myself, you won't want to miss their paper on tagged DFAs: http://re2c.org/2017_trofimovich_tagged_deterministic_finite... It is a somewhat recent follow on from Laurikari's 2000 paper and Kuklewicz 2007 paper on the same topic. Specifically, tagged DFAs let you do submatch extraction via the DFA itself. Other finite state machine regex libraries either don't do this at all, or fall back to slower engines in the general case (such as RE2). I think the downside of tagged DFAs is that they can be quite large!
Re:Re2c: A free and open-source lexer generator for C and C++
by jhallenworld on Friday March 01, 2019 @06:09PM
(#19286204)
It's interesting that it's using Laurikari's algorithm- I looked at the 2000 paper years ago and could not get a working algorithm out of it. Eventually I found another way to do this that I use for JOE's regular expression library. So now I'm curious which one is faster...
"However, Thompson-style algorithms can be adapted to track submatch boundaries without giving up efficient performance. The Eighth Edition Unix regexp(3) library implemented such an algorithm as early as 1985, though as explained below, it was not very widely used or even noticed. "
So in JOE's matcher, when simulating the NFA with the multithreaded machine, each succeeding thread will correspond to one of the sub-match possibilities. Augment this machine to keep the ones you want (the ones with the longest matching strings starting from the left usually).
Re:Re2c: A free and open-source lexer generator for C and C++
by burntsushi on Friday March 01, 2019 @07:38PM
(#19286661)
> So in JOE's matcher, when simulating the NFA with the multithreaded machine, each succeeding thread will correspond to one of the sub-match possibilities. Augment this machine to keep the ones you want (the ones with the longest matching strings starting from the left usually).
Yeah, this is the standard way to do it. It's what I meant by "fall back to a slower engine," since simulating the NFA is much slower than a DFA.
The nice thing about it is that it doesn't impose any constraints on the structure of your code.
lex/flex probably have options not generate piles of messy C code with global variables at this point, but IMO there's no reason not to use re2c instead, so it's moot.
Re:Re2c: A free and open-source lexer generator for C and C++
by chubot on Friday March 01, 2019 @03:58PM
(#19285194)
update: Also re2c gives you "semantic actions" like Ragel or yacc, which is better IMO. Not sure if lex/flex does that. I think it has a hard-coded notion of tokens.
Re:Re2c: A free and open-source lexer generator for C and C++
by cjohansson on Friday March 01, 2019 @09:45AM
(#19281055)
I have ported the PHP re2c lexer logic to elisp in my in-progress attempt at a Semantic PHP mode for Emacs. Using the lexer tokens as foundation for syntax-coloring and indentation
Re:Re2c: A free and open-source lexer generator for C and C++
by quotemstr on Friday March 01, 2019 @12:55PM
(#19283483)
I usually reach for Ragel [1] for this kind of work. It has a very steep learning curve and is a bit more complex than a straight-up translation of a conventional regular expression, but the state machine combination algebra is incredibly powerful and flexible. I've used the tool in numerous projects over the years.
Re:Re2c: A free and open-source lexer generator for C and C++
by megous on Friday March 01, 2019 @01:46PM
(#19284001)
I use re2c a lot. It's a great tool for safely working with text in C programs, without any runtime dependencies. It's a secret sauce in many of my programs.
Re:Re2c: A free and open-source lexer generator for C and C++
by mncharity on Friday March 01, 2019 @05:04PM
(#19285779)
Hyperscan was recently posted.[1] A fast multi-pattern regex matcher. As in 1000's of patterns. Though optimized for deep packet inspection, I'm curious to try it for tokenizing.
Re:Re2c: A free and open-source lexer generator for C and C++
by cleeus on Friday March 01, 2019 @05:20PM
(#19285903)
I replaced a handwritten lexer of a proprietary PDF library with a re2c generated lexer. Besides fixing a bunch of dormant bugs, the resulting lexer was much faster.
Also the state of c++ std regex implementations is pretty sad (at least it was the last time I checked) and if you just need to match with a few compile-time-known regexes, re2c is there for you to solve the problems.
Re:Re2c: A free and open-source lexer generator for C and C++
by unsatchmo on Friday March 01, 2019 @11:21AM
(#19282143)
Safari used to use re2c to identify Unicode Top Level Domains in order to avoid cross-site-scripting attacks. The Unicode support has no parallel that I’m aware of.
Re:Re2c: A free and open-source lexer generator for C and C++
by pubby on Friday March 01, 2019 @01:37PM
(#19283892)
Lexer generators seem like a useful algorithm wrapped up in heaps of framework bullshit. Wouldn't it be better to have a simple program that inputs regex and outputs a DFA than one that handles the entire lexing process?
Re:Re2c: A free and open-source lexer generator for C and C++
by jules on Friday March 01, 2019 @03:00PM
(#19284697)
Lexers run multiple regexes in tandem to determine the next token type. To do that with your method you'd have to run multiple DFAs on the same string. Lexers can do it with 1 DFA by having multiple different final states (non-final + 1 per token type vs non-final + final).
https://sourceforge.net/p/joe-editor/mercurial/ci/default/tr...
It uses a variant a Thompson's matcher, see:
https://swtch.com/~rsc/regexp/regexp1.html
"However, Thompson-style algorithms can be adapted to track submatch boundaries without giving up efficient performance. The Eighth Edition Unix regexp(3) library implemented such an algorithm as early as 1985, though as explained below, it was not very widely used or even noticed. "
So in JOE's matcher, when simulating the NFA with the multithreaded machine, each succeeding thread will correspond to one of the sub-match possibilities. Augment this machine to keep the ones you want (the ones with the longest matching strings starting from the left usually).
Yeah, this is the standard way to do it. It's what I meant by "fall back to a slower engine," since simulating the NFA is much slower than a DFA.
https://github.com/ninja-build/ninja/blob/master/src/lexer.i...
and then used it for my own huge shell lexer:
The Oil Lexer: Introduction and Recap http://www.oilshell.org/blog/2017/12/15.html
The nice thing about it is that it doesn't impose any constraints on the structure of your code.
lex/flex probably have options not generate piles of messy C code with global variables at this point, but IMO there's no reason not to use re2c instead, so it's moot.
https://github.com/cjohansson/emacs-phps-mode/blob/master/ph...
[1] http://www.colm.net/open-source/ragel/
For example, my JSON parser that doesn't use intermediate object tree and works directly on strings: https://megous.com/git/sjson/tree/sjson.c
https://github.com/php/php-src/blob/master/Zend/zend_languag...
[1] https://news.ycombinator.com/item?id=19270199
Like... re2c?
(nice with https://github.com/fbb-git/bisoncpp as parser generator)
Very well written with good documentation.