Question
Use Python to evaluate prefix expressions. Your algorithm will be recursive. We must be able to recognize tokens in the expression. Notice that we can't
Use Python to evaluate prefix expressions. Your algorithm will be recursive.
We must be able to recognize tokens in the expression. Notice that we can't simply split the expression on spaces since "(" and "+" are separate symbols, but are not separated by spaces.
-It must tokenize the expression using Python's regex (use the re module).
-Reset the global current parsing pos to 0
Sample Output:
> (* 2 15)
30
> (- (* 12 2)(* 2 3 ))
18
> (* 5 (/ 5 2))
10
> (or (> 6 13) (< 15 2))
False
> (and (> 13 6) (> 17 3))
True
> (+ 1 (* 12 4 )(* 2 2)
Missing closing ')'
> (/ (+ 3 5))
Incorrect number of operands - must be 2 for '/'
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