17

目前是否可以将 C# 代码转换为抽象语法树?

编辑:一些澄清;我不一定希望编译器为我生成 AST - 解析器会很好,尽管我想使用“官方”的东西。不幸的是,Lambda 表达式是不够的,因为它们不允许我使用我正在寻找的语句体。

4

12 回答 12

19

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;
于 2011-10-27T17:32:37.017 回答
12

目前是否可以将 C# 代码转换为抽象语法树?

是的,在特殊情况下微不足道(= 使用新的表达式框架):

// 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.

于 2008-10-17T19:20:53.440 回答
6

查看 .NET CodeDom支持。有一篇关于C# CodeDOM 解析器的代码项目的旧文章,但它不支持新的语言功能。

根据此贴文,#develop 中也应该支持从 C# 源代码生成 CodeDom

于 2008-10-17T19:13:28.807 回答
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/

于 2011-03-08T09:19:24.753 回答
3

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

于 2012-05-06T21:31:11.013 回答
2

It looks like this sort of functionality will be included with whatever comes after C# 4, according to Anders Hejlsberg's 'Future of C#' PDC video.

于 2008-11-18T18:12:35.047 回答
2

The ANTLR Parser Generator has a grammar for C# 3.0 which covers everything except for LINQ syntax.

于 2008-11-25T21:49:52.547 回答
2

ANTLR is not very useful. LINQ is not what you want.

Try Mono.Cecil! http://www.mono-project.com/Cecil

It is used in many projects, including NDepend! http://www.ndepend.com/

于 2009-10-04T06:47:40.377 回答
1

Our C# front end for DMS parses full C# 3.0 including LINQ and produces ASTs. DMS in fact is an ecosystem for analyzing/transforming source code using ASTs for front-end provided langauges.

EDIT 3/10/2010: ... Now handles full C# 4.0

EDIT: 6/27/2014: Handles C# 5.0 since quite awhile.

EDIT: 6/15/2016: Handles C# 6.0. See https://stackoverflow.com/a/37847714/120163 for a sample AST.

于 2009-08-17T08:51:30.063 回答
1

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

于 2010-02-06T21:37:19.500 回答
0

请参阅 R# 项目(抱歉文档是俄语的,但有一些代码示例)。它允许对 C# 代码进行 AST 操作。

http://www.rsdn.ru/projects/rsharp/article/rsharp_mag.xml

项目的 SVN 在这里:(URL 已更新,谢谢,derigel

另请参阅Nemerle语言。它是一种.Net 语言,对元编程有很强的支持。

于 2008-10-17T19:13:54.970 回答
0

It is strange that nobody suggested hacking the existing Mono C# compiler.

于 2011-03-08T09:25:32.087 回答