Raghupathy wrote:

> Rob,
>  
>    The input file you described is not correct, since
> it has
> #ifdef def2
> statement3;
> #else
> statement4;
> #endif
> nested within another #ifdef ... #else ... #endif.
>  
>  My input file is the output of
> "diff -D def file1 file2" (on unix). This will
> generate a file which has the following patterns and
> none of the patterns can be nested within the other:
> 
> #ifdef .... #else ... #endif
> #ifndef .... #else ... #endif
> #ifdef .... #endif
> #ifndef .... #endif
> 
>    I need to substitute the above patterns to be read
> by a home grown program.
>  
>    I encountered a problem due to the following
> reason.
> There was:
> 
> #ifndef def .... #endif def    -  Call it sentence1
> #ifndef def ... #else def ... #endif def   - Call it
> sentence2
> 
>    I tried the following line but it matched sentence1
> and sentence2 together. I need to match sentence1 and
> sentence2 seperately.
> $line1 =~
> m/#ifndef\s+def(.*?)#else\s+def(.*?)#endif\s+def/isg
> 
> For this
> #ifndef def (...1...) #else def (...2...) #endif def
> 
> should be matched only if #if is not there within
> (...1...) and (...2...).
> 
>   Hopefully I have conveyed it more clearly.

still not quit sure what you need but try the following:

#!/usr/bin/perl -w

use strict;

my $code =<<C;
#ifdef debug
        debug defined!
#endif debug

#ifndef fork
        no fork
        no pfork
#else fork
        great os
#endif fork
C

while($code =~ /
                \#if n? def
                \s+
                ([^\n]+)
                (.*?)

                (?:
                \#else
                \s+
                \1
                (.*?)
                )?

                \#endif
                \s+
                \1
                /gsx){

        my $if   = $2;
        my $else = $3 || '';

        print "$1:",$if;
        print "$1 else: ",$else if($else);
}

__END__

prints:

debug:
        debug defined!
fork:
        no fork
        no pfork
fork else:
        great os

not sure if that's what you want.

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to