Monday, 15 April 2013

flex Start Conditions in the same line -



flex Start Conditions in the same line -

i'm trying sort out, it's school thing , need little help. i'm trying figure how split single line multiple parts, , since in flex there no grouping, i'm trying done start conditions, it's not working expected.

%s line_name %s line_grade ws [ \t] dni [0-9]{7,8}-[a-za-z] dnierror [0-9a-za-z]+-[0-9a-za-z]+ nota [0-9].[0-9]{1,2}|10.[0]{1,2} notaerror [0-9]{2}.[0-9]{2,} nombre [a-z][a-z]+ nombrecompleto {nombre}{ws}+{nombre}","{ws}*{nombre} %% <initial>^{dni} { printf("%s;", yytext); begin line_name;} <line_name>^{ws}*{nombrecompleto} { printf("%s;", yytext); begin line_grade;} <line_grade>^{ws}*{nota} { printf("%s\n", yytext); ;} %% int main(int argc, char* argv[]){ yylex(); }

my input file like

11223344-z alonso barreiro, ana 5.68 01234567-b alonso barros, antonio 4.8 12345678-x alonso calvo, andres 2.8 13345678-x barreiro calvo, luis 3.68

it should producing output like

11223344-z;alonso barreiro, ana;5.68 01234567-b;alonso barros, antonio;4.8 12345678-x;alonso calvo, andres;2.8 13345678-x;barreiro calvo, luis;3.68

but it's recognizing first state 11223344-z; , vomiting rest unparsed.

i understand code should work on input splits each part in separate lines, need know if can i'm doing on single line can retrieve each part , separate them token ";" or whatever.

thanks in advance.

update: after next rici's reply i've edited code this

%s line_name %s line_grade %s line_ok %s line_error_dni %s line_error_grade ws [ ] dni [0-9]{7,8}-[a-za-z] dnierror [0-9a-za-z]+-[0-9a-za-z]+ nota [0-9].[0-9]{1,2}|10.[0]{1,2} notaerror [0-9]{2}.[0-9]{2,} nombre [a-zÁÉÍÓÚ][a-záéíóúü]+ nombrecompleto {nombre}{ws}+{nombre}","{ws}*{nombre} %option nodefault %% <initial>^{dni} { printf("%s;", yytext); begin line_name;} <initial>^{dnierror} { printf("%s; x;", yytext); begin line_error_dni;} <line_name>^\t{nombrecompleto} { printf("%s;", yytext); begin line_grade;} <line_grade>^\t{nota} { printf("%s", yytext); begin line_ok;} <line_grade>^\t{notaerror} { printf("%s; x", yytext); begin line_error_grade;} <line_error_dni>.*\n { printf(" - dni error\n"); begin(initial);} <line_error_grade>.*\n { printf(" - grade error\n"); begin(initial);} <line_ok>{ws}*\n { printf(" - good\n"); begin(initial);} \n { printf(" - unexpected end of line\n"); begin(initial);} <<eof>> { yyterminate();} .* { printf(" ");} %% int main(int argc, char* argv[]){ yylex(); }

it's still not working , every line in file says ' - unexpected end of file' matching wrong?

of course, if add together rule this

<initial>^{dni}\t{nombrecompleto}\t{nota} { printf("%s;", yytext); begin line_ok;}

it recognizes line, that's not i'm trying accomplish since wouldn't different {dni}\t{nombrecompleto}\t{nota} , strtok-ing

nothing in of patterns recognizes newline. when flex hits newline, default rule match, , you'll still in start status {nota}.

i recommend using %option nodefault, produce error rather invoking default action. , you'll have insert own matches other string. simple error action match character , skip newline or eof. don't forget reset start status initial when nail newline. in fact, might want utilize following:

\n { begin(initial); }

although won't signal error if grade missing.

flex isn't ideal tool sort of parsing, way you're using start conditions reasonable.

flex-lexer

No comments:

Post a Comment