免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 雨丝风片
打印 上一主题 下一主题

有多少种方法可以去掉c代码中的注释? [复制链接]

论坛徽章:
0
51 [报告]
发表于 2006-10-25 09:06 |只看该作者
原帖由 gvim 于 2006-10-25 00:32 发表
在网上找了个lex的,更简单的,可处理任意 /* ... */ 注释
"/*"([^\*]|(\*)*[^\*/])*(\*)*"*/"

[code]
%{
%}
comment  "/*"([^\*]|(\*)*[^\*/])*(\*)*"*/"
%%
...


这个强。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
52 [报告]
发表于 2006-10-25 09:15 |只看该作者
gvim兄给的 确实非常精简,保留了。还有那个 awk的 。

论坛徽章:
0
53 [报告]
发表于 2006-10-25 09:37 |只看该作者
受益匪浅

[ 本帖最后由 Macolex 于 2006-10-25 09:39 编辑 ]

论坛徽章:
0
54 [报告]
发表于 2006-10-25 09:43 |只看该作者
网友 [Namelessxpt ] 在Perl版写道:







  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. $/ = undef;
  5. while (<>){
  6.         $_ =~ s/\/\*.*?\*\///igs;
  7.         $_ =~ s/(\r?\n)+/\n/igs;
  8.         print;
  9. }
  10. exit;
复制代码



perl xxx.pl input.c > output.c

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
55 [报告]
发表于 2006-10-25 15:00 |只看该作者
waker的不能处理这样的情况:
/*
* xxx
* yyy
*/
而这在vim写的代码里是很常见的。

论坛徽章:
0
56 [报告]
发表于 2006-10-25 15:38 |只看该作者
原帖由 gvim 于 2006-10-25 15:00 发表
waker的不能处理这样的情况:
/*
* xxx
* yyy
*/
而这在vim写的代码里是很常见的。



没问题啊,
  1. [~]$
  2. [~]$ cat foo.c
  3. /* sflkjlsdf;safjas;jf
  4.    sdjfa;lsjfa;sljf;as
  5.    lksjfal;sjfa;lsjf;al */
  6. /* sdkjfal;sjfasljf */
  7. int aaaa; /* slfjasljfkas;ldkjfa; */
  8. /* ssjdflasjdf;lsdjkf */int bbbb;
  9. int cccc; /* jf;lasjdkf;lsjdfas
  10.              sdlkfja;sljfa;lsjdfa;sdjf
  11.              lsdkajfa;sljf;lasdjk */
  12. /* jf;asldjfa;sdflja;sd
  13. * sjf;lsajdf;asldj
  14. * slakjf;lsadjf;ladj */
  15. [~]$
  16. [~]$
  17. [~]$ awk 'BEGIN{RS="/\*|\*/"} NR%2' foo.c




  18. int aaaa;


  19. int bbbb;
  20. int cccc;




  21. [~]$
  22. [~]$
复制代码

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
57 [报告]
发表于 2006-10-25 15:53 |只看该作者
原帖由 雨丝风片 于 2006-10-25 15:38 发表



没问题啊,
[code][~]$
[~]$ cat foo.c
/* sflkjlsdf;safjas;jf
   sdjfa;lsjfa;sljf;as
   lksjfal;sjfa;lsjf;al */
/* sdkjfal;sjfasljf */
int aaaa; /* slfjasljfkas;ldkjfa; */
/* ssjdfla ...


哈,随便找个/usr/src/sys下面的文件试试。
比如sys/kern/init_main.c

论坛徽章:
0
58 [报告]
发表于 2006-10-25 16:11 |只看该作者
原帖由 gvim 于 2006-10-25 15:53 发表


哈,随便找个/usr/src/sys下面的文件试试。
比如sys/kern/init_main.c



那倒不是因为waker的脚本不支持你说的那种注释形式,否则我自己写的测试文件也不可能通过。根本原因在于,在之前的测试中,waker的脚本“碰巧”成功了,因为在之前的测试文件中的“/”字符仅出现在/*和*/的组合中。但一旦在代码的其它地方出现了“/”,那个脚本就行不通了。

这是因为那个脚本想把/*和*/定义为记录分隔符,但记录分隔符的定义只认第一个字符,因此,脚本实际定义的记录分隔符为“/”。

论坛徽章:
0
59 [报告]
发表于 2006-10-25 16:25 |只看该作者
不过waker用的应该是gawk,gawk是可以把RS定义成多个字符的。
这是fc4上的测试结果:
  1. [~]$
  2. [~]$ cat foo.c
  3. /* sldfjsla;fjaslfsd;fjsa;f
  4. sdfj;asldj / ;lsjf;aslfj
  5. ;sdjf;sdljfl j*/

  6. int a=c/d;
  7. int b=a*k;

  8. [~]$
  9. [~]$
  10. [~]$
  11. [~]$ awk 'BEGIN{RS="/\\*|\\*/"} NR%2' foo.c



  12. int a=c/d;
  13. int b=a*k;


  14. [~]$
  15. [~]$ awk --version
  16. GNU Awk 3.1.4
  17. Copyright (C) 1989, 1991-2003 Free Software Foundation.

  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2 of the License, or
  21. (at your option) any later version.

  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. GNU General Public License for more details.

  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  29. [~]$
  30. [~]$
复制代码



这是fb6.1上的测试结果:
  1. [~]$
  2. [~]$ cat foo.c
  3. /* ldfjsla;fjaslfsd;fjsa;f
  4. sdfj;asldj / ;lsjf;aslfj
  5. ;sdjf;sdljfl j*/

  6. int a=c/d;
  7. int b=a*k;
  8. [~]$
  9. [~]$
  10. [~]$ awk 'BEGIN{RS="/\*|\*/"} NR%2' foo.c

  11. ;lsjf;aslfj
  12. ;sdjf;sdljfl j*
  13. d;
  14. int b=a*k;

  15. [~]$
  16. [~]$
  17. [~]$ awk -V
  18. awk version 20050424 (FreeBSD)
  19. [~]$
复制代码

论坛徽章:
0
60 [报告]
发表于 2006-10-25 16:57 |只看该作者
【GAWK: Effective AWK Programming】:
When using gawk, the value of RS is not limited to a one-character string. It can be any regular expression.

The use of RS as a regular expression and the RT variable are gawk extensions; they are
not available in compatibility mode. In compatibility mode, only the first character of the value of RS is used to determine the end of the record.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP