delete comments in .c file

T

Timex

I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?

Please show me example code.
 
J

Jem Berkes

I want to delete all comments in .c file.
Size of .c file is very big.

Any good idea to do this?

Assuming you have C comments /* like this, right? */
You can use UNIX sed (stream editor). You're not going to believe this,
but: cat input.c | sed -e 's/\/\*.*\*\///g' > output.c

Do a diff to make sure it's working correctly.
 
E

Ed Morton

Jem said:
Assuming you have C comments /* like this, right? */
You can use UNIX sed (stream editor). You're not going to believe this,
but: cat input.c | sed -e 's/\/\*.*\*\///g' > output.c

Do a diff to make sure it's working correctly.

That would only work if all comments are in the form you describe, which
isn't likely. e.g. it won't work if the comments are:

/* Start of a fairly common form
* of comment block.
*/

or:

/* set x: */ x = 7; /* now more stuff... */

In the first case it won't delete the comment while in the second
unusual case it'll delete both the comments plus the "x = 7;" assignment
between them.

You need to Google around or if you want a UNIX tool solution, post this
to a UNIX NG (e.g. comp.unix.questions or comp.unix.shell).

Ed.
 
K

Keith Thompson

Jem Berkes said:
Assuming you have C comments /* like this, right? */
You can use UNIX sed (stream editor). You're not going to believe this,
but: cat input.c | sed -e 's/\/\*.*\*\///g' > output.c

Do a diff to make sure it's working correctly.

That won't detect multi-line comments. It also fails to properly
ignore comment delimiters inside string and character literals. It
deletes everything from the first "/*" on a line to the last "*/" on
the same line; for example, it transforms this:
x = /* one comment */ 42; /* another comment */
to this:
x =
And it replaces each comment by nothing rather than by a blank, so the
following valid C fragment:
x = sizeof/*comment*/int;
is replaced with this:
x = sizeofint;

If you're dealing with C99 code you'll have to worry about "//"
comments (many pre-C99 compilers support these as an extension).

Stripping C comments is a lot more complex than it looks; you almost
have to duplicate most of the functionality of the preprocessor to get
it right.

I really have to ask the original poster: why do you want to do this?
 
D

Derk Gwen

# I want to delete all comments in .c file.
#
# Size of .c file is very big.
#
# Any good idea to do this?
#
# Please show me example code.

tclsh <<':eof'
set c [open something.c]
set b [read $c]
close $c

regsub -all {//[^\n]*\n} $b \n b
regsub -all {/[*]([^*]|[*](?!/))*[*]/} $b {} b

set c [open something-1.c w]
puts $c $b
close $c
:eof
 
K

Keith Thompson

Derk Gwen said:
# I want to delete all comments in .c file.
#
# Size of .c file is very big.
#
# Any good idea to do this?
#
# Please show me example code.

tclsh <<':eof'
set c [open something.c]
set b [read $c]
close $c

regsub -all {//[^\n]*\n} $b \n b
regsub -all {/[*]([^*]|[*](?!/))*[*]/} $b {} b

set c [open something-1.c w]
puts $c $b
close $c
:eof

This seems to add an extra blank line at the end of the output file.
It transforms "token/**/pasting" to "tokenpasting", which doesn't
violate the original poster's requirements, but it doesn't match the
way comments are treated in C.

It also doesn't ignore comment delimiters in string and character
literals.
 
C

Colin Newell

Assuming you have access to the C preprocessor (cpp) you can do a quick hack
to use that. Isolate the file from it's include files and run like,

cpp -nostdinc file.c > new_file.c

It should warn that it cannot find all the include files. Now put your
#includes back in and you should have a comment free file.

This is a quick and dirty solution because the C preprocessor is trying to
do a whole load of stuff aswell as stripping comments. You need to make
sure that there are no #defines because otherwise they will be expanded out.
Put them back in after you have run the processor.

I have only come across the C preprocessor as a seperate program called
'cpp' on *nix boxes. It may be available on windows but I do not know where
or how.


Colin.
 
T

Tim Hagan

Timex said:
I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?

Please show me example code.

This will replace each /* ... */ style comment with a single space:

#include<stdio.h>
int main(void){i\
nt c,p=-1,k=0,s=0
;while((c=getchar
())!=EOF){if(s==0
){if(p=='/'&&c==
'*'){s=1;k=2;}el\
se if(c=='\"'&&p
!='\\'&&p!='\'')s
=2;}else if(s==1)
{if (p=='*'&&c==
'/')s=0;}else if(
s==2){if(c=='\"'
&&p!='\\')s=0;}if
(k==1)putchar(' '
);if(p>0&&s!=1){
if(!k)putchar(p);
if(--k<0)k=0;}p=c
;}putchar(p);ret\
urn 0;}
 
J

Jeremy Yallop

Tim said:
This will replace each /* ... */ style comment with a single space:

#include<stdio.h>
int main(void){i\
nt c,p=-1,k=0,s=0
;while((c=getchar
())!=EOF){if(s==0
){if(p=='/'&&c==
'*'){s=1;k=2;}el\
se if(c=='\"'&&p
!='\\'&&p!='\'')s
=2;}else if(s==1)
{if (p=='*'&&c==
'/')s=0;}else if(
s==2){if(c=='\"'
&&p!='\\')s=0;}if
(k==1)putchar(' '
);if(p>0&&s!=1){
if(!k)putchar(p);
if(--k<0)k=0;}p=c
;}putchar(p);ret\
urn 0;}


It doesn't handle line-splicing. Also, putchar(-1) is not portable.

Jeremy.
 
T

Tim Hagan

Jeremy said:
It doesn't handle line-splicing. Also, putchar(-1) is not portable.

putchar(-1) is never executed in the above code, but you're right
about the line-splicing. Oh, well, back to the drawing board ...
 
M

Mark A. Odell

I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?

Please show me example code.

Just grab an evaluation copy of Codewright from Borland. Do a
search-replace on . <-- regexp and restrict "to comments" with a
replacement string of nothing. I just did it to a very large file in about
5 seconds. Now what does this have to do with the C language? The C
language does not specify how to delete comments.
 
T

Tim Hagan

.... unless one tries to remove the comments from an empty file. :)
putchar(-1) is executed if EOF is encountered immediately.

So just insert 'if (p > 0)' before the final putchar.
 
R

Richard Heathfield

Mark said:
Now what does this have to do with the C language? The C
language does not specify how to delete comments.

The Standard says: "Each comment is replaced by one space character." If
that doesn't specify how to delete comments, I don't know what does.
 
M

Mark McIntyre

The Standard says: "Each comment is replaced by one space character." If
that doesn't specify how to delete comments, I don't know what does.

I guess Mark's point is that to be sure of being syntactically
identical to the original you should replace all comments by a space.
For instance
int i = 23/* */12;
should still generate a syntax error . :)

What the OP would expect it to do with
double i = 32 //* */ 4
;
is anyone's guess. I guess you'd have to have a C99 and a C89 mode.
 
S

Stephen Samuel

Here's a perl script which will handle *MOST* sane C code...

Some things that it will miss (scan manually for it first:

a double quote inside of single quotes (e.g.)
char confusion = '"';

C-99 // comments like this

I'm sure that some people can come up with other convoluted counter-examples.

It reads and plays with the entire file, so it will need to hold
at least two or three copies of it in RAM. (for today's computers,
that would be some number of megabytes).

If you want any of the above fixed, feel free to send me a cheque.
____________________________________________________
#!/usr/bin/perl
$s=join("",<>);
# printf "[[%s]]\n\n",$s;
$s=~ s/("(\\\\|\\"|[^"])*")|(\/\*([^*]|\*(?=[^\/]))*\*\/)|(\/\/.*)/[[$1 ]]/g;
printf "[[%s]]\n\n",$s;
____________________________________________________
Yep, That's it... 5 lines including the shell header.
 
I

Irrwahn Grausewitz

Stephen Samuel said:
Here's a perl script which will handle *MOST* sane C code...
<snip>

Since when is perl topical in c.l.c?

BTW:
Does your "solution" account for comment delimiters inside string
literals? (I'm unfortunately unable to decrypt the line-noise
provided.)

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top