将 C# 代码翻译成 AST?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/213427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:23:40  来源:igfitidea点击:

Translate C# code into AST?

c#compiler-constructionabstract-syntax-tree

提问by Erik Forbes

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

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

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.

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

采纳答案by Paul Rubel

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

罗斯林项目是在Visual Studio 2010,让您的编程访问的语法树,等等。

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

回答by Rob Walker

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

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

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

根据这篇文章,#develop 也应该支持从 C# 源代码生成 CodeDom 树。

回答by Alexander Gladysh

Please see the R# project (sorry the docs are in Russian, but there are some code examples). It allows AST manipulations on C# code.

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

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

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

Project's SVN is here: (URL updated, thanks, derigel)

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

Also please see the Nemerlelanguage. It is a .Net language with strong support for metaprogramming.

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

回答by Konrad Rudolph

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

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

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 intand 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:

这为 lambda 创建了一个表达式树,即一个函数接受 anint并返回 double。您可以使用 Expressions 框架(= 该命名空间中的类)修改表达式树,然后在运行时编译它:

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.

请注意,所有表达式都是不可变的,因此必须通过组合重新构建它们。在这种情况下,我预先添加了 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.

请注意,这些表达式树仅适用于真正的表达式,即在 C# 函数中找到的内容。您无法通过这种方式获得更高结构(例如类)的语法树。对这些使用 CodeDom 框架。

回答by Erik Forbes

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.

根据Anders Hejlsberg 的“C# 的未来” PDC 视频,看起来这种功能将包含在 C# 4 之后的任何版本中。

回答by Erik Forbes

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

ANTLR解析器生成器具有用于C#3.0语法,其覆盖除了LINQ语法一切。

回答by Ira Baxter

Our C# front end for DMSparses 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.

我们用于 DMS 的 C# 前端解析包括 LINQ 在内的完整 C# 3.0 并生成 AST。DMS 实际上是一个使用 AST 分析/转换源代码的生态系统,用于前端提供的语言。

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

2010 年 3 月 10 日编辑:...现在可以处理完整的 C# 4.0

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

编辑:2014 年 6 月 27 日:处理 C# 5.0 已经有一段时间了。

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

编辑:2016 年 6 月 15 日:处理 C# 6.0。有关示例 AST,请参阅https://stackoverflow.com/a/37847714/120163

回答by yeeen

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

ANTLR 不是很有用。LINQ 不是你想要的。

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

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

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

它被用于许多项目,包括 NDepend!http://www.ndepend.com/

回答by Dinis Cruz

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

我刚刚在 StackOverflow 上的另一个线程上回答了一个解决方案,我在其中实现了一个 API 来从 C# 源代码创建和操作 AST

回答by NN_

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

有比 R# 强大得多的项目。Nemerle.Peg:

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

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 !

它有 C# Parser,它可以解析所有 C# 代码并将其转换为 AST !

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

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

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

您可以在此处下载安装程序:https: //code.google.com/p/nemerle/

回答by SK-logic

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

奇怪的是,没有人建议破解现有的 Mono C# 编译器。