tags:

views:

3427

answers:

12
+14  Q: 

Parser for C#

Which parsers are available for parsing C# code?

I'm looking for a C# parser that can be used in C# and give me access to line and file informations about each artefact of the analysed code.

+4  A: 

http://www.codeplex.com/csparser

Galwegian
+19  A: 

Works on source code:

Works on assembly:

The problem with assembly "parsing" is that we have less informations about line and file (the informations is based on .pdb file, and Pdb contains lines informations only for methods)

I personnaly recommend Mono.Cecil and NRefactory.

madgnome
it helped, thanks
amazedsaint
Great list of parsers.
TcKs
+5  A: 

Mono (open source) includes C# compiler (and of course parser)

aku
What is the advantage of using Mono over other parser? Can i get info of the AST of a C# program using a visitor? If so, can u direct me to the page that shows the page for that?
yeeen
+2  A: 

SharpDevelop, an open source IDE, comes with a visitor-based code parser which works really well. It can be used independently of the IDE.

Akselsson
+2  A: 

Consider to use reflection on a built binary instead of parsing the C# code directly. The reflection API is really easy to use and perhaps you can get all the information you need?

Hallgrim
Reflection is a bad way to do static analysis; it provides only the information that the reflection-logic can extract (e.g., "names of methods in class". It does not provide detail information ("what's the right hand side of this assignment?") and so severely limits that kind of static analysis one can do.
Ira Baxter
@Ira Baxter There are some limitations, but remember that you can also get the IL code via reflection. This means you can understand what methods are called, what are assigned to which variables, etc. I cannot think of many cases where it isn't enough. Just look at what all the Reflector plugins can do.
Hallgrim
@Hallgrim, how do you get the actual IL code via Reflection? As far as I'm aware Reflection doesn't provide this and you need to use CCI See: http://stackoverflow.com/questions/2824086/is-it-possible-to-inspect-an-assemblys-il-instructions-programmatically-using-ma
Ash
+2  A: 

Try this: www.SharpRecognize.com

A: 

GPPG might be of use, if you are willing to write your own parser (which is fun).

leppie
+1  A: 

Have a look at Gold Parser. It has a very intuitive IU that lets you interactively test your grammar and generate C# code. There are plenty of examples available with it and it is completely free.

sbeskur
The OP asked for something that can parser C#, not something in C# that parse something else.
Ira Baxter
+2  A: 

I've implemented just what you are asking (AST Parsing of C# code) at the OWASP O2 Platform project using SharpDevelop AST APIs.

In order to make it easier to consume I wrote a quick API that exposes a number of key source code elements (using statements, types, methods, properties, fields, comments) and is able to rewrite the original C# code into C# and into VBNET.

You can see this API in action on this O2 XRule script file: ascx_View_SourceCode_AST.cs.o2 .

For example this is how you process a C# source code text and populate a number of TreeViews & TextBoxes:

    public void updateView(string sourceCode)
    {   
        var ast = new Ast_CSharp(sourceCode);
        ast_TreeView.show_Ast(ast);
        types_TreeView.show_List(ast.astDetails.Types, "Text");
        usingDeclarations_TreeView.show_List(ast.astDetails.UsingDeclarations,"Text");
        methods_TreeView.show_List(ast.astDetails.Methods,"Text");
        fields_TreeView.show_List(ast.astDetails.Fields,"Text");
        properties_TreeView.show_List(ast.astDetails.Properties,"Text");
        comments_TreeView.show_List(ast.astDetails.Comments,"Text");

        rewritenCSharpCode_SourceCodeEditor.setDocumentContents(ast.astDetails.CSharpCode, ".cs");
        rewritenVBNet_SourceCodeEditor.setDocumentContents(ast.astDetails.VBNetCode, ".vb");                                
    }

The example on ascx_View_SourceCode_AST.cs.o2 also shows how you can then use the information gathered from the AST to select on the source code a type, method, comment, etc..

For reference here is the API code that wrote (note that this is my first pass at using SharpDevelop's C# AST parser, and I am still getting my head around how it works):

Dinis Cruz
+1  A: 

Not in C#, but a full C# 2/3/4 parser that builds full ASTs is available with our DMS Software Reengineering Toolkit.

DMS provides a vast infrastructure for parsing, tree building, construction of symbol tables and flow analyses, source-to-source transformation, and regeneration of source code from the (modified) ASTs. (It also handles many other langauges than just C#.)

Ira Baxter
+1  A: 

If you are going to compile C# v3.5 to .net assemblies:

var cp = new Microsoft.CSharp.CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });

http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx

zproxy
A: 

If you're familiar with ANTLR, you can use Antlr C# grammar.

prosseek