![]() |
|
Your skill will accomplish what the force of many cannot |
|
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Any pointers to code snippets would be great, before I roll my own. The simplest way I know to convert Infix to RPN is to use stacks. You will need two stacks, one for operators, and the other a container for your RPN formated expression. Then the algorithm basically goes something like this:
I got this algorithm from a old book on FORTH, and they had this table in it, which I found helpful in visually explaining the algorithm. source string | operator stack | program stack ----------------+----------------+---------------- A+(B*C-D)/E | | +(B*C-D)/E | | A (B*C-D)/E | + | A B*C-D)/E | +( | A *C-D)/E | +( | AB C-D)/E | +(* | AB -D)/E | +(* | ABC D)/E | +(- | ABC* )/E | +(- | ABC*D /E | + | ABC*D- E | +/ | ABC*D- | +/ | ABC*D-E | | ABC*D-E/+ Of course, this code and algorithm somewhat relies on properly nested parens, and will not handle the 'max()' part of your expression, but it's something to start with. The more complex way to do it (and both more flexible and robust) is to build a parse tree out of your expression, then you can get infix, postfix, prefix, whatever-fix order you want just by traversing the tree in different ways. I actually show examples of this conversion-through-traversal in the docs for my module Tree::Binary (more specifically the examples are in Tree::Binary::Visitor::InOrderTraversal, Tree::Binary::Visitor::PreOrderTraversal and Tree::Binary::Visitor::PostOrderTraversal in their SYNOPSIS sections). The algorithm for building a parse tree is not entirely unlike the stack version above, except its more complex and you are not building a program stack, but a parse tree. The operator stack is still a very useful tool in helping you decide to go up or down the tree. Unfortunately I don't have any example code for this, and besides, it is very dependent upon how you structure your tree anyway. I am sure though that a quick google search on building parse trees will provide you with some insight.
-stvn
In reply to Re: Generic RPN Translator available?
by stvn
|
|