#Flex and memory complexity.html
Flex is one of those tools that strongly expects you to do things a specific way.
Something similar that comes to mind is one time I had unreadable C# code,
with all kinds of warnings, because I was trying to write it C style.
What ended up resolving the situation was introducing new classes.
Flex is a close parallel with it's states.
{@begin=flex@
// The following example parses a text input and filters out k&r C comments:
%option noyywrap
%%
\/\*([^*]|\*+[^/*])*\*+\/ { ; }
%%
// */ ] // <- i wish not to mess up my highlighting
@end=flex@}
Now, war is ugly and microwaved kefir is not nice either,
but that regex...
Do note, that we need ALL that to prevent:
/* */ */
from matching as a single comment.
Also note that with the default rules and -lfl, it is a fully fetched,
complete flex program.
At the same time it is its own little self contained snippet,
so clearly, we have no reason to convert it into multiple rules
like BELOW, right?
{@begin=flex@
%option noyywrap
%x COMMENT
%%
\/\* { BEGIN COMMENT; }
{
. { ; }
\*\/ { BEGIN INITIAL; }
}
%%
@end=flex@}
Look at all that bloat!
And readability is objective, therefor clearly you should go with
the shorter version!
Well, not so fast, lets compile and test them first...
{@begin=sh@
flex -o pattern.yy.c pattern.l
flex -o state.yy.c state.l
gcc -o pattern.out pattern.yy.c -lfl -g
gcc -o state.out state.yy.c -lfl -g
python -c '''print("/*", "".join([ "a" for i in range(0,1000000)]), "*/")''' | valgrind --tool=massif --massif-out-file=massif.out.pattern ./pattern.out
python -c '''print("/*", "".join([ "a" for i in range(0,1000000)]), "*/")''' | valgrind --tool=massif --massif-out-file=massif.out.state ./state.out
ms_print massif.out.pattern
ms_print massif.out.state
@end=sh@}
Partial massif diagnostics of the first, complex regex solution:
MB
1.005^ #
| ::::::::::::::::::::::::::::::::::::::::::::::::::::#
| : #
| : #
| : #
| : #
| : #
| : #
| : #
| : #
| @@@@@@@@@@@@@@@: #
| @ : #
| @ : #
| @ : #
| @ : #
| :::@ : #
| : @ : #
| : @ : #
|:: @ : #
|:: @ : #
0 +----------------------------------------------------------------------->Gi
0 5.398
Partial massif diagnostics of the second, state transition solution:
KB
21.13^ #
|:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
|: #
0 +----------------------------------------------------------------------->Mi
0 89.89
Mind the different in units!