C言語のコメント内容を削除する

機能:
Cコードのコメント内容を取り除く
1.一行目コメント//;
2.複数行目のコメント内容/**/;
3.“\”末尾は次の行目コメント
4.文字列のコメントが処理しない

サンプルコード:

#include

int main(int argc, char* argv[]) {
enum {
literal,
single,
multiple,
string
} mode = literal;
char last = 0, current;

while ((current = getchar()) != EOF) {
switch (mode) {
case single: {
if (last != '\\’ && (current == '\n’ || current == '\r’)) {
putchar(current);
current = 0;
mode = literal;
}
} break;
case multiple: {
if (last == '*’ && current == '/’) {
current = 0;
mode = literal;
}
} break;
case string: {
if (last == '\\’) {
putchar(last);
putchar(current);
} else if (current != '\\’) {
putchar(current);
if (current == '"') {
mode = literal;
}
}
} break;
default: {
if (last == '/’) {
if (current == '/’) {
mode = single;
} else if (current == '*’) {
mode = multiple;
} else {
putchar(last);
putchar(current);
}
} else if (current != '/’) {
putchar(current);
if (current == '"') {
mode = string;
}
}
} break;
}
last = current;
}

return 0;
}

Source

Posted by arkgame