Regex: Parsing Lisp with Python

Paul Rubin phr-n2002b at
Thu Aug 8 19:34:42 CEST 2002

Thomas Guettler <zopestoller at> writes:
> I tried it like this, but this gives me all tokens
> serialized. It is hard to get the second symbol without
> counting all open and close tokens. Is there a way to get
> the tokens in nested lists?

No there's no way to do that with traditional regexps.
You have to parse the s-expressions.  Normally you do that with
recursion: on seeing an open-paren, parse additional s-expressions 
til you see a close-paren, and make a list of them.

You might look at source code of some lisp interpreters to see how
this works.  SIOD (Scheme In One Day) is a nice simple one written in
C, that you can probably find on Google.

More information about the Python-list mailing list