Tao Wang wrote:

> Hi Everyone,
>   I'm trying to parse lines of macros definitions as
> following:
> 
> AV   $(G)/er $(M)/q $(T)/w/f
> G    ter/eee
> M    $(W)
> T    g/ee/fet
> W    $(AV)
> 

here is another way of doing it:

#!/usr/bin/perl -w
use strict;

my $code =<<'CODE';
AV   $(G)/er $(M)/q $(T)/w/f
G    ter/eee
M    $(W)
T    g/ee/fet
W    $(AV)
P    1/2
K    $(P)
C    $(M)
CODE

my %macro;
my %buf;
my $s;

for(split(/\n/,$code)){
        my($m,$c) = split(/\s+/,$_,2);
        push(@{$macro{$m}},$1) while($c =~ /\$\((\w+)\)/g);
}

print "circular:\n";

while(my($m,$c) = each %macro){
        $s = $m;
        %buf = ();
        cir(\%macro,$m);
}

sub cir{
        my $macro = shift;
        my $target = shift;
        for(@{$macro->{$target}}){
                if(exists $buf{$_} && $buf{$_} > 1){
                        print "\t$s\n";
                        return;
                }else{
                        $buf{$_}++;
                        cir($macro,$_);
                }
        }
}

__END__

prints:

circular:
        W
        M
        C
        AV

it reports C as circular becasue M is circular.

david

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

Reply via email to