Wednesday, April 16, 2008

No reason to laugh at Irony

Being a DSL geek I just want to give a shout out to Roman Ivantsovs Irony project. Irony is a LALR parser generator that lets you write your grammar in C#. If you’re looking to build your own little Domain Specific Language, Irony is a good alternative to ANTLR andGold Parser Builder.

A grammar written in C# with Irony looks as much like EBNF notation as possible given the constraints that writing them in C# imposes. For example, this definition of three non terminals in EBNF:

Expr  ::=  n | v | Expr BinOp Expr | UnOp Expr | '(' Expr ')'
BinOP ::=  '+' | '-' | '*' | '/' | '**'
UnOp  ::=  '-'

…translates into this C# code for an Irony grammar:

Expr.Rule = n | v | Expr + BinOp + Expr |
           UnOp + Expr | "(" + Expr + ")";
BinOp.Rule = Symbol("+") | "-" | "*" | "/" | "**";
UnOp.Rule = Symbol("-"); 

(Add to this a line for declaring each of the three non terminal variables - I left them out to prove a point ;-)

The syntax of the C# and the ANTLR versions are impressively similar, though there still is some noise left. It’s not as pretty as what Ghilad Bracha can do in Newspeak, but then again, C# wins by being a language that is actually used by developers.

Gold Parser Builder and ANTLR may be more mature than Irony, but what I like the most about the latter is the fact that the grammar is compiled along with the application that uses the grammar to parse DSL code. That means less switching between Visual Studio and ANTLR og Gold. Less fuss. Shorter path from language design to language test to language use.

The abstract syntax three (AST) that my Irony generated parser makes is nice and clean. It can even filter out punctuation characters, so that for instance parenthesis do not make up nodes themselves, only the expression within the parenthesis is a node in the tree.

That’s it for the extremely brief introduction to Irony. You can check out some more detailed samples by downloading. Irony is definitely a part of my toolbox so stay tuned for more on Irony.

One Response to 'No reason to laugh at Irony'

Subscribe to comments with RSS or TrackBack to 'No reason to laugh at Irony'.

  1. Soren On Software » Looking for an open source text editor component with syntax highlighting said,

    ON APRIL 17TH, 2008 AT 10:27 PM

    […] trying to give a little back to an open source project, namely Irony that I blogged about. My idea was this: We’re getting used to intellisense features in mainstream .NET languages […]

No comments:

Post a Comment