m.raynal m.raynal - 4 years ago 92
C++ Question

Is flex-lexer a performant(fast) converter for simple regex recognition and replacement?

I'm working on a project for my university, requiring to go though decently large files (>50MB), and i used flex to basically rewrite a file composed of line such as

1 1:41 3:54 7:40 13:8
2 4:7 7:8 23:85


into

1 1 3 7 13
2 4 7 23


(basically, turning the
number_a:number_b
into
number_a
, and printing the output in a file at execution)

My question is, since i'm writing the rest of my program in C++ : was that a good reflex since flex is supposed to be fast (the
f
in flex standing for fast), or am I just wrong and there's much simpler and still efficient way to do that in C++ ?

I'm pretty new to C++, so I have a lot of C coding reflexes and little knowledge of all the tools available, and of their performance.

Here is the piece of code i wrote in flex :

%{
#include<stdio.h>
unsigned int nb_doc = 1;//prend en compte le premier doc
unsigned int i;
%}
couple_entiers [0-9]+:[0-9]+
retour_chariot \n[0-9]+
autre .
%%
{couple_entiers} {i=0;
while(yytext[i] != ':'){
printf("%c",yytext[i]);
i++;
}
}
{retour_chariot} {nb_doc ++; printf("%s",yytext);}
{autre} {printf("%s",yytext);}

%%
int main (void){
yylex();
printf("\n\n%d",nb_doc);
return 0;
}

Answer Source

Consider replacing your custom code with a general solution:

system("sed -E 's/:\d+ / /g'");

:)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download