Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing a DSL for the .Net platform

Tags:

c#

.net

I am thinking of writing a DSL to be run in the .Net environment. I am most likely to implement this in C#, though I am flexible on this.

Is there a recommended online resources that show the main steps involved in writing a DSL for the .Net platform?

Ideally, I would like a resource that would provide at least an overview on the following:

  • 'Spec'ing a DSL
  • How to map the specs to the .Net framework
  • Preferably a helloworld example of a trivial DSL implemented in a .Net language

[Edit]

Actually, I have just seen this article - but it is slightly dated. Does anyone have a view on whether the article is a good starting point or not (the .Net framework and C# seem to evolve at a very rapid pace)

like image 522
morpheous Avatar asked Sep 03 '10 10:09

morpheous


2 Answers

If you are willing to buy a book on the topic, I highly recommend "DSLs in Boo: Domain Specific Languages in .NET" by Ayende Rahien. Very informative and gently takes you through the process of writing a DSL. The author uses a lightweight .NET language called Boo to serve as basis for the DSL's syntax.

Also you can look into VS2012 corner:

  • Microsoft Visual Studio 2012 Visualization & Modeling SDK
  • Microsoft Visual Studio 2010 Visualization & Modeling SDK
like image 94
Mohammad Avatar answered Oct 01 '22 08:10

Mohammad


There's a bunch of different solutions you could use, including the article you linked, but some other examples from MS...

  • FsLex/FsYacc - Ports of the popular Lex and Yacc lexer/parsers for F#, but don't be turned off right away. If you've not used it before, F# has a feature called "pattern matching", which allows you to match very complex constructs (such as walking a tree), without an extensive amount of if/else/or blocks all over. This is perfectly suited to language compiling - because almost all DSL solutions you will find will work by parsing the language into an AST (Abstract syntax tree). In this F# solution, you get a strongly typed tree to work with. You can grab the F# Parsed Language Started to get you going. (There's plenty of existing grammars for Lex/Yacc that can help you out too).

  • SQL Server Modeling Tools (formerly "Oslo") - Contains a language called M, formerly broken into several parts, one being MGrammar. It's quite an advanced parser and can save you plenty of time over other grammars - code patterns (or generic grammar rules) and precedence are built in and easy to use. I would perhaps recommend this if you're starting out with parsing, because it comes with a visual tool - Intellipad, which has a 3-panel DSL mode, where you type in your language and some example code - and it'll show you the output AST as you type - it's quite productive to use. The generated AST is a valid M language constructor (MGraph), which can be used with services like SQL and XML. A downside to MGrammar IMO, is that walking the AST from C# or elsewhere is a tiresome process. There's nothing strongly typed, and you're working with objects and searching with strings - awkward and easy to make mistakes. There's some samples on msdn, and some vids on channel9 which can help you get started like this lengthy overview

  • The Visualization and Modeling SDK - An entire solution built into VS, which focuses largely on building your with Visual Studio's design tools over code. It comes with a minimum language starter template to help you. Haven't any experience with this to recommend it.

There's plenty of other non-MS solutions, like the one you've mentioned, C# targets for ANTLR etc. These are particularly useful if you're re-using existing grammars - because there's dozens already out there.

like image 26
Mark H Avatar answered Oct 01 '22 08:10

Mark H