Re: AWK Code to Strip C comments
- From: "William James" <w_a_x_man@xxxxxxxxx>
- Date: 25 Mar 2006 15:27:54 -0800
pardha.saradhik@xxxxxxxxx wrote:
Hi all,
can any one help me out in writing an awk script that can completely
strip the comments out of a C file.????
i hav tried doing it but unable to strip all comments...
Regards,
Partha
BEGIN { ORS = "" }
{ code = code $0 "\n" }
END {
while ( length(code) )
code = process( code )
}
function process( text )
{
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" )
return remove( text, "*/", " " )
}
function remove( text, ender, replacement )
{
print replacement
return substr( text, index(text, ender) + length( ender ) )
}
function quoted( text, starter )
{
if ( "'" == starter )
match( text, /^(\\.|[^'])*'/ )
else
match( text, /^(\\.|[^"])*"/ )
print starter substr( text, 1, RLENGTH )
return substr( text, RSTART + RLENGTH )
}
.
- Follow-Ups:
- Re: AWK Code to Strip C comments
- From: William James
- Re: AWK Code to Strip C comments
- References:
- AWK Code to Strip C comments
- From: pardha . saradhik
- AWK Code to Strip C comments
- Prev by Date: Re: AWK Code to Strip C comments
- Next by Date: Re: AWK Code to Strip C comments
- Previous by thread: OT: Re: AWK Code to Strip C comments
- Next by thread: Re: AWK Code to Strip C comments
- Index(es):
Relevant Pages
|