Question
Please write a Reverse Polish Notation Analyzer code in Rust. Certain RPN programs are ill-formed, like the following which attempts to perform an addition operation
Please write a Reverse Polish Notation Analyzer code in Rust. Certain RPN programs are ill-formed, like the following which attempts to perform an addition operation with too few integers on the stack: You'll implement a program that analyzes an RPN program to determine (1) whether it is well formed (won't produce errors when run) and (2) if it is well formed, how large a stack the program will require to run.
In general, RPN programs may require stacks that scale with the size of the program being executed. For example:
1 2 3 4 5 6 7 8 ... N + ... + + + + + + + done
pushes a bunch of integers, from 1 to N for some large N, then repeatedly adds the results until only the sum of all the values 1 to N is left on the stack.
- -1 if the RPL program on stdin was invalid according to the grammar; or
- N if the the RPL program on stdin was valid, where N is the maximum number of stack slots required total, in both the main and auxiliary stacks, to execute the program.
The following are input and output to run
Examples
Program | Result | Reason |
---|---|---|
1 2 3 done | 3 | |
1 2 3 | -1 | Missing done |
1 2 + 3 + 4 + 5 + done | 2 | |
1 2 3 4 5 + + + + done | 5 | |
1 2 + save 3 restore + done | 3 | Main stack 2 + auxiliary stack 1 |
1 2 + done 3 4 5 done | 2 | All code after the first done is ignored |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started