Task

Create a stack-based evaluator for an expression in   reverse Polish notation (RPN)   that also shows the changes in the stack as each individual token is processed as a table.



 Assume an input of a correct, space separated, string of tokens of an RPN expression
 Test with the RPN expression generated from the   Parsing/Shunting-yard algorithm   task: 

         3 4 2 * 1 5 - 2 3 ^ ^ / + 

 Print or display the output here


Notes

    ^    means exponentiation in the expression above.
    /    means division.


See also

   Parsing/Shunting-yard algorithm for a method of generating an RPN from an infix expression.
   Several solutions to 24 game/Solve make use of RPN evaluators (although tracing how they work is not a part of that task).
   Parsing/RPN to infix conversion.
   Arithmetic evaluation.


.ig
    RPN parser implementation in TROFF
 
..
.\" \(*A stack implementation
.nr Ac 0
.af Ac 1
.de APUSH
.if (\\n(Ac>=0)&(\\n(Ac<27) \{ \
.	nr Ac +1
.	af Ac A
.	nr A\\n(Ac \\$1
.	af Ac 1
\}
..
.de APOP
.if (\\n(Ac>0)&(\\n(Ac<27) \{ \
.	af Ac A
.	rr A\\n(Ac \\$1
.	af Ac 1
.	nr Ac -1
..
.\" Facility to print entire stack
.de L2
.af Ac 1
.if \\n(Li<=\\n(Ac \{ \
.	af Li A
\\n(A\\n(Li
.	af Li 1
.	nr Li +1
.	L2
\}
..
.de APRINT
.nr Li 1
.L2
.br
..
.\" Integer exponentiation algorithm
.de L1
.if \\n(Li<\\$2 \{ \
.	nr Rs \\n(Rs*\\$1
.	nr Li +1
.	L1 \\$1 \\$2
\}
..
.de EXP
.nr Li 0
.nr Rs 1
.L1 \\$1 \\$2
..
.\" RPN Parser
.de REAP
.af Ac A
.nr O2 \\n(A\\n(Ac
.af Ac 1
.nr Ai \\n(Ac-1
.af Ai A
.nr O1 \\n(A\\n(Ai
.APOP
.APOP
..
.de RPNPUSH
.ie '\\$1'+' \{ \
.	REAP
.	nr Rs \\n(O1+\\n(O2
\}
.el \{ \
.	ie '\\$1'-' \{ \
.		REAP
.		nr Rs \\n(O1-\\n(O2
\}
.	el \{ \
.		ie '\\$1'*' \{ \
.			REAP
.			nr Rs \\n(O1*\\n(O2
\}
.		el \{ \
.			ie '\\$1'/' \{ \
.				REAP
.				nr Rs \\n(O1/\\n(O2
\}
.			el \{ \
.				ie '\\$1'%' \{ \
.					REAP
.					nr Rs \\n(O1%\\n(O2
\}
.				el \{ \
.					ie '\\$1'^' \{ \
.						REAP
.						EXP \\n(O1 \\n(O2
\}
.					el .nr Rs \\$1
\}
\}
\}
\}
\}
.APUSH \\n(Rs
.APRINT
..
.de RPNPRINT
.if \\n(Ac>1 .tm ERROR (rpn.roff): Malformed input expression. Evaluation stack size: \\n(Ac > 1 .
\\n(AA
..
.de RPNPARSE
.RPNPUSH \\$1
.ie \\n(.$>1 \{ \
.	shift
.	RPNPARSE \\$@
\}
.el .RPNPRINT
..
.RPNPARSE 3 4 2 * 1 5 - 2 3 ^ ^ / + \" Our input expression