This project is read-only.

Executing C# code on DLR

Jan 7, 2010 at 5:52 AM


I am creating a DSL to do functional testing of browser based applications. So my sample language file/text looks like


Open page ''

Type 'username' in textbox 'txtuser'

Click on button 'btnSubmit'


Each line of grammar is to be translated/executed as multiple C# lines, for example

Open page ''

should be translated/executed as

MyBrowser obj = new MyBrowser();



Any idea how to implement such language into DLR, so that in my language I can also directly call C# functions.



Jan 7, 2010 at 7:28 AM

The best thing you can do is just to read the documentation, first knock of the introduction and explanation of the DLR and then follow the sympl.doc tutorial where .NET InterOp is explain in detail during the first 10 chapters.

Jan 7, 2010 at 3:30 PM

Thank fholm for the reply. I have gone through the documents and still confused on how to build DRL AST for my language. My language is not to be translated as single line expression, but each line written in my language needs to be translated into multiple lines of C# code, then this lines of c# code has to be executed. I am not getting how to build DLR AST for this.


Can you please help in prodiing high level steps/pseduo code for how to generate AST for this. Anything that can help me to move forward will be highly appreaciated. As of now I am totally stuck



Jan 7, 2010 at 3:41 PM

If this is what you're after I don't think that the DLR fits your needs, the DLR does not have any form of lexer or parser built in. What you need to do is build a custom grammar (in say and then translate that AST into C# code. The DLR fits nowhere here really.

Jan 7, 2010 at 7:13 PM

Another good method for generating your AST is to use gplex and gppg - they are equivalent to gnu's lex and yacc respectively.They are written in C#, and MS uses the code in Babel (

gplex =>

gppg =>

To get my language started I used this: - the source is here:

This doesn't work with the current DLR - mass hacking, slashing, burning and looting is required.

It'll get you started on how to write a grammar and wire it up to the DLR, then compile and invoke.

You'll also have to learn the LOL language in the process.

Someday someone will write an article on how to wire this all together, but until then it can be a bit confusing for someone that hasn't used lex/yac and/or DLR.

good luck, and don't even think about doing any of this without reading the documentation.

Jan 7, 2010 at 7:53 PM

Have you considered using an existing language (e.g. IronRuby or IronPython) for this task? Ruby is particularly known for its flexibility that allows you to write Ruby expressions that look like your own language.
If you wish to write the test cases in a language that is even closer to English you can also try Cucumber test framework (see e.g. It is much easier to learn Ruby than write a new language from scratch.


Jan 7, 2010 at 8:13 PM

1.  Since I am creating a DSL and not as general purpose programming language but as tasks/actions that users perform, language statements are more abstract/complex and don't transform to a single expression. Users will  code functionality in C#, and only some of the business logic part will be coded in DSL. So I need a way to inovke/execute my DSL statements from C# code and vice-versa. I thought DLR is the option for interopability between languages.

2. I am trying to mix DLR and OLSO as per the suggestions/samples from

and So the lexer and parser will be provided by OSLO, which will validate and generate AST.

3. My language will be very small/easy one is not going to support all scenarios for browser based testing. Can say its more for learning DSL/DLR/OSLO


Jan 7, 2010 at 8:39 PM

So I assume you already have an AST for your language,with nodes like

class OpenPageCommand : Command { string Url; } that represents [Open page '<url>'] command,

class EnterTextCommand : Command { string Text; ControlKind ControlKind; string ControlName; } for [Type '<text>' in <kind> '<name>'],

class ClickCommand : Command { ControlKind ControlKind; string ControlName; } for [Click on <kind> '<name>'] and

class CommandList { List<Command> Commands; } for a collection of commands.

If all commands look like this you can write a simple interpreter for this AST - define a virtual method Run on Command and implement it on all your nodes. E.g. Run on OpenPageCommand would be implemented as

override Run() { MyBrowser obj = new MyBrowser(); obj.Navigate(Url) }

You can indeed transform your AST to a DLR AST, compile it to IL and run it but so far I don't see any features of your language that would benefit from this approach. Does you language have variables, control flow or arbitrary method invocations?

Jan 8, 2010 at 5:09 AM

1. Yes Tomas, I am able to write an interpreter for AST nodes and run my DSL.

2. No, language will not be having variables. Its more of "Write tasks that user perform in a high level english like text". Its not a programming language.

3. Why I am looking into DLR - Language Interoperability.

My language will a simple one, so user can't write all scenario in that language. There should be way by which  users can use c#, my language or any other languge to perform there task.

for example User have to "write Web Test in visual studio". Instead of creating a complex langugae which generates a VS web test dll, what i want is that user create a Web Test project from VS and for every [testmethod] they can somehow call code written in my DSL (in a clean syntax not like File.Load().Execute). SO to C# code it should just look like a function call. Similary in my languge if user want to do tracing they can write code  like



I hope I was able to clear my requirement.

Please guide me if DLR is the correct approach for this language interoperability.



Jan 8, 2010 at 10:38 PM

DLR language interop will allow you to get/set/invoke methods/properties on your language objects from other languages like C#. If that’s the sort of interop you’re looking for, then yes DLR is the right tool for the job!

As other folks have said Sympl is a great place to start. You should also take a look at DynamicObject. It’s an easy way to create objects with dynamic behavior that can be invoked from C# using the “dynamic” keyword and normal C# syntax.

- John