Last active
December 11, 2015 06:08
-
-
Save juanplopes/4556536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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