school/cs235/lab04/shunting/__init__.py
2016-04-06 20:46:10 -07:00

24 lines
403 B
Python

from collections import deque
op_prec = {
'*': 3,
'/': 3,
'%': 3,
'+': 2,
'-': 2,
}
def shunt(tokens):
tokens = deque(tokens)
output = deque()
operators = deque()
while tokens:
token = tokens.pop()
if token.isdigit():
output.append(token)
elif token in op_prec:
operators.append(token)
return output, operators