// blah blah
/* blah blah */
int a; /* blah blah
blah blah
blah blah
*/ int b;
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
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
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
What have you tried?
perl -0777 -pe '
s{
/\*.*?\*/
| //[^\n]*
| (
"(?:\\.|.)*?"
| '\''(?:\\.)?.*?'\''
| \?\?'\''
| .[^'\''"/?]*
)
}{$1 eq ""?" ":$1}exsg' < that-file.c
gives me:
<<
int a; int b;
>>
--
Stéphane
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
Sorry, I just copied it directly from the Perl FAQ.
man perlfaq6
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.
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
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.
All the __LINE__...
http://groups.google.com/group/comp.unix.shell/browse_frm/thread/274d5d8dcd180158
--
Stephane
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.