Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2003
    Posts
    10

    Question Unanswered: Matching and Extracting Comments

    Hello everyone!!

    Does anyone knows a module that parses and extracts comments of a given source code file?? (such as C, C++, Java, PL/SQL, PERL, etc)!!?

    There´s a module in CPAN that was designed to do this (Regexp::Common::comment) but this module don´t preview all cases for example in PERL!!

    Because.. there are special cases that should be contemplated such as:

    @array=/1 # 2 3 4/;
    print"#This is not a comment";
    $size=?#array;
    @array=("#", 1,2,3);

    How can i do this?

    There is another module Text::Balanced that should work to.. with the function extract_delimited, where the delimitators would be starting with # and ending with \n! But i can´t make this work! (

    Another issue.. is this regex that should get the comments in Perl but it doesn´t work also!!

    ~ m{
    ( \# .*? \n ) # extract a comment wich starts with # to \n
    | " (?: [^"\#]* | \#. )* " # skip over "..."
    | ' (?: [^'\#]* | \#. )* ' # skip over '...'
    | . [^\#"']* # skip over non-comments-or-quotes
    }xgs;

    Can someone tell me why??

    Thank you all very much!


    darkname...

  2. #2
    Join Date
    Jul 2003
    Posts
    3

    Re: Matching and Extracting Comments

    Hello darkname
    If your difficulty is: How to remove '#' from my codes, I try to help.
    This script remove all '#' character off determinade script
    --------------------------------------------------------------------------
    #!/usr/bin/perl -w
    $| = 1;

    open (CONT, "file");
    my @lines = <CONT>;
    close (CONT);

    open (CONT, "> file");
    print CONT "";

    my $i = 0;
    foreach $line (@lines)
    {
    $line =~ s/#//g;
    $i += 1;
    print CONT $line;
    }
    close (CONT);
    --------------------------------------------------------------------------
    If need, you can use 'chop' to extract the last character like '\n'
    and you probably it wants to modify '#' to '/*' and '*/' to last character,
    but if you want to remove all lines with comments you can make one array to enumerate the lines and use de 'system ("sed '$Linenumberd' ")' to delete lines that they contain comments where 'd' character is a command to delete line and '$Linenumber' is a variable to indicate the line to delete. After you need to redirect the sult to fileHandle.
    I wait to have helped
    See you later.

  3. #3
    Join Date
    Jul 2003
    Posts
    3

    Cool Re: Matching and Extracting Comments

    Hello darkname
    If your difficulty is: How to remove '#' from my codes, I try to help.
    This script remove all '#' character off determinade script
    --------------------------------------------------------------------------
    #!/usr/bin/perl -w
    $| = 1;

    open (CONT, "file");
    my @lines = <CONT>;
    close (CONT);

    open (CONT, "> file");
    print CONT "";

    my $i = 0;
    foreach $line (@lines)
    {
    $line =~ s/#//g;
    $i += 1;
    print CONT $line;
    }
    close (CONT);
    --------------------------------------------------------------------------
    If need, you can use 'chop' to extract the last character like '\n'
    and you probably it wants to modify '#' to '/*' and '*/' to last character,
    but if you want to remove all lines with comments you can make one array to enumerate the lines and use de 'system ("sed '$Linenumberd' ")' to delete lines that they contain comments where 'd' character is a command to delete line and '$Linenumber' is a variable to indicate the line to delete. After you need to redirect the sult to fileHandle.
    I wait to have helped
    See you later.

  4. #4
    Join Date
    Jul 2003
    Posts
    3

    Re: Matching and Extracting Comments

    Hello darkname
    If your difficulty is: How to remove '#' from my codes, I try to help.
    This script remove all '#' character off determinade script
    -----------------------------
    #!/usr/bin/perl -w
    $| = 1;

    open (CONT, "file");
    my @lines = <CONT>;
    close (CONT);

    open (CONT, "> file");
    print CONT "";

    my $i = 0;
    foreach $line (@lines)
    {
    $line =~ s/#//g;
    $i += 1;
    print CONT $line;
    }
    close (CONT);
    -----------------------------
    If need, you can use 'chop' to extract the last character like '\n'
    and you probably it wants to modify '#' to '/*' and '*/' to last character,
    but if you want to remove all lines with comments you can make one array to enumerate the lines and use de 'system ("sed '$Linenumberd' ")' to delete lines that they contain comments where 'd' character is a command to delete line and '$Linenumber' is a variable to indicate the line to delete. After you need to redirect the sult to fileHandle.
    I wait to have helped
    See you later.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •