将 C# 代码转换为 AST?

发布于 2024-07-07 03:29:19 字数 140 浏览 13 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

人疚 2024-07-14 03:29:19

Roslyn 项目位于 Visual Studio 2010 中,可让您以编程方式访问 语法树等事物。

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

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;
梦里的微风 2024-07-14 03:29:19

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

是的,在特殊情况下很简单(=使用新的表达式框架) :

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

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

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

请注意,所有表达式都是不可变的,因此必须通过组合重新构建它们。 在本例中,我在前面添加了 1。

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

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.

泪是无色的血 2024-07-14 03:29:19

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

#develop 还应该支持根据此 发布

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.

故笙诉离歌 2024-07-14 03:29:19

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

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

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

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

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

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/

℡Ms空城旧梦 2024-07-14 03:29:19

就我个人而言,我会使用 NRefactory,它是免费的、开源的并且很受欢迎。

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

潇烟暮雨 2024-07-14 03:29:19

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.

許願樹丅啲祈禱 2024-07-14 03:29:19

ANTLR 解析器生成器 具有 C# 3.0 语法,涵盖除 LINQ 语法之外的所有内容。

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

风蛊 2024-07-14 03:29:19

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

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

它被用在很多项目中,包括 NDepend! http://www.ndepend.com/

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/

分开我的手 2024-07-14 03:29:19

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

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

一直在等你来 2024-07-14 03:29:19

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

编辑 3/10/2010:...现在处理完整的 C# 4.0

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

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

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.

孤凫 2024-07-14 03:29:19

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

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

项目的 SVN 位于此处:(网址已更新,谢谢,derigel)

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

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

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

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

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

生死何惧 2024-07-14 03:29:19

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文