Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

removing comments

5 views
Skip to first unread message

gauta...@gmail.com

unread,
Mar 7, 2006, 12:15:35 AM3/7/06
to
hi all,
i am still not able to remove all the comments from a C/C++ file using
perl.
the comments could be in the following formats.

// blah blah

/* blah blah */


int a; /* blah blah
blah blah
blah blah
*/ int b;

jakob

unread,
Mar 7, 2006, 12:28:16 AM3/7/06
to

I did this in two steps:

perl -pe 's/\/\/.*//' <file> | \
perl -e '$_ = join "", <>; s/\/\*.*?\*\///gs; print'

It could easily be combined into a single script.

jakob

Xicheng

unread,
Mar 7, 2006, 12:38:03 AM3/7/06
to

if you don't have //, /* or */ in any of your string variables or some
like things, i.e.

char *str = "abs // sdf ";

you may try:

perl -0777 -pe 's{//.*?$|/\*.*?\*/}{}smg' myfile.c

otherwise, try some more robust solutions(you may search Stephane's
post in the topic"Ignore Comments in Recursive diff?"),

Xicheng

John W. Krahn

unread,
Mar 7, 2006, 3:34:51 AM3/7/06
to

Perl has a FAQ for that:

perldoc -q "How do I use a regular expression to strip C style comments from a
file"


If you don't have the Perl docs on your system:

perl -0777 -pe'
s{/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)}
{ defined $2 ? $2 : '' }gse
' yourfile.c


John
--
use Perl;
program
fulfillment

Stephane CHAZELAS

unread,
Mar 7, 2006, 3:50:49 AM3/7/06
to
2006-03-6, 21:15(-08), gauta...@gmail.com:

What have you tried?

perl -0777 -pe '
s{
/\*.*?\*/


| //[^\n]*
| (

"(?:\\.|.)*?"
| '\''(?:\\.)?.*?'\''
| \?\?'\''
| .[^'\''"/?]*
)
}{$1 eq ""?" ":$1}exsg' < that-file.c

gives me:

<<

int a; int b;
>>

--
Stéphane

Stephane CHAZELAS

unread,
Mar 7, 2006, 4:02:02 AM3/7/06
to
2006-03-07, 08:34(+00), John W. Krahn:
[...]

That line was not intended to be a shell line at first, I think
(problems with single quotes).

As already discussed in
http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/c6dca9d6d8525e77

you can make it simpler by considering the special way
alternations and non-greedy operators work in perl. Also, you
need to replace a comment with a space, not to remove it.

foo/* bar */baz is foo baz.

--
Stéphane

John W. Krahn

unread,
Mar 7, 2006, 7:48:53 AM3/7/06
to

Sorry, I just copied it directly from the Perl FAQ.

man perlfaq6

Ed Morton

unread,
Mar 7, 2006, 8:19:25 AM3/7/06
to
gauta...@gmail.com wrote:
> hi all,
> i am still not able to remove all the comments from a C/C++ file using
> perl.

Just let the pre-processor do it for you, e.g.:

sed 's/#/__POUND__/g' orig.c | gcc -E - | sed -e '/#/d' -e 's/__POUND__/#/g'

I've never tried any of the perl solutions going around, but I have
tried many of the allegedly "working" sed solutions posted in various
forums and none of them work for all cases so I'm not real inspired to
try any other solution that doesn't involve a language parser and there
just doesn't seem to be any point when there's parsers around built for
the job.

Ed.

Stephane Chazelas

unread,
Mar 7, 2006, 8:49:12 AM3/7/06
to
[...]

And, as we discussed at length some time ago, the above does
more than removing commands (it expands some predifined macros
for instance) and assumes the GNU C compiler is installed on the
machine.

--
Stephane

Ed Morton

unread,
Mar 7, 2006, 9:30:38 AM3/7/06
to

Sorry, I don't recall any discussion on expanding macros that wasn't
taken care of by removing all the #includes as above. Please refresh my
memory...

and assumes the GNU C compiler is installed on the
> machine.
>

Well, yes, and the perl solutions assume perl is installed on the
machine. I know gcc is easily downloaded and installed and I don't know
but I assume the same is true for perl so I don't see that as an issue
either way..

Ed.

Stephane Chazelas

unread,
Mar 7, 2006, 9:43:18 AM3/7/06
to
On Tue, 07 Mar 2006 08:30:38 -0600, Ed Morton wrote:
[...]
>> And, as we discussed at length some time ago, the above does
>> more than removing commands (it expands some predifined macros
>> for instance)
>
> Sorry, I don't recall any discussion on expanding macros that wasn't
> taken care of by removing all the #includes as above. Please refresh my
> memory...

All the __LINE__...
http://groups.google.com/group/comp.unix.shell/browse_frm/thread/274d5d8dcd180158

--
Stephane

Ed Morton

unread,
Mar 7, 2006, 4:38:24 PM3/7/06
to

Ahh yes, now I remember. Good discussion. Looks like I ended up thinking
that:

sed 's/a/aA/g;s/__/aB/g;s/b/bA/g;s/#/bB/g' file.c |
gcc -P -E - |
sed 's/bB/#/g;s/bA/b/g;s/aB/__/g;s/aA/a/g'

was the best solution since given a file like this:

-----------------
#include "stdio.h"

#define GOOGLE(txt) printf("Google web page = " #txt "\n")

int main(void) {
GOOGLE(http://www.google.com);
}
------------------

I could just add a "-ansi" flag to gcc (i.e. make it "gcc -P -E -ansi
-") to get it to understand what version of C I was trying to parse and
so produce this compilable C:

------------------
#include "stdio.h"

#define GOOGLE(txt) printf("Google web page = " #txt "\n")

int main(void) {
GOOGLE(http://www.google.com);
}
------------------

rather than this (which would not compile) as my original sed/gcc
suggestion and your perl suggestion would produce:

------------------
#include "stdio.h"

#define GOOGLE(txt) printf("Google web page = " #txt "\n")

int main(void) {
GOOGLE(http:
}
------------------

Regards,

Ed.

0 new messages