Thread: ignoring comments

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    25

    ignoring comments

    Hi,

    Trying to write a program that reads C files and so need to ignore comments. I'm using the strtok function

    strtok(myline, "//")

    to remove single line comments. However, this tokenises everything after just one forwards slash.

    I then tried to store two forwards slashes in an array, eg:

    char *delim[1] = {"//"};
    strtok(myline, delim[0]);

    to try to solve this, my the line still tokenises after one forward slash. This causes problems if, for example, there is a mathematcial division in the line.

    Any help greatly appreciated.

    Bill

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Don't you have to use an escape sequence to represent a '/'? I think '//' just represents a single backslash. Try '///' or '////' to represent two of them.

    Or, it could be the fact that you are tokenizing after with a single character. delim[0] = '/', delim = "//". I don't really know if you can use strtok with strings or not. Just trying to throw some ideas out there.
    Last edited by drharv; 04-24-2002 at 08:12 AM.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Try the strstr function (find string in string).
    Be carefull with removing single line comment:
    Code:
    int i = 0; // single line comment

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    still having no joy.

    Any more ideas?

    Thanks

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Single line comments aren't a part of the C language. Unless you're parsing C99 code, the only comment allowed by ANSI C is
    /* This is a a C comment */
    // This is not C89

    So that solves your problem there. If you read "/*" then ignore everything until you reach "*/".

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    But they're allowed by Microsot developer studio and i have to cater for every possibility.

    With the /*....*/ comment, I will have the same probem, as the strtok function seems only to recognise the first charcter.

    Any help appreciated!!!!

    Bill

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I will have the same probem, as the strtok function seems only to recognise the first charcter
    Correct, the delimiter string for strtok can be thought of as an array where if the string is "//", strtok sees it as '/', '/'. The logical conclusion is that strtok is not your best tool for finding comments.

    Try this, read a token and delimit it with a space and newline. If in that token you find two /'s right next to each other, then it's a comment and you can ignore everything from that point to the next newline.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    sorry, i don't understand what you mean with your new suggestion.

    is there any way that i could use the strstr function (which recognises whole strings)? Something along the lines of when it finds a "//", returns a pointer value to th eposition it was found. Then I could use this value in the strtok function. Would that be possible?!

    Thanks.

    Bill

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Then I could use this value in the strtok function
    That wouldn't work, do you have to use the strtok function? Because it would be considerably easier to do something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( void )
    {
      int x;
      char *comment = NULL,
           p[] = "while ( a[i] != '\\0' ) i++; // Comments here";
      if ( ( comment = strstr ( p, "//" ) ) != NULL ) {
        for ( x = 0; &p[x] != comment; x++ )
          putchar ( p[x] );
      }
      else
        puts ( p );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Still have a problem with quotes (see reply vVv).
    Code:
    p[] = "printf(\" // This is no comment\\n\")";

  11. #11
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    vVv,

    I wasn't criticize you but now I will...
    You're half-way. You found out about the code tags but please use spacing or idents so I can read it.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    thanks for your interest, i've solved it like this.....

    commentsRemoved = (strstr(copy, "//"));

    if (commentsRemoved) {
    (int)commentsRemoved = commentsRemoved- copy;
    lineWithoutComments = malloc(sizeof(commentsRemoved));
    strncpy(lineWithoutComments, copy, (int)commentsRemoved);
    }

    which i think is quite efficient.

    thanks to evberyone who contibuted....

    Bill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing comments of type '//' and '/*'
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2007, 02:24 AM
  2. program to remove comments from source
    By Abda92 in forum C Programming
    Replies: 12
    Last Post: 12-25-2006, 05:18 PM
  3. Comments problem with MSVC 2003
    By mikahell in forum Tech Board
    Replies: 7
    Last Post: 09-04-2006, 06:04 PM
  4. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  5. The Art of Writing Comments :: Software Engineering
    By kuphryn in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2002, 05:18 PM