免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: yjh777

去除C注释的三类方法 [复制链接]

论坛徽章:
0
发表于 2009-03-26 09:22 |显示全部楼层
看一下

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2009-03-26 09:25 |显示全部楼层
看看楼主贴的和我贴的一样不一样?
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 状态机,


#http://objectmix.com/awk/26721-awk-code-strip-c-comments-2.html

{
#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'

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

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2009-03-26 09:29 |显示全部楼层
大家想知道一样不一样?只好自己回复了,呵呵

论坛徽章:
0
发表于 2009-03-26 09:54 |显示全部楼层
lex

论坛徽章:
0
发表于 2009-03-26 12:49 |显示全部楼层
好东西啊,非常需要

论坛徽章:
0
发表于 2009-03-26 13:01 |显示全部楼层

回复 #1 yjh777 的帖子

要學習只有回了。

论坛徽章:
0
发表于 2009-03-26 13:15 |显示全部楼层
居然给隐咯。

论坛徽章:
0
发表于 2009-03-26 13:18 |显示全部楼层
学习一下

论坛徽章:
0
发表于 2009-03-26 13:22 |显示全部楼层
see see

论坛徽章:
0
发表于 2009-03-26 13:31 |显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP