-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSem6_toRpn.py
32 lines (26 loc) · 984 Bytes
/
Sem6_toRpn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import re
import sys
def toRpn(infixStr):
# divide string into tokens, and reverse so I can get them in order with pop()
tokens = re.split(r' *([\+\-\*\^/]) *', infixStr)
tokens = [t for t in reversed(tokens) if t!='']
precs = {'+':0 , '-':0, '/':1, '*':1, '^':2}
#convert infix expression tokens to RPN, processing only
#operators above a given precedence
def toRpn2(tokens, minprec):
rpn = tokens.pop
while len(tokens)>0:
prec = precs[tokens[-1]]
if prec<minprec:
break
op=tokens.pop
# get the argument on the operator's right
# this will go to the end, or stop at an operator
# with precedence <= prec
arg2 = toRpn2(tokens,prec+1)
rpn += " " + arg2 + " " +op
return rpn
return toRpn2(tokens,0)
print (toRpn("5+3*4^2+1"))
print (toRpn("3/4+(2-y)"))
#prints: 5 3 4 2 ^ * + 1 +