Для C# хороший вариант - Roslyn
class Program {
static void Main(string[] args) {
SyntaxTree tree = SyntaxTree.ParseText(
@"using System;
namespace HelloWorld {
class Program {
static void Main(string[] args) {
var i = 10 * 20;
String s = t.ToString();
}
}
}");
var comp = Compilation.Create("HelloWorld")
.AddReferences(MetadataReference.CreateAssemblyReference("mscorlib"))
.AddSyntaxTrees(tree);
var model = comp.GetSemanticModel(tree);
new Walker(model).Visit(tree.GetRoot());
Console.ReadKey();
}
class Walker : SyntaxWalker {
private readonly SemanticModel model;
public Walker(SemanticModel model) : base() {
this.model = model;
}
public override void VisitVariableDeclarator(VariableDeclaratorSyntax node) {
var declaredSymbol = (LocalSymbol) model.GetDeclaredSymbol(node);
Console.WriteLine("{0} {1} {2}", declaredSymbol.Type, declaredSymbol.Name, node.Initializer.GetText());
}
}