这个程序适用于linux下的程序,windows下的需稍作修改
#include <stdio.h>
#include <stdlib.h>
/*
* 该程序用于消除C/C++源代码中的注释
*/
enum STATE
{
STATE_SLASH,
STATE_SLASH_SLASH,
STATE_SLASH_SLASH_BACKSLASH,
STATE_SLASH_POINT,
STATE_SLASH_POINT_POINT,
STATE_QUOTA,
STATE_QUOTA_BACKSLASH,
STATE_APOSTROPHE,
STATE_APOSTROPHE_BACKSLASH,
STATE_NORMAL
};
int main(int argc, char** argv) {
if( argc != 1 )
{
printf("usage: remove_comment xxx\n");
exit(1);
}
int m_state = STATE_NORMAL;
FILE *fp = fopen( argv[1], "r");
if( fp == NULL )
{
printf("open file failed\n");
exit(1);
}
int i_char;
while((i_char = fgetc(fp)) != EOF)
{
switch(i_char)
{
case '/':
switch(m_state)
{
case STATE_NORMAL:
m_state = STATE_SLASH;
break;
case STATE_SLASH:
m_state = STATE_SLASH_SLASH;
break;
case STATE_SLASH_POINT_POINT:
m_state = STATE_NORMAL;
break;
case STATE_SLASH_SLASH:
case STATE_SLASH_POINT:
break;
case STATE_QUOTA:
putchar(i_char);
break;
case STATE_QUOTA_BACKSLASH:
putchar(i_char);
m_state = STATE_QUOTA;
break;
case STATE_APOSTROPHE:
putchar(i_char);
break;
case STATE_APOSTROPHE_BACKSLASH:
putchar(i_char);
m_state = STATE_APOSTROPHE;
break;
case STATE_SLASH_SLASH_BACKSLASH:
m_state = STATE_SLASH_SLASH;
break;
}
break;
case '"':
switch(m_state)
{
case STATE_NORMAL:
m_state = STATE_QUOTA;
putchar(i_char);
break;
case STATE_SLASH:
m_state = STATE_NORMAL;
putchar('/');
putchar(i_char);
break;
case STATE_SLASH_POINT_POINT:
m_state = STATE_SLASH_POINT;
break;
case STATE_SLASH_SLASH:
case STATE_SLASH_POINT:
break;
case STATE_QUOTA:
m_state = STATE_NORMAL;
putchar(i_char);
break;
case STATE_QUOTA_BACKSLASH:
putchar(i_char);
m_state = STATE_QUOTA;
break;
case STATE_APOSTROPHE:
putchar(i_char);
break;
case STATE_APOSTROPHE_BACKSLASH:
putchar(i_char);
m_state = STATE_APOSTROPHE;
break;
case STATE_SLASH_SLASH_BACKSLASH:
m_state = STATE_SLASH_SLASH;
break;
}
break;
case '\'':
switch(m_state)
{
case STATE_NORMAL:
m_state = STATE_APOSTROPHE;
putchar(i_char);
break;
case STATE_SLASH:
m_state = STATE_NORMAL;
putchar('/');
putchar(i_char);
break;
case STATE_SLASH_POINT_POINT:
m_state = STATE_SLASH_POINT;
break;
case STATE_SLASH_SLASH:
case STATE_SLASH_POINT:
break;
case STATE_QUOTA:
putchar(i_char);
break;
case STATE_QUOTA_BACKSLASH:
putchar(i_char);
m_state = STATE_QUOTA;
break;
case STATE_APOSTROPHE:
putchar(i_char);
m_state = STATE_NORMAL;
break;
case STATE_APOSTROPHE_BACKSLASH:
putchar(i_char);
m_state = STATE_APOSTROPHE;
break;
case STATE_SLASH_SLASH_BACKSLASH:
m_state = STATE_SLASH_SLASH;
break;
}
break;
case '\n':
switch(m_state)
{
case STATE_NORMAL:
putchar(i_char);
break;
case STATE_SLASH:
m_state = STATE_NORMAL;
putchar('/');
putchar(i_char);
break;
case STATE_SLASH_POINT_POINT:
m_state = STATE_SLASH_POINT;
break;
case STATE_SLASH_SLASH:
m_state = STATE_NORMAL;
putchar(i_char);
break;
case STATE_SLASH_POINT:
break;
case STATE_QUOTA:
// 这个情况语法出错了
break;
case STATE_QUOTA_BACKSLASH:
// 这个情况语法出错了
break;
case STATE_APOSTROPHE:
// 这个情况语法出错了
break;
case STATE_APOSTROPHE_BACKSLASH:
// 这个情况语法出错了
break;
case STATE_SLASH_SLASH_BACKSLASH:
m_state = STATE_SLASH_SLASH;
break;
}
break;
case '*':
switch(m_state)
{
case STATE_NORMAL:
putchar(i_char);
break;
case STATE_SLASH:
m_state = STATE_SLASH_POINT;
break;
case STATE_SLASH_POINT_POINT:
m_state = STATE_SLASH_POINT;
break;
case STATE_SLASH_SLASH:
break;
case STATE_SLASH_POINT:
m_state = STATE_SLASH_POINT_POINT;
break;
case STATE_QUOTA:
putchar(i_char);
break;
case STATE_QUOTA_BACKSLASH:
putchar(i_char);
m_state = STATE_QUOTA;
break;
case STATE_APOSTROPHE:
putchar(i_char);
break;
case STATE_APOSTROPHE_BACKSLASH:
putchar(i_char);
m_state = STATE_APOSTROPHE;
break;
case STATE_SLASH_SLASH_BACKSLASH:
m_state = STATE_SLASH_SLASH;
break;
}
break;
case '\\':
switch(m_state)
{
case STATE_NORMAL:
putchar(i_char);
break;
case STATE_SLASH:
m_state = STATE_SLASH_POINT;
putchar('/');
putchar(i_char);
break;
case STATE_SLASH_POINT_POINT:
m_state = STATE_SLASH_POINT;
break;
case STATE_SLASH_SLASH:
m_state = STATE_SLASH_SLASH_BACKSLASH;
break;
case STATE_SLASH_POINT:
break;
case STATE_QUOTA:
m_state = STATE_QUOTA_BACKSLASH;
putchar(i_char);
break;
case STATE_QUOTA_BACKSLASH:
putchar(i_char);
m_state = STATE_QUOTA;
break;
case STATE_APOSTROPHE:
m_state = STATE_APOSTROPHE_BACKSLASH;
putchar(i_char);
break;
case STATE_APOSTROPHE_BACKSLASH:
putchar(i_char);
m_state = STATE_APOSTROPHE;
break;
case STATE_SLASH_SLASH_BACKSLASH:
m_state = STATE_SLASH_SLASH;
break;
}
break;
case '\ ':
case '\t':
switch(m_state)
{
case STATE_NORMAL:
putchar(i_char);
break;
case STATE_SLASH:
m_state = STATE_NORMAL;
putchar('/');
putchar(i_char);
break;
case STATE_SLASH_POINT_POINT:
m_state = STATE_SLASH_POINT;
break;
case STATE_SLASH_SLASH:
case STATE_SLASH_POINT:
break;
case STATE_QUOTA:
putchar(i_char);
break;
case STATE_QUOTA_BACKSLASH:
putchar(i_char);
m_state = STATE_QUOTA;
break;
case STATE_APOSTROPHE:
putchar(i_char);
break;
case STATE_APOSTROPHE_BACKSLASH:
putchar(i_char);
m_state = STATE_APOSTROPHE;
break;
case STATE_SLASH_SLASH_BACKSLASH:
m_state = STATE_SLASH_SLASH_BACKSLASH;
break;
}
break;
default:
switch(m_state)
{
case STATE_NORMAL:
putchar(i_char);
break;
case STATE_SLASH:
m_state = STATE_NORMAL;
putchar('/');
putchar(i_char);
break;
case STATE_SLASH_POINT_POINT:
m_state = STATE_SLASH_POINT;
break;
case STATE_SLASH_SLASH:
case STATE_SLASH_POINT:
break;
case STATE_QUOTA:
putchar(i_char);
break;
case STATE_QUOTA_BACKSLASH:
putchar(i_char);
m_state = STATE_QUOTA;
break;
case STATE_APOSTROPHE:
putchar(i_char);
break;
case STATE_APOSTROPHE_BACKSLASH:
putchar(i_char);
m_state = STATE_APOSTROPHE;
break;
case STATE_SLASH_SLASH_BACKSLASH:
m_state = STATE_SLASH_SLASH;
break;
}
break;
}
}
fclose(fp);
return (EXIT_SUCCESS);
}
|
阅读(817) | 评论(0) | 转发(0) |