Automata based alternative to re2
I am needing to implement regular expressions in a C++ program I am writing, and I wanted to use re2 but I could not compile it on windows. Does anyone know of another regular expression library or whatever it's called that compiles easily on windows and isn't a "backtracking" regex engine, but an automata-theory based one (whateve开发者_JAVA百科r that means) like re2 is?
Or just figuring out how to compile re2 on windows would be perfect.
Regular expressions are part of the TR1 standard, so chances are you already have a <tr1/regex>
header that contains a std::tr1::regex
class and related functions.
Have a look at
http://www.complang.org/ragel/
It's an external DSL so not technically C++. However as it generates pure C++ / C from the regular expressions it should be much faster than anything that is built at runtime./
For example.
action dgt { printf("DGT: %c\n", fc); }
action dec { printf("DEC: .\n"); }
action exp { printf("EXP: %c\n", fc); }
action exp_sign { printf("SGN: %c\n", fc); }
action number { /*NUMBER*/ }
number = (
[0-9]+ $dgt ( '.' @dec [0-9]+ $dgt )?
( [eE] ( [+\-] $exp_sign )? [0-9]+ $exp )?
) %number;
main := ( number '\n' )*;
get's compiled down to
st0:
if ( ++p == pe )
goto out0;
if ( 48 <= (*p) && (*p) <= 57 )
goto tr0;
goto st_err;
tr0:
{ printf("DGT: %c\n", (*p)); }
st1:
if ( ++p == pe )
goto out1;
switch ( (*p) ) {
case 10: goto tr5;
case 46: goto tr7;
case 69: goto st4;
case 101: goto st4;
}
if ( 48 <= (*p) && (*p) <= 57 )
goto tr0;
goto st_err;
I have a port of RE2 for Windows at http://code.google.com/p/re2win/ . It builds with lots of warnings, no errors.
精彩评论