将 C# 代码翻译成 AST? [英] Translate C# code into AST?

查看:41
本文介绍了将 C# 代码翻译成 AST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

一些说明;我不一定期望编译器为我生成 AST - 解析器会很好,尽管我想使用官方"的东西.不幸的是,Lambda 表达式是不够的,因为它们不允许我使用我正在寻找的语句体.

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.

推荐答案

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

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;

这篇关于将 C# 代码翻译成 AST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆