I have an S-expression in Python which I need to convert it into tuple with operators (add,multiply) inside the semi-colon ie as a string.
I have the following code snippet:
This code works fine, but the requirement of the work is that the user doesnot input tuple like ('add', ('multiply', 3, 4), 5)
instead pass an s-expression like "(add (multiply 2 3) 2)"
from the system command line.
The command to put an input will be like this:
python calc.py "(add (multiply 2 3) 2)"
For now I have already defined expression in the code, but the users will be giving the expression and we fetch it using sys.argv
. So how to convert the user input "(add (multiply 2 3) 2)"
to ('add', ('multiply', 3, 4), 5)
so that I can use the above code easily.
Update:
I tried the following code but it didnt give the desired result.
def from_expression_string(expression_string):
tokens = expression_string.strip().split()
#['(add', '(multiply', '2', '3)', '2)']
stack = []
for token in tokens:
if token == '(':
# print("hello")
pass
elif token == ')':
args = [stack.pop(), stack.pop()]
stack.append((stack.pop(), *reversed(args)))
else:
try:
stack.append(int(token))
except ValueError:
stack.append(token)
return stack[0]
The output of this snippet gives (add
. The above code seems easy to understand but is not working.