Saturday, March 12, 2005

C# Compiler Written in C#

This is too cool not to point out: a C# compiler written in C#. It looks like it's roughly beta quality, but I can already think of a few uses for it even so. The parser/lexer alone could be quite useful.

The author has put the source code in the public domain, so it's available to use however you like. Looks like he might also create a SourceForge project so the code can be collectively improved upon in a more organized manner.

Via Peter Provost's Geek Notes.


  1. Isn't the mono C# compiler also written in C#? I'm more impressed by that production-quality compiler than this guy's almost-compiler.

  2. I have no idea about the mono compiler. If it is, then great: in open source, more is better. E.g. what's the license on the mono compiler?

    I'm still impressed that one guy whipped this up in his spare time. :)

  3. Yep, I remember a slashdot article a couple years back about the mono compiler compiling itself (and the converse people complaining that csc wasn't written in C#).

    The C# compiler is under GPL:

  4. Oh and I am also impressed that dude would create a C# compiler, and I understand that at the time there wasn't one (since Mono was in its infancy when he said he wrote it 3.5 years ago). But by now, it's very much a NIH (Not Invented Here) scenerio to bother to write your own C# compiler.

  5. Actually, licensing alone is enough to prevent me from using the Mono compiler code. The GPL is completely unusuable in many scenarios. Having something public domain, or under a less restrictive license (e.g. MIT) is a useful thing.

  6. Have you even read the GPL?

    Unless you modify the source code, you don't have to publish a single character of your own source code. Even when you do modify the source code, you only have to publish that which relates.

    In most cases, since it's a COMPILER, you'd be shipping an executable. If you modify it, you'd simply need to keep your changes apart from the rest of your code and ship a compiler EXE and ship the source along with that.

    "1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty;"

    "2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program... provided that you also meet all of these conditions:

    a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change.

    b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License."

    Don't believe the MS-FUD that the GPL is a viral license. If it were viral, everything would have to be GPL'ed since you can pipe one program through another. Piping output is perfectly acceptable and doesn't cause your code to need to be GPL'ed.

    Even if you were to convert the Mono compiler into a usable OO class library, only that usable OO class library would need to be governed under the GPL. The GPL actually doesn't even mention "linking" (which doesn't even really count in C# anyway), which is what you'd do in the class library scenerio.

  7. Yes, I have read the GPL. Many times. FlexWiki uses the CPL, which is actually quite similar.

    I don't actually have any problem with the GPL, although I personally prefer the MIT license. But as I've said here before, many organizations and open source projects have a "No GPL code" policy. So I can't use GPL code, even if I want to.

    As far as linking, the GPL FAQ is pretty clear about what that means, and it does include DLL and DLL-like applications, which I interpret to cover C# class libraries. The LGPL exempts you from that limitation and is a different story.

    Doesn't matter, though. Enough people don't want GPL code touching theirs that I basically can't use it in any of the projects I'm working on. I realize that has nothing to do with what the GPL *actually* says, but social constructs like software projects are often impacted more by perception than reality, a point on which we clearly agree (your FUD comment as evidence).

    I do appreciate your attempt to clarify - the more discussion about what the GPL actually means, the more awareness is raised, and the better the chances I can start using GPL code in places where I'd like to, but currently can't.

  8. On second reading of my post, it sounds a lot more argumentative than I meant it. Not intentional.

    To clarify my linking point, the statement in the GPL:

    "These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works."

    Allows you to ship your GPL section separately provided you do something such as create a pluggable model of use for it so you can provide an alternate implementation (which I guess in the case of functionality you might want to link to in a compiler that isn't shelled out may not quite work).

  9. I've always gone by what the GPL FAQ has to say on this subject.

    Their definition of "derivative" includes programs that load DLL and DLL-like GPL modules. Worse, it includes your plugins when loaded by a GPL program.

    I'm not a lawyer, but unless one from the company I'm working for were to tell me otherwise, I would take this definition to be authoritative, since it's from itself.

  10. Craig,

    The GPL does not make plugins loaded by a GPL program GPL. If that were true than any program loaded on Linux would automatically become open source, as it is being dynamically run by a GPL program (the Linux Kernel).

    Your interpretation would also mean that GCC could not create commercial programs or programs with non-gpl licenses which is clearly not true.

    By that logic, just running Cygwin on top of Windows causes Windows to become open source.

  11. It's not my interpretation, it's from the GPL FAQ. I provided the link above. Here it is again:

    But the distinction you're missing is the one they call out: whether the linkage is via library calls, or via fork/exec. So they specifically define linking to not include execution by the kernel. However, as a C# programmer, I'm still screwed if I want to use a GPL library, or write a plugin for a GPL program. This, too, is spelled out very clearly in the FAQ above.

    I appreciate everyone's input, but since these comments have drifted *way* away from the point of the original post, and run the risk of attracting GPL flame wars (although comments so far have been polite), I'm hereby declaring this topic closed. Further comments about the GPL will be deleted. Please feel free to contact me using the link on the left if you'd like to continue the discussion in private.

  12. can u tell the details of that project



  13. Sorry, I don't know anything more than I posted. Follow the link for more information.

  14. Your interpretation would also mean that GCC could not create commercial programs or programs with non-gpl licenses which is clearly not true.

  15. Also not true. Read the FAQ: output of compilers is not GPL, unless the compiler copies substantial portions of itself into the output.

    I think you're confusing compilation with dynamic linking. If the compiler were a GPL library, and I linked it with my program to produce something that called the compiler via a library call, then my program would indeed need to be licensed via the GPL if I chose to distribute it.

    Merely running the compiler does not cause the output to be GPL, however, that doesn't change the fact that linking still works that way. This "problem" is the reason for the existence of the LGPL - it specifically addresses the case of libraries.

  16. How can I write Lexer and Grammar using C# Programming?

    and i want to write New Language based on CLR

  17. Those are two different questions, but I'd start by looking at AntLR and at the documentation for System.CodeDOM.

  18. The mono compiler is written primarilly in C, but has recently been bootstrapped to C#. It was not originally in C#. Simmilarly the Microsoft C# compiler is bootstrapped in C# and Microsoft C is written in C and C++ in C++ etc. It is fairly common practice :)

  19. Hello everybody

    I am using c#.

    I want to know to programatically change my C# code in order to get it compiled by distcc.

    does any body know how to use distcc with c# sample program ?

    or maybe how to change the compiler in C# and use the distcc instead.

    please help.

  20. i am using c# at low level
    any body mail me c# compiler coding (lexical analyzer, semantic error, error detection etc).

    using C# code - - - - -> on - - - - -> *.txt file.
    thank u