Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl regex to remove c-comments, taking into account string literals

Reply
Thread Tools

Perl regex to remove c-comments, taking into account string literals

 
 
Saeed
Guest
Posts: n/a
 
      07-08-2004

I have seen searching for a code example that removes c-style comments,
but none of these take into account strings literals, e.g.

----------------------------------------------------
/*
** a comment
*/

printf /* blah */ ("Comments begin with /*\n" );

printf ( "Comments end with */\n" ); /* blah */
----------------------------------------------------

I want this stripped to:

----------------------------------------------------
printf ("Comments begin with /*\n" );

printf ( "Comments end with */\n" );
----------------------------------------------------

but the sample's I've seen would most probably give:

----------------------------------------------------
printf ("Comments begin with

\n" );
----------------------------------------------------

 
Reply With Quote
 
 
 
 
Lukas Mai
Guest
Posts: n/a
 
      07-10-2004
Saeed schrob:

> I have seen searching for a code example that removes c-style comments,
> but none of these take into account strings literals, e.g.

[...]

That's a FAQ; see perldoc -q comments. But that solution is incomplete,
too:

/??/
* foo *\
/
is a single comment, according to the C standard. "??/" is a trigraph
expanding to "\", and backslash-newline pairs are deleted before
tokenizing the program, so the above is equivalent to

/* foo */

The following script should do the job:

#!/usr/local/bin/perl -wp0777
use strict;

# this script reads files, removes C comments,
# and prints the results to stdout

s{
/
(?: (?: \\ | \?\?/) \n)*
(?:
/ (?: (?: \\ | \?\?/) \n | [^\n] )*
|
\* [^*]* \*+ (?: (?: \\ | \?\?/) \n)*
(?: [^/*][^*]* \*+ (?: (?: \\ | \?\?/) \n)* )*
(/)
)
|
(
" (?: (?: \\ | \?\?/) . | [^"])* "
|
' (?: (?: \\ | \?\?/) . | [^'])* '
|
. [^'"/]*
)
}{
(defined $1 ? ' ' : '') . (defined $2 ? $2 : '')
}gsex
__END__

HTH, Lukas
--
print+74.117.115.116,,qq.\c!..not::.her,Perl=>q$ha cker,$,!($,=$")
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can the pre-processor convert string literals into chars? steve.lorimer@gmail.com C++ 10 04-02-2007 03:06 PM
can the pre-processor convert string literals into chars? steve.lorimer@gmail.com C++ 2 03-30-2007 07:47 AM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
integer addition taking CARRY into account archilleswaterland@hotmail.com C Programming 16 04-07-2005 06:02 AM
Difference in module_eval taking block vs. taking string (1.8 bug?) Jim Cain Ruby 1 07-18-2003 02:01 AM



Advertisments