Lisping towards enlightenment

Me: a C/C++ programmer of 12 years (professionally), a Lisp dabbler (programmer is too strong a word to use here) of, let’s say ~3 months. I’ve done enough functional stuff to be comfortable with that side, but I haven’t (hadn’t) yet written a project big enough to get me used to Lisp syntax.

The project: an LL(1) parser. That is, given a suitable grammar and set of legal lexemes, produce a function that will (top-down predictive) parse legal strings of said grammar. This isn’t rocket science by compiler standards, and I’m not sure it’s even likely that the grammar I have in mind to parse can even be made LL(1), but I have implemented precisely this in C++ before, so it’s a good test. Straight out of the Dragon Book section 4.4.

So far I have completed Lisp functions to produce a parsing table, using appropriate algorithms to construct First and Follow sets. This is about 110 lines of Lisp. Looking at the old C++ code (about 5 years ago) it comes to about 230 lines. From this I conclude that even a newb like me can gain immediate benefit from Lisp, and that I don’t yet know enough Lisp to make this about half the size it is (e.g. I’ve used no macros).

Time taken to get this far in Lisp: about 2 days. Time taken to get this far in C++: unknown, but the entire thing (grammar design, lexer, parser, bytecode compilation & VM execution) took on the order of a few months. The C++ version is production code that shipped in a real life game. Make of that what you will.

Even after 2 days, I’m already fine with the brackets. They really do not bother. The code is readable through indentation. I do sometimes run into problems with misplaced brackets when editing code, but it’s akin to a syntax error which is immediately found and fixed. I expect this to go away as I get more proficient.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.