Removing C Comments 
Author Message
 Removing C Comments

Has anyone written a small perl script that will remove _all_ C comments.

e.g.
        /* A comment */

        /* A /* nested comment */ */

        /*
         * A comment
         * split over several lines
         */

It should not remove formal parameter type specifiers (as used on UNIX):

e.g.

void some_function(/*_ char _*/)
char *str;
{
        .
        .

Quote:
}

Thanx in advance,

John



Sat, 15 Mar 1997 20:26:13 GMT  
 Removing C Comments

Quote:
>Has anyone written a small perl script that will remove _all_ C comments.
>    /* A /* nested comment */ */

Just picking a nit: C doesn't have nested comments.

Quote:
>It should not remove formal parameter type specifiers (as used on UNIX):
>void some_function(/*_ char _*/)

Ugh, distinguishing those from other comments is going to be extremely
difficult.  Especially if the programmer writes it in the form:

void
some_function(/* char */)

The second line looks just like a function call with a comment in the
argument list.  Do you always use the underscores in such comments?

P.S. I tried to reply by mail, but your posting had the bogus
tron.NoSubdomain.NoDomain domain, and you didn't put a valid address in
your .signature.
--

Barry Margolin
BBN Internet Services Corp.



Sun, 16 Mar 1997 00:48:50 GMT  
 Removing C Comments

Quote:
>Has anyone written a small perl script that will remove _all_ C comments.
>    /* A  comment  */


Quote:
>void some_function(/*_ char _*/)

Here is a way to just get rid of all comments.

     #!/usr/bin/perl -p
     $/='*/';              # set paragraph mode to */ for multi-line comments
     s#/\*(.|\n)*\*/##;    # using (.|\n)* because
                           # .* will not match the newlines!

If you want to ignore function parameters you can try this ugly monster

     #!/usr/bin/perl -p
     $/='*/';            
     s#/\*(.|\n)*\*/## unless m#\(([^)]|\n)*/\*([^)]|\n)*\*/#;

which will remove comments which are in parenthesis. So this will work on

     void some_function(/*int x, int y*/)

or on multiple lines

     void some_function(/*int x,
                          int y*/)

as long as you don't have multiple comments like

     void some_function(/*int x, */
                        /*int y  */)

However, since this will not remove comments inside ANY parenthesis, it will
not remove these comments:

    if ( x /* x is a variable */ == y )

If you do this in your code however, you deserve to have to take each comment
out manually.

--Eric

Eric Wenokor--------------------------------------

53 Wall Street
New York, N.Y 10005                 (212) 858-7510
--------------------------------fax (212) 858-7620



Mon, 17 Mar 1997 20:53:02 GMT  
 Removing C Comments

Quote:

|> Jonathan Hatcher writes:

|> >Has anyone written a small perl script that will remove _all_ C comments.
|> >      /* A  comment  */
|>
|> Here is a way to just get rid of all comments.
|>
|>   #!/usr/bin/perl -p
|>   $/='*/';              # set paragraph mode to */ for multi-line comments
|>   s#/\*(.|\n)*\*/##;    # using (.|\n)* because
|>                         # .* will not match the newlines!

Just a note about (.|\n).... that's _several_ orders of magnitude slower
than the trick that someone (Uwe?) pointed out the other day... using [\D\d].
[^\000] is probably just as fine, but [\D\d] is pretty unambiguous.

As for the solution above, it will break in painful ways if there are any
comment-like things within quotes. Consider

   msg = "some */ /* string"; /* REAL COMMENT */

On Sep 6th, just before my recently-completed vacation, I posted a complete
program that should remove all comments:

    $/ = undef;
    $_ = <>;
    s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|\n+|.[^/"'\\]*)#$2#g;
    print;

It should handle quoted strings, quoted characters, escaped things, and all
manner of malicious-yet-correct stuff.

BTW, in my talk yesterday about the comment-stripping FAQ, I completely
forgot to address the stuff-in-strings issue. Well, it's there above...

|> If you want to ignore function parameters you can try this ugly monster

I could probably extend the above to leave comments within a single level
of parens, but it would be really, really ugly (and not worth the time).
So we'll "leave it as an excercise to the interested reader" (-:

        *jeffrey*
-------------------------------------------------------------------------

See my Jap/Eng dictionary at http://www.omron.co.jp/cgi-bin/j-e
                          or http://www.cs.cmu.edu:8001/cgi-bin/j-e



Tue, 18 Mar 1997 10:44:54 GMT  
 Removing C Comments

Quote:
>Has anyone written a small perl script that will remove _all_ C comments.

c2ph - in the distribution - has a first stab at removing regular
comments.
Note, though, that nothing of what I have seen so far in perl is able
to trap the following:

        printf ("/* I am not a comment */\n");

Which may require a table driven-like contraption (I am unsure of what
C says about single quoted '/*' - also ignored , i daresay).

Best regards,
        Alessandro



Fri, 21 Mar 1997 18:03:32 GMT  
 Removing C Comments
|> >Has anyone written a small perl script that will remove _all_ C comments.
|>
|> c2ph - in the distribution - has a first stab at removing regular
|> comments.  Note, though, that nothing of what I have seen so far in perl
|> is able to trap the following:
|>
|>      printf ("/* I am not a comment */\n");

Lordy, this is the _third_ time in less than a month that I've posted
this... maybe it's not getting out (I've never seen a single reponse to
it... does anyone see ever see my posts?).

The following is a complete perl script that should remove all comments
from any valid C program:

    $/ = undef;
    $_ = <>;
    s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|\n+|.[^/"'\\]*)#$2#g;
    print;

*jeffrey*
-------------------------------------------------------------------------

See my Jap/Eng dictionary at http://www.omron.co.jp/cgi-bin/j-e
                          or http://www.cs.cmu.edu:8001/cgi-bin/j-e



Sat, 22 Mar 1997 08:58:20 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Is there an easy way to remove C-like comment from a file

2. Remove Nested Comments

3. Regexp to remove comments

4. Removing C and C++ comments

5. Remove comments in email addresses

6. Removing comments from C files using perl

7. Removing of nested C-like comments.

8. Removing comments from C files ?

9. removing shell comments from ksh scripts

10. Remove comments

11. removing HTML comments

12. Removing comment lines

 

 
Powered by phpBB® Forum Software