Domain Specific Languages

by Martin Fowler, with Rebecca Parsons

2010

A detailed guide on implementing both internal and external DSLs

Domain Specific Languages (DSLs) have been around since I've been in computing, but it's hard to find much information about how to work with them. DSLs are small languages, focused on a particular aspect of a software system. You can't build a whole program with a DSL, but you often use multiple DSLs in a system mainly written in a general purpose language.

DSLs come in two main forms: external and internal. An external DSL is a language that's parsed independently of the host general purpose language: good examples include regular expressions and CSS. External DSLs have a strong tradition in the Unix community. Internal DSLs are a particular form of API in a host general purpose language, often referred to as a fluent interface. The way mocking libraries, such as JMock, define expectations for tests are good examples of this, as are many of the mechanisms used by Ruby on Rails. Internal DSLs also have a long tradition of usage, particularly in the Lisp community.

People find DSLs valuable because a well-designed DSL can be much easier to program with than a traditional library. This improves programmer productivity, which is always valuable. In particular it may also improve communication with domain experts, which is an important tool for tackling one of the hardest problems in software development. CSS is an excellent example of this, most people who program CSS don't consider themselves to be programming. Despite this, however, I don't generally think that end-users will usually write in DSLs directly - it's the communication enhancement that's important.

Although DSLs have been around for a long time, the lack of knowledge of how to program with them is a significant barrier - which is exactly why I've worked on this book. The books provides techniques to develop both internal and external DSLs, giving you both the information to choose between them and a good package of information to begin your work. I also stress the importance of layering a DSL over a library, so that you usually build both together. There's also material on code-generation, which is an occasionally essential part of working with a DSL.

Like P of EAA this book is a Duplex Book, providing both a narrative to learn about DSLs and a reference book for the details, so don't be scared by the high page count. You can get a good grasp of the topic by reading the narrative section (142 pages) and use the rest as a reference to dip into when you need it.

Electronic copies of this book are available. Amazon has a version for kindle. InformIT has epub and pdf versions. Apple has an epub version in the ibookstore.

Errata

Further Reading

DSL Guide

My guide page on this web site is where I pull together information that I have on DSLs.

Free First Chapter

The freely available first chapter of the book provides a good overview of DSLs and my approach with them.

DSL Patterns Catalog

Reference URIs for the patterns in the book. Each one lists the pattern intent and chapter reference.

Translations

Japanese, Korean, Portuguese