How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the XPL0 programming language

Table of Contents

Problem Statement

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.

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the XPL0 programming language

Source code in the xpl0 programming language

real Stack(10);
int  SP;

proc Push(X);
real X;
[Stack(SP):= X;  SP:= SP+1];

func real Pop;
[SP:= SP-1;  return Stack(SP)];

char Str;  real Top;  int Token, I;
[Str:= "3 4 2 * 1 5 - 2 3 ^^ ^^ / + ";
SP:= 0;
Format(6, 8);
loop    [repeat Token:= Str(0);  Str:= Str+1;
        until   Token # ^ ;             \skip space characters
        case Token of
          ^+:   [Top:= Pop;  Push(Pop+Top)];
          ^-:   [Top:= Pop;  Push(Pop-Top)];
          ^*:   [Top:= Pop;  Push(Pop*Top)];
          ^/:   [Top:= Pop;  Push(Pop/Top)];
          ^^:   [Top:= Pop;  Push(Pow(Pop, Top))];
          $A0:  quit                    \space with MSB set
        other [Push(float(Token-^0))];  \single digit number
        ChOut(0, Token);
        for I:= 0 to SP-1 do            \show stack
            RlOut(0, Stack(I));
        CrLf(0);
        ];
]

  

You may also check:How to resolve the algorithm Make directory path step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Voronoi diagram step by step in the Tcl programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Ada programming language
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Keyboard input/Flush the keyboard buffer step by step in the Python programming language