Pre-order LISP Expression Parsing
Expression parsing in Python-
Suppose you are given an expression (in language like LISP) where operator precedes the operands or a pre-ordering of operations. (* 6 7) means 67. OR something like: ( 5 (+ 2 3)). You are asked to write a program that outputs the result of the expression, 25 for the second case. LISP programming language uses this kind of prefix notation for expressiong expressions. For the problem you can assume that the input statement is valid.
There are many ways to do it.
(Path-1) You can split the string by ( and then split by ). One by one. Finally you have something like [”* 5”,”+ 8 7”,””]. Now what you have to do is, concatenate first item with last to maintain the precedence. Then we can reverse the list, evaluate the first item(pop+evaluate) and concatttenate the result with the next index i+1. Finally we will have our results
Path-2 There is even a better way to solve this problem. Remember in compiler design or data structures we simply push the operators and operands in the stack. When we encounter a matching closing )}] we pop everything until the last ({[. Check the following code. Might have to add spaces in pre-processing.
Solution—
Output—
The practice output code is generated online using tutorialspoint free online IDE for python. https://www.tutorialspoint.com/online_python_ide.php