This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Re: Can `cpp' strip off comments in C source code?
- From: Robert Kiesling <kiesling at earthlink dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 24 Dec 2007 09:53:41 -0500 (EST)
- Subject: Re: Re: Can `cpp' strip off comments in C source code?
- Reply-to: rkies at cpan dot org
[ Charset ISO-8859-1 unsupported, converting... ]
> On 12/24/07, PRC <panruochen@gmail.com> wrote:
> > Things are a little complicated. I've googled a sed script on:
> > http://sed.sourceforge.net/grabbag/scripts/remccoms2.sh.txt
> >
> > But the script can't handle this testcase:
> > printf("/* Haha \n"); /* Haha */
> > printf("// Haha \n"); // Haha
> >
> > I think a simple regular expression filter is not enough. A lexical parsing is required to ensure the results come out right in all cases.
> > A prefessional utility is more suitable than a script written by me since I am not an expert in ANSI C standard.
> >
> > >AWK itself can do it, as can sed.
> >
> >
>
>
> Here's an AWK solution for you:
>
> http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.awk/2006-03/msg00200.html
>
> See if it suits your needs. For something like this, I think awk
> might be more capable than sed... not sure, though.
Perl does this very well. I'm certain awk and sed can also accomplish
the task, though I haven't looked up your reference, and I don't have the
manuals in front of me.
perl -e 'undef $/; $i = <>; $i =~ s|/*.*?\*/||gsm; $i =~ s|//.*?\n||gsm; print $i;' <infile
(To answer the OP's question, btw, cpp does not have an option to remove comments without
preprocessing the input, IIRC.)
--
Ctalk Home Page: http://www.ctalklang.org/