I need a parser or grammar for C# 3.0 (open-source license). What's the best choice today?
Check out Metaspec's C# parser:
The Metaspec C# parser is fully compliant with ECMA-334 and ECMA-335 standards. In addition, it supports Microsoft-specific extensions. For details about the C# parser library, see the online documentation (C# edition or C++ edition).
C# 3.0 supported features:
- new C# 3.0 type inference algorithm
- implicitly typed local variables
- object initializers
- collection initializers
- anonymous object creation expressions
- lambda expressions
- anonymous types
- extension methods
- query expressions
- partial methods
C# 2.0 supported features:
- generics
- nullable types
- anonymous methods
Check out the ANTLR project. http://www.antlr.org/
You can get C# grammar from here: http://www.antlr.org/grammar/list
Depends on what you are optimzing on.
If you are optimizing on ability to transform C#, see the C# Front End for a parser that handles C# 1.2, 2.0 and 3.0 (including LINQ syntax). The C# Front End is built on top of DMS Software Reengineering Toolkit which provide parsing, automatic building of ASTs, support for symbol tables, source-to-source program transformation using source-level syntax with patterns, and AST back to source text prettyprinting. If you are looking to transform C# code, this is the tool to do it. (DMS is also used to analyze and transform code in Java, C, C++, JavaScript, COBOL and many other langauges).
If you are optimizing on open source, this isn't the tool you want.
I recommend at least looking at the LINQOverC# project hosted on codeproject.com.
URL: http://www.codeplex.com/LinqOverCSharp
There are some (minor?) known issues, and it hasn't been updated since Jan 2008 (which could be a pretty big issue), but the source code for a (fast, and 100% .Net) C# 3.0 parser is there for the taking.
My favorite things about this parser:
It can load a Visual Studio project file (csproj) pretty much out of the box, and parse the whole shebang (including assembly references).
You can query, enumerate, filter, etc, the parsed object model (tree) using LINQ. Which it makes it almost trivial to traverse up and down and all around whatever you're parsing.
Here's a sample LINQ query for finding a variable or parameter in a method, where the variable's 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();