Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4842349
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类:

2009-03-24 18:41:21

1、strip_c_comment1.awk 正则方法


BEGIN { ORS = "" }

{ code = code $0 "\n" }

END {
        while ( length(code) )
        code = process( code )
}

function process( text )
{
        if ( c99 ) {
                if ( match( text, /"|'|\/\*|\/\// ) )
                        return span( text )
        } else {
                if ( match( text, /"
|'|\/\*/ ) )
                        return span( text )
        }
        print text
        return ""
}

function span( text , starter )
{
        print substr( text, 1, RSTART - 1 )
        starter = substr( text, RSTART, RLENGTH )
        text = substr( text, RSTART + RLENGTH )

        if ( "\"" == starter || "'
" == starter )
                return quoted( text, starter )
        if ( "
//" == starter )
                return remove( text, "
\n", "\n" )
        ## Allow for
        ## /* foo *\
        ## /
        return remove( text, "
\\*(\\\\\n)?/", " " )
}

function remove( text, ender, replacement )
{
        print replacement
        return substr( text, match(text, ender) + RLENGTH )
}

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

==============================

----------------------------------------------------------------------------------------------------------------
2、strip_c_comment2.awk 状态机,


#

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

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

        if (state == 0) {
                if (c == "/") {
                        d = substr($0, i+1, 1)
                        if (d == "*")
                                state = 2
                        else if (d == "/" && c99)
                                state=3
                        else
                                printf("%s", c d)

                        i=i+2
                        continue
                }
                else if (c == "\"")
                        state = 1
        } else if (state == 1) {
                if (c == "\"" && substr($0, i-1, 1) != "\\")
                        state = 0
        } else if (state == 2 && i > 1 && substr($0, i-1,2) == "*/") {
                state = 0
                c = " "
        }

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

}

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

if (state < 2)
        print ""

}

==============================

----------------------------------------------------------------------------------------------------------------
3、借用C预处理器,,


#!/bin/sh

sed '
s/#/-_-/g' "$1" |

cpp - | sed '/#/d' |
sed 's/-_-/#/g'

==============================

阅读(1733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~