Robin E Davies

I'm contemplating writing a micro-language that does rewriting C# 2.0 code. The immediate challenge: what kind of parser to use to parse C# 2.0 code.

One of the things I found particularly troubling in the C# 2.0 standard was the section about resolution of the following code:


(Is that a template or a pair of arguments The C# 2.0 standard says this it's a template name).

I've hunted for off-the-shelf grammars for C#2.0 in commonly used parser tools with no luck (lots of partially implented C# 1.0 grammars, so far, but, no 2.0 grammars).

If anyone could answer the following question, I'd be grateful.

Can C#2.0 be parsed with LALR(1) parser tools LL(k) The example above really looks like it needs LL(*), or LL(k) with backtracking/predicates. (All things being equal, I'd really prefer to use an LALR(1) parser).

Any advice appreciated.

Re: Visual C# Language Grammar class for C# 2.0?


C# as of v2 is a context-sensitive language so some tokens would be keywords in some contexts but not in others. value where and others come to mind. The language is LR with infinite lookup from what I've heard from a MS developer. The source code to the C# compiler is available in the ROTOR download for v2. You can start with it as a base. It is a recursive descent parser.

Michael Taylor - 1/10/07

Re: Visual C# Language Grammar class for C# 2.0?

Robin E. Davies

Thanks. That more-or-less cofirms my impression, from the "A<B,C>D" addendum rule.

That's a shame. The rotor license is pretty unpalatable. (At least GPL allows bunding with commercial products). It really would be a good thing to have a base on which C# 2.0 parsers could be built, given that there are all kinds of interesting applications for it. But I'll certainly take a look. An AST generator that's not encumbered by a prohibitive license would be good enough for most practical purposes.

I've also looked at the mono sources; but it's very difficult to tell what state the 2.0 support is in. Probably not good, if the 2.0 grammar is inifinite-lookahead. My vague recollection was that the mono grammar was LL<k> with very complex (and somewhat suspect) predicates. Have to check.



Re: Visual C# Language Grammar class for C# 2.0?

Someone wrote an LALR grammer for C# for use with the GOLD parser ( It is not officially supported by Microsoft, and seemed to have a couple of inconsistencies (although I didn't look at it really closely); also, I believe it was written for C# 1.1, not 2.0. But if you're looking for a place to start, that might be better than nothing.

Re: Visual C# Language Grammar class for C# 2.0?

Robin E. Davies

I read further in the rotor code. Extraordinary. At least now I know. Thanks for the advice. Hand-rolled recursive descent parser LL(I don't know), with heavy use of infinite look-ahead pre-scanning.