The LIFO Stack - A Gentle Guide |
Written by Harry Fairhead | ||||||||
Thursday, 21 March 2019 | ||||||||
Page 4 of 4
Reverse Polish -RPNThis stack operator algorithm is well known and there are lots of variations on it. It also gives rise to the once very well-known Reverse Polish (RP) notation. If you write operator expressions so that each operator acts on the two items to its left then the expression can be evaluated using an even simpler stack algorithm. For example, in RP the expression 2+3*4 would be written 2, 3,4*,+ and it can be evaluated simply by scanning from left to right and pushing values on the stack. Each time you hit an operator you pop the top two items from the stack, apply the operator and push the result back on the stack. When you get to the end of the RP expression the result is on the top of the stack. Try it and you will discover it works. In fact you can use a stack to convert standard operator, known as infix, expressions to RP. Back in the days when hardware was more expensive it was thought to be a good idea to get people to work directly in RP. You could and still can buy calculators that work in RP notation. To add 2 and 3 you enter 2, then 3 and press the plus button. Burroughs even built a mainframe computer that didn’t have any other type of working memory than a stack. Everything it did was effectively in RP notation applied to the central stack. The stack machine architecture was simple but not fast enough to compete with more general architectures. However the stack approach to computing hasn't completely died a death. For example, the difference between the usual Java Virtual Machine - JVM and the one used in Android - Dalvik is the use of a stack. The standard JVM is register based machine but this is too expensive and power hungry for a mobile device hence the Dalvik VM is a stack oriented VM. ForthPerhaps the longest standing overuse of the stack approach was, and is, the language It turns out that with a little effort you can build an entire programming language that can be written down as operators in RP notation. Of course making it work is just a matter of using a stack to push operators and operands onto.This makes it easy to implement but arguably difficult to use. However the fun that people had thinking in RP is such that they became really good at it! The fascination for this sort of language is so great that you still find it in use in niche environments particularly where hardware is limited and it has its enthusiast following. The LIFO stack is a wonderful invention but it isn’t the only thing computer science has to offer us as a practical tool! If all you know is the LIFO stack then everything looks like a pop or push. Related ArticlesIntroduction to data structures Stack architecture demystified Javascript data structures - Stacks
What Programmers Know
Contents
* Recently revised
Comments
or email your comment to: comments@i-programmer.info To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, Facebook or Linkedin.
<ASIN:B000TDRHG8> <ASIN:B00004TFL6> |
||||||||
Last Updated ( Thursday, 21 March 2019 ) |