Skip to content

Instantly share code, notes, and snippets.

@juanplopes juanplopes/gist:4556536
Last active Dec 11, 2015

Embed
What would you like to do?
def parse(tokens):
def add(tokens):
e = mult(tokens)
while(tokens[0][0] in "+-"):
kind, image = tokens.pop(0) #+-
e = e + mult(tokens) + [kind]
return e
def mult(tokens):
e = primary(tokens)
while(tokens[0][0] in "*/"):
kind, image = tokens.pop(0) #*/
e = e + primary(tokens) + [kind]
return e
def primary(tokens):
kind, image = tokens[0]
if kind == 'N':
tokens.pop(0) #N
return [int(image)]
elif kind == '(':
tokens.pop(0) #(
e = add(tokens)
tokens.pop(0) #)
return e
return add(tokens + [("EOF", "")])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.