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

    Actually concat is there:

    sub concat ($) {$decommendedText .= $_[0]; 1;}

    :)

    Update: Thanks for the other tips - especially the returned result from file and eof detection.


    DWIM is Perl's answer to Gödel
      It was in main::. In the caller. Why would a module rely on the calling script to provide its internal functions? That's no good. I moved the function into the module where it should be. The problem becomes extremely evident when you Precompile (as you should).