(Mostly) Imperative Packrat Parsing

I’ve been tinkering with monadic parser combinators combined with continuations (see Monads, Mother of All), but my latest toy simplifies all that by using PEG grammars. PEGs and CFGs are nearly interchangable for most parsing of computer languages, and can be parsed in linear time and space by packrat parsing — parsing with backtracking, but […]

Diversion

A little Python snippet, good for a diversion. I couldn’t fit it into a tweet though: s=[];op=dict((o,eval(‘lambda a,b:b%sa’%o)) for o in ‘+-*/%’) while not any(s.append(op[w](s.pop(),s.pop())if w in op else float(w)) for w in raw_input().split()): print ‘=’,s[-1] Just a simple 4-function RPN calculator, accepting input from console: 1 = 1.0 2 2 + = 4.0 1 […]

Eval in Python

I’ll just leave this here for you: Wait what. Python compiles? That is correct. CPython and PyPy (the implementations worth caring about currently) are in fact creating a code object from the string you pass to exec or eval before executing it. And that’s just one of the things many people don’t know about the exec statement. […]