如何操作 C# AST?

发布于 2024-07-30 06:32:40 字数 799 浏览 4 评论 0原文

我正在开展一个逆向工程学校项目,该项目需要翻译已编译的 C# 项目的 AST 操作。 我看过“将 C# 代码翻译成 AST?”的帖子 在这个网站上,但它看起来不像我要找的。

据我所知,目前 C# 没有提供与 Java 类似的库类: http://help.eclipse.org/help33/index.jsp? topic=/org.eclipse.cdt.doc.isv/reference/api/org/eclipse/cdt/core/dom/ast/ASTVisitor.html。 如果C#中有这样的库类,这里一切就都解决了。

我咨询过某人,以下是可能的解决方案。 但我在制定解决方案时也遇到了问题:

  1. 找到另一个编译器,它提供了一个允许公开 AST 进行操作的库。 但我找不到这样的编译器。
  2. 使用 ANTLR Parser Generator 来生成我自己的编译器来执行此操作(这将是一个更加困难和更长的过程)。 那里的下载提供了不同语言的示例语法,但不提供 C#(它有用各种语言编写的语法,包括 C#,但不生成 C# 语法)。 因此问题是我找不到 C# 语法。

解决这个问题最短、最快的方法是什么? 如果我真的必须采取上述替代方案之一,我应该如何解决我面临的这些问题。

I am working on a Reverse Engineering school project, which requires to translate manipulate AST of compiled C# project. I have seen the post on "Translate C# code into AST?" in this website, but it doesn't look like the one I am looking for.

According to what I know, currently C# doesn't provide a library class that does something like that for Java: http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.cdt.doc.isv/reference/api/org/eclipse/cdt/core/dom/ast/ASTVisitor.html. If there is such library class in C#, everything here is solved.

I have consulted with someone, and here are the possible solutions. But I have problems with working out on the solutions as well:

  1. Find another compiler that provides a library which allows its AST to be expose for manipulation. But I can't find a compiler like that.
  2. Use ANTLR Parser Generator to come out with my own compiler that does that (it will be a much more difficult and longer process). The download there provides sample grammars for different languages but not C# (it has grammars written in various languages including C# but not to produce C# grammar). Hence the problem is I can't find C# grammar.

What is shortest and fastest way to approach this issue? If I really have to take one of the alternative above, how should I go about solving those problems I faced.

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

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

发布评论

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

评论(5

手心的温暖 2024-08-06 06:32:41

我知道这个答案很久以前就被接受了。 但我也有类似的问题,不确定有什么选择。 我对作为 SharpDevelop 一部分提供的 NRefactory 库做了一些调查。 它确实从 C# 代码生成 AST。

这是 NRefactory 演示应用程序的图像,它是 SD 源代码的一部分。 输入一些 C# 代码,它会生成 AST 并在树视图中显示。

替代文本

I know the answer for this one was accepted long ago. But I had a similar question and wasn't sure of the options out there. I did a little investigation of the NRefactory library that ships as part of SharpDevelop. It does generate an AST from C# code.

Here's an image of the NRefactory demo application that is part of the SD source code. Type in some C# code and it generates and displays the AST in a treeview.

alt text

执笔绘流年 2024-08-06 06:32:41

为什么不尝试NRefectory。 我在一些 SharepDevelop 论坛上看到过关于 AST 问题的讨论。

这是关于此主题的 CodeProject 的文章。

Why don't you try NRefectory. I've seen it discussed for AST thing on some SharepDevelop forums.

Here is an article on CodeProject regarding this topic.

天赋异禀 2024-08-06 06:32:41

ANTLR 不是一个好的选择。 我现在正在尝试使用 Mono Cecil 代替。 Mono Cecil 非常适合分析任何可以编译成通用中间语言 (CIL) 的源代码。 缺点是它没有适当的文档。

ANTLR is not a good choice. I am now trying out using Mono Cecil instead. Mono Cecil is good for analyzing any souce codes that can be compiled into Common Intermediate Language (CIL). The disadvantage is that it doesn't have properly documentation.

闻呓 2024-08-06 06:32:41

我刚刚在 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-08-06 06:32:41

我们的 DMS 软件重新工程工具包(DMS for短的)。 它已被用来准确处理数以万计的 C# 文件。 它提供自动化的 AST 构建、树遍历、
表面语法模式匹配和转换等等。
作为商业产品,它可能不适合学生项目。

ANTLR 可以说提供了一个 C# 解析器,但我不知道它是否完整或强大,
或者它是否真的构建了 AST。

[2010 年 1 月 25 日编辑:C# 4.0 解析器现在可用于具有上述所有属性的 DMS]

[2016 年 5 月编辑:C# 6.0 解析器可用于 DMS。]

A full C# 3.0 parser is available with our DMS Software Reengineering Toolkit (DMS for short). It has been used to process tens of thousands of C# files accurately. It provides automated AST building, tree traversals,
surface-syntax pattern matching and transformation and lots more.
As a commercial product it might not work out for a student project.

ANTLR arguably offers a C# parser, but I don't know complete or robust it is,
or whether it actually builds ASTs.

[EDIT Jan 25 2010: C# 4.0 parser now available for DMS with all the above properties]

[EDIT May 2016: C# 6.0 parser available for DMS.]

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