Flex yylineno set to 1

2020-02-12 05:11发布

问题:

I'm writing a simple parser for tcpdump logs, could you please tell me why I can't get proper line number?

%{
char str[80];
%}
%option yylineno

...
%%

^{HOURS}:{MINUTES}:{MINUTES} if(input()=='.') { strcpy(str, yytext);  BEGIN(A); } else {printf("Wrong hour %d", yylineno); }
<A>({NDPS}|{DPS})\.({NDPS}|{DPS})\.({NDPS}|{DPS})|\.{NDPS} printf("Wrong IP!, %d", yylineno);
<A>[ ]{DPS}\.{DPS}\.{DPS}\.{DPS} strcat(str, " from "); strcat(str, yytext+1); BEGIN(B);
...

回答1:

When I tried this, it turned out that I had to have a rule that actually matches newline for yylineno to be updated. With the following rule it worked, and without it yylineno never changed:

\n { }


标签: flex-lexer