<p>Which parsers are available for parsing C# code?</p>

<p>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.</p>

[{'answer_id': 81415, 'author': 'Galwegian', 'author_id': 3201, 'author_profile': 'https://Stackoverflow.com/users/3201', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.codeplex.com/csparser" rel="nofollow noreferrer">http://www.codeplex.com/csparser</a></p>\n'}, {'answer_id': 81427, 'author': 'Julien Hoarau', 'author_id': 12248, 'author_profile': 'https://Stackoverflow.com/users/12248', 'pm_score': 8, 'selected': True, 'text': '<p>Works on source code:</p>\n\n<ul>\n<li><a href="http://www.codeplex.com/csparser" rel="noreferrer">CSParser</a>:\nFrom C# 1.0 to 2.0, open-source</li>\n<li><a href="http://www.csharpparser.com/csparser.php" rel="noreferrer">Metaspec C# Parser</a>:\nFrom C# 1.0 to 3.0, commercial product (about 5000$)</li>\n<li><a href="http://www.sharprecognize.com/" rel="noreferrer">#recognize!</a>:\nFrom C# 1.0 to 3.0, commercial product (about 900€) (answer by <a href="https://stackoverflow.com/questions/81406/parser-for-c#185380">SharpRecognize</a>)</li>\n<li><a href="http://www.icsharpcode.net/OpenSource/SD/" rel="noreferrer">SharpDevelop Parser</a> (answer by <a href="https://stackoverflow.com/questions/81406/parser-for-c#81434">Akselsson</a>)</li>\n<li><a href="https://github.com/icsharpcode/NRefactory/" rel="noreferrer">NRefactory</a>:\nFrom C# 1.0 to 4.0 (+async), open-source, parser used in SharpDevelop. Includes semantic analysis.</li>\n<li><a href="http://www.inevitablesoftware.com/Products.aspx" rel="noreferrer">C# Parser and CodeDOM</a>:\nA complete C# 4.0 Parser, already support the C# 5.0 async feature. Commercial product (49$ to 299$) (answer by <a href="https://stackoverflow.com/questions/81406/parser-for-c/7631455#7631455">Ken Beckett</a>)</li>\n<li><a href="http://msdn.microsoft.com/en-us/roslyn" rel="noreferrer">Microsoft Roslyn CTP</a>:\nCompiler as a service. </li>\n</ul>\n\n<p>Works on assembly:</p>\n\n<ul>\n<li>System.Reflection</li>\n<li><a href="http://ccimetadata.codeplex.com/" rel="noreferrer">Microsoft Common Compiler Infrastructure</a>:\nFrom C# 1.0 to 3.0, Microsoft Public License. Used by <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772&amp;displaylang=en" rel="noreferrer">Fxcop</a> and <a href="http://research.microsoft.com/en-us/projects/specsharp/" rel="noreferrer">Spec#</a></li>\n<li><a href="http://www.mono-project.com/Cecil" rel="noreferrer">Mono.Cecil</a>:\nFrom C# 1.0 to 3.0, open-source</li>\n</ul>\n\n<p>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)</p>\n\n<p>I personnaly recommend <strong>Mono.Cecil</strong> and <strong>NRefactory</strong>.</p>\n'}, {'answer_id': 81429, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.mono-project.com/" rel="noreferrer">Mono</a> (open source) includes C# compiler (and of course parser)</p>\n'}, {'answer_id': 81434, 'author': 'Akselsson', 'author_id': 8862, 'author_profile': 'https://Stackoverflow.com/users/8862', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.icsharpcode.net/OpenSource/SD/" rel="nofollow noreferrer">SharpDevelop</a>, an open source IDE, comes with a visitor-based code parser which works really well. It can be used independently of the IDE.</p>\n'}, {'answer_id': 81473, 'author': 'Hallgrim', 'author_id': 15454, 'author_profile': 'https://Stackoverflow.com/users/15454', 'pm_score': 2, 'selected': False, 'text': '<p>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?</p>\n'}, {'answer_id': 186117, 'author': 'leppie', 'author_id': 15541, 'author_profile': 'https://Stackoverflow.com/users/15541', 'pm_score': -1, 'selected': False, 'text': '<p><a href="http://plas.fit.qut.edu.au/gppg/" rel="nofollow noreferrer">GPPG</a> might be of use, if you are willing to write your own parser (which is fun). </p>\n'}, {'answer_id': 231082, 'author': 'sbeskur', 'author_id': 10446, 'author_profile': 'https://Stackoverflow.com/users/10446', 'pm_score': 2, 'selected': False, 'text': '<p>Have a look at <a href="http://www.devincook.com/goldparser/" rel="nofollow noreferrer">Gold Parser</a>.  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.</p>\n'}, {'answer_id': 2214810, 'author': 'Dinis Cruz', 'author_id': 262379, 'author_profile': 'https://Stackoverflow.com/users/262379', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve implemented just what you are asking (AST Parsing of C# code) at the <a href="http://www.owasp.org/index.php/OWASP_O2_Platform" rel="nofollow noreferrer">OWASP O2 Platform</a> project using SharpDevelop AST APIs. </p>\n\n<p>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.</p>\n\n<p>You can see this API in action on this O2 XRule script file: <a href="http://code.google.com/p/o2platform/source/browse/trunk/O2%20-%20All%20Active%20Projects/O2_XRules_Database/_Rules/DotNet/ascx_View_SourceCode_AST.cs.o2" rel="nofollow noreferrer">ascx_View_SourceCode_AST.cs.o2</a> . </p>\n\n<p>For example this is how you process a C# source code text and populate a number of TreeViews &amp; TextBoxes:</p>\n\n<pre><code>    public void updateView(string sourceCode)\n    {   \n        var ast = new Ast_CSharp(sourceCode);\n        ast_TreeView.show_Ast(ast);\n        types_TreeView.show_List(ast.astDetails.Types, "Text");\n        usingDeclarations_TreeView.show_List(ast.astDetails.UsingDeclarations,"Text");\n        methods_TreeView.show_List(ast.astDetails.Methods,"Text");\n        fields_TreeView.show_List(ast.astDetails.Fields,"Text");\n        properties_TreeView.show_List(ast.astDetails.Properties,"Text");\n        comments_TreeView.show_List(ast.astDetails.Comments,"Text");\n\n        rewritenCSharpCode_SourceCodeEditor.setDocumentContents(ast.astDetails.CSharpCode, ".cs");\n        rewritenVBNet_SourceCodeEditor.setDocumentContents(ast.astDetails.VBNetCode, ".vb");                                \n    }\n</code></pre>\n\n<p>The example on <a href="http://code.google.com/p/o2platform/source/browse/trunk/O2%20-%20All%20Active%20Projects/O2_XRules_Database/_Rules/DotNet/ascx_View_SourceCode_AST.cs.o2" rel="nofollow noreferrer">ascx_View_SourceCode_AST.cs.o2</a> also shows how you can then use the information gathered from the AST to select on the source code a type, method, comment, etc..</p>\n\n<p>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):</p>\n\n<ul>\n<li><a href="http://code.google.com/p/o2platform/source/browse/trunk/O2%20-%20All%20Active%20Projects/O2%20Modules%20Using%203rd%20Party%20Dlls/O2_External_SharpDevelop/AST/AstDetails.cs" rel="nofollow noreferrer">AstDetails.cs</a></li>\n<li><a href="http://code.google.com/p/o2platform/source/browse/trunk/O2%20-%20All%20Active%20Projects/O2%20Modules%20Using%203rd%20Party%20Dlls/O2_External_SharpDevelop/AST/AstTreeView.cs" rel="nofollow noreferrer">AstTreeView.cs</a></li>\n<li><a href="http://code.google.com/p/o2platform/source/browse/trunk/O2%20-%20All%20Active%20Projects/O2%20Modules%20Using%203rd%20Party%20Dlls/O2_External_SharpDevelop/AST/AstValue.cs" rel="nofollow noreferrer">AstValue.cs</a></li>\n<li><a href="http://code.google.com/p/o2platform/source/browse/trunk/O2%20-%20All%20Active%20Projects/O2%20Modules%20Using%203rd%20Party%20Dlls/O2_External_SharpDevelop/AST/Ast_CSharp.cs" rel="nofollow noreferrer">Ast_CSharp.cs</a></li>\n</ul>\n'}, {'answer_id': 2408588, 'author': 'Ira Baxter', 'author_id': 120163, 'author_profile': 'https://Stackoverflow.com/users/120163', 'pm_score': 0, 'selected': False, 'text': '<p>Not in C#, but a full C# 2/3/4 parser that builds full ASTs is available with our <a href="http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html" rel="nofollow noreferrer">DMS Software Reengineering Toolkit</a>.</p>\n\n<p>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 languages than just C#.)</p>\n\n<p>EDIT (September) 2013:  This answer hasn\'t been updated recently.  DMS has long handled C# 5.0</p>\n'}, {'answer_id': 2408671, 'author': 'zproxy', 'author_id': 94411, 'author_profile': 'https://Stackoverflow.com/users/94411', 'pm_score': 3, 'selected': False, 'text': '<p>If you are going to compile C# v3.5 to .net assemblies:</p>\n\n<pre><code>var cp = new Microsoft.CSharp.CSharpCodeProvider(new Dictionary&lt;string, string&gt;() { { "CompilerVersion", "v3.5" } });\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx</a></p>\n'}, {'answer_id': 3920241, 'author': 'prosseek', 'author_id': 260127, 'author_profile': 'https://Stackoverflow.com/users/260127', 'pm_score': 3, 'selected': False, 'text': '<p>If you\'re familiar with ANTLR, you can use <a href="http://antlrcsharp.codeplex.com/" rel="noreferrer">Antlr C# grammar</a>.</p>\n'}, {'answer_id': 4561545, 'author': 'SeeSoft', 'author_id': 556927, 'author_profile': 'https://Stackoverflow.com/users/556927', 'pm_score': 2, 'selected': False, 'text': '<p>Maybe you could try with Irony on irony.codeplex.com.</p>\n\n<p>It\'s very fast and a c# grammar already exists.</p>\n\n<p>The grammar itself is written directly in c# in a BNF like way (acheived with some operators overloads)</p>\n\n<p>The best thing with it is that the "grammar" produces the AST directly.</p>\n'}, {'answer_id': 7631455, 'author': 'Ken Beckett', 'author_id': 910651, 'author_profile': 'https://Stackoverflow.com/users/910651', 'pm_score': 2, 'selected': False, 'text': '<p>We have recently released a C# parser that handles all C# 4.0 features plus the new async feature:  <a href="http://www.inevitablesoftware.com/Products.aspx" rel="nofollow" title="C# Parser and CodeDOM">C# Parser and CodeDOM</a></p>\n\n<p>This library generates a semantic object model which retains comments and formatting information and can be modified and saved.  It also supports the use of LINQ queries to analyze source code.</p>\n'}, {'answer_id': 11523386, 'author': 'Stéphane', 'author_id': 156415, 'author_profile': 'https://Stackoverflow.com/users/156415', 'pm_score': 0, 'selected': False, 'text': '<p>Something that is gaining momentum and very appropriate for the job is <a href="http://nemerle.org/" rel="nofollow">Nemerle</a> </p>\n\n<p>you can see how it could solve it in these videos from NDC : </p>\n\n<ul>\n<li><a href="http://vimeo.com/43690661" rel="nofollow">Igor Tkachev - Metaprogramming with Nemerle</a></li>\n<li><a href="http://vimeo.com/43588432" rel="nofollow">Igor Tkachev - Nemerle Programming Language</a></li>\n</ul>\n'}, {'answer_id': 23073744, 'author': 'Jason', 'author_id': 3534027, 'author_profile': 'https://Stackoverflow.com/users/3534027', 'pm_score': 2, 'selected': False, 'text': '<p>You should definitely check out Roslyn since MS just opened (or will soon open) the code with an Apache 2 license <a href="http://roslyn.codeplex.com/" rel="nofollow">here</a>. You can also check out a way to parse this info with this code from <a href="https://github.com/Zenoware/RoslynParser" rel="nofollow">GitHub</a>.</p>\n'}]
['https://Stackoverflow.com/questions/81406', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12248/']