Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

how do I print content of a cpp file without cpp comments?

by jsmith (Initiate)
on Sep 19, 2006 at 13:14 UTC ( [id://573709]=perlquestion: print w/replies, xml ) Need Help??

jsmith has asked for the wisdom of the Perl Monks concerning the following question:

I don't want to print c++ program comments only all of c++ program but not comments that start with // and /*  .... */. what I have is this and it prints everything (includes comments as well).
open(city, "city.cpp.txt") || die ("sorry, file couldn't be opened. $! +"); $content = <city>; while ($content) { print "$content"; $content = <city>; } close (city);

20060919 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: how do I print content of a cpp file without cpp comments?
by PreferredUserName (Pilgrim) on Sep 19, 2006 at 13:47 UTC
    Another way:
    # first get the File::Comments module from CPAN use File::Comments; my $file = File::Comments->new(); print $file->stripped("city.cpp");
Re: how do I print content of a cpp file without cpp comments?
by hgolden (Pilgrim) on Sep 19, 2006 at 13:37 UTC
    Update: Pardon my well-intentioned mistake. See comments below.

    Hey

    I tried to stick close to your structure, and I did this a little unconventionally to avoid the multi-line match. I'm sure that someone else will tell you (correctly) that this is more cleanly done with such a match, but I think it's easier to mimic your structure this way, and that helps to see what's going on.

    use warnings; open(CITY, "city.cpp.txt") || die("sorry, file couldn't be opened. $!" +); while ($content=<CITY>) { if ($content!~m#//|/\*#) { print "$content"; } elsif ($content=~m#/\*#) { while (<CITY>!~m#\*/#) { } } }
    This program works by printing lines that don't have comment bars and aren't the beginning of multiline comments. If a line is the beginning of a multiline comment, it does a while loop that doesn't do anything except skip through lines until it finds the end of the comment. Now when the filehandle is passed back to your initial while loop, the next line is the one after the multiline comment.

    Let me know if you need more help figuring out how this works.

    Hays

      I came to C++ from a C background, and thus I often wrote code as follows:

      /* This does something cool */ do_something_cool(); /* This doesn't */ //....

      Which is to point out that your current example has a subtle bug wherein it appears you'll drop all of the text in my example, rather than just the 3 lines of comments.

      Still, I'm sure jsmith can work on that.

      All the best,

      jarich

      It's been a while since I've written much C or C++, but doesn't your code have a slight problem when the comment character sequences appear in quoted strings?

      Which (along with jarich's bug) just goes to illustrate the problems of trying to parse complex data using regular expressions. You'll almost certainly fall over subtle edge cases unless you use a real parser.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        Yep, I concede. File::Comments it is. This is what happens when you learn Perl without going through C first, and forget to always check CPAN :)

        Hays

      thanks a bunch!
Re: how do I print content of a cpp file without cpp comments? (link)
by tye (Sage) on Sep 19, 2006 at 15:45 UTC
Re: how do I print content of a cpp file without cpp comments?
by rir (Vicar) on Sep 19, 2006 at 20:46 UTC
    I did this once long ago in C. Nibble through your file char by char and change state at the appropriate events. You need to track states like: directive, code, comment, comment_plus, string_literal, char_literal, directive_string, and directive_angle_string (system lib names). You also need to track if you are at the start_of_line, and would need to look ahead a char on occasion and deal with escapes.

    As I recall the compilation sequence of trigraphs, preprocessing, and processing did not have any real impact. I just bailed on trigraphs; I have still never seen a live one. Trigraphs may be non-events for your purpose, I had not considered that on my first go-round.

    The Right-Thing is to replace a comment with a single space.

    Be well,
    rir

Re: how do I print content of a cpp file without cpp comments?
by smokemachine (Hermit) on Sep 19, 2006 at 18:24 UTC
    perl -ne '($f.=$_)=~s;//.*$;;;END{$f=~s;/\*.*\*/|^\s*$;;gsm;print $f}' test.c

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-23 07:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found