需要为 C# 代码构建 XML 表示

发布于 2024-07-19 13:32:23 字数 204 浏览 5 评论 0原文

我需要将 C# 代码转换为等效的 XML 表示形式。
我计划将 C# 代码(C# 2.0 代码片段,无泛型或可为空类型)转换为 AST,然后将 AST 转换为 XML。 正在寻找一个用于 C# 的简单词法分析器/解析器,它输出 AST。
任何有关将 C# 代码转换为 XML 表示形式(可以转换回 C#)的指针也会非常有帮助。

亲切的问候,

I need to convert C# code to an equivalent XML representation.
I plan to convert the C# code (C# 2.0 code snippets, no generics or nullable types) to an AST and then convert the AST to XML.
Looking for a simple lexer/parser for C# which outputs an AST.
Any pointers on converting C# code to an XML representation (which can be converted back to C#) would also be very helpful.

Kind regards,

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

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

发布评论

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

评论(3

平定天下 2024-07-26 13:32:23

我们的 DMS 软件再工程工具包是一个用于构建代码分析器和转换器的生态系统。 DMS 通过语言定义进行参数化,并具有 C#、Java、C++、C、PL/SQL、PHP、JavaScript、COBOL 和各种其他语言的语言定义。 当 DMS 根据语言定义进行解析时,它会自动构建 AST。 DMS 提供的 AST 库可以以类似 Lisp 的括号形式或 XML 格式打印树。
DMS 可以直接从 AST 重新生成源代码,而不是将 XML 转换回源代码。 DMS 还提供源到源的转换以允许操作 AST。

Our DMS Software Reengineering Toolkit is an ecosystem for building code analyzers and transformers. DMS is parameterized by a language definition, and has language definitions for C#, Java, C++, C, PL/SQL, PHP, JavaScript, COBOL and a variety of other langauges. When DMS parses according to a langauge definition, it automatically builds an AST. An AST library provided by DMS can print the tree in Lisp-like parenthesized form, or in XML format.
Rather than convert XML back into source code, DMS can regenerated the source code directly from the AST. DMS also provides source-to-source transformations to allow manipulation of the AST.

单调的奢华 2024-07-26 13:32:23

正如 Mitch 所说,Antlr 可以成为您的解决方案。 您可以根据需要转换 Antlr 的 AST 输出,然后使用 xstream 对其进行序列化。 这就是我在我的 bs 项目中使用的方法,如果有人知道更好的方法,这对我来说也很棒。

您可以找到 csharp 语法示例,例如 http://www.antlr.org/grammar /1127720913326/tkCSharp.ghttp://www.antlr.org /grammar/1151612545460/CSharpParser.g 但您可能必须使其适应 ANTLRV3 或您自己的需求。

As Mitch says, Antlr can be your solution. You can transform Antlr's AST output depending on your needs and then serialize it with xstream. That's the approach I'm using in my bs project, If anyone knows a better way It'll be great for me aswell.

You can find csharp grammar samples like for example http://www.antlr.org/grammar/1127720913326/tkCSharp.g or http://www.antlr.org/grammar/1151612545460/CSharpParser.g but you might have to adapt it to ANTLRV3 or to your own needs.

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