January 4th, 2009


Nerd post: meaningless python

Much of my spare time in the last week has gone to writing a parser for python (I like writing pasrers). I was inspired to work on this by this page about Top-Down Operator Precedence parsing, although I didn't wind up using that technique.

This is my most ambitious (and successful) parser yet. So far, it parses the complete grammar for Python expressions. 400 lines of Python code excluding the lexer.

I'm also using a cool technique to auto-generate the unit tests. The generated unit tests contain some cool Python expressions that are actually syntactically valid (albeit meaningless):

[ x for x in lambda * x : x ]
lambda x , x = x , : x
{ x : lambda : x }
x [ lambda : x : ]
( x for x , x in x )
x ( * x * x )
x [ x : x | x : x ]

I guess it goes to show, with sufficient determination, you can pervert any programming language.