bsb:

OK ... I've toyed around with the code and did a version with the extra parenthesis removal. Here ya go:

#!/usr/bin/perl -w use strict; use warnings; my @stack = (); sub binop { my $op = shift; my $prec = shift; my $lh = pop @stack; my ($lhop, $lhprec) = ($$lh[0], $$lh[1]); my $rh = pop @stack; my ($rhop, $rhprec) = ($$rh[0], $$rh[1]); # Wrap operand(s) with lower precedence $lhop = '(' . $lhop . ')' if $prec > $lhprec; $rhop = '(' . $rhop . ')' if $prec > $rhprec; push @stack, [ $lhop . $op . $rhop, $prec ]; } while (<DATA>) { chomp; print "TOS='" . (@stack ? $stack[$#stack][0] : '-nil-') . "' " . "stmt = '" . $_ . "'\n"; my ($operator, $operand) = split; if ($operator eq 'push') { # A constant is atomic, so it has high precedence so # we don't wrap it push @stack, [$operand, 999]; } elsif ($operator eq 'print') { my $op = pop @stack; my ($operand, $prec) = ($$op[0], $$op[1]); print "out: '" . $operand . "'\n"; } elsif ($operator eq 'add') { binop '+', 1; } elsif ($operator eq 'mul') { binop '*', 2; } } __DATA__ push 3 push 5 add push 7 push 9 add mul print
Which gives us:
root@swill ~/PerlMonks $ ./stack_to_infix2.pl TOS='-nil-' stmt = 'push 3' TOS='3' stmt = 'push 5' TOS='5' stmt = 'add' TOS='5+3' stmt = 'push 7' TOS='7' stmt = 'push 9' TOS='9' stmt = 'add' TOS='9+7' stmt = 'mul' TOS='(9+7)*(5+3)' stmt = 'print' out: '(9+7)*(5+3)' root@swill ~/PerlMonks $
--roboticus

P.S. I don't like my code for getting arguments off the stack, but all my attempts at simplifying it failed miserably. I'm sure it's something simple, but I haven't figured it out. What's a better way to do the following? (Or is my problem a bit earlier and I'm pushing the wrong thing on the stack?)

my $op = pop @stack; my ($operand, $prec) = ($$op[0], $$op[1]);

In reply to Re: Convert stack-based code to "conventional" (was Re: Graphing SQLite's VDBE) by roboticus
in thread Graphing SQLite's VDBE by bsb

Title:
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, details, 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, summary, 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.