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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re^2: Using overload to parse arithmetic terms by blokhead
in thread Using overload to parse arithmetic terms by esskar

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2018-01-19 16:30 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (221 votes). Check out past polls.