Meine Blogeinträge! Durchgeführte Projekte! Meine Interessen! MiCasaIsSuCasa! This is me!
MYBLOG

"The programmer, like the poet, works only slightly removed from pure thought-stuff.
He builds his castles in the air, from air, creating by exertion of the imagination.

Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.
- Frederick P. Brooks

Mini-Languages erstellt am 01.02.2009

Last week I was at the OOP 2009 in Munich. There I met a friend with who I talked about domain-specific languages. He thought critical about this topic and the theory for him seems to be clear, but he wanted an easy to understand example of an implementation.  In this article I will talk about the usefulness of mini languages and show you how to implement one.

If you have programmed much since yet, you would have probably recognized the change of the way you think about solutions of your problem. What all programmers do, using a general purpose language, is that they convert their problem description to a description in their language. This means that computer languages influence how we think about a problem, and how we think about communicating. Every language has its own features and therefore designing a solution will produce different results.

When you listen to stakeholder talking about requirements of a proposed system, you will notice that they are using their own vocabulary. In object oriented programming, we always try to write code using the vocabulary of the problem domain (naming of classes, methods etc.). In some cases we can  go further to the next level and actually program using the vocabulary, syntax and semantic - the language - of the domain.

Imagine you got some requirements of a proposed system. A user tells you exactly how the system works.

"The machine is listening for incoming tasks forcing it to draw lines with a specified pencil. The task description firstly explains what pencil to select and what to draw. To begin drawing lines the pencil has to be put down. Afterwards you the pencil has also to be put up.”

As you probably recognize there will be a number of such well-bounded statements to describe a drawing task. Therefore you can invent a mini language tailored to the application domain that expresses exactly what you want. Drawing a something could look like this:

SELECT_PEN 2
PUT_YOUR_PEN_DOWN
DRAW_LEFT 2
DRAW_DOWN 2
DRAW_RIGHT 2
DRAW_UP 2
PUT_YOUR_PEN_UP

This will make your application more flexible by being an interpreter of a language instead of hard coding these functions in your application. To convincingly show you that interpreting such a language isn’t much overhead and could be really valuable I created a code example at its simplest by using a line-oriented, easily parsed format. Each line consists only of a command and when needed a parameter.

<command> <parameter>

The example application is built in C# and uses WinForms for the user interface. At first I will discuss the parser, which is responsible for analyzing input data. Afterwards I will go over to the interpreter, which processes data and looks up the implementation of the language command.
Because I want to make the language extendable the parser is table driven. Each entry in the table contains the name of the command, a flag to say whether an argument is required, and the delegate to the concrete implementation, which handles the particular command.

The parser is pretty easy, it reads a line, looks up a command, watches for an argument if required and then calls the handler function.

The function that looks up a command performs a linear search of the table, returning either the matching entry or null.

For now, that’s it. I suggest, download the code and try it yourself. If you have any further questions don’t hesitate to contact me under denis.dwornitzak@studentpartners.de.


1 Kommentar
 
  Kommentar hinzufügen | Permalink  


Anzeigename: deSiriX-oNe
Realname: Denis Dwornitzak
Studiengang:
Software-Engineering

Kontakt: E-Mail

Weitere Informationen gibt's hier...
 
Die letzten Einträge
 
Links
 
Terminübersicht

C# 3.0 und LINQ

Datum: 24.09.2009 - 09:00 Uhr
Dauer: ca. 4 Std.
Ort: HS Esslingen, Geb.1 Raum F1.205
Referent: Denis Dwornitzak, Andreas Maier

Downloads: Präsentation und Materialien

Windows Presentation Foundation

Datum: 25.09.2009 - 09:00 Uhr
Dauer: ca. 4 Std.
Ort: HS Esslingen, Geb.1 Raum F1.205
Referent: Denis Dwornitzak, Andreas Maier

Downloads: Präsentation und Materialien

The almighty .NET language - C++/CLI

Datum: 30.05.2009 - 08:30 Uhr
Dauer: ca. 4 Std.
Ort: HS Heilbronn im Raum F025
Referent: Denis Dwornitzak, Andreas Maier

Downloads: Präsentation und Materialien

Windows Presentation Foundation

Datum: 02.03.2009 - 08:30 Uhr
Dauer: ca. 4 Std.
Ort: HS Heilbronn im Raum F025
Referent: Denis Dwornitzak, Andreas Maier

Downloads: Präsentation und Materialien