3 ms·
Create a Language Compiler for the .NET Framework Using C#
- peregrine 18y agoWow this is a really great article. Thanks!
- scott_s 18y agoYou might have noticed that this language definition is short on specificity. I haven't specified how big the number can be (such as if it can be bigger than a 32-bit integer) or even if the number can be negative. The grammar itself (which, in this case, is expressed in EBNF notation) says nothing about details such as the size of numbers. Grammars only describe what can be said, not what that means. The C grammar, for example, has many different number types. But it's not the grammar which determines if an unsigned long is 32 or 64-bit. That's determined by parts of the language specification outside of the grammar.
- palish 18y agoYes. That said, it was probably a mistake for C not to define standard fixed-width types such as int8, int16, int32, int64, uint8, uint16, uint32, and uint64. (This limitation is worked around via the <stdint.h> header file; however, that header isn't present in Visual Studio, so anyone who wishes to use those fixed-width types needs to manually download <stdint.h> and include it in their projects. ( http://en.wikipedia.org/wiki/Stdint.h http://en.wikipedia.org/wiki/Stdint.h )
- scott_s 18y agoI agree. But, even if there was, say, a uint16 type in C, it wouldn't be the grammar that defines it as 16 bits.
- palish 18y agoWould the grammar define it as a reserved identifier / keyword?
- scott_s 18y agoCertainly. But the semantics of what "uint16" would mean is not in the grammar. There's nothing to prevent me from defining "uint16" to be a double precision floating point number. That would be stupid and unintuitive, but still possible. If you look at the specification for any language, the grammar is the smallest part. The C++ grammar takes up less than a dozen pages, but the entire standard is over 800.
- palish 18y agoCan I subscribe to your newsletter?
- scott_s 18y agoI honestly can't tell if you're complimenting me or calling me crazy.
- palish 18y agoCompliment... you seem to have a lot of experience with C++.
- scott_s 18y agoIn that case, thank you. I've done a lot of systems programming with C and C++. Understanding exactly what is defined by a language's grammar, though, originally comes from my undergrad courses in programming languages and compilers.
- palish 18y agoCool. Would you say formal education has made you a significantly better programmer? (The definition of 'significantly' in this context is up to you to decide.) I'm completely self-taught, but I've wondered if I missed out on something valuable by not going to college.
- ComputerGuru 18y agoVisual studio has __int8, __int16, __int32, and __int64. Any of these can be prefixed with "unsigned" to got their unsigned equivalents. They're compatible with their stdint equivalents.
- jacquesm 18y agoIt's not exactly a replacement for 'the dragon book' but it certainly takes some of the mysticism out of compiler hacking. If you have never actually made a language from the ground up, then please do so ! It will teach you plenty and it will also help you to write better code because you understand better what a compiler will do internally with the code you wrote. Another great source of insight is the 'produce assembly output' switch for your favourite compiler. Have a look at what the compiler does when it parses your code and turns it in to assembler ready to be turned into machine language.
- ChadB 18y agoThis actually is a neat article. Especially the hit-it-home sentence: Now, you too can share some of this glory by writing your own compiler for the Microsoft® .NET Framework. Made me ready to pick up the phone and buy a "10 Steps to Making Trillions on eBay" book... or write a CLR-targeted compiler.