ColonelPanic has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; { my $exp; chomp ($exp = <STDIN>); init_regexes(); my $foo = treeify($exp); use Data::Dumper; print Dumper($foo); } my $TERM = ""; my $FUNCTION_CALL = ""; my $ATOM = ""; my $PAREN_EXP = ""; my $CONDITIONAL = ""; my $PAREN_COND = ""; my $OPERATOR = ""; my %OPS = ( '&' => 1, '|' => 2, '&&' => 3, '||' => 4, ); #regexes to parse C conditionals (more or less). sub init_regexes { $TERM = qr/ (?: (?: (??{$PAREN_COND}) ) | (?: (??{$FUNCTION_CALL}) ) | (?: (??{$ATOM}) ) ) /x; $PAREN_EXP = qr/ \( \s* (?: (?> [^()&|]+ ) | (??{$PAREN_EXP}) ) \s* \) /x; $ATOM = qr/ (?: (?: (?: [!*&] | \b ) \w+\b ) | (??{$PAREN_EXP}) ) /x; $FUNCTION_CALL = qr/ \b\w+\s? \( \s*(??{$TERM})\s* (?: ,\s*(??{$TERM})\s* )* \) /x; $OPERATOR = qr/ (?: && | \|\| | & | \| | \^ ) /x; $CONDITIONAL = qr/ ((??{$TERM})) (?: \s* ( (??{$OPERATOR}) ) \s* (?: ((??{$TERM})) ) )+ /x; $PAREN_COND = qr/ !? \( (?: (??{$CONDITIONAL}) ) \) /x; } #make a tree out of a C expression. sub treeify { my $exp = shift; my $tree; #find the first term in the string; put in $1 and delete. while ($exp =~ s/^($TERM)//) { my $term = $1; my $type = 'atom'; if ($term =~ /\s*($CONDITIONAL)\s*/) { $term = $1; $type = 'cond'; } my $new_term = { 'type' => $type, #if the term itself should be a node, process it. 'value' => $term, 'ref' => undef }; if ($type eq 'cond') { $new_term->{'ref'} = treeify($term); } push @{ $tree->{'terms'} }, $new_term; #now find, put in $1, and delete the next operator. if ($exp ne '' and $exp =~ s/^\s*($OPERATOR)\s*//) { $tree->{'op'} ||= $1; } else { return $tree; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Recursion Woes
by itub (Priest) on May 11, 2004 at 22:02 UTC | |
|
Re: Recursion Woes
by hv (Prior) on May 11, 2004 at 21:52 UTC | |
by ColonelPanic (Friar) on May 12, 2004 at 13:20 UTC | |
|
Re: Recursion Woes
by Plankton (Vicar) on May 11, 2004 at 21:40 UTC | |
|
Re: Recursion Woes
by dave_the_m (Monsignor) on May 11, 2004 at 21:48 UTC | |
by ColonelPanic (Friar) on May 12, 2004 at 12:16 UTC | |
by dave_the_m (Monsignor) on May 13, 2004 at 21:40 UTC |