How to comment C++ code using a Perl Script 
Author Message
 How to comment C++ code using a Perl Script

Hi ,

Can anyone provide me with a sample perl script which will comment out
a line of code using C++ style comments in a C++ program.

Regards

Sajith.



Tue, 02 Mar 2004 07:06:50 GMT  
 How to comment C++ code using a Perl Script

Quote:

> Hi ,

> Can anyone provide me with a sample perl script which will comment out
> a line of code using C++ style comments in a C++ program.

perl -pe 'print "//" if /foo/ .. /bar/' file.C > newfile.C

Will comment blocks of lines, starting with a line containing foo,
ending at a line containing bar.

This oneliner is untested.

--
"I think not," said Descartes, and promptly disappeared.



Tue, 02 Mar 2004 08:15:26 GMT  
 How to comment C++ code using a Perl Script

Quote:

> Can anyone provide me with a sample perl script which will comment out
> a line of code using C++ style comments in a C++ program.

$ perl -wi.bak -pe 's\(.+)\/* $1 */\ if $.==42' file.c

-mona



Tue, 02 Mar 2004 08:18:21 GMT  
 How to comment C++ code using a Perl Script

Quote:
> Hi ,

> Can anyone provide me with a sample perl script which will comment out
> a line of code using C++ style comments in a C++ program.

From the FAQ:

How do I use a regular expression to strip C style comments from a file?
While this actually can be done, it's much harder than you'd think. For
example, this one-liner

    perl -0777 -pe 's{/\*.*?\*/}{}gs' foo.c
will work in many but not all cases. You see, it's too simple-minded for
certain kinds of C programs, in particular, those with what appear to be
comments in quoted strings. For that, you'd need something like this,
created by Jeffrey Friedl and later modified by Fred Curtis.

    $/ = undef;
    $_ = <>;

s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#
$2#gs
    print;
This could, of course, be more legibly written with the /x modifier, adding
whitespace and comments. Here it is expanded, courtesy of Fred Curtis.

    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;

Wyzelli-- ($a,$b,$w,$t)=(' bottle',' of beer',' on the wall','Take one down,
pass it around');$d='$_$a$s$b$w';$e='$_$a$s$b';sub
d{$h=shift;$h=~s/\$(\w+)/${$1}/g;return$h}sub
e{return(shift!=1)?'s':''}for(reverse(1..100)){$s=e($_);$f=d($d);$g=d($e);$c
.="$f\n$g\n$t\n";$_--;$s=e($_);$e=d($d);$c.="$e\n\n";}print"$c*hic*";



Tue, 02 Mar 2004 08:41:54 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Grabbing Comments from C, C++ code.

2. using system() in Perl to execute compiled C++ code

3. Perl script to counting c/c++ code

4. Perl scripts to parse C++ code

5. Perl Script count lines of c/c++ code

6. using perl scripts from C++

7. REQUEST: Need AutoDoc Script for C/C++ Code

8. Routine to Color-Code Java/C/C++ Code

9. gcc and // C++ type comments?

10. Deleting C++ comments (s/\/\*.*\*\///)

11. C/C++ Comment Extractor :[Newbie]

12. Removing C and C++ comments

 

 
Powered by phpBB® Forum Software