473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Determing if a line is a comment

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 */

Aug 8 '06 #1
16 1832

"Christopher" <ch******@gmail.coma écrit dans le message de news:
11*********************@m73g2000cwd.googlegroups.c om...
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 */
What about tab (\t characters)..... a tab is not a white space!
Aug 8 '06 #2
Christopher wrote:
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 */
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!!!
Aug 8 '06 #3
Eric Pruneau wrote:
[..]
What about tab (\t characters)..... a tab is not a white space!
Huh?
Aug 8 '06 #4

"Victor Bazarov" <v.********@comAcast.neta écrit dans le message de news:
eb**********@news.datemas.de...
Eric Pruneau wrote:
>[..]
What about tab (\t characters)..... a tab is not a white space!

Huh?
If a line begin with a tab and after it has // it is a pure comment...
but when you read the text file the tab character and the space character
are
2 different characters...
Aug 8 '06 #5
On 8 Aug 2006 10:19:08 -0700 in comp.lang.c++, "Christopher"
<ch******@gmail.comwrote,
>first preceeding valid /* = looking back, the first /* from the current
point in the file where the line does not have a // before the /*
I think you will do better by scanning forward than by trying to go
back. Don't forget lines with /**/ followed by code. Don't forget
"/*" strings. Set a flag if you see a comment, another if you see
non-comment, at the end of the line you have your answer.

Aug 8 '06 #6
Eric Pruneau wrote:
"Victor Bazarov" <v.********@comAcast.neta écrit dans le message de news:
eb**********@news.datemas.de...
>Eric Pruneau wrote:
>>[..]
What about tab (\t characters)..... a tab is not a white space!
Huh?

If a line begin with a tab and after it has // it is a pure comment...
but when you read the text file the tab character and the space character
are
2 different characters...
There is a difference. a tab char is not a space char, but they are
both "white space". There's a difference.
Aug 8 '06 #7
On Tue, 8 Aug 2006 14:27:44 -0400 in comp.lang.c++, "Eric Pruneau"
<ep******@infinition.comwrote,
>but when you read the text file the tab character and the space character
are
2 different characters...
isspace('\t') still returns true.

Aug 8 '06 #8
David Harmon wrote:
I think you will do better by scanning forward than by trying to go
back. Don't forget lines with /**/ followed by code. Don't forget
"/*" strings. Set a flag if you see a comment, another if you see
non-comment, at the end of the line you have your answer.
If you then say "set a flag if you have a string", you have multiple
booleans where you should have a true state table. Add a couple more
booleans, and you instantly get tangled code.

So start with a Regular Expression. You could probably Google for [regex C++
comment] and hit one right away.

red floyd wrote:
There is a difference. a tab char is not a space char, but they are
both "white space". There's a difference.
Call them "blanks". Sometimes they are not spaces, and sometimes they are
not white...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 8 '06 #9
On Tue, 08 Aug 2006 18:56:22 GMT in comp.lang.c++, "Phlip"
<ph******@yahoo.comwrote,
>David Harmon wrote:
>I think you will do better by scanning forward than by trying to go
back. Don't forget lines with /**/ followed by code. Don't forget
"/*" strings. Set a flag if you see a comment, another if you see
non-comment, at the end of the line you have your answer.

If you then say "set a flag if you have a string", you have multiple
booleans where you should have a true state table. Add a couple more
booleans, and you instantly get tangled code.
The flags do not and any complexity to the state table or whatever
you are using to scan. The flags merely collect information on what
you have seen, and don't change the scanning behavior in any way
whatsoever.
>So start with a Regular Expression. You could probably Google for [regex C++
comment] and hit one right away.
They might well be viable, but I don't see it. At first thought,
constructing a regex to handle all the possibilities sounds like a
nightmare. Perhaps you could expand with an example.

Aug 8 '06 #10
David,

Yeah, I see how going forward would be a lot easier. Unfortunately
that really isn't much of an option.

I'm taking a unified diff output and I'm trying to determine if the
output is a line of code or a comment. To determine if the line is a
comment, I'll have to actually look at the file. The diff simply gives
me a good starting point. I'm really trying to avoid parsing the
entire file.

I'm thinking about doing a forward parse through the entire file and
then capturing the line #s of fully commented lines.

As for concerns about using this as the only metric, i agree.
Obviously only measuring what was added does not fully capture how much
work was done. Nevertheless, it can be a useful metric when you use it
appropriately.
In the mean time, I'm simply not going to distinguish between a sloc
and a comment. They both represent effort.

-Chris

Aug 8 '06 #11
For what it's worth to everyone, I've basically concluded that I'll
have to get to know flex.

best of luck!

-Christopher

Aug 8 '06 #12
Phlip wrote:
David Harmon wrote:
There is a difference. a tab char is not a space char, but they are
both "white space". There's a difference.

Call them "blanks". Sometimes they are not spaces, and sometimes they
are not white...
Call them "white space" because that's what the Standard does.


Brian
Aug 8 '06 #13
On 8 Aug 2006 12:12:51 -0700 in comp.lang.c++, "Christopher"
<ch******@gmail.comwrote,
>I'm taking a unified diff output and I'm trying to determine if the
output is a line of code or a comment. To determine if the line is a
comment, I'll have to actually look at the file. The diff simply gives
me a good starting point.
To find a position in a file, given a line number, you have to scan
anyway. To find if you are in a comment or not (for sure) you have
to go back an unbounded amount. I think your goal should be to scan
the file *only once*. Saving a list of line numbers sounds good.

char bogus[] = "What the heck\
/* do you call */\
this?";

Aug 8 '06 #14
Phlip wrote:
red floyd wrote:
>There is a difference. a tab char is not a space char, but they are
both "white space". There's a difference.

Call them "blanks". Sometimes they are not spaces, and sometimes they are
not white...

2.1/3 "The source file is decomposed into preprocessing tokens (2.4) and
sequences of white-space characters (including comments)."

2.4.2 "Preprocessing tokens can be separated by /white space/; this
consists of comments (2.7), or /white-space characters/ (space,
horizontal tab, new-line, vertical tab, and form-feed), or both."

Given that the Standard calls it white space, I'll do the same.

Aug 8 '06 #15
red floyd wrote:
Given that the Standard calls it white space, I'll do the same.
And if the ISO C++ Standard just ... jumped off a cliff, would you?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 10 '06 #16

"Christopher" <ch******@gmail.comwrote in message news:11*********************@m73g2000cwd.googlegro ups.com...
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.
[snip]

Look at https://sourceforge.net/projects/cncc/

-----------------------------------------------------
NAME
cncc - count C/C++ source lines and bytes

SYNOPSIS
cncc [OPTIONS]... [FILE]...

DESCRIPTION
Count code-lines, empty-lines, comment-lines,
code-fields, empty-fields, comment-fields of C/C++-sources
which have been successfully compiled.
-----------------------------------------------------

Try to do reverse engineering.

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Aug 11 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rob | last post by:
Hello all, If I have the following code fragment: /* comment bla bla */ ....code... With a regular expression, how do I get/extract the comment inside this multi line comment block. With...
15
by: Riko Wichmann | last post by:
Dear all, is there a way in Python to comment out blocks of code without putting a # in front of each line? Somethings like C's /* block of code here is commented out */ Thanks,
10
by: Monk | last post by:
Hi, Have a query regarding comments that extend over multiple-lines. Would like to know if the standard's view of this, so that we can create a code which doesn't run into compiler specific...
0
by: Stu | last post by:
Hi, Im using VB dot net and crystal 11 I have a mainReport with a parameter @Idcustomer and in the mainReport I have a subreport @customerId They are linked using crystal. I would like to...
3
by: Robert W. | last post by:
Long ago I developed a simple algorithm for calculating how much space is required to display a multi-line label in a limited width. It seemed to be working okay but then my testing revealed a...
8
by: cj | last post by:
Has MS included in VB2005 any multi-line comment methods like in C? /* This is a multi-line comment in C */ It's something I'd like to have. I did read somewhere that I could use Ctrl+K,...
6
by: Markus Ernst | last post by:
Hi Searching for a possibility to display some text with preserved white space and line breaks, but with long lines being wrapped, I found this CSS declaration, which I found helpful: pre {...
11
by: xdevel | last post by:
Hi, I don't understand option. if I write: #line 100 "file" I change file numeration to start to line 100 but what "file" ? any example?
1
by: Nethra | last post by:
Hi... I am trying a program which convert the multiline comment type in a program to single line comment type ie. /*have a nice day */ to //have a //nice day
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.