Code generation

Transforming a DSL to a common language. For example JSP, jsp source files are transformed to a java servlet source and compiled at runtime.

Parser Generators like JavaCC, Antlr or Yacc & Lexx can be used to do this line of work. Actually these tools use a EBNF or simular notation which is a dsl on it's own.

< an antlr example, here>

Byte code generation

Probably a more populair approach, java classes straight from the languages source. Java class files are in no way linked to the java language, classes can be generated with any source language. In fact many alternative languages (scripting) already exist.

Your compiled dsl will act as a normal java program and enjoy the benefits that a jvm offers. e.g: hotspot, garbage collection, etc.

One could write byte code generators by hand but this may a bit to tendious. Lucky several frameworks exists that let you construct/alter classes:


  • ObjectWeb ASM
  • Apache's BCEL
  • cglib
  • Kimbly Classfile Reader & Writer
  • Cojen
  • Javassist from Jboss
  • JBET
  • JClassLib from ej-Technologies
  • Jiapi
  • Package gnu.bytecode
  • SERP
  • Trove

< maybe an asm example here >

String parsing

When the DSL is simple and liniar, it can be simply parsed executed. Using the StreamTokenizer for example.

< smaller example here >


Instead of writing one big language, several smaller compose one big one, using the chain of reponsibility pattern.

An XHTML document is built from several distinct languages. Each part of the document maybe parsed and executed by another engine. Even type of languages which where not known when the XHTML specification where written. For example apart from the standard html languages (javascript, css) The lastest version of Mozilla Firefox supports SVG, XSLT, MATH ML, XLink embeded in a XHTML document. XML namespaces are exactly meant for that.

<?xml version="1.0"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
    <script type="text/javascript">
      function change_style(style_string)
      document.getElementById("circ").setAttribute("style", style_string);
    <button onclick="change_style('fill:red;');">red</button>
    <button onclick="change_style('fill:blue;');">blue</button>
    <svg xmlns="" height="200">
      <circle cx="100" cy="100" r="1cm" id="circ" style="fill:red;"/>

When the example rendered by Mozilla Firefox the html would be groked by Gecko, Spider Monkey would pick out the javascript and SVG engine render the SVG.


If the language is more a runtime language for the system than a DSL.

Done by extending an existing scripting engine with an api and embedding it into the host application.

Consider for example javascript in a web browser which is almost GPL with a specialised dom api. With rihno (java scripting engine written in java) from mozilla you could do the same with your own application. Would replace the dom api with an your own api.

< embedded rihno example >

Other scripting engines:

  • Groovy
  • Jython
  • JRuby
  • Commons-el
  • etc


Using a existing extendable language which describe structure and syntax. XML is good example of this approach.

Benefit for this approach that the toolset and knowledge inherited.


Instead defining a whole new language, a subset of an existing language is taken to from the base of a new DSL. For example clips rule engine(Jess for java) which is uses a subset from lisp.


An API can also been seen as a DSL.

Domain Specific Modeling

Graphical models are also languages. In a domain specific model the elements of the model are specific to that domain consider UML for example. These models can also take applicative forms.

Language Workbench

A language workbench is complete toolset to built DSL's with all the features that we expect from modern IDE's. Consider for example Meta Programming System (MPS) which is a language workbench from Jetbrain (creators of IDEA IntelliJ). These tools would reduce the cost to develop your own DSL to a minimum.


Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-4) was last changed on 12-Oct-2005 12:32 by