将 C# 代码转换为 AST?
目前是否可以将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Roslyn 项目位于 Visual Studio 2010 中,可让您以编程方式访问 语法树等事物。
The Roslyn project is in Visual Studio 2010 and gives you programmatic access to the Syntax Tree, among other things.
是的,在特殊情况下很简单(=使用新的表达式框架) :
这会为 lambda 创建一个表达式树,即一个采用
int
并返回 double 的函数。 您可以使用表达式框架(=该命名空间中的类)修改表达式树,然后在运行时编译它:请注意,所有表达式都是不可变的,因此必须通过组合重新构建它们。 在本例中,我在前面添加了 1。
请注意,这些表达式树仅适用于实际表达式,即 C# 函数中找到的内容。 您无法通过这种方式获得更高结构(例如类)的语法树。 为此,请使用 CodeDom 框架。
Yes, trivially in special circumstances (= using the new Expressions framework):
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: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.
查看 .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.
有比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/
就我个人而言,我会使用 NRefactory,它是免费的、开源的并且很受欢迎。
Personally, I would use NRefactory, which is free, open source and gains popularity.
根据 Anders Hejlsberg 的《未来》,此类功能似乎将包含在 C# 4 之后的任何版本中C# 的 PDC 视频。
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.
ANTLR 解析器生成器 具有 C# 3.0 语法,涵盖除 LINQ 语法之外的所有内容。
The ANTLR Parser Generator has a grammar for C# 3.0 which covers everything except for LINQ syntax.
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/
我刚刚在 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
我们的 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.
请参阅 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.
奇怪的是,没有人建议破解现有的 Mono C# 编译器。
It is strange that nobody suggested hacking the existing Mono C# compiler.