BT

Developing a Complex External DSL

Posted by Vaughn Vernon on Apr 13, 2009 |

The use of a domain-specific language, or DSL, is becoming a realistic and even necessary solution for software developers on all sorts of software development projects. You've heard about DSLs, and you may know that DSLs are divided into a few different styles, internal and external. But what is an internal DSL and external DSL? When would you decide to use one or the other? And, primarily, how would you go about developing a complex external DSL? This article answers these questions, with a focus on developing a complex external DSL.

Defining Domain-Specific Language

A domain-specific language (DSL) is a computer language that is developed to specialize in addressing the needs of a given problem domain. The domain itself could be many things. It could be specific to an industry, such as insurance, education, aerospace, medicine, etc., or to a technology or methodology, such as JEE, .NET, database, services, messaging, architecture, or domain-driven design.

The reason I would develop a DSL is to make dealing with a set of challenges in a domain I am working in more elegant and easier to deal with. That's because the language I create will be just what I need to address my unique set of challenges, and no more than that. And of course, if I provide my language for others to use it may have to broaden a bit to address what they need, but still nothing more. The effort has the goal of making it feel more natural to use the DSL than to use a general purpose programming language or some other non-targeted tool.

An important distinction for this article is internal DSLs and external DSLs. Each of these is a style of DSL, and it is important to understand what style is appropriate for a specific problem domain. It is out of the scope of this article to delve deeply into what defines a DSL in general and what makes an internal and external DSL. Martin Fowler and others have lead such thought experiments and I suggest that you read such work for more detail on the subject. However, I do provide some basic context setting here.

Internal DSL

The language that is developed could be very closely tied to and implemented on top of the primary general purpose programming language in use in your project, such as Java, C#, or Ruby.

The Rails framework has been called a Ruby-based DSL that manages web applications written in Ruby. One of the reasons that Rails is called a DSL is because some of the Ruby language features are used to make programming against Rails seem different from programming against the general purpose Ruby language. When thought of as a language, Rails being created on top of Ruby as its foundation had a big head start in becoming a language in its own right.

I am not sure if Dave Thomas (PragDave) views Rails as a whole as a DSL, but he notes that several features of Rails are supported by different DSLs. The example he presents is Active Record Declarations as a DSL. By using some simple jargon specific to domain model entity associations, Rails developers allow the DSL to manage all sorts of complex infrastructure and operations behind the scenes while themselves focusing on high-level entity association concepts.

Whether its creators or huge consumer base view Rails as a whole a DSL, or only some features within Rails (Active Record Declarations), what I have been discussing here is an internal DSL. This DSL style has been labeled internal because, again, it is closely tied to and implemented on top of a primary programming language, but employs techniques to make it seem like it comprises a specialized language in its own right.

One of the key defining characteristics of the API of a framework or library fitting the bill of internal DSL, according to Martin Fowler and Eric Evans, is that it has a fluent interface. It basically means that you can stitch together short object expressions to form longer expressions that read more naturally.

I have been using and designing fluent APIs off and on for a while. For example, much of my early experience with fluent APIs was in Smalltalk. There are two ways to develop and use fluent interfaces in Smalltalk. First you can make the answer (return value) from one object message expression (method invocation) become the receiver of the next message:

   1 + 2 + 4

Here the number (object) 1 receives the + message with the number (object) 2 as its argument and the resulting number 3 (implicit) itself becomes the receiver of the next + message with the argument of number 4. (For clarity, number literals are not primitives in Smalltalk; they are first-class objects.) Of course this seems natural to all of us, so much so that its roots aren't even in programming. Well, that's the point. I could have accomplished the same thing like this:

  result := 1 add: 2.
  result := result add: 4.

But that's not natural or fluent to most. At first glance the fluent expression says it's clearly 7; not so the second example. And since this technique is not limited to a numeric math domain, the fluent nature of Smalltalk programming makes it easy to deal with the domain-specific aspects of many different domains. You are welcome to look into Smalltalk cascades, which is a second language facility that supports fluent interfaces. I demonstrated the first approach since it is supported by modern object-oriented languages (but in some cases not in the same way with number literals).

The significance here is that the fluency around a given API is designed in to make dealing with the given problem domain more elegant and efficient, even natural to experts within the domain. That's what makes it a DSL.

Of course we are each free to think of things like this in a manner that makes most sense to us. Whether or not we personally choose to think of a given API as a DSL or not is our choice. But it is important to recognize what Martin Fowler and others see what I have described above as internal DSLs. They tend to have a lot of influence on what the rest of the industry thinks and does. It's a nomenclature that is likely to stick around and come up in discussions for a while.

In my experience we tend to design and develop internal DSLs when we need a technical API for ourself and fellow developers to use. It helps when the general purpose programming language on which we base the internal DSL has the rich rich set of facilities that support this style. Clearly some languages such as Smalltalk and Ruby make this easier. Some languages such as Java and C# make it less easy. Targeting a fluent API and other internal DSL facilities at capable software developers cuts complexity and development time. However, if we are trying to simplify and add power to non-programmer domain experts, internal DSLs are not an option.

External DSL

Frankly I never thought of using, designing, and developing fluent APIs as work in domain-specific languages. So I have to admit that because of a long history with fluent APIs, it is difficult for me to shoehorn the concept into the internal DSL category. But I'm learning. On the other hand, when I first read about DSLs I immediately associated the definition with my work of creating a number of “little languages.” Because of this I'd suggest that if you had trouble swallowing the definition of internal DSL above, this flavor will taste much better.

Defining an external DSL is much easier than defining an internal DSL. It is analogous to creating a general purpose programming language that is either compiled or interpreted. The language has a formal grammar; that is to say, the language is constrained to allow only well-defined keywords and expression types. The source code of a program written in the language is kept in one or more files that have a text format, or might be more of a tabular or even graphical format. In the textual DSL case you'd create the source file using a text editor or a full fledged IDE. You then compile the source code and run it as part of a resulting program, or otherwise run the source code directly through an interpretor.

The main difference between a general purpose language source and an external DSL source is that when compiled the DSL is generally not output as a directly executable program (but it could be). Generally the external DSL will be translated into a resource that is compatible with the core application's operational environment, or it may be translated into source code of the same primary general purpose programming language used by and built as part of the core application.

An example of an external DSL that is translated into a resource that is consumed by the application is the object-relational mapping files used by Hibernate and NHibernate. Another example is presented by Jay Fields of ThoughtWorks on “Business Natural Languages.” You can imagine an external DSL that contains metadata that your application needs to, say, validate user input. You would read the metadata, transform it into an efficient internal format, and use it at runtime.

An example of an external DSL that gets translated into the source code of a target application are the languages developed using the MetaCase MetaEdit + Workbench and the Jetbrains Meta Programming System (MPS). Another example is Markus Völter's work on “Architecture as Language.” In this example Markus is able to define a software architecture, validate it, and generate code from textual architecture descriptions.

An external DSL may be used to directly support the design and development effort and thus be used by software developers. In that case it could be used to generate code that consumes a fluent API developed as an internal DSL. An external DSL is also appropriate for use by non-programmer domain experts, if properly designed and targeted for that class of user.

In many cases an external DSL is best when accompanied with language support, such as tooling. When only a small group of software developers, including the DSL's authors, are its users, you may only need a simple text editor. But when the DSL is distributed outside its team of creators, or when it is targeted at non-programmer domain experts, a syntax highlighting and code assist editor is essential to the success of the DSL. Other tooling may also be appropriate or necessary.

Language Complexity

I consider a complex external DSL one that:

  1. Is not easy to parse. A comma delimited file of text records is relatively easy to parse. A language such as Java is not relatively easy to parse. A complex external DSL is somewhere in between the two, and probably leans closer to the Java language than a CSV in terms of complexity. (You may still want to develop a formal grammar for a CSV anyway, but my point is it is not essential and possibly quicker not to.)

  2. Requires a complex internal representation once parsed. The complex representation is a tree or object graph that contains the source artifact expressions in a convenient and optimized state. This representation supports validation and both/either interpreted and/or generative tooling.

  3. Facilities the generation of one, several, or even many complex target artifacts from a single source file or multiple source files. This of course is only the case if the language is not only interpreted but is also/instead used by generative tooling. If you only decorate the target artifact with a few minor details not provided by the source DSL, I'd question why the target source language was not used in the first place.

With the above context you may be wondering how you'd actually implement a complex external DSL. I describe that in the next section.

Design and Development

Designing and developing any complex language is a big challenge. Even if you have a good idea of what you want out of a language, working out the details of a complex language can bend your mind. It seems like just about the time you think you've got all the language features identified and syntax worked out, your future user base (including yourself) thinks of something new. This is many times even more so the case than when designing a general purpose language.

Of course, like anything useful in our field, a language will undergo enhancement after enhancement over time. One thing we need to do is make sure that we can support those enhancements over time. The language design, therefore, needs to embrace change. Further, a good language design will make developing the language far easier.

I mention a wide range of language syntaxes in this section, including graphical and textual. However, I must limit my scope to just one syntax type in order to keep this article to a reasonable length. Thus, I have chosen to focus on textual DSLs. A textual DSL is easily understood, and is likely applicable even when a graphical DSL is in use (see why below).

Syntax Design For Now and Later

Fortunately most language designers don't wake up in the morning and say “I'm going to create a language today, and I wonder what it will be.” If we are considering the development of a language we have a good idea why. This is important because if we really don't have a clear vision for the language our resulting design will be just as weak. So an important first step in language design is to know a lot about what you want your language to do.

Knowing what you want your language to do doesn't necessarily mean you know what it's syntax will be. A language's syntax is not only key to its usability, but can also influence its ability to adapt to enhancements. Of course a language's syntax must be appropriate to its audience. If your language's users are technically inclined, even programmers, the language syntax you choose will be different from the language you choose for non-programmer domain experts.

In discussing syntax I am not limiting my context to textual DSLs, even though that is my focus for the remainder of the article. It is possible that your language will have two syntaxes: one that the language user interfaces with and the one that gets saved in a file, and gets parsed and translated. In such a case you will likely develop the user-centric syntax as a graphical user interface (such as a table with rows and columns) or even a glyph/shape based language (similar to Visio diagrams) where the user draws the syntax instead of writing it. In the background, at the file level, your language can be as technical as necessary. We tend to think of graphical syntaxes as models, but models are not limited to graphics just as complex DSLs are not limited to the textual variety. As you will soon see, at some point you will start to think of your language in terms of a model no matter what its syntax is like.

If the technical syntax of your complex DSL is not flexible enough to support future enhancements you face the danger of limited or zero backward compatibility. That is, you may create a new language feature that invalidates sources that existed before the enhancements. I believe that this danger is greater when supporting dual syntaxes. Of course it is possible to provide a source file (model) upgrade transformation tool. However, even file syntax upgrade utilities have limited benefits depending on the nature and complexities of the enhancements. They also are an unwelcome necessity to the user and spell resistance to upgrades.

I can provide a few suggestions for choosing an appropriate and extensible syntax, which are in order of relevance as your design efforts advance from early to later stages:

  1. Study other languages. Think about why languages such as Java and C# are designed the way they are and are so successful. For certain Java and C# syntaxes are designed for acceptance by communities of developers that existed before these newer languages. But there are other reasons. Block based scoping languages are inherently extensible because new blocks of various types may be added adjacent to existing block types and also nested within them. This does not mean that you should reinvent such languages. It means that you can reuse certain aspects of successful languages in your own. Make sure you think about other languages such as Ruby, Smalltalk, Perl, and Python. What do you like and dislike about them? If you could change and blend any set of languages, what would you do? Can you reuse aspects of an existing successful language to improve your language ideas?

  2. Experiment with various syntax ideas using agile techniques. How does it feel to write using each syntax? What do others think of the experimental syntaxes? Can the syntaxes be defined as a formal grammar? What would it be like to support tooling for your favorite syntax?

  3. Identify as many language features as possible. Experiment with various syntaxes (as suggested in point #2) that supports the top 70-80% of those features and withhold the rest. Once you think you have a winning syntax consider what it would require to add the remaining 20-30% of withheld features. Is the first version of the language brittle or is it extensible? Besides withholding and later adding features, purposely implement some of the syntax incorrectly. Consider what is involved in correcting the mistakes and ask yourself what would happen if you had to continue to support both the wrong syntax and the new corrected and enhanced syntax. Is there something about the syntax that makes dealing with these issues better or worse? Is there anything you could do to the syntax to make the issues easier to deal with?

  4. Present your language before communities of potential users. What do they think of the syntax? Is it intimidating to even smart people? Ask people you want to use your language for their honest opinion.

  5. Beta test your language. It is easier to get users to accept high impact changes to a language at version 0.9 than at or above version 1.0. Prepare your beta testers for the possibility that their feedback will result in obsoleting their work of creating source artifacts.

Of course if you are developing a graphical DSL and it will never be authored outside the graphical environment, it is appropriate and perhaps best to use XML. However, I would never suggest that an XML schema be used as a source syntax for any directly edited textual DSL. Just imagine your language users authoring Ant (or Nant) programs and I think you'll get my drift. As Martin Fowler stated: “XML makes it easy to parse, although not as easily readable as a custom format might be. People do write plug-ins for IDEs to help manipulate the XML files for those who find that angle brackets hurt the eyes.” If you are today designing a directly edited textual DSL avoid XML-based syntax like the plague.

As a final reemphasizing statement about DSL syntax, a complex textual DSL needs to be definable as a formal grammar using a BNF (or EBNF). If your language cannot be expressed as a formal grammar then it is going to be very difficult or impossible to parse. More on parsing and BNF is provided below.

Designing the Language Metamodel

Think of source code written in a language's grammar (syntax) as a model of concepts that you are describing. The concepts you are describing could be data, structure, and behavior, which are typical concepts in computerlandia. From the language designer's point of view, the descriptions of these concepts are a model, not just source code. Thus, when you parse a source model and put its representational contents into objects, the objects are called a metamodel.

Even if a language source artifact is loaded into an abstract syntax tree (AST), an AST is a metamodel of sorts. An AST is a metamodel of the parts of a source syntax pertinent to describing its abstract structure, albeit closely tied to the syntax. In any event, I'd suggest that many complex textual DSLs should not be loaded into an AST but rather into a richer metamodel. I favor making the metamodel a graph (as necessary) that is much like the Model layer of the Model-View-Controller pattern; a Domain Model. In this case, however, the graph is not a model but a metamodel of the source model. (Note that Martin Fowler uses the name Semantic Model for what I here call Metamodel. He also defines this concept as an object model that is a Domain Model.)

Although this topic follows that of syntax design, it does not mean that the language's metamodel cannot be considered prior to a finalized syntax. The fact is, your language's metamodel is as important to the internal workings of your DSL as your syntax is to its external acceptance and future enhancements. The metamodel's design can begin even before the language syntax is conceived because the metamodel is not (or should not be) strongly tied to a syntax.

To illustrate this, recently James Gosling called Java's formal grammar (syntax) “a fraud” (video at approximately 27:00 and also 60:00) because the language's original design didn't call for a C-like syntax. Nonetheless, Java internally still had interfaces, classes, methods, fields, threads, primitives, and ran on byte codes. If there was no effort to attract C/C++ programmers to Java by use of a familiar syntax Java could have ended up looking to us much different than it does today. However, you can be sure that Java's metamodel didn't have to change (or at least not drastically) in order to facilitate a C-like syntax (perhaps for concepts such as pre- and post-increments, unless those where already supported by a non-C syntax). That's because the underlying metamodel defined the language's concepts in an abstract way that could be mapped from multiple syntaxes. This feature is what makes the Java VM a great host for scripting languages such as Groovy and JRuby.

When thinking about a metamodel remember that it is an object model that holds meta information about the source model. Thus, any of the concepts of your language should be expressed richly in the metamodel. Let's take a familiar example, a metamodel of an object-oriented language. The classes in the object-oriented language metamodel would include:

1

The class MetaClass would contain metadata about the class in the source model that it represents. For instance, if some source code defined a class named EmailAddress then you'd have a MetaClass instance that has a nameattribute/field set to the string “EmailAddress”. Class MetaClass would also contain a collection of MetaFieldinstances and another collection of MetaMethod instances. If the model source class EmailAddress had a field declared with the name address then the MetaClass would contain at least one element in its fields collection, an instance of class MetaField that has its name attribute/field set to the string “address”. Further, each MetaFieldinstance would have a reference to the MetaClass type of that model field. Thus, the metamodel forms a graph.

You would use meta classes analogous to this example, but for the particular model representation expressed in your DSL. I suggest considering a meta class hierarchy that starts with the abstract base class MetaObject. The MetaObjectwould be used to provide default state and behavior for all meta subclasses. For example, perhaps many meta objects your language supports have a name. In that case class MetaObject would contain the name attribute/field and accessors in behalf of all subclasses. After you have defined a useful MetaObject you can start designing the full class hierarchy of your metamodel. Of course over time as your metamodel grows you would refactor common state and behavior down into classMetaObject.

To take this approach one step further, if you are familiar with Eric Evans' “Domain-Driven Design” patterns (DDD), you could apply those patterns to your metamodel. I use this approach for my DomainMETHOD tool, which is a DSL that facilitates DDD and generates a working application domain layer. So I get the best of both worlds: I design and develop my tool using DDD, and I support the design and generation of working DDD-based domain layers. My tool's design is complete with Entities, Value Objects, Aggregates, Repositories, and more. I store metamodel object Aggregates that are read from model source in my MetaObjectRepository. I use a ProjectRepository to find project configurations and custom generation directives, and to create, find, and store generated target artifacts, which are eventually persisted to output files. I also have a repository that I use for finding and for managing the state of source templates, aptly namedTemplateRepository.

Using DDD can be a practical and powerful means of conceptualizing, designing, and implementing your metamodel. This might not always work out depending on the DSL characteristics, but if it can you should consider using DDD.

Relationships Between Metamodels and Target Models

In this article I have mentioned three potential target uses of an external DSL. I want to restate these here in the context of metamodels to show interesting relationships between metamodels and target artifacts or models.

Here are the three targets I have mentioned: the DSL source model could be parsed and translated into source code to become part of your application; the DSL source model could be parsed and interpreted in your application's runtime environment; the DSL source model could be parsed and translated into another form of data for your application to consume at runtime.

The first use of the metamodel, to achieve target source code artifact output, is a straightforward albeit usually more complex one. Essentially you are transforming one or more source models into a metamodel and then into a set of target models (or source artifacts).

The last two variations, interpreted and translated data format, have similarities because an interpreted model and any other form of translated data are just varying types of metamodels. In essence taking your metamodel built by your parser and turning it into another data format supported by your application could be thought of and even be executed as a model-to-model transformation. But the point is, it can remain a metamodel. This notwithstanding, if you persist the data format metamodel rather than keep it only in memory, a final persistent transformation as part of a generative process will need to occur.

There may be other differences between these last two variations since an interpreted model may be more behavioral or a mixture of behavior with state and state transitions. However, if the interpretation goal is simple in nature you will see real similarities between the two.

Metamodel Generation

It is possible to get your metamodel for free. The openArchitectureWare Xtext tool (now part of the Eclipse Modeling Framework) creates your metamodel automatically as an artifact of its tooling. All you do is define your formal grammar using Xtext, a step you would likely take anyway with a different parser-generator tool, and then generate artifacts from it. Your metamodel is generated along with a parser. When your resulting parser parses the DSL source model the parser instantiates corresponding parts of the metamodel, making it ready for validation, interpretation, and/or code generation. That's pretty handy.

Defining the Parser

I purposely named this subsection “Defining the Parser” rather than “Developing the Parser.” Since a complex external DSL has a complex syntax that is not easy to parse, I believe that attempting to manually design and code a language parser is an exercise in futility. Many experienced in language parsing agree. There are very few developers who are qualified to create a custom language lexical analyzer and parser. Even for those who could do so, unless rave performance is essential, they would probably choose to use a tool that allows them to define, rather than hand code, a parser. It is simply easier, faster, more efficient, and less error prone to do so.

Most parser-generator tools support the description of a language's formal grammar (it's syntax) using the Backus-Naur Form (BNF) or an extended variation (EBNF). The BNF provides a formal way to define a formal syntax, and an EBNF makes it even easier to do so:

INTEGER_LITERAL : ('-')? ('0'..'9')+ ;

As can be seen above, an EBNF improves BNF by supporting optional and repeating specifications. In the case of INTEGER_LITERAL, the specification states that it may or may not have a minus sign and will be made up of one or more digits between 0 and 9. If you know regular expressions you will have a big head start here. While EBNF is not the same as regular expressions, they look like next of kin.

A typical language grammar for a general purpose language may be defined at its highest level as follows:

prog : expr+ ;

This simply states that the program (prog) is made up of one or more expressions (expr). Of course you are not done yet. You must define what an expression (expr) is, which is where the fun begins:

expr : ... ;

Further decomposing a language's syntax from the highest abstraction down to its lowest detail has a learning curve. Nonetheless, this approach is much easier and must faster than hand coding a buggy lexer and parser. Just run the parser-generator tool against your properly defined language formal grammar definition and in a second or two you have a working, bug free lexical analyzer and parser to use. Even better, once you've gotten the hang of the EBNF that your tool supports your productivity will soar.

There are several open source and otherwise low cost parser-generator tools available. I like the open source tool ANTLR (pronounced “antler”) for several reasons. ANTLR supports the generation of parsers in several target languages. ANTLR version 3 has very sophisticated source stream look ahead techniques that saves you hours (or days!) of frustration in resolving grammatical ambiguities. Grammatical ambiguities occur when various parts of your language clash and confuse the parser. This is a complicated topic in itself and I can't provide further information now. But believe me, if you haven't experienced this problem you don't want to, and if you have experienced it you just want to download ANTRL 3 and be done with it. ANTRL supports parameter passing to element definitions, and simple and complex return values as well, and may more professional features. By the way, ANTRL's EBNF is a great example of a complex external DSL.

Most modern parser-generators allow you to associate custom code with language element definitions. This boils down to inserting your custom code at the points where the parser evaluates the source stream as matching an element condition. In DSL terms Martin Fowler names this the Foreign Code pattern since the custom code is foreign to the EBNF language. Since your custom code gets inserted into the parser it allows you to, among other things, instantiate your metamodel on matching events. Thinking back to the object-oriented language example above, here's how it works (in an incomplete yet simplified fragment):

classDef[MetaObjectRepository repo]
  : 'class' (ident = identifier) '{' cb = classBody '}'
  {
    MetaClass metaclass = new  MetaClass($ident.text);
    // ...
    repo.addClass(metaclass);
  }
  ;

The classDef element is matched by the generated parser when the lexical token stream has the following sequence:

  1. The text string “class”

  2. An identifier element (defined elsewhere)

  3. The text string “{”

  4. A classBody element (defined elsewhere)

  5. The text string “}”

In the generated parser code (Java in this case) the matcher for classDef has the custom code between the (unquoted) curly braces inserted. When a match occurs the custom code is executed. This allows the new MetaClass to be instantiated with its class name set to the value of the text string in the ident variable. The ident variable was assigned upon matching of the identifier element (the same goes with the assignment of cb with the match of classBody). Notice that the MetaObjectRepository instance repo is passed as an argument to the classDef element, and available for the custom code to use. Once the new MetaClass is instantiated and fully constituted (code not show) it is added to the repository.

There is no magic here. The key to understanding how this all fits together is in the fact that each EBNF element is generated as a Java (or other parser target language) method. Thus, there is a method generated for classDef, for identifier, for classBody, and for all other elements defined in the grammar.

Parsing One or Multiple Models

You will need to determine if one or many DSL model sources will be parsed into a single metamodel instance. For example, if the DSL sources represent an abstraction of target application sources, it is very likely that DSL source authors will produce many such sources. In that case your parser will need to know how to look for multiple sources.

This requirement defines a simple directory-file crawler. The DSL tooling might accept a project base path and crawl the directory structure matching all files with a file extension defined by your language specification. As each properly named file is encountered and matched it is read, parsed, and the results placed into a single metamodel instance. Associations between sources represented in the metamodel thus form a graph. Once the single metamodel instance has been constituted from all DSL source models you are ready to take one of the three common actions: translate and generate, interpret, or transform into a different kind of model such as a data format suitable for your application.

2

If your DSL and tooling support only a single DSL source model for constituting your metamodel, your parser will be a bit simpler. Just design your tooling to accept a single model source, parser it, build your metamodel, and then take one of the noted three actions.

If you are generating code or some form of data from your DSL, the next subsection will be of interest.

Generating Code

In discussing code generation note that the principles here also apply to data generation. It's just that data generation is usually a tad or a lot simpler than code generation, so I address code generation head on. The good news is that if all you need to do is generate some kind of data format from your DSL you will probably be able to use the more simplistic strategies I here discuss.

At first it may seem that code generation from a metamodel is a straightforward exercise. Indeed, depending on the simplicity of your DSL, your metamodel, and the target artifacts, it may be just that. In my experience, however, a complex external DSL means that code generation from its corresponding metamodel is rarely simple. I will progress from common code generation strategies to the more powerful ones, outlining the competing forces with each approach.

Direct From Model

One approach to code generation is to translate the target artifact output without creating a metamodel at all. This technique replaces all or most of the custom code presented above used to constitute your metamodel with code that outputs directly to target artifacts. If you can use this strategy then why not? Certainly if you don't have to go to the trouble to design a metamodel and then write the custom parser code snippets to constitute it, you are way ahead of the curve to completing your overarching task and moving on.

3

If you believe that your DSL will never need a metamodel then I suggest considering this approach first. My only caution is, if your DSL source is an abstraction so close to your target artifact output, is it possible that you should be coding directly to your target artifacts in the first place? You will have to answer this question, and it is possible that the answer is “no,” which is good for you.

Are you targeting multiple artifacts from a single source? Are you targeting multiple artifacts from multiple sources? If so it is very likely impractical to use a direct-from-model generation approach.

Walking the Metamodel

After your metamodel has been fully constituted from DSL model source, you will have to “walk the metamodel” to determine what target artifacts (source code, configuration files, etc.) must be generated. This involves starting at a main aggregate root or iterating across many main aggregate root objects, and then navigating down through them looking for significant metadata and exercising needed meta domain behavior.

The primary problem with this approach is when you reach some metamodel context you may not (and likely will not) have all the context you need to properly generate a given target artifact. Necessary metadata will tend to be spread more widely throughout the metamodel:

4

If you have a separate specialty code generator for each target artifact type, you may find that you have to walk the model multiple times to find all metadata you need to generate each target artifact. You will end up developing multiple complex navigators for each artifact type. Or you will design your metamodel as a more complex graph so you can associate dependent metadata for all necessary context. Using either of these approaches to solve navigation needs can be difficult to achieve.

Eventing Metamodel With Region-Aware Artifacts

A very practical solution to the complexities of walking the metamodel multiple times is to design an eventing metamodel. This can be done with a simple Publish-Subscribe pattern. Design a single metamodel walker that fires events when it encounters significant parts of the metamodel. Subscribers are delivered the events as they occur, and respond to the events by generating code just in time. This pattern has proved very valuable in my DSL code generation efforts.

The eventing metamodel may be incomplete without support for artifacts with region awareness and workspaces. Source code artifacts of all sorts have regions:

5

As events of interest occur to each code generator listener, inject newly generated source code into the appropriate regions of the artifact. The regions can be managed by name and/or index. Nested region artifact management is even more powerful, because you can keep tacking on generative output as needed and where appropriate:

6

But what if your specific generator receives an event, and once again it doesn't have enough context with the current metamodel metadata? A knee-jerk reaction is to immediately walk the model to find what you need right now. But with a bit of patience and an artifact workspace you can wait for the context you need and generate code more elegantly. When an event delivers incomplete metadata context, just store the partial context in a well defined (unique) workspace area of the artifact. When subsequent events occur build on the saved workspace metadata. Finally when the necessary contexts have all been delivered and the source segment is completely built, grab the source segment out of its unique workspace area, remove the workspace area, and save the completed source segment into the appropriate artifact region:

7

Once the target artifact is completed it can be saved to the appropriate output file.

8

You'll need to design an artifact persister that knows how to collapse nested artifact regions into correct artifact files.

Code Templates

Unless the source code you are generating is very simple, you will benefit from the use of code templates and a template engine. Consider the alternative to the use of code templates. As you reach the point of a particular source code generation event, you need to create the following C# property:

private string _address;
public string Address
{
  get { return this._address; }
  set { this._address = value; }
}

You could take a common approach and create a C# source snippet text string as follows:

string propertyDef =
  “private” + propertyType + “ ” + hiddenPropertyName “;\n” +
  “public ” + propertyType + “ ” + propertyName + “\n” +
  “{” + “\n” +
  indent + “get { return this._” +  hiddenPropertyName + “; }\n” +
  indent + “set { this._” + hiddenPropertyName + “ = value; }\n” +
  “}” + “\n”;

Honestly, as I wrote this example I was confused a few different times as to where I was in the generative stream. I know how to create a C# property, but stitching these generative fragments together makes for confusion and several round trips before the generated code is correct. In fact I am not now certain that the above fragment is correct. Nonetheless, this example is one the the simplest pieces of code you will need to generate for a C# class.

Next consider the use of a code template and template engine. First of all, here is a template for creating a C# property:

property(propertyType, propertyName, hiddenPropertyName) ::= [[
private $propertyType$ $hiddenPropertyName$;
public $propertyType$ $propertyName$
{
  get { return this.$hiddenPropertyName$; }
  set { this.$hiddenPropertyName$ = value; }
}
]]

The template is much clearer. First note that the template definition has a name, property, and takes a set of parameters, the propertyType, propertyName , and hiddenPropertyName. So it looks familiar, like a function or method. The body of the template–everything between the ::=<< and >> (Note: A formatting error prevents the display of these characters in the code sample above so I used [[ and ]] instead) tokens–basically looks a lot like the C# source you would write if you were creating the property manually, but with a few minor differences. The template body is parameterized. By marking parameterized values with surrounding $ characters we allow the template engine to search and replace the matching $propertyType$ and other such placeholders with the passed in values.

To use the template above you would do the following:

StringTemplate template = getTemplate(“property”);
template.setAttribute(“propertyType”, “string”);
template.setAttribute(“propertyName”, “Address”);
template.setAttribute(“hiddenPropertyName”, “_address”);
String code = template.toString();

In a real code generator the parameter values set as template attributes would be soft so you could reuse the above code to generate any number of C# properties as specified by a given DSL source model.

I suggest that the template engine you use should support not only parameterized values, but also conditionals, repeating expressions (collections), and auto-indentation. The template example above is the syntax and API of ANTLR's StringTemplate subproject, which supports a rich set of template features.

Clearly you should use templates and a template engine if you do even simplistic code generation.

Conclusion

I have provided a high-level overview of what DSLs are in general and a bit more specifically what internal and external DSLs are. I also cover the main challenges and patterns involved in developing a complex external DSL. This provides a brief but firm foundation for doing meaningful DSL development. Obtaining the proper tools to define and generate parsers and metamodels will help you make rapid progress, but there is no tool that will replace the thought and design that goes into your language's formal grammar, metamodel, and code generation.

If you have not yet ventured into the development of a complex external DSL I hope this information has brought you a few steps closer to doing so. I welcome your feedback and would be glad to discuss with you details on this topic.

About the Author

Vaughn Vernon is an independent consultant with more than 26 years of experience as a software developer, architect, and designer. He has conceptualized and developed several software development tools, including DomainMETHOD, a DSL supporting rapid design and generation of domain models based on the patterns of domain-driven design. Vaughn has authored numerous articles and patterns, and has spoken at a variety of technical conferences. You can reference more of his work at www.shiftmethod.com and contact him at vvernon at shiftmethod dot com.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

DOMAIN SPECIFIC language by Rusty Wright

I've always been irritated with people calling an "internal DSL" a DSL because when I see the words domain specific language the only thing that makes sense to me is an external DSL. If it's really DOMAIN SPECIFIC then I shouldn't be able to just drop down into the hosting language and start using it.

Someone needs to come up with a better name instead of DSL when they're referring to internal DSL.

RE: DOMAIN SPECIFIC language by Chris Peterson

Someone needs to come up with a better name instead of DSL when they're referring to internal DSL.

Maybe "domain-driven design" or simply "code"? If someone is writing a lot of code or modules that are not related to their application domain, maybe they should reconsider their choice of programming languages.

Re: DOMAIN SPECIFIC language by Werner Schuster

"MultiParadigm Programming"?

Re: DOMAIN SPECIFIC language by Rusty Wright

I don't have a problem with the term Domain Specific Language, as long as it's used to describe a language that really is specific to a domain, which means an external DSL.

For example, when you have syntax errors, its error messages should be specific to the domain language, not the underlying hosting language. And, as I said before, you shouldn't be able to drop down into the hosting language; even though that might be quite useful, my issue is simply with calling such pseudo languages a DSL.

Re: DOMAIN SPECIFIC language by Vaughn Vernon


you shouldn't be able to drop down into the hosting language


Are you saying your DSL users should not be able to author Foreign Code? I'd agree that non-programmers should not, but it really depends on your DSL target user community. If your DSL is targeted at programmers then allowing Foreign Code is a possibility.

Vaughn

Re: DOMAIN SPECIFIC language by Vaughn Vernon

As far as naming an "internal DSL" if you don't like that nomenclature, perhaps fluent API says it best. The concept of fluent API, while not necessarily the name, has been around for a long time.

Vaughn

Re: DOMAIN SPECIFIC language by Rusty Wright

Are you saying your DSL users should not be able to author Foreign Code?


No. What I'm kvetching about is calling something that allows that a DSL. I'm not against doing the things you can do with these so-called DSLs, just calling them a DSL; they're clearly not Domain Specific.

Re: DOMAIN SPECIFIC language by Vaughn Vernon


No. What I'm kvetching about is calling something that allows that a DSL. I'm not against doing the things you can do with these so-called DSLs, just calling them a DSL; they're clearly not Domain Specific.


That's an interesting perspective. From your definition, then, you'd have to conclude that ANTLR's support for including custom/foreign code snippets to be inserted into the generated browser means that ANTLR's EBNF cannot be classified a DSL. It seems to me that (E)BNF is clearly abstracting away the details of writing a lexer and parser manually and brings us closer to a pure parser domain. That to me renders it a DSL. But by your definition Terence Parr would have to come up with some sort of EBNF extension that manages metamodels and semantic models (which, has nothing to do with parsers), similar to supporting ASTs, and we'd use that instead of Foreign Code. If so it sounds impractical and limits our ability to deliver solutions that can be used by a broader community. Either we'd fail to support unimagined targets or we'd end up creating a much broader and more complex DSL.

One thing that you are hitting on that makes a lot of sense is like the last statement made in the presentation "DSLs: The Good, the Bad, and the Ugly" posted just a day or two before my article. The fellow states something like 'the narrower our domain the higher the benefit/reward of developing a DSL.' That is very true, but I'd also suggest that we don't all have the luxury of narrowing our domain that finely.

Re: DOMAIN SPECIFIC language by Rusty Wright

Again, I'm not saying anything about how flexible or narrowly defined a so-called DSL should be, or how much or how little it's capable of doing. I'm not saying a so-called DSL shouldn't be allowed to drop down into the hosting language. All I'm fussing over is that once you allow that, it's really not correct to call it DOMAIN SPECIFIC. At the moment I can only think of one example of a domain specific language; PostScript. If you make a syntax error in PostScript you'll get a PostScript specific error. PostScript isn't implemented inside or on top some other language that you can drop down into. Therefore I think it's correct to call PostScript a DSL; a page description Domain Specific Language.

On the other hand, Active Record allows you to drop down into Ruby code. If you make a Ruby syntax error you get an error from the Ruby interpreter. What I'm not saying is that Active Record should not have been implemented on top of Ruby. I'm also not saying that Active Record should have been implemented as an external DSL. I'm not concerned about the capabilities or implementation of Active Record. All I'm saying is that because of those things it's obviously not DOMAIN SPECIFIC, so let's call a spade a spade and call it something else, something closer to the truth.

Re: DOMAIN SPECIFIC language by Rusty Wright

In response to your ANTLR remarks, I haven't used ANTLR but it sounds sort of similar to what I have used, Yacc and Bison, where with those you had EBNF for the parsing rules and after each parser rule was C code that did the underlying grunt work.

I think the difference is is that back in the days when Yacc and Bison were big, programmers weren't so self deluding and presumptuous to call that parser generator configuration language a Domain Specific Language.

I often feel like the term DSL is a more of a marketing term; it's much more impressive sounding than calling these things something like a "hosted pseudo language" or a "hosted configuration language". People are making a purse out of a sow's ear and calling it a silk purse when it's obviously not.

Re: DOMAIN SPECIFIC language by Vaughn Vernon


All I'm fussing over is that once you allow that, it's really not correct to call it DOMAIN SPECIFIC.


I have not misunderstood what you are saying. Your viewpoint reminds me of a summer job I had back when I was in school. I worked for Mr. H and he was there to teach me a thing or two, every day. One of the things he taught me was that I used a pushbroom incorrectly. Whenever I got to a corner of the building he told me to sweep I would pick the broom up and set it against the wall and pull the dirt backward toward me. Mr. H couldn't handle that. He said "you are using that incorrectly because if you were supposed to pull the broom they would have called it a pullbroom, but it's called a pushbroom which means you push it." Mr. H then grabbed the broom out of my hands and demonstrated. Of course I only used the broom the way I did because unlike a spider I couldn't scale a wall and turn to put leverage on the broom to push it forward away from the wall. And of course when Mr. H demonstrated how I should push the broom he never got within three feet of a wall. While I still call it a pushbroom, I still from time to time will get caught pulling the broom toward me. Oh well.

From Mr. H's viewpoint I'd have to invent something called a push-pull-side-to-side-leaning-or-hanging-broom. The leaning-or-hanging part is because you can't push, pull, or sweep side-to-side constantly. You have to stop at some point and lean the thing against a wall or hang it on a wall.


If you make a syntax error in PostScript you'll get a PostScript specific error.


In my DomainMETHOD DSL, if I make a syntax error in my DSL source I get a DomainMETHOD specific error message. But once I am past the code generation part and a user of the domain model tries to set an invalid value into some domain object they will get an error (exception) produced by the language that the model was generated in.

It appears that by your definition a DSL can only be interpreted or generated into a final machine executable format. While I have no vested interest in what an internal or external DSL is called, or what the pure definition of both is, I disagree with the assertions that none of these things are DSLs.

I personally have chosen to use the term DSL so others will know what I am talking about. I also believe that the efforts of those who place an emphasis on the possible need to create new languages in a given domain is important to the advancement of innovative software solutions in a lot of places. Since I have been using what are now called DSLs for a long time I could throw my arms up and say that the new industry nomenclature is wrong. I have simply decided to go along with the naming because it makes sense that this important direction have a name.

I would suggest, however, that if you feel you have a strong point to make I am the wrong person to change industry direction. Like many things in this industry I just end up consuming them and sometimes writing or presenting about them. You probably owe it to yourself to talk to those who can actually change the way things are. For the rest of us, we are much better off staying within a ubiquitous language within our industry so everyone else can relate to what we are saying and doing.

Re: DOMAIN SPECIFIC language by Knut Forkalsrud

... let's call a spade a spade and call it something else, something closer to the truth.


Maybe the term jargon is a better fit than "language". In my dictionary "jargon" is described as "a form of language regarded as barbarous, debased, or hybrid."

Re: DOMAIN SPECIFIC language by Rusty Wright

I finally figured out my problem; I am taking the term literally given the order of the words. Similar to your push broom analogy, if I were to say that I have a plastic orange bowl I think that most people would assume that I have a bowl that is plastic and orange. Not that I have a bowl that I use for holding plastic oranges. So what I finally realized is that a DSL is a (pseudo) language for a domain specific problem.

My problem is that when I see the 3 words Domain Specific Language I naturally see the is meaning.

Re: DOMAIN SPECIFIC language by Vaughn Vernon


if I were to say that I have a plastic orange bowl


I won't go all English professor on you (because I am not one), but I think proper English is "orange plastic bowl." See "Concepts In Composition" p343-344. Clearly, however, the way in which something is named can cause confusion. I think in the case of DSL it's something that we need to accept because it represents something more than the name itself may be able to convey on its own.

I think as far as the representation goes, it can mean either thing: (1) it is a DSL in the sense that you originally desired it to mean (e.g. PostScript is a page description DSL) if you create the language in the appropriate way for that to be true; (2) it is a language for a domain specific problem, as in my DomainMETHOD tool that brings a designer closer to the domain of domain-driven design and then translates the design into an object model that is about the domain that the designer is targeting. (I could also argue that DomainMETHOD is a domain modeling DSL, and it probably is even by your definition, but it is not important for me to argue that point.)

One thing I should clear up, which I thought was obvious, but may not be. I don't claim that the domain model code generated from my DomainMETHOD DSL tool is automatically an internal DSL. It may well be if you accept that a fluent API is an internal DSL, and if the modeling techniques used by the designer form a fluent API. But the generated domain model code is not always fluent just as a natural outcome and side effect of using the tool. Also, in general, there is no reason that any external DSL tool needs to automatically generate an internal DSL in order for the source code that tool parses and translates to qualify as an external DSL.

Re: DOMAIN SPECIFIC language by Rusty Wright


if I were to say that I have a plastic orange bowl


I won't go all English professor on you (because I am not one), but I think proper English is "orange plastic bowl."


More semantic hair splitting from me: ordinarily yes; taken out of context, yes. But in certain situations I think it could mean a bowl for a plastic orange. Let's suppose we're talking about a precious plastic orange I have and a cut crystal bowl that I only use to put that plastic orange in. If I were to then refer to my "plastic orange bowl" I think most would understand that I mean the bowl for my plastic orange.

Unless you're talking about whether orange goes in front of plastic, or vice versa. That's splitting the hairs even finer than I would venture.

Re: DOMAIN SPECIFIC language by Vaughn Vernon


More semantic hair splitting from me...

Since I risk no longer being sure what we are talking about, I'll have to go with the following and call it a day :)

I think in the case of DSL it's something that we need to accept because it represents something more than the name itself may be able to convey on its own.

It's been fun. Have a great weekend!

Re: DOMAIN SPECIFIC language by Steven Kelly

I read "plastic orange bowl" as meaning "a plastic bowl for holding oranges", so there's another interpretation. Try interpreting the phrase in the following contexts, and see the different meanings you can get:
- a shop selling plastic fruit
- a shop staffed by non-native speakers selling bowls of various colours and materials
- a shop selling specialized fruit bowls
- discussion of playing surfaces in intercollegiate American football
- scale models of historical American football stadiums
...Enough already!

That's a feature of a DSL: the context - the domain - gives information without which the statements are ill-defined, even for someone who recognizes the meanings of the individual components (e.g. "plastic", "orange", "bowl"). That's why DSLs can get by with less verbiage than even well-designed GPLs, and more importantly why statements in a DSL are faster and less error-prone to read and to write than in a GPL.

Sharing experiences by Johan den Haan

Hi Vaughn,

Thanks for sharing your experiences! Triggered by your effort I've also shared some of my experiences with DSL development. See: DSL development: 7 recommendations for Domain Specific Language design based on Domain-Driven Design

Re: Sharing experiences by Vaughn Vernon

Hi Johan,

I enjoyed your article. It appears that we have very similar experiences and I liked the way you expressed yours. Also the Mendix product line is an excellent example of putting DSLs to work with graphical editors.

Thanks!

Vaughn

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

19 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT