删除嵌套注释bz lex [英] Removing nested comments bz lex
本文介绍了删除嵌套注释bz lex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我应该如何在lex(或flex)中删除嵌套注释从文本中打印程序,只打印不在评论中的文本?
我可能会在某些情况下识别状态,当我在评论和块标签的标签开始的数量。
b $ b 1.block评论
/ *
块评论
* /
2。行注释
//行注释
3。
show / * comment / * comment * / comment * / show
:
show show
b $ b
示例2
show / * // comment
* /
show
输出:
显示
显示
示例3
show
/// * comment
comment
// / *
comment
// * / comment
//
注释* /
显示
输出:
show
show
解决方案
这里有一个简单的实现;可以改进。
%x COMMENT
%%
%{
int comment_nesting = 0;
%}
/ *BEGIN(COMMENT); ++ comment_nesting;
//。* / * //注释到行尾* /
< COMMENT> [^ * /] * / *使用非注释分隔符* /
< COMMENT>/ *++ comment_nesting;
< COMMENT>* /if(--comment_nesting == 0)BEGIN(INITIAL);
< COMMENT> [* /] / *如果不匹配注释序列,则使用/ / * * /
/ *可能已经是| \\\
ECHO,这是更有效率。 * /
([^ /] *([/] [^ / *])*)* ECHO;
%%
How should I do program in lex (or flex) for removing nested comments from text and print just the text which is not in comments? I should probably somehow recognize states when I am in comment and number of starting "tags" of block comment.
Lets have rules:
1.block comment
/*
block comment
*/
2. line comment
// line comment
3. Comments can be nested.
Example 1
show /* comment /* comment */ comment */ show
output:
show show
Example 2
show /* // comment
comment
*/
show
output:
show
show
Example 3
show
///* comment
comment
// /*
comment
//*/ comment
//
comment */
show
output:
show
show
解决方案
You got the theory right. Here's a simple implementation; could be improved.
%x COMMENT
%%
%{
int comment_nesting = 0;
%}
"/*" BEGIN(COMMENT); ++comment_nesting;
"//".* /* // comments to end of line */
<COMMENT>[^*/]* /* Eat non-comment delimiters */
<COMMENT>"/*" ++comment_nesting;
<COMMENT>"*/" if (--comment_nesting == 0) BEGIN(INITIAL);
<COMMENT>[*/] /* Eat a / or * if it doesn't match comment sequence */
/* Could have been .|\n ECHO, but this is more efficient. */
([^/]*([/][^/*])*)* ECHO;
%%
这篇关于删除嵌套注释bz lex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文