确定一行是否是评论 [英] Determing if a line is a comment

查看:81
本文介绍了确定一行是否是评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个评论逻辑问题。我正在尝试确定给定的C / C ++ / C#/ Java行是否是评论。你可以请

评论我的逻辑。


我需要先定义一些条款:

以=开头只有白色线上的空间直到它到达...

首先有效/ * =回头看,第一个/ *来自当前

点在该行所在的文件中没有//之前/ *


a行是一个纯注释行如果:

该行以//
$ b $开头b或

线开始wih / *



第一个有效/ *之前发生在第一个前面* /
之后

Hello all,

I''ve got a question of comment logic. I''m trying to determine if a
given line of C/C++/C#/Java is a comment or not. Can you please
comment on my logic.

I need to define some terms first:
starts with = has only white space on the line untill it reaches ...
first preceeding valid /* = looking back, the first /* from the current
point in the file where the line does not have a // before the /*

a line is a pure comment line if:
the line starts with //
or
the line starts wih /*
or
the first preceeding valid /* occurs after the first preceeding */

推荐答案



" Christopher" < ch ****** @gmail.comaécritdansle message de news:
11 ********************* @ m73g2000cwd.googlegroups.c om ...

"Christopher" <ch******@gmail.coma écrit dans le message de news:
11*********************@m73g2000cwd.googlegroups.c om...

大家好,


我有一个评论逻辑问题。我正在尝试确定给定的C / C ++ / C#/ Java行是否是评论。你可以请

评论我的逻辑。


我需要先定义一些条款:

以=开头只有白色线上的空间直到它到达...

首先有效/ * =回头看,第一个/ *来自当前

点在该行所在的文件中没有//之前/ *


a行是一个纯注释行如果:

该行以//
$ b $开头b或

线开始wih / *



第一个有效/ *之前发生在第一个前面* /
之后
Hello all,

I''ve got a question of comment logic. I''m trying to determine if a
given line of C/C++/C#/Java is a comment or not. Can you please
comment on my logic.

I need to define some terms first:
starts with = has only white space on the line untill it reaches ...
first preceeding valid /* = looking back, the first /* from the current
point in the file where the line does not have a // before the /*

a line is a pure comment line if:
the line starts with //
or
the line starts wih /*
or
the first preceeding valid /* occurs after the first preceeding */



标签怎么样(\t字符).....标签不是空格!

What about tab (\t characters)..... a tab is not a white space!


Christopher写道:
Christopher wrote:

a line是一个纯注释行如果:

该行以//

或<开头br />
线开始wih / *



前一个有效的/ * o在第一个前面的* /
a line is a pure comment line if:
the line starts with //
or
the line starts wih /*
or
the first preceeding valid /* occurs after the first preceeding */



之后发生。解析是一个无穷无尽的话题。请考虑以下评论:


std :: string foo =" / *不是评论* /" ;;


现在从这里,您可以将来源借给''astyle'',这可能有一个

中的原始解析器。


或者你可以写一个匹配评论的正则表达式。


或者你可以踢,并忽略字符串中的注释。


所以问题出现了为什么你要解析你的来源以找到评论?


这可能是一个指标,如 ;评论数量,或

非评论代码行数。如果是这样,请了解有更多

重要指标(例如行数_removed_)。 LOC指标可以在很多方面轻易被滥用。


您可以得到代码语句数量的便宜近似值,

通过计算; \\\


格式的数量来忽略像{或}这样的结构线。结束语句分隔符......


-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客!!!

Parsing is an endless topic. Consider this comment:

std::string foo = " /* not a comment */ ";

Now from here, you could borrow the source to ''astyle'', which probably has a
primitive parser in it.

Or you could write a Regular Expression that matches comments.

Or you could punt, and ignore comments in strings.

So the question arises why you want to parse your source to find comments?

This might be for a metric, such as "number of comments", or "number of
non-comment code lines". If so, understand that there are many more
important metrics (such as number of lines _removed_). The LOC metric can be
easily abused in many ways.

You can get a cheap approximation of the number of code statements,
disregarding structural lines like { or }, by counting the number of ";\n"
formations. The end-of-statement delimiter...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


Eric Pruneau写道:
Eric Pruneau wrote:

[..]

标签怎么样(\t字符) .....标签不是白色空间!
[..]
What about tab (\t characters)..... a tab is not a white space!



嗯?

Huh?


这篇关于确定一行是否是评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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