January 5, 2009 oslo

What is all the fuss about how you can write DSLs in Lisp?

I found an interesting post on domain-specific languages and Lisp from June of 2007. It has this to say about designing a DSL:

There are three approaches to designing programming language syntax. The first is to develop a good understanding of programming language grammars and parsers and then carefully construct a grammar that can be parsed by an LALR(1), or LL(k) parser. The second approach is to `wing it’ and make up some ad-hoc syntax involving curly braces, semicolons, and other random punctuation. The third approach is to `punt’ and use the parser at hand.”

I like these categories. I find most folks do #2 with no tooling support or #3 using XML. MGrammar in Oslo is about making #1 easier than #2 and providing the tooling support, e.g. language-specific Intellisense services.

The real topic of the thread, however, is how to do a DSL in Lisp. The questioner would like to implement the following syntax:

trade 100 shares(x) when (time < 20:00) and timingisright()

I find this syntax to be reasonable for an event and it wouldn’t be hard to imagine a system with a bunch of rules expressed this way and I could imagine a developer and a business person working together on such a system to ensure things were expressed properly. I don’t think I could imagine the business person keeping up as well with syntax expressed as the answerer suggests, however:

(when (and (< time 20:00)
                   (timing-is-right))
      (trade (make-shares 100 x)))

IMO, that’s not a DSL — that’s just a set of function calls in an existing language.