Writing a domain specific language in c#

Download Visual Studio solution - NET programmers who wish to write their own little computer languages see part two here.

Writing a domain specific language in c#

Download Visual Studio solution - NET programmers who wish to write their own little computer languages see part two here.

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: 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: Move pixels right. Move pixels up.

writing a domain specific language in c#

Move pixels left. Move 50 pixels down. It's a far cry from Cbut 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 articlethen 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 '. 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 Formand the following is a slightly non-standard version of BNF describing the program:Domain Specific Language is a term that could indicate different things. To me there are two families of domain-specific language: internal DSLs they basically are fluent interfaces.

You just use your language and try to create classes, functions and metaprogramming feature to create a way to express more clearly your intent.

The Domain-Specific Language Designer Wizard appears.

This article was published in:

Note Preferably, the name that you type should be a valid Visual C# identifier, because it might be used to generate code. It's deployed though a series of method calls whose arguments are either common language primitives (string, double, etc.), Collections (IEnumerable, HashSet, ) or objects in a domain-specific library (CMLMolecule, Point3, RealSquareMatrix).

Domain Specific Languages are languages created to support a particular set of tasks, as they are performed in a specific domain. You could be familiar with the typical programming languages (a.k.a. General Programming Languages or GPLs). You may also be unfamiliar with the term Domain Specific Language (usually abbreviated to DSL).

It is a limited form of computer language designed for a specific class of problems. It is a limited form of computer language designed for a specific class of problems.

writing a domain specific language in c#

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.

Getting Started with Domain-Specific Languages - Visual Studio | Microsoft Docs