Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Creating Your Own Domain-Specific Language

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:

import com.drDobbs.common.String
package com.drDobbs {
	entity Author {
		name : String
		many articles: Article
	entity Article {
		author: Author
		title: String

package com.drDobbs.common {
	datatype String	

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.

grammar org.example.domainmodel.Domainmodel 
	with org.eclipse.xtext.common.Terminals

generate domainmodel "http://www.example.org/domainmodel/Domainmodel"


	'Hello' name=ID '!';

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:

generate domainmodel "http://www.example.org/domainmodel/Domainmodel"

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.