Skip to content

Instantly share code, notes, and snippets.

@mgronhol
Created May 30, 2015 20:12
Show Gist options
  • Select an option

  • Save mgronhol/a90cadf61a3e39f212ea to your computer and use it in GitHub Desktop.

Select an option

Save mgronhol/a90cadf61a3e39f212ea to your computer and use it in GitHub Desktop.
Simple python lisp parser using shlex
#!/usr/bin/env python
import shlex, pprint
text = """
(define mauri (lambda (x y)
(+ x y)
)
)
(display (mauri 1 2) )
"""
lexer = shlex.shlex( text )
stack = []
bucket = []
for token in lexer:
if token == "(":
stack.append( list( bucket ) )
bucket = []
elif token == ")":
old_bucket = stack.pop()
old_bucket.append( list( bucket ) )
bucket = old_bucket
else:
bucket.append( token )
pprint.pprint( bucket )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment