Best parser for C #?

I need a parser or grammar for C # 3.0 (open source license). What is the best choice today?

+6
c # parsing
source share
4 answers

Check out the ANTLR project. http://www.antlr.org/

You can get the C # grammar here: http://www.antlr.org/grammar/list

+2
source share

Check out Metspec C # parser :

The Metaspec C # parser is fully compliant with ECMA-334 and ECMA-335 standards. In addition, it supports Extensions for Microsoft. For more information on the C # parser library, see the online documentation (C # edition or C ++ release).

C # 3.0 supported features:

  • new C # 3.0 type inference algorithm
  • implicitly entered local variables
  • object initializers
  • collection initializers
  • anonymous object creation expressions
  • lambda expressions
  • anonymous types
  • extension methods
  • query expressions
  • partial methods

Supported C # 2.0 Features:

  • generics
  • nullable types
  • anonymous methods
+3
source share

I recommend at least watching the LINQOver # project, hosted on codeproject.com.

URL: http://www.codeplex.com/LinqOverCSharp

There are some (minor?) Known issues, and it has not been updated since January 2008 (which can be a pretty big problem), but the source code for the (fast and 100% .Net) C # 3.0 parser exists for adoption.

My favorite things about this parser are:

  • It can load the Visual Studio project file (csproj) pretty much out of the box and parse the entire shebang (including assembly references).

  • You can query, list, filter, etc. the analyzed object model (tree) using LINQ. Which makes it almost trivial to move up and down and around what you are parsing.

Here's an example LINQ query to find a variable or parameter in a method, where the variable name = VariableName:

variable = (from v in method.Variables where string.Compare(v.Name, VariableName, false) == 0 select v as LanguageElement).Union( from p in method.FormalParameters where string.Compare(p.Name, VariableName, false) == 0 select p as LanguageElement).FirstOrDefault(); 
+1
source share

Depends on what you are optimizing for.

If you are optimizing the ability to convert C #, see C # Front End for a parser that handles C # 1.2, 2.0, and 3.0 (including LINQ syntax). C # Front End is built on top of the DMS Software Reengineering Toolkit , which provides parsing, automatic AST construction, support for symbol tables, -source program conversion using source-level syntax with templates, and AST back to texturing the source text. If you want to convert C # code, this is the tool for this. (DMS is also used to analyze and transform code in Java, C, C ++, JavaScript, COBOL and many other languages).

If you are optimizing with open source, this is not the tool you need.

0
source share

All Articles