2

I need to parse C/C++ in C#, and get out a neat AST for analysis & visualisation. My methods are currently : find a C++ parser written in C++ (handwritten or generated by a parser generator), save this data into JSON or similar, switch to C#, use JSON library to read data into C# objects for easy analysis.

Is there a project that allows me something like this? Basically I want a full AST and not just the list of members in a code file. I could start with a C++ parser written in C++, or if there's a parser generator that can generate C# code, and someone's already done it for C++, it would be most useful.

Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • 2
    You know about [clang](http://clang.llvm.org/) and [LLVM](http://llvm.org/)? – Some programmer dude Apr 09 '14 at 08:27
  • Questions asking for library recommendations are off topic here. You should find somewhere else to post this. – David Heffernan Apr 09 '14 at 08:34
  • 1
    @David - Then what *is* on-topic? Soon asking about error messages will also be *off-topic*, I guess, which includes half of all programming questions. – Robin Rodricks Apr 09 '14 at 08:35
  • No. Questions asking the meaning of specific error messages are on topic. This question is off topic. I suggest that you move it somewhere else. If you would like to know more about what is on topic I refer you to the help: http://stackoverflow.com/help/on-topic – David Heffernan Apr 09 '14 at 08:36
  • 5
    @DavidHeffernan opinions differ and this question is borderline. Too many valid questions get closed already – Panagiotis Kanavos Apr 09 '14 at 08:43
  • 1
    @DavidHeffernan This is not the place to discuss this. But not every question that is best answered with the recommendation of a external library is automatically off-topic. Not even every question that points this out is. See http://meta.stackexchange.com/questions/139399/what-exactly-is-a-recommendation-question for a good discussion of this issue. – DeVadder Apr 09 '14 at 08:44

1 Answers1

4

You can use a parser generator like ANTLR to parse the input string and get the AST.

ANTLR has a .NET version whose version 3 is used by NHibernate and ASP.NET MVC. You may already have a reference to its runtime libraries in your project, although I'd suggest you use the latest version, 4.

The download probably doesn't contain the syntax files for the specific versions of C or C++ you want, so you should look for the appropriate syntax files. ANTLR has been around for a long time so there are many syntax files available.

You can find a list of grammars for multiple languages managed by the creator of ANTLR on Github. The list includes a grammar for C11.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236