DSLs, Compilers and the Irony of it All

In a previous post, I posted about the Lang.NET symposium and rolling your own compiler.  I cited an MSDN article that was a good starter for someone interested in writing that new language in .NET we've all been waiting for.  It's not by any means to get you towards finishing IronRuby by yourself, but some of the fundamentals are covered.

The reason I've stated that I'm pretty interested is that of course I'm a geek, but I'm also interested in Domain Specific Languages (DSLs) lately as you may notice from my blog and what you can do with compilers to help that along.

DSLs in General

So, why DSLs and why am I interested?  I think Oren Eini, aka Ayende laid the reasons out well here and here.  In short he covers quite a few scenarios in which you might be interested in them, such as:
  • Mapping layers for DTO/Domain objects from incoming objects
  • Configuration scripting for IoC containers and so on
  • Business situations and Business Rule Engines
And the list can go on.  In fact, he's actually writing a book about it, if you've been hiding under a rock somewhere...

Ayende has been quite busy with the DSL posts that I encourage you to check out.  If you're looking for basic samples, check out these posts on Building an External DSL, and Implementing a DSL.  As you may note, most samples are written in Boo, but you could imagine them written in any language for that matter in the .NET space.

Compiler Basics

Now that we're through that section, let's take a step back to compilers in general.  If you're interested in these things, I would suggest the Compilers: Principles, Techniques, and Tools aka the Dragon Books.  Even though the first one was published back in 1977, there has been the Purple Edition released back in 2006.  There are several sample chapters available online if you wish.  This will give you a good foundation on how compilers work and give you more of a headstart than just the simple MSDN sample. 

The Irony Of It All

Greg Young pointed me towards Irony by Roman Ivantsov which is available off CodePlex.  If you paid attention to the Lang.NET 2008 symposium, Irony  and language challenges in ERP systems were covered as noted here.
 
Off the site, it explains:

Irony is a new-generation .NET compiler construction kit. It utilizes the full potential of c# 2.0 and .NET Framework to implement a completely new and streamlined technology of compiler construction.
Unlike most existing yacc/lex-style solutions Irony does not employ any scanner or parser code generation from grammar specifications written in proprietary meta-language. In Irony the target language grammar is coded directly in c# using operator overloading to express grammar constructs. Irony's scanner and parser modules use the grammar encoded as c# class to control the parsing process.

So, basically it takes the idea from that MSDN article and raises it to a whole new level.  And best of all, it's written in C#, so it opens compilers to a different audience instead.  For a full walkthrough of the possibilities, check out the article posted on CodeProject.  What's really cool is that he's opened it up for anyone to contribute to should you find something missing or not quite right.

Greg brings out an idea of using Abstract Syntax Trees (ASTs) inside custom attributes to bring more of Spec# to the .NET framework.  He posted this simple sample of some of the possibilities:

private void MyMethod( [Requires("value>5 and value<10")] int i) { ... }

I think it's an interesting approach and I'm going to have to try it out.  I can't turn down a challenge like this.

Conclusion

So, as I continue on the journey of F#, DSLs and compilers, these things come to light and I find even after all these years, I'm still learning something new every day.  I hope you found this shallow dive into the deep end interesting into this geekdom.  Well, that and after going through some of these samples that my old lex/yacc skills aren't quite what they used to be.  With some time and perseverance, maybe it will...

And yes, I'll be getting back to some code samples soon enough.  Until next time...

kick it on DotNetKicks.com

No Comments