有时候一个程序中如果注释太多,也会带来很多的不方便,于是便产生了删除注释的需要,删除注释有很多种方法,不过最近实现的用确定有限自动机(DFA)实现起来感觉也是比较方便的。
经过分析需要删除的注释大概有一下4中情况:
1、int a ; //这里是注释 ;
2、int a ; /*这里
是注释
是的*/ ;
3、int a ; /*这里是注释*/ ;
4、对于字符串中的注释符不做处理如:
printf("adfadsf/*sdfaf*/ \n"); 对其中的/**/之间的注释不做处理
printf("adfasdf//adsfadsf//afdadsf\n");对其中的//注释也不做处理
5、对于换行符 \ 不做处理;
6、要求对于其中出现的空行要能够删除(这个我只实现了对于开头没有空格空行的删除,还需要改进的地方)
更具以上要求,对状态进行分析,得出以下的装态转换矩阵:
以下是对于这个的C语言代码实现(From是要删除注释的C文件名,To是删除后的文件名):
void removec(char* From,char* To) { //printf("%s %s\n",From,To); int state; bool flag=true; char c; FILE *fp,*tp; if((fp=fopen(From,"r")) == NULL) { printf("The file can not be opened.\n"); return; } if((tp=fopen(To,"w")) == NULL) { printf("The file can not be opened.\n"); return; } //ÒƳý²Ù×÷ state = 0; while ((c = fgetc(fp))!=EOF) { if (state == 0 && c == '/') // ex. [/] state = 1; else if (state == 1 && c == '*') // ex. [/*] state = 2; else if (state == 1 && c == '/') // ex. [//] state = 4; else if (state == 1) { // ex. [<secure/_stdio.h> or 5/3] fputc('/',tp); state = 0; } else if (state == 2 && c == '*') // ex. [/*he*] state = 3; else if (state == 2) // ex. [/*heh] state = 2; else if (state == 3 && c == '/') // ex. [/*heh*/] state = 0; else if (state == 3) // ex. [/*heh*e] state = 2; else if (state == 4 && c == '\\') // ex. [//hehe\] state = 9; else if (state == 9 && c == '\\') // ex. [//hehe\\\\\] state = 9; else if (state == 9) // ex. [//hehe\<enter> or //hehe\a] state = 4; else if (state == 4 && c == '\n') // ex. [//hehe<enter>] state = 0; else if (state == 0 && c == '\n' ) // ex. [//hehe<enter>] state = 0; else if (state == 0 && c == '\'') // ex. ['] state = 5; else if (state == 5 && c == '\\') // ex. ['\] state = 6; else if (state == 6) // ex. ['\n or '\' or '\t etc.] state = 5; else if (state == 5 && c == '\'') // ex. ['\n' or '\'' or '\t' ect.] state = 0; else if (state == 0 && c == '\"') // ex. ["] state = 7; else if (state == 7 && c == '\\') // ex. ["\] state = 8; else if (state == 8) // ex. ["\n or "\" or "\t ect.] state = 7; else if (state == 7 && c == '\"') // ex. ["\n" or "\"" or "\t" ect.] state = 0; if ((state == 0 && c != '/') || state == 5 || state == 6 || state == 7 || state == 8){ if(flag && '\n'==c) continue; else if('\n'==c) flag=true; else flag=false; fputc(c,tp); } } fclose(fp); fclose(tp); printf("The Process is done!\n"); }