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'
==============================
|