Flex yylineno设置为1 [英] Flex yylineno set to 1

查看:133
本文介绍了Flex yylineno设置为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为tcpdump日志编写一个简单的解析器,您能告诉我为什么我无法获得正确的行号吗?

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);
...

推荐答案

当我尝试此操作时,原来我必须有一个实际上与换行符匹配的规则才能更新yylineno.遵循以下规则,它可以工作,没有它,yylineno永远不会改变:

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 yylineno设置为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆