C# 2.0 parsing

I have recently been working on the parsing of C# and VB.Net as part of our development of Clover.NET for Visual Studio 2005 and .NET 2.0. I think it is fair to say that the changes to the language specs for both languages have increased the complexity of parsing both languages quite significanly. Over the next few days I’ll post some example code snippets that show off some of the problems we have encountered in testing our parsers for the new language features. If I get time I also want to discuss some of the general techniques we have used to simplify parsing.

To start with, today I’ll give a little example program which will compile with csc 1.1 but which fails to compile with csc 2.0 (I’m using the July CTP at this time). This example is inspired by the examples in the C# specification of March 2005 and centres around the ambiguity of the >> symbol in the presence of generics (I have to work hard not to type “templates” there). If I read the spec correctly, this should compile ok but, so far, Microsoft disagree and class this compatibility breakage as “by design”.

using System;

class X
{
static void Main ()
{
X x = new X();
x.runTest();
}

public int F(bool a, int b) {
return 2;
}

public void runTest() {
int a = 5;
int b = 6;
int c = 7;

int blah = F(a<b ,c>>2);
Console.WriteLine("Blah = " + blah);
}
}

The csc 2.0 compiler interprets the arguments in the call to F to be a generic, rather than two separate arguments. I guess it’s not a big deal in practice as the example is somewhat contrived. Nevertheless, I find the compatibility break disturbing.

3 Replies to “C# 2.0 parsing”

  1. The compiler of the final VS 2005 release doesn’t like it either:

    ------ Build started: Project: Conor, Configuration: Release Any CPU ------
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:TRACE /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug:pdbonly /optimize+ /out:obj\Release\Conor.dll /target:library X.cs Properties\AssemblyInfo.cs
    C:\Dokumente und Einstellungen\stefan.bodewig\Lokale Einstellungen\Anwendungsdaten\Temporary Projects\Conor\X.cs(18,24): error CS0246: The type or namespace name 'b' could not be found (are you missing a using directive or an assembly reference?)
    C:\Dokumente und Einstellungen\stefan.bodewig\Lokale Einstellungen\Anwendungsdaten\Temporary Projects\Conor\X.cs(18,27): error CS0246: The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)
    C:\Dokumente und Einstellungen\stefan.bodewig\Lokale Einstellungen\Anwendungsdaten\Temporary Projects\Conor\X.cs(18,22): error CS0307: The variable 'a' cannot be used with type arguments
    
    Compile complete -- 3 errors, 0 warnings
    ========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========
  2. Fixed – although it’s now pretty wide :-). Anyway, it’s still a compatibility break and I doubt that will change.

Comments are closed.