Today I turned my 750+ lines of ugly PHP into 200 lines of much nicer PHP, and the shorter version works much better than the longer version. So I'm feeling happier with myself than I was yesterday. I did finally manage to make my parser recursive and remove all the special-case handling code. I had an interesting experience where I was diligently hacking away at my code, and then I noticed everything was actually working and had been working for quite some time without my noticing.
Still if I was a better programmer it wouldn't have taken me a month to figure this out.
My only worry now is that if anyone but me ever has to use or maintain these scripts, they're going to have to understand recursion and understand what I was going for here. That's a scary thought. One guy who interviewed me for a job a few years ago said that he asks a lot of people to explain recursion to him during job interviews, largely because a lot of programmers don't even know what the word means.