Flex不会在多行注释上正确计算行数 [英] Flex not counting lines properly on multiline comments
本文介绍了Flex不会在多行注释上正确计算行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用上述regex在Flex中标识多行注释:
I`m using the above regex to identify multiline comments in Flex:
[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] { /* DO NOTHING */ }
但在我看来,flex / bison没有正确返回行计数器。
例如:
But seems to me that flex/bison is not returning properly the line counter. For example:
输入:
1 ___bqmu7ftc
2 // _qXnFEgQL9Zsyn8Ohtx7zhToLK68xbu3XRrOvRi
3 /* "{ output 6 = <=W if u7 do nN)T!=$||JN,a9vR)7"
4 -758939
5 -31943.6165480
6 // "RND"
7 '_'
8 */
9 [br _int]
输出:
1 TK_IDENT [___bqmu7ftc]
4 [
4 TK_IDENT [br]
4 TK_IDENT [_int]
4 ]
此行应为9而不是4。
任何想法?
推荐答案
这是我在 Flex手册中找到的解决方案
This is the solution I found on Flex manual
请记住在定义范围上声明 int comment_caller;
。
Remember to declare int comment_caller;
on your definition scope.
%x comment
%x foo
%%
"/*" {comment_caller = INITIAL;
BEGIN(comment);
}
<foo>"/*" {
comment_caller = foo;
BEGIN(comment);
}
<comment>[^*\n]* {}
<comment>"*"+[^*/\n]* {}
<comment>\n {++line_num;}
<comment>"*"+"/" BEGIN(comment_caller);
这篇关于Flex不会在多行注释上正确计算行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文