This project has moved and is read-only. For the latest updates, please go here.

Using Irony to build C# AST tree

Feb 10, 2011 at 6:55 AM

   Hi all! First of all I wanted to thank Roman for creating such a wonderful kit as Irony is. I know my question may sound stupid but I am not that good at formal languages. I've looked through the forum but haven't found a definite answer to my problem.

   I am planning to implement an algorithm of finding code clones and I need AST tree as an input. As I've mentioned I am not that good at formal languages. I've read source code  and I've found something like that:

 public LanguageFlags LanguageFlags = LanguageFlags.CreateAst;


Anyhow, I've intuitively understood that  the grammar should be written in a way to support AST. I'm not capable of rewriting it with my current knowledge. Can you either correct my reasonings or recommend my further actions as 1) Some existant AST building grammars, 2) The best way to learn the knowledge to write my own grammar, 3) Some other tools/kits.


Thank you for reading my post

Feb 10, 2011 at 6:31 PM

Thanks! AST - first of all I would advise to read a bit, some intro articles into what AST is - wikipedia would be a good start. 

Irony provides facilities for building AST trees, one example is expression evaluator. It also well explained and illustrated here:


Feb 11, 2011 at 7:42 PM

Thanks for your reply. I've read the article, I've analized it. I've tried to build a sample project trying to get AST tree in it. But as far as I've understood there is no possibility of creating ASTtree without modifying the grammar provided in 020.Irony.Samples.2010. So I want to make sure there is no problem about such code if the grammar supports AST tree creation:

CSharpGrammar g = new CSharpGrammar();
Parser p = new Parser(g);
ParseTree t = p.Parse(textBox1.Text);
var astnode = (AstNode)t.Root.AstNode;
astnode.AcceptVisitor(new PrintVisitor());

Is there any C# grammar supporting AST trees?

Feb 11, 2011 at 8:36 PM

First of all, are you sure you need AST tree? maybe Parse Tree (that Irony produces first) is enough for you?

For AST tree you have to create custom AST node classes for every grammar construct you have in the language; Irony-provided AST classes cover only expression evaluator language; for c# you have a lot of work to do, yourself. Have a second look at what is your goal, maybe parse tree is enough.


Feb 12, 2011 at 5:05 PM
Edited Feb 12, 2011 at 5:06 PM

Thank you for explanations! I've finally understood my future tasks. Go on with your project, Roman, I really admire such individual projects which grow to become massive.