Saeed schrob:
> I have seen searching for a code example that removes c-style comments,
> but none of these take into account strings literals, e.g.
[...]
That's a FAQ; see perldoc -q comments. But that solution is incomplete,
too:
/??/
* foo *\
/
is a single comment, according to the C standard. "??/" is a trigraph
expanding to "\", and backslash-newline pairs are deleted before
tokenizing the program, so the above is equivalent to
/* foo */
The following script should do the job:
#!/usr/local/bin/perl -wp0777
use strict;
# this script reads files, removes C comments,
# and prints the results to stdout
s{
/
(?: (?: \\ | \?\?/) \n)*
(?:
/ (?: (?: \\ | \?\?/) \n | [^\n] )*
|
\* [^*]* \*+ (?: (?: \\ | \?\?/) \n)*
(?: [^/*][^*]* \*+ (?: (?: \\ | \?\?/) \n)* )*
(/)
)
|
(
" (?: (?: \\ | \?\?/) . | [^"])* "
|
' (?: (?: \\ | \?\?/) . | [^'])* '
|
. [^'"/]*
)
}{
(defined $1 ? ' ' : '') . (defined $2 ? $2 : '')
}gsex
__END__
HTH, Lukas
--
print+74.117.115.116,,qq.\c!..not::.her,Perl=>q$ha cker,$,!($,=$")
|