469,954 Members | 1,777 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,954 developers. It's quick & easy.

removing comment

how to remove comment from c source code.

Sep 26 '07 #1
14 3653
da***********@gmail.com wrote:
how to remove comment from c source code.
Select it and press "delete."

David
Sep 26 '07 #2
vim
Use text processing languages
lex and yaac
Use regular expressions in that

Sep 26 '07 #3
On Sep 26, 8:55 pm, daskumardi...@gmail.com wrote:
how to remove comment from c source code.
If you use VIM, I think the built-in regxp is a good idea.
But I not good at it, so I can't help you~~~~

Sep 26 '07 #4
In article <11**********************@n39g2000hsh.googlegroups .com>,
vim <vg*****@gmail.comwrote:
>Use text processing languages
lex and yaac
Use regular expressions in that
You cannot reliably detect comments using regular expressions.
#define COMMENT /*
#define ENDCOMMENT */
COMMENT
this is a comment
#ifdef __GCC
ENDCOMMENT
#error The license of this example forbids use under GCC
#else
Ah, that's better
ENDCOMMENT
#endif
#include <stdio.h>
int main(void) { printf("Hello /*\"Zippity\"*/World"); return 0; }
The points:
- comments can be triggered by macros
- comment formation is affected by preprocessor conditions that a
textual parser cannot know the answer to
- inside string literals, comment-like sequences are just
regular characters
- determining whether you are inside a string literal requires
matching double-quotes, which is made non-trivial due to
backslashes

Summary:
You cannot remove comments from source without something approaching
the power of the preprocessor, with complete knowledge of the
defines in effect.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Sep 26 '07 #5
Walter Roberson said:

<snip>
- comments can be triggered by macros
Comment removal occurs before macro expansion.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 26 '07 #6
da***********@gmail.com wrote:
>
how to remove comment from c source code.
Consider the DELETE key.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 26 '07 #7
Walter Roberson wrote On 09/26/07 10:07,:
In article <11**********************@n39g2000hsh.googlegroups .com>,
vim <vg*****@gmail.comwrote:
>>Use text processing languages
lex and yaac
Use regular expressions in that


You cannot reliably detect comments using regular expressions.
#define COMMENT /*
#define ENDCOMMENT */
This much defines COMMENT as an object-like
macro with an empty replacement list.
COMMENT
This expands COMMENT, producing nothing at all.
this is a comment
This is syntax error, and there's no point
in further analysis.
[...]
The points:
- comments can be triggered by macros
If "triggered by" means "generated by expansion of,"
this is false.
- comment formation is affected by preprocessor conditions that a
textual parser cannot know the answer to
False.
- inside string literals, comment-like sequences are just
regular characters
True. The same holds for character literals, too:

char c = '/* no comment starts here';
char d = '// nor here';
- determining whether you are inside a string literal requires
matching double-quotes, which is made non-trivial due to
backslashes
True.
Summary:
You cannot remove comments from source without something approaching
the power of the preprocessor, with complete knowledge of the
defines in effect.
False. The preprocessor has no knowledge of comments,
no influence on comments, and nothing to do with comments.
You need only a lexical analyzer; no knowledge or processing
of macros is required or desired.

--
Er*********@sun.com
Sep 26 '07 #8
Walter Roberson wrote:
In article <11**********************@n39g2000hsh.googlegroups .com>,
vim <vg*****@gmail.comwrote:
>Use text processing languages
lex and yaac
Use regular expressions in that

You cannot reliably detect comments using regular expressions.
Of course we can, I have even written such a lex spec myself. :)

IIRC, the "lex & yacc" book, even provide an example for the /**/
comments, and handling the // comment case is easier.

#define COMMENT /*
#define ENDCOMMENT */
COMMENT
this is a comment
#ifdef __GCC
ENDCOMMENT

Wrong. After translation phase:

1. Trigraph replacement
2. Line-splicing
3. replace WS sequences & comments with a space

this reads:

#define COMMENT
COMMENT
this is a comment
Macro expansion comes in translation phase 4.

--
Tor <torust [at] online [dot] no>
Sep 26 '07 #9
In article <N7*********************@telenor.com>,
Tor Rustad <to********@hotmail.comwrote:
>Walter Roberson wrote:
>You cannot reliably detect comments using regular expressions.
>Of course we can, I have even written such a lex spec myself. :)
lex does not use strict regular expressions. lex and yacc together
are LALR, which requires a pushdown stack, a feature missing
in regular expressions.
--
Programming is what happens while you're busy making other plans.
Sep 26 '07 #10
On Sep 26, 8:55 am, daskumardi...@gmail.com wrote:
how to remove comment from c source code.
Search the web for Richard Heathfields page on solutions to problems
posed in K&R2
You are looking for the solutions to exercise 1-23. Many of the
regular clc participants contributed solutions, although (IIRC) not
many offered bulletproof implementations.

Look here -http://users.powernet.co.uk/eton/kandr2/krx123.html if
nowhere else <grin>

HTH
--
Lew

Sep 26 '07 #11
Eric Sosman <Er*********@sun.comwrites:
Walter Roberson wrote On 09/26/07 10:07,:
[...]
>Summary:
You cannot remove comments from source without something approaching
the power of the preprocessor, with complete knowledge of the
defines in effect.

False. The preprocessor has no knowledge of comments,
no influence on comments, and nothing to do with comments.
You need only a lexical analyzer; no knowledge or processing
of macros is required or desired.
I agree with your conclusion, but not necessarily with your statement
that the preprocessor has no knowledge of comments.

The standard defines 8 translation phases. Comments are replaced by
space characters in phase 3; preprocessing directives are executed and
macros are expanded in phase 4. But the standard doesn't define what
"the preprocessor" is. In an implementation which has a distinct
"preprocessor" component, perhaps a separate program, that program
could handle phases 1 through 4, or 1 through 5, or even 1 through 6.

(See C99 5.1.1.2 for the descriptions of the translation phases.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 26 '07 #12
da***********@gmail.com writes:
how to remove comment from c source code.
As you've seen from the variety of responses, you need to be more
specific in your question. You can remove comments manually, or you
can write a program to do it automatically, or you can use some
existing program. Or you can print out the C source code and write
over the comments with a black marker.

What exactly are you trying to do, and *why* do you want to remove
comments? The only good reason I can think of for doing so is if
you're writing a compiler, but in that case you've got a lot of work
ahead of you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 26 '07 #13
Lew Pitcher said:
On Sep 26, 8:55 am, daskumardi...@gmail.com wrote:
>how to remove comment from c source code.

Search the web for Richard Heathfields page on solutions to problems
posed in K&R2
You are looking for the solutions to exercise 1-23. Many of the
regular clc participants contributed solutions, although (IIRC) not
many offered bulletproof implementations.

Look here -http://users.powernet.co.uk/eton/kandr2/krx123.html if
nowhere else <grin>
Please don't look there. Look here instead:

http://clc-wiki.net/wiki/K%26R2_solu...%3AExercise_23

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 26 '07 #14
Walter Roberson wrote:
In article <N7*********************@telenor.com>,
Tor Rustad <to********@hotmail.comwrote:
>Walter Roberson wrote:
[snipped context and good advice by vim added]
>>vim said:
"Use text processing languages
lex and yaac
Use regular expressions in that"
>>You cannot reliably detect comments using regular expressions.
>Of course we can, I have even written such a lex spec myself. :)

lex does not use strict regular expressions. lex and yacc together
are LALR, which requires a pushdown stack, a feature missing
in regular expressions.
Well, yacc is a LALR parser, but that's beside the point, since yacc
isn't needed here. OP's request can be done with lex alone, see e.g.
"lex and yacc" example 2-9, or look at

http://www.lysator.liu.se/c/ANSI-C-grammar-l.html

for ideas. Another alternative, is writing a small C program, an
untested initial version, took me ca. 15 minutes to write.

--
Tor <torust [at] online [dot] no>
Sep 26 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Noud Aldenhoven | last post: by
16 posts views Thread by qwweeeit | last post: by
16 posts views Thread by graham.reeds | last post: by
32 posts views Thread by Stephen | last post: by
7 posts views Thread by Simon Hart | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.