Click here to Skip to main content
13,199,685 members (68,756 online)
Click here to Skip to main content
Add your own
alternative version


215 bookmarked
Posted 15 Jun 2008

Writing Your First Domain Specific Language, Part 1 of 2

, 26 Apr 2009
Rate this:
Please Sign up or sign in to vote.
A guide to writing a compiler in .NET for beginners, using Irony.


This two-part article is aimed at experienced C# .NET programmers who wish to write their own little computer languages (see part two here). Historically, this has been reasonably difficult due to requiring in-depth knowledge of compilation theory and/or the use of one or more tools, each of which had its own learning curves. Recently though, there has been somewhat of a revolution in this area, with tools being developed which greatly simplify the writing of compilers. The Irony Compiler Construction Toolkit for .NET is used in this tutorial due to the fact that it requires no configuration files etc. (Just drop the Irony DLL into your project) and it simplifies many aspects of compiler construction.

The Sample Problem

Imagine you are writing a content-management system for websites where your users can upload their own images. Now, imagine you have a client who wants to upload a large image, and then display only part of the image, but have the image scrolling around, like so:


And oh, here's the next thing: they want to be able to set up the way the "camera" scrolls across the image, and change it themselves. So, it is decided that the users will type instructions into a textbox to control the scrolling of the images. The language will look something like this:

Set camera size: 400 by 300 pixels.
Set camera position: 100, 100.
Move 200 pixels right.
Move 100 pixels up.
Move 250 pixels left.
Move 50 pixels down.

It's a far cry from C#, but having a language which basically reads like English will (hopefully) be appreciated by the users. This language is so simple that you could easily write your own parser that extracts the data from the string; however, as soon as the language gets a little more complicated (for example, if you introduce "if" statements and variables, as in part II of this article), then writing a bona-fide compiler will pay dividends.

So, how do we write a compiler for this language? Well, the first step is to formally describe the language.

Defining the Grammar

Looking at the language example above, we can say that a program starts with a line to set the camera size, then a line to set the initial position, and then one or more lines to move the camera. Each line ends with a '.' character. Most of the text is just fixed keywords (such as "Move" and "camera"), with the exception of numbers and directions ("up", "down", "left", "right"). The language can be written in upper-case or lower-case (or a combination).

The paragraph above explains the grammar; however, it is always a good idea to write this description in a more formal manner. A very common way to describe a grammar is using Backus–Naur Form, and the following is a slightly non-standard version of BNF describing the program:

<Program> ::= <CameraSize> <CameraPosition> <CommandList>
<CameraSize> ::= "set" "camera" "size" ":" <number> "by" <number> "pixels" "."
<CameraPosition> ::= "set" "camera" "position" ":" <number> "," <number> "."
<CommandList> ::= <Command>+
<Command> ::= "move" <number> "pixels" <Direction> "."
<Direction> ::= "up" | "down" | "left" | "right"

You can read "::=" as something like "is made up of", and "|" as "or". So, for example, a "Direction" is made up of one of four strings.

If you have written in BNF previously, you may be surprised to see "<Command>+", which uses the regular-expression convention to mean "one or more commands". This is not standard in grammars (or in BNF); however, Irony lets you do this (I will not go into the more complicated traditional way to achieve the same thing).

A grammar always ends up as a tree with a single root. For any (valid) program that we input into the compiler, a tree will be created with "Program" as the root node. This will have three children: "CameraSize", "CameraPosition", and "CommandList". This will continue down into the tree until the leaf nodes, which will be the keywords, numbers, and directions. The most important job of the compiler then is to convert the source code into a tree which is termed as an "Abstract Syntax Tree" (AST). So, based on the grammar above, with the following program...

Set camera size: 400 by 300 pixels.
Set camera position: 100, 100.
Move 200 pixels right.
Move 100 pixels up.

...we should get the following Abstract Syntax Tree:


Note that the blue nodes are called "Non-terminals" whereas the orange nodes are called "Terminals". The terminals correspond to the actual code the user has entered. While conventionally terminals such as "set", "camera", and "size" would come under the "CameraSize" node, when using Irony, you can ignore these non-useful terminals (more on this later).

Once we have a tree, we can then generate the code to execute the program. So, how do we do all this with Irony?

Writing the Grammar in Irony

After you have downloaded Irony, create a new project in Visual Studio, and add a reference to Irony.dll. Then, add a new class to define the grammar: CameraControlGrammar.cs will do.

After referencing the correct Irony namespace (using Irony.Compiler;), make your class inherit from the abstract "Grammar" class:

public class CameraControlGrammar : Grammar

The grammar of the language is defined in the constructor. The first part is to do some preparation for the actual grammar, and set a few options, for example, making it case-insensitive:

this.CaseSensitive = false;

Then, define all the terminals and non-terminals needed:

var program = new NonTerminal("program");
var cameraSize = new NonTerminal("cameraSize");
var cameraPosition = new NonTerminal("cameraPosition");
var commandList = new NonTerminal("commandList");
var command = new NonTerminal("command");
var direction = new NonTerminal("direction");
var number = new NumberLiteral("number");

And finally, specify which non-terminal is the root node in the abstract syntax tree:

this.Root = program;

OK, with the ingredients all cut up and prepared, we can start cooking. Defining the grammar in Irony is surprisingly similar to writing it in BNF form:

// <Program> ::= <CameraSize> <CameraPosition> <CommandList>
program.Rule = cameraSize + cameraPosition + commandList;

// <CameraSize> ::= "set" "camera" "size" ":" <number> "by" <number> "pixels" "."
cameraSize.Rule = Symbol("set") + "camera" + "size" + ":" + 
                  number + "by" + number + "pixels" + ".";

// <CameraPosition> ::= "set" "camera" "position" ":" <number> "," <number> "."
cameraPosition.Rule = Symbol("set") + "camera" + "position" + 
                      ":" + number + "," + number + ".";

// <CommandList> ::= <Command>+
commandList.Rule = MakePlusRule(commandList, null, command);

// <Command> ::= "move" <number> "pixels" <Direction> "."
command.Rule = Symbol("move") + number + "pixels" + direction + ".";

// <Direction> ::= "up" | "down" | "left" | "right"
direction.Rule = Symbol("up") | "down" | "left" | "right";

Irony has overloaded the "+" and "|" operators so that you can join together the different parts of the grammar in a very natural way. The only exception is where the first part of the rule is a string: in general, you should wrap the first string in a Symbol() method call so that the C# compiler knows that the string is actually a special Irony construct. Also note, rather than having "set camera size:" in a single string, we add each word separately, which allows any whitespace (including new lines) to go between each word.

Finally - and this is optional, but recommended - you can specify certain strings as "punctuation". On each line, there are lots of keywords that we don't actually care about once we are using the generated AST (for example, on each "move" line, we only care about the number of pixels and the direction). We can tell Irony that we don't want the AST cluttered with keywords by registering all the "punctuation":

this.RegisterPunctuation("set", "camera", "size", ":", 
        "by", "pixels", ".", "position", ",", "move");

This means a "Command" will have only two children (number and direction) instead of five children ("move", number, "pixels", direction, and ".").

Compiling the Source code

We are now ready to parse some code. This is done by passing an instance of your grammar to a LanguageCompiler object and getting a reference to the root node of the AST:

CameraControlGrammar grammar = new CameraControlGrammar();
LanguageCompiler compiler = new LanguageCompiler(grammar);
AstNode program = compiler.Parse("the source code as a string");

Assuming the source code successfully compiled, you can run this in debug mode and browse through the ChildNodes property of each node in the tree to view it. (See the attached project to see one way to handle invalid source code.)

Using an Abstract Syntax Tree

An elegant way to generate code from an AST is to write a class for each non-terminal node in the tree, and then each node in the tree simply generates the piece of code that it is responsible for. This is a very good pattern to use, especially when dealing with more complex languages; however, to keep things relatively simple, I will simply act directly on the returned AST (for an example which uses classes to generate code, see JSBasic which converts from BASIC to JavaScript).

Examining the tree above, we know that the first child node of the tree is the camera size declaration, which should have two children itself: the width and the height of the camera:

AstNode cameraSizeNode = program.ChildNodes[0];
Token widthToken = (Token)cameraSizeNode.ChildNodes[0];
int width = (int)widthToken.Value;
Token heightToken = (Token)cameraSizeNode.ChildNodes[1];
int height = (int)heightToken.Value;

Note that the actual number entered is considered a Token (in fact, each piece of text in the source code is a Token); however, not all ChildNodes are Tokens (e.g., "CommandList" is a ChildNode of Program; however, it is a non-terminal, so not a token). This is why we must convert the children to Token objects before accessing their values.

A similar deal is used to get the initial position. These camera sizes and positional values are used in the demo project to create some JavaScript which initialises the photo. (The actual JavaScript is not explained here as it is outside the scope of this article.)

Next, we know that the third child of the program is a list of Command nodes, which we can then loop through:

// loop through the movement commands
foreach (AstNode commandNode in program.ChildNodes[2].ChildNodes)
    // get the number of pixels to move
    Token pixelsToken = (Token)commandNode.ChildNodes[0];
    int pixelsToMove = (int)pixelsToken.Value;
    // get the direction
    Token directionToken = (Token)commandNode.ChildNodes[1];
    string directionText = directionToken.Text.ToLower();

Using the pixelsToMove and directionText values, JavaScript is generated for each command in the program. Again, this is left out as it isn't important in terms of how to write your language.

And We're Done

And that's all there is to it, really. Of course, once you introduce variables, branch and conditional statements etcetera, you would probably want to look into creating your own AST node classes and look at the AstNode.Evaluate method; however, that is for another tutorial, along with many other Irony features which have been left out in this tutorial. Hopefully though, this should give you enough information to allow you to write your own simple little languages.

And don't forget to try the online demo.

Useful Links


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Daniel Flower
Software Developer
China China
Daniel has a Bachelor of Science with First Class Honours from the University of Auckland, and has designed and developed software in companies large and small.

You may also be interested in...


Comments and Discussions

GeneralRe: Excellent article Pin
RugbyLeague17-Jun-08 3:47
memberRugbyLeague17-Jun-08 3:47 
GeneralIrony can surely handle this Pin
Roman Ivantsov17-Jun-08 7:43
memberRoman Ivantsov17-Jun-08 7:43 
GeneralRe: Irony can surely handle this Pin
RugbyLeague25-Jun-08 0:14
memberRugbyLeague25-Jun-08 0:14 
GeneralCongratulation! Pin
Drucik_16-Jun-08 0:01
memberDrucik_16-Jun-08 0:01 
GeneralRe: Congratulation! Pin
Daniel Flower16-Jun-08 2:01
memberDaniel Flower16-Jun-08 2:01 
Generalneato Pin
Sacha Barber15-Jun-08 22:38
mvpSacha Barber15-Jun-08 22:38 
GeneralRe: neato Pin
Daniel Flower16-Jun-08 1:58
memberDaniel Flower16-Jun-08 1:58 
GeneralRe: neato Pin
Sacha Barber16-Jun-08 3:16
mvpSacha Barber16-Jun-08 3:16 
Ah im just a chap doing stuff like everyone else, I am nothing special...Just learning really. This is cool, so I said so thats all.

Credit where its due man

Sacha Barber
  • Microsoft Visual C# MVP 2008
  • Codeproject MVP 2008
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog :

GeneralRe: neato Pin
Daniel Flower17-Jun-08 2:18
memberDaniel Flower17-Jun-08 2:18 
GeneralRe: neato Pin
Roger Alsing17-Jun-08 8:36
memberRoger Alsing17-Jun-08 8:36 
GeneralRe: neato Pin
Sacha Barber17-Jun-08 9:09
mvpSacha Barber17-Jun-08 9:09 
GeneralAwsome Pin
Simon Stevens15-Jun-08 22:02
memberSimon Stevens15-Jun-08 22:02 
GeneralRe: Awsome Pin
Daniel Flower16-Jun-08 1:57
memberDaniel Flower16-Jun-08 1:57 
GeneralCongratulations Pin
Ed.Poore15-Jun-08 13:18
memberEd.Poore15-Jun-08 13:18 
GeneralRe: Congratulations Pin
Daniel Flower16-Jun-08 1:55
memberDaniel Flower16-Jun-08 1:55 
GeneralRe: Congratulations Pin
Ed.Poore16-Jun-08 3:06
memberEd.Poore16-Jun-08 3:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 26 Apr 2009
Article Copyright 2008 by Daniel Flower
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid