Translate C# code into AST?

13,315

Solution 1

The Roslyn project is in Visual Studio 2010 and gives you programmatic access to the Syntax Tree, among other things.

SyntaxTree tree = SyntaxTree.ParseCompilationUnit(
    @" C# code here ");
var root = (CompilationUnitSyntax)tree.Root;

Solution 2

Is it currently possible to translate C# code into an Abstract Syntax Tree?

Yes, trivially in special circumstances (= using the new Expressions framework):

// Requires 'using System.Linq.Expressions;'
Expression<Func<int, int>> f = x => x * 2;

This creates an expression tree for the lambda, i.e. a function taking an int and returning the double. You can modify the expression tree by using the Expressions framework (= the classes from in that namespace) and then compile it at run-time:

var newBody = Expression.Add(f.Body, Expression.Constant(1));
f = Expression.Lambda<Func<int, int>>(newBody, f.Parameters);
var compiled = f.Compile();
Console.WriteLine(compiled(5)); // Result: 11

Notice that all expressions are immutable so they have to be built anew by composition. In this case, I've prepended an addition of 1.

Notice that these expression trees only work on real expressions i.e. content found in a C# function. You can't get syntax trees for higher constructs such as classes this way. Use the CodeDom framework for these.

Solution 3

Check out .NET CodeDom support. There is an old article on code project for a C# CodeDOM parser, but it won't support the new language features.

There is also supposed to be support in #develop for generating a CodeDom tree from C# source code according to this posting.

Solution 4

There is much powerful than R# project. Nemerle.Peg:

https://code.google.com/p/nemerle/source/browse/nemerle/trunk/snippets/peg-parser/

And it has C# Parser which parsers all C# code and translates it to AST !

https://code.google.com/p/nemerle/source/browse/nemerle/trunk/snippets/csharp-parser/

You can download installer here: https://code.google.com/p/nemerle/

Solution 5

Personally, I would use NRefactory, which is free, open source and gains popularity.

Share:
13,315

Related videos on Youtube

Erik Forbes
Author by

Erik Forbes

Updated on April 19, 2022

Comments

  • Erik Forbes
    Erik Forbes about 2 years

    Is it currently possible to translate C# code into an Abstract Syntax Tree?

    Edit: some clarification; I don't necessarily expect the compiler to generate the AST for me - a parser would be fine, although I'd like to use something "official." Lambda expressions are unfortunately not going to be sufficient given they don't allow me to use statement bodies, which is what I'm looking for.

  • DucDigital
    DucDigital over 15 years
    I've used ANTLR in the past, and it's quite nice. I haven't used the C# grammar, but most of the contributors there are pretty cluey.
  • yeeen
    yeeen over 14 years
    This is helpful to see what C# don't offer a library for us to manipulate C# API. It is due to it's compiler is a classical one, a black box!
  • Ira Baxter
    Ira Baxter over 14 years
    Erik accepted this? It uses the very lambda forms he said he didn't want.
  • Konrad Rudolph
    Konrad Rudolph over 14 years
    Ira: you should pay attention to the development of the discussion. This entry was posted before Erik’s edit/clarification. Apparently, none of the other answers were better at the time (notice: one year ago!) so he didn’t accept another answer. Your answer is probably what he would have wanted.
  • Ira Baxter
    Ira Baxter over 13 years
    @Cheeso: Hmm, 2004 would mean we scooped MS. Well, it never do to suggest that, so I modified it say 2010. Fixed.
  • NN_
    NN_ almost 13 years
    Moreover. Nemerle can compile C# sources using Nemerle compiler ! :)