Design

Creating Your Own Domain-Specific Language

By Sebastian Zarnekow, August 23, 2011

Part 1: Writing the grammar without a course in parsing
theory

The Eclipse Xtext framework [1] provides a straightforward way to craft external domain specific languages. Simply define the syntax of your language and you'll end up with an Eclipse-based language-aware IDE. In this tutorial, I will illustrate how to perform the first steps towards designing your own language. You'll become familiar with the syntax of the Xtext grammar definition language and learn how to define validation rules for your concepts.
For this tutorial, we will use a well known abstraction that has been addressed by different frameworks in one or the other way: the entity model. The entity model is handled differently by various frameworks to overcome the mismatch between relational databases and object-oriented software systems. Ruby On Rails [2] derives a lot of information from the database structure, Groovy's Grails [3] framework uses an internal DSL to describe the structure of the entities, and Spring Roo [4] has a strong emphasize on scaffolding.

Because it is such a common use case, Xtext 2.0 already ships with an example that demonstrates how such a domain model language looks. In this tutorial series, I want to develop that language from scratch , explaining the grammar and APIs step by step.

The first iteration of the language is pretty simple and contains a few intuitive concepts. It allows you to define packages that contain entities and data types. In turn, an entity defines properties. A property refers to a type and may be multi- or single-valued. An example file looks like this:

Getting Started

To follow this tutorial, you must ensure that Xtext is properly installed in your Eclipse IDE. Once this is done, launch the wizard that guides you through the necessary steps to create a new Xtext project:

File -> New -> Project... -> Xtext -> Xtext project

Then choose the following settings (as shown in Figure 1) on the next wizard page before you hit Finish:

Main project name: org.example.domainmodel

Language name: org.example.domainmodel.Domainmodel

DSL-File extension: dmodel

Figure 1: Settings.

The "Hello World" grammar in the Domainmodel.xtext file illustrates a trivially simple language that allows you to write a number of "Hello World!" sentences.

In addition to the preamble, the grammar file contains two parser rules that specify the structure of a valid sentence of the language. It's important to realize that an Xtext grammar is not only a description of the syntax of a DSL, it's also a concise notation that describes the mapping to a type-safe object model. The types and the expected structure of these objects is called "abstract syntax." It is either derived from the grammar definition itself, or defined externally and imported into the grammar definition. These imports allow for language modularization. During this tutorial lesson, we will stick with the defaults and the generated domainmodel, which is declared in the preamble:

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!