Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: Using overload to parse arithmetic terms

by blokhead (Monsignor)
on Feb 07, 2006 at 14:41 UTC ( #528524=note: print w/replies, xml ) Need Help??

in reply to Re: Using overload to parse arithmetic terms
in thread Using overload to parse arithmetic terms

To expand (and generalize) on this a bit, let each type of operation have a precedence. Then as you walk the expression tree, you need to parenthesize operations that have a lower precedence than the expression they appear within.

A good way to do this is via OO, representing your operation types as subclasses:

package Op; sub new { my $pkg = shift; bless [@_], $pkg; } sub children { @{ $_[0] } } sub display { my ($self, $precedence) = @_; $precedence = 0 if not defined $precedence; my $s = join $self->op, map { $_->display($self->precedence) } $self->children; $s = "($s)" if $precedence > $self->precedence; return $s; } package Op::Addition; @ISA = qw[Op]; sub precedence { 10 } sub op { " + " } package Op::Multiplication; @ISA = qw[Op]; sub precedence { 20 } sub op { " * " } # ... package Op::Term; @ISA = qw[Op]; sub precedence { 100 } sub display { $_[0][0] }
Then you just call the display method on an Op tree. For example,
Op::Multiplication->new( Op::Term->new(5), Op::Addition->new( Op::Term->new(6), Op::Term->new(7) ) )->display; # 5 * (6 + 7) Op::Addition->new( Op::Term->new(5), Op::Multiplication->new( Op::Term->new(6), Op::Term->new(7) ) )->display; # 5 + 6 * 7
And you can see how easy it would be to add new types of operations, due to the beauty of subclassing.


Replies are listed 'Best First'.
Re^3: Using overload to parse arithmetic terms
by japhy (Canon) on Feb 07, 2006 at 15:38 UTC
    That is a beautiful approach and technique. ++ well deserved.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re^3: Using overload to parse arithmetic terms
by esskar (Deacon) on Feb 07, 2006 at 16:07 UTC
    perfect ... thanks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://528524]
[Corion]: perldigious: That seems to be more the export and likely it's the recipients of that export that like the titles changes
[Corion]: ... "changed"
[Corion]: I usually expect fixed header names, but am sometimes lenient in the order of columns. But changing the report titles often sounds to me as if you are not the sole consument of the export ;)
[shmem]: perldigious: as always - if it ain't broke, don't fix it. Ther must be a very compelling reason for changing column names in a database. Those are rare.
[Corion]: If you have whitespace in the column names in the database, whap the DBAs ;)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2017-05-25 13:35 GMT
Find Nodes?
    Voting Booth?