C Parser in C# or generally .Net [closed]

2020-02-07 03:01发布

问题:


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed last month.

For my diploma thesis I need to implement certain static C code analysis and I am desperately looking for a framework/library that would allow me to parse C source code, split it up into single functions, for every function determine what variables are changed in the function body and derive certain annotations for the code automatically. Is there any good framework written in C# or generally as .Net class for this purpose?

回答1:

What about googling for "C Parser written in C#"?

I got this as first link: http://code.google.com/p/cpp-ripper/

Also, I think the C grammar can be found in quite a lot of places, so you might just want to open up your .NET variant of lex/yacc and go from there?



回答2:

You might like to check ANTLR. It comes with versions of several versions, included C and C#. There are some free grammars on ANTLR web site, including C.



回答3:

I had a similiar problem and having done a research about YACC tools for C# I have chosen Gold Parsing System with Semantic Engine. My project was parsing SQL queries and generating logical query plans (from T-SQL grammar subset).

I really recommend it. Those 2 libraries make parsing stuff painless and allow to map grammar to the object model in your code. It feels very intuitive and made my project successful :) However, it may lack some advanced ANTLR features, so recognize your needs carefully.

Gold Project http://www.devincook.com/goldparser/

Semantic Engine Lib http://code.google.com/p/bsn-goldparser



回答4:

If you're ok with using GPL'd code, you might want to take a look at the GCC source code. If you need to do it within .Net, you can always use p/invoke to call code from the GCC libraries.