基于DFA的C程序注释删除
2015-11-01 19:59
337 查看
有时候一个程序中如果注释太多,也会带来很多的不方便,于是便产生了删除注释的需要,删除注释有很多种方法,不过最近实现的用确定有限自动机(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");
}
经过分析需要删除的注释大概有一下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");
}
相关文章推荐
- Java____Synchronized++之前线程进程学习链接
- 信息安全系统设计基础期中总结
- spring与web整合(交鸡肋,因为有前台框架封装了servlet)
- 哈夫曼树知识点
- Java环境变量配置
- BC#62C题求二叉树不同节点数的子树的个数
- Unity重力的测试
- 永久性保存
- JS学习实例:JavaScript时钟及Date对象学习总结
- 大整数取模
- 会议2_2015阿帕奇路演(中国站)大数据生态链分场内容概览
- org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
- Node.js实践HTTP安全认证之一~~基本认证
- Python_List对象内置方法详解
- Linux下的段错误(Segmentation fault)产生的原因及调试方法(经典)
- SQL性能优化
- 信息安全系统设计基础期中学习总结
- 继承关系
- JavaScript HTML DOM 事件
- HDU4455