Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parser for C# [closed]

Tags:

c#

parsing

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.

like image 231
Julien Hoarau Avatar asked Sep 17 '08 09:09

Julien Hoarau


People also ask

Which parser is used in C?

C is (mostly) parseable with an LALR(1) grammar, although you need to implement some version of the "lexer hack" in order to correctly parse cast expressions.

How does C parser work?

C is compiled with a compiler, which is run once before the program may be run thousands of times. Most C compilers make several 'passes': lexing input into tokens, parsing the tokens into a tree, then modifying the Abstract Syntax Tree to generate symbol tables for each of the scopes of execution.

Is C hard to parse?

C is a bit hard to parse because statements like `A * B();` will mean different things if A is defined as a type or note. C++ is much harder to parse because the template syntax is hard to disambiguate from less than or greater than.


2 Answers

Works on source code:

  • CSParser: From C# 1.0 to 2.0, open-source
  • Metaspec C# Parser: From C# 1.0 to 3.0, commercial product (about 5000$)
  • #recognize!: From C# 1.0 to 3.0, commercial product (about 900€) (answer by SharpRecognize)
  • SharpDevelop Parser (answer by Akselsson)
  • NRefactory: From C# 1.0 to 4.0 (+async), open-source, parser used in SharpDevelop. Includes semantic analysis.
  • C# Parser and CodeDOM: A complete C# 4.0 Parser, already support the C# 5.0 async feature. Commercial product (49$ to 299$) (answer by Ken Beckett)
  • Microsoft Roslyn CTP: Compiler as a service.

Works on assembly:

  • System.Reflection
  • Microsoft Common Compiler Infrastructure: From C# 1.0 to 3.0, Microsoft Public License. Used by Fxcop and Spec#
  • Mono.Cecil: From C# 1.0 to 3.0, open-source

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.

like image 114
Julien Hoarau Avatar answered Oct 04 '22 08:10

Julien Hoarau


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

like image 42
aku Avatar answered Oct 04 '22 08:10

aku