3

I was wondering if there is something similar to GCC_XML for C#3; basically a way to represent a program's entire syntactic structure in XML.

Once the representation is created, I was hoping to parse it into an XDocument and interpret or query it from there.

Are there tools out there for this?

Pierreten
  • 9,917
  • 6
  • 37
  • 45
  • Perhaps you can serialize an expression tree, but that won't help you with raw source code. This is the kind of stuff that Micrsoft is thinking about for C# 5+ – Jonathan Allen May 05 '10 at 04:48
  • Yeah, exactly; i'd basically LOVE to have something that would be the logical limit of an expression tree (an expression tree that represents an entire executable program: statements, expressions, operators, types, methods, properties, attributes etc etc) I figured that it would be a bit far out there; so I'd be happy to work with XML as an intermediate for the time being. – Pierreten May 05 '10 at 05:06

1 Answers1

2

Our DMS Software Reengineering Toolkit can do this with C# 2/3/4. (EDIT 2014: and now C#5, EDIT 2020: now C#7 working on C#8)

DMS has compiler accurate parsers for C# (as well as Java and many other languages).

It automatically builds full Abstract Syntax Trees for whatever it parses. Each AST node is stamped with file/line/column for the token that represents that start of that node, and the final column can be computed by a DMS API call. It attaches comments to tree nodes so they aren't lost. DMS can also regenerate valid code from the AST, or from a modified AST; this enables it to be used for code modification or generation.

It has a built-in option to generate XML from the ASTs, complete with node type, source position (as above), and any associated literal value. The command line call is:

 run DMSDomainParser ++XML  <path_to_your_file>

DMS itself provides a vast amount of infrastructure for manipulating the ASTs it builds: traversing, pattern matching (against patterns coded essentially in source form), source-to-source transforms.

It has control flow, data flow, points-to analysis, global call graphs for C, COBOL and Java; that's all coming for C#.

DMS was designed to be a much better solution than XML for manipulating such code.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341