-
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
-
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.
-
Try the strstr function (find string in string).
Be carefull with removing single line comment:
Code:
int i = 0; // single line comment
-
still having no joy.
Any more ideas?
Thanks
-
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
-
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
-
>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
-
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
-
>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
-
Still have a problem with quotes (see reply vVv).
Code:
p[] = "printf(\" // This is no comment\\n\")";
-
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.
-
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