in reply to C comment stripping preprocessor
I have a few little improvements.
The following addresses these issues.
use strict; use warnings; use Parse::RecDescent; my $decomment = <<'GRAMMAR'; { use strict; use warnings; sub concat { $decommendedText .= $_[0]; } } file : <rulevar: local $decommendedText = ''> | block(s) /\Z/ {$return = $decommendedText; 1;} block : string {concat ($item{string}); 1;} | m{((?!/\*|"|').)+}s {concat ($item[-1]); 1;} | comment {concat ($item{comment}); 1;} string : /"([^"]|\\")*"/ {$return = $item[-1] . ($text =~ /^\n/ ? "\n" : ''); 1;} | /'([^']|\\')*'/ {$return = $item[-1] . ($text =~ /^\n/ ? "\n" : ''); 1;} comment : '/*' commentBlock '*/' {$return = $text =~ /^\n/ ? "\n" : ''; 1;} commentBlock : m{((?! \*/ | /\* ).)*}sx comment m{((?! \*/ | /\* ). +)*}sx {$return = "\n"; 1;} | m{((?! \*/ | /\* ).)+}sx {$return = ''; 1;} GRAMMAR ... my $decommendedText = $parse->file($input); die "Parse failed\n" if not defined $decommendedText; print $decommendedText;
Update: Now fixes $decommendedText being a global.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: C comment stripping preprocessor
by GrandFather (Saint) on Aug 09, 2006 at 18:37 UTC | |
by ikegami (Patriarch) on Aug 09, 2006 at 18:43 UTC |