Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1281283
  • 博文数量: 168
  • 博客积分: 3483
  • 博客等级: 中校
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 13:17
文章分类

全部博文(168)

文章存档

2015年(6)

2014年(9)

2013年(47)

2012年(11)

2011年(13)

2010年(18)

2009年(11)

2008年(42)

2007年(11)

分类: IT业界

2013-09-27 16:06:51


点击(此处)折叠或打开

  1. #1、strip_c_comment1.awk 正则方法
  2. BEGIN { ORS = "" }

  3. { code = code $0 "\n" }

  4. END {
  5.         while ( length(code) )
  6.         code = process( code )
  7. }

  8. function process( text )
  9. {
  10.         if ( c99 ) {
  11.                 if ( match( text, /"|'|\/\*|\/\// ) )
  12.                         return span( text )
  13.         } else {
  14.                 if ( match( text, /"|'|\/\*/ ) )
  15.                         return span( text )
  16.         }
  17.         print text
  18.         return ""
  19. }

  20. function span( text , starter )
  21. {
  22.         print substr( text, 1, RSTART - 1 )
  23.         starter = substr( text, RSTART, RLENGTH )
  24.         text = substr( text, RSTART + RLENGTH )

  25.         if ( "\"" == starter || "'" == starter )
  26.                 return quoted( text, starter )
  27.         if ( "//" == starter )
  28.                 return remove( text, "\n", "\n" )
  29.         ## Allow for
  30.         ## /* foo *\
  31.         ## /
  32.         return remove( text, "\\*(\\\\\n)?/", " " )
  33. }

  34. function remove( text, ender, replacement )
  35. {
  36.         print replacement
  37.         return substr( text, match(text, ender) + RLENGTH )
  38. }

  39. function quoted( text, starter )
    {
            if ( "'" == starter )
                    match( text, /^(\\.|[^'])*'/ )
            else
                    match( text, /^(\\.|\?\?\/.|[^"])*"/ )
            print starter substr( text, 1, RLENGTH )
            return substr( text, RSTART + RLENGTH )
    }



点击(此处)折叠或打开

  1. #2、strip_c_comment2.awk 状态机
  2. #http://objectmix.com/awk/26721-awk-code-strip-c-comments-2.html

  3. {
  4. #state
  5. #0=normal, 1=in string , 2=in C comm, 3= in C++ comm

  6. for(i=1; i <= length($0); i++)
  7. {
  8.         c = substr($0, i, 1)

  9.         if (state == 0) {
  10.                 if (c == "/") {
  11.                         d = substr($0, i+1, 1)
  12.                         if (d == "*")
  13.                                 state = 2
  14.                         else if (d == "/" && c99)
  15.                                 state=3
  16.                         else
  17.                                 printf("%s", c d)

  18.                         i=i+2
  19.                         continue
  20.                 }
  21.                 else if (c == "\"")
  22.                         state = 1
  23.         } else if (state == 1) {
  24.                 if (c == "\"" && substr($0, i-1, 1) != "\\")
  25.                         state = 0
  26.         } else if (state == 2 && i > 1 && substr($0, i-1,2) == "*/") {
  27.                 state = 0
  28.                 c = " "
  29.         }

  30.         if (state < 2)
  31.                 printf("%s", c)

  32. }

  33. if (state == 3 && !/\\$/)
  34.         state = 0

  35. if (state < 2)
  36.         print ""

  37. }

点击(此处)折叠或打开

  1. #3、借用C预处理器,,
  2. #!/bin/sh

  3. sed 's/#/-_-!/g' "$1" |
  4.     cpp - | sed '/#/d' |
  5.     sed 's/-_-!/#/g'



点击(此处)折叠或打开

  1. // 测试C文件
  2. #include "stdio.h"

  3. #define GOOGLE(txt) printf("Google web page = " #txt "n")

  4. a + b; printf("/*");// this a comment

  5. /* this a comment
  6. */

  7. kkkkkkkkkk/* this a comment
  8. */ ccccccccccccc

  9. int main(void) {
  10. GOOGLE(http://www.google.com);

  11. }

  12. #include "stdio.h"

  13. int main(void) {
  14. printf("comments start with "/*" and end with "*/"n");
  15. }

  16. #include "stdio.h"

  17. int main(void) {
  18. printf("comments start with ??/"/*??/" and end with ??/"*/??/"n");
  19. }

  20. #include "stdio.h"

  21. int main(void) {

  22. return 0;
  23. }


阅读(1434) | 评论(0) | 转发(0) |
0

上一篇:xmlrpc-client.sh

下一篇:${BASH_SOURCE}

给主人留下些什么吧!~~