Skip to content

Instantly share code, notes, and snippets.

@sunary
Created October 30, 2018 00:41
Show Gist options
  • Save sunary/2fb3657c2745dc247ead5f9aa8fb01fd to your computer and use it in GitHub Desktop.
Save sunary/2fb3657c2745dc247ead5f9aa8fb01fd to your computer and use it in GitHub Desktop.
lisp parser
def read_from_tokens(tokens):
token = tokens.pop(0)
if token == '(':
L = []
while tokens[0] != ')':
L.append(read_from_tokens(tokens))
tokens.pop(0)
return L
elif token == ')':
raise SyntaxError('unexpected )')
else:
return token
tk = str.split('( + 1 ( * ( - 10 9 ) 2 ) )', ' ')
print read_from_tokens(tk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment