Re: How to determine if a given line is a C/C++ comment

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
12 Aug 2006 13:07:27 -0400

          From comp.compilers

Related articles
How to determine if a given line is a C/C++ comment chtaylo3@gmail.com (2006-08-09)
Re: How to determine if a given line is a C/C++ comment tom@infoether.com (Tom Copeland) (2006-08-10)
Re: How to determine if a given line is a C/C++ comment haberg@math.su.se (2006-08-10)
Re: How to determine if a given line is a C/C++ comment nicola.musatti@gmail.com (Nicola Musatti) (2006-08-10)
Re: How to determine if a given line is a C/C++ comment johnmillaway@yahoo.com (John Millaway) (2006-08-10)
Re: How to determine if a given line is a C/C++ comment mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2006-08-12)
Re: How to determine if a given line is a C/C++ comment listas@nicolasb.com.ar (Nico) (2006-08-12)
Re: How to determine if a given line is a C/C++ comment gah@ugcs.caltech.edu (glen herrmannsfeldt) (2006-08-14)
Re: How to determine if a given line is a C/C++ comment nicola.musatti@gmail.com (Nicola Musatti) (2006-08-18)
Re: How to determine if a given line is a C/C++ comment zebedee@zebedee.net (zebedee) (2006-09-12)
| List of all articles for this month |

From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Newsgroups: comp.compilers
Date: 12 Aug 2006 13:07:27 -0400
Organization: cbb software GmbH
References: 06-08-042
Keywords: C++, lex
Posted-Date: 12 Aug 2006 13:07:27 EDT

On 9 Aug 2006 00:03:49 -0400, chtaylo3@gmail.com wrote:


> I'm trying to determine if a given line is a C/C++ style comment.


The answer depends on the context and the direction of scanning the
text. A SNOBOL-like pattern that matches C/C++ comments without nested
/* */ could be


'/*' *(END/|%): '*/' | '//'...END/


But you must be sure that the position where you start to scan is not
in another comment, string literal etc.


> My motivation for this is determing 4 metrics:
> how many lines of code have been added/changed
> how many lines of code have been removed
> how many lines of pure comments have been added/changed
> how many lines of pure comments have been removed.


Basically you should parse the program text and perhaps; as our
moderator has suggested, a lexer would be the best solution.


As for patterns you could use immediate assignment to get on
metrics. That is - when a comment pattern matches a comment in the
text, it assigns a variable, which in turn causes some action, like
metric change:


Comment_Var=('/*' *(END/|%): '*/' | '//'...END/)


which would be a subpattern of a larger patter that matches the whole
program and as a side effect produces metrics of.


--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.