Where can I find C# 3.0 grammar?

I'm planning to write a C# 3.0 compiler in C#. Where can I get the grammar for parser generation?

Preferably one that works with ANTLR v3 without modification.

13.10.2009 16:52:57
You are aware that we already ship for free a compiler that compiles C# 3, right? :-) But seriously, why are you building your own? Just for fun, or is there some business purpose? (The reason I ask is because we are very interested in learning what "services" people want out of our compiler other than simply "spit me out some IL for this source code".)
Eric Lippert 13.10.2009 16:55:34
Eric: Primarily for fun. However, I come up with some language ideas from time to time that I wish I could test.
Mehrdad Afshari 13.10.2009 16:57:09
@Mehrdad, if you do get some code going, can i play around with it :)
Stan R. 13.10.2009 17:07:47
@Mehrdad: "...language ideas" is pretty vague. If you want to do anything interesting with C# (or really any other language) you not only need a parser (therefore grammar) but you also need to build trees, build symbol tables, analyze symbol usage, ... There's a lot more to this than just the grammar. The Mono framework might be a lot more helpful than you think.
Ira Baxter 2.11.2009 02:37:45
Ira: Of course I have seen that question. I explicitly mentioned C# 3.0, since I've found a bunch of stuff for 1.0. -- I said "primarily for fun" by the way. For me, it's more fun to write my own and use that to test my stuff rather than try to understand the structure of code done by Mono guys.
Mehrdad Afshari 2.11.2009 03:55:26
4 ОТВЕТА

Are you looking for something like this or this?

Please also refer to C# ANLTR grammar question.

3
23.05.2017 12:01:26
The linked grammar is not C# 3.0. It doesn't support lambdas. That's specifically important to me.
Mehrdad Afshari 13.10.2009 17:11:19
It would seem that adding support for lambdas in terms of existing constructs in the grammar is fairly trivial, since you only need to define argument list. This will probably need LL(), however, since you can parse something like (a** and not know if this will end up being an expression like (a**b) (i.e. multiply a by the result of a dereference of b), or a lambda expression (a** b) =>, until you hit the =>. Since there's no limit on amount of indirection (pointer to pointer to ...), it looks like it it's LL() to me. But since ANTLR3 supports opt-in LL(*), it's not a problem.
Pavel Minaev 13.10.2009 19:27:56
@Pavel: It's not just that. It doesn't support generics. I'll probably write my own parser or grammar from scratch if I can't find a reasonably good C# 3.0 grammar.
Mehrdad Afshari 13.10.2009 19:51:11

Take a look at COCO/R it seems that they have the language specification for C# 3.0.

3
14.10.2009 06:47:07

Take a look at C# Language Specification. In the chapter B. Grammar you'll find the grammar.

12
14.10.2009 06:52:46
Yeah, of course the spec contains grammar. However, the grammar in that Word document is scattered through the whole doc and is unsuitable for parser generation.
Mehrdad Afshari 14.10.2009 09:46:29
It's not only scattered throughout; we have an appendix at the end with the whole thing in one place. You are probably right that it would take some modification to make it work for a parser generator.
Eric Lippert 14.10.2009 14:28:50
Eric: Oh, didn't notice that section. Thanks for pointing out.
Mehrdad Afshari 14.10.2009 16:27:46
Micheal: It's less than 40 pages long. When I think about it, it's possible to deal with it and start from scratch. +1
Mehrdad Afshari 14.10.2009 16:30:05
РЕШЕНИЕ

I ran into ANTLR C# Grammar on CodePlex. It's a relatively new project and uses ANTLR 3.2. It says it supports C# 4.0 and is licensed under the Eclipse Public License (EPL).

I played with it a little. It has a bunch of test files containing expressions. It supports lambdas, unsafe context, ... as you'd naturally expect. It parses a C# file and hands you an abstract syntax tree. You can do whatever you want with it.

7
21.01.2010 14:55:38