Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Modify C comment removal code to kill newlines

by Velaki (Chaplain)
on Aug 21, 2006 at 13:48 UTC ( [id://568543]=note: print w/replies, xml ) Need Help??


in reply to Modify C comment removal code to kill newlines

Here's a little snippet that produces the output you desire. However, it doesn't really take into account nested comments or multiline comments, but you can extend that on your own.

By the way, your last line of sample code /*The end!/*} is not a valid comment terminator. I think you meant /*The end!*/}.

#!/usr/bin/perl use strict; use warnings; undef $/; my $code = <DATA>; # $code =~ s|^\s*/\*.*?\*/||gm; # Changed as per anjiro's comments $code =~ s#^\s*/\*.*?\*/##gm; $code =~ s#(?<=\S)\s/\*.*?\*/\s*?$##gm; $code =~ s#\n##ms; print $code; __DATA__ /*This is a bogus function*/ void function foo(void) /*My function is the best*/ { int i; /*i is an integer*/ int j; /*j is also an integer*/ /*Now I'm going to set i to 1*/ i = 1; /*Also j*/ j = 1; /*Here's some incrementing!*/ i++; /*And more!*/ j++; /*The end!*/}

Hope this helped,
-v.

"Perl. There is no substitute."

Update: Changed the regexes to reflect anjiro's comments.

Replies are listed 'Best First'.
Re^2: Modify C comment removal code to kill newlines
by anjiro (Beadle) on Aug 21, 2006 at 14:39 UTC
    It also doesn't kill the end-of-line comments:
    void function foo(void) /*My function is the best*/ { int i; /*i is an integer*/ int j; /*j is also an integer*/ i = 1; j = 1; i++; j++; }
    And you're right about "The end!/*" - I'll fix that now.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://568543]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found