Question

I am working on a Reverse Engineering school project, which requires to translate manipulate AST of compiled C# project. I have seen the post on "Translate C# code into AST?" in this website, but it doesn't look like the one I am looking for.

According to what I know, currently C# doesn't provide a library class that does something like that for Java: http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.cdt.doc.isv/reference/api/org/eclipse/cdt/core/dom/ast/ASTVisitor.html. If there is such library class in C#, everything here is solved.

I have consulted with someone, and here are the possible solutions. But I have problems with working out on the solutions as well:

  1. Find another compiler that provides a library which allows its AST to be expose for manipulation. But I can't find a compiler like that.
  2. Use ANTLR Parser Generator to come out with my own compiler that does that (it will be a much more difficult and longer process). The download there provides sample grammars for different languages but not C# (it has grammars written in various languages including C# but not to produce C# grammar). Hence the problem is I can't find C# grammar.

What is shortest and fastest way to approach this issue? If I really have to take one of the alternative above, how should I go about solving those problems I faced.

Was it helpful?

Solution 3

ANTLR is not a good choice. I am now trying out using Mono Cecil instead. Mono Cecil is good for analyzing any souce codes that can be compiled into Common Intermediate Language (CIL). The disadvantage is that it doesn't have properly documentation.

OTHER TIPS

I know the answer for this one was accepted long ago. But I had a similar question and wasn't sure of the options out there. I did a little investigation of the NRefactory library that ships as part of SharpDevelop. It does generate an AST from C# code.

Here's an image of the NRefactory demo application that is part of the SD source code. Type in some C# code and it generates and displays the AST in a treeview.

alt text

Why don't you try NRefectory. I've seen it discussed for AST thing on some SharepDevelop forums.

Here is an article on CodeProject regarding this topic.

I've just answered on another thread here at StackOverflow a solution where I implemented an API to create and manipulate AST from C# Source Code

A full C# 3.0 parser is available with our DMS Software Reengineering Toolkit (DMS for short). It has been used to process tens of thousands of C# files accurately. It provides automated AST building, tree traversals, surface-syntax pattern matching and transformation and lots more. As a commercial product it might not work out for a student project.

ANTLR arguably offers a C# parser, but I don't know complete or robust it is, or whether it actually builds ASTs.

[EDIT Jan 25 2010: C# 4.0 parser now available for DMS with all the above properties]

[EDIT May 2016: C# 6.0 parser available for DMS.]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top