perlfaq6

V

Vittal

Hello All,

I am going the FAQ of perl. In perlfaq6, a method has been given to
remove C/C++ comments. I am not understanding how to use it. Can
somebody help me in understanding?
man perlfaq6 ( How do I use a regular expression to strip C style
comments from a file?)


s{
/\* ## Start of /* ... */ comment
[^*]*\*+ ## Non-* followed by 1-or-more *?s
(
[^/*][^*]*\*+
)* ## 0-or-more things which don?t start with
/
## but do end with ?*?
/ ## End of /* ... */ comment

? ## OR various things which aren?t comments:

(
" ## Start of " ... " string
(
\\. ## Escaped char
? ## OR
[^"\\] ## Non "\
)*
" ## End of " ... " string

? ## OR

? ## Start of ? ... ? string
(
\\. ## Escaped char
? ## OR
[^?\\] ## Non ?\
)*
? ## End of ? ... ? string

? ## OR

. ## Anything other char
[^/"?\\]* ## Chars which doesn?t start a comment,
string or escape
)
}{$2}gxs;
A slight modification also removes C++ comments:

s#/\*[^*]*\*+([^/*][^*]*\*+)*/?//[^\n]*?("(\\.?[^"\\])*"??(\\.?[^?\\])*??.[^/"?\\]*)#$2#gs;
 
P

Paul Lalli

Vittal said:
Hello All,

I am going the FAQ of perl. In perlfaq6, a method has been given to
remove C/C++ comments. I am not understanding how to use it. Can
somebody help me in understanding?
man perlfaq6 ( How do I use a regular expression to strip C style
comments from a file?)

<snipped absurdly long regexp>

It's a search-and-replace operation. You need to build a string which
contains the contents of your file, apply this search and replace
operation to that string, and write that string back to your file.

perldoc -f open
perldoc -f readline
perldoc perlvar (search for 'slurp')
perldoc perlretut (search for 'search and replace')
perldoc -f print

Paul Lalli
 
V

Vittal

I have still problems in using this regexp. Here is my perl script and
the C file which it takes as input.
--------------------------------------------------------------------------------
#!/usr/bin/perl

open my $fh,"test.c" or die $!;
local $/;
$_= <$fh>;
close $fh;


s#/\*[^*]*\*+([^/*][^*]*\*+)*/?//[^\n]*?("(\\.?[^"\\])*"??(\\.?[^?\\])*??.[^/"?\\]*)#$2#gs;

print $_;

--------------------------------------------------------------------------------
/* This is the comment part */
int main (int argc, char *argv[])
{
printf("Hello World \n");
}
--------------------------------------------------------------------------------

May I doing anything wrong in the script???

Any help is greatly appreciated.

Thanks & Regards
-Vittal
 
A

Anno Siegel

Vittal said:
I have still problems in using this regexp. Here is my perl script and
the C file which it takes as input.
--------------------------------------------------------------------------------
#!/usr/bin/perl

open my $fh,"test.c" or die $!;
local $/;
$_= <$fh>;
close $fh;


s#/\*[^*]*\*+([^/*][^*]*\*+)*/?//[^\n]*?("(\\.?[^"\\])*"??(\\.?[^?\\])*??.[^/"?\\]*)#$2#gs;

print $_;

That is not the regex from the "comment" faq (for Perl v5.8.1). In
particular, the double "??" are in error.

Anno
 
J

Jim Keenan

Anno said:
s#/\*[^*]*\*+([^/*][^*]*\*+)*/?//[^\n]*?("(\\.?[^"\\])*"??(\\.?[^?\\])*??.[^/"?\\]*)#$2#gs;

print $_;


That is not the regex from the "comment" faq (for Perl v5.8.1). In
particular, the double "??" are in error.

There's something amiss with Vittal's character mapping. Some
characters (e.g., the vertical bar (|) and the single quote (') are
being rendered as question marks (?).

jimk
 
B

brian d foy

Vittal said:
Hello All,

I am going the FAQ of perl. In perlfaq6, a method has been given to
remove C/C++ comments. I am not understanding how to use it. Can
somebody help me in understanding?

I'm not sure where you had trouble, but it looks like the example
in perlfaq6 had a syntax error. It was missing a semicolon after
the substitution. I've fixed this in the repository, but it won't
show up in perl until the next release.


$/ = undef;
$_ = <>;

s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]
*)#$2#gs;
print;

Save this in a file (say, strip_comments), and call it with the file
you want to strip:

perl strip_comments file.c
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top