bsb:

I really like your conversion from the stack-based code to the graphical format. One suggestion though: You don't show the stack-based code for the graph shown, so it's not as easy as one would like to see how they tie together.

As far as code pointers go, I'll leave that to monks better-versed in Perl than I.

Anyway, on to your other question: Assuming that by "conventional code" you mean a more algebraic form, then here's my take on it:

The most trivial form is to build it by parenthesizing every expression as you encounter it. Suppose, for example, you have the following bit of code:

[1] push 3 [2] push 5 [3] add [4] push 7 [5] push 9 [6] add [7] mul [8] print
We'd build an algebraic expression with something like this:
#!/usr/bin/perl -w use strict; use warnings; my @stack = (); while (<DATA>) { chomp; print "TOS='" . (@stack ? $stack[$#stack] : '-nil-') . "', " . "stmt = '" . $_ . "'\n"; my ($operator, $operand) = split; if ($operator eq 'push') { push @stack, $operand; } elsif ($operator eq 'print') { print "out: '" . (pop @stack) . "'\n"; } elsif ($operator eq 'add') { my $lhs = pop @stack; my $rhs = pop @stack; push @stack, '(' . $lhs . ' + ' . $rhs . ')'; } elsif ($operator eq 'mul') { my $lhs = pop @stack; my $rhs = pop @stack; push @stack, '(' . $lhs . '*' . $rhs . ') '; } } __DATA__ push 3 push 5 add push 7 push 9 add mul print
Which gives us the output:

root@swill ~/PerlMonks $ ./stack_to_infix.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 $
Now this trivial technique will do it correctly, but will give you too many parenthesis. With a bit more work, you can get rid of the extra parenthesis to get a minimal expression. Here's how to do it: Instead of storing a single string on the stack, keep a pair: The string and the precedence of the operator. Now, when you're working on an operator, check the precedence of the items you pop from the stack with the precedence of the operator you're processing now. If any operator has a lower precedence, wrap it in parenthesis.

--roboticus


In reply to 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.