3 ms·
> Outputting to C is a well-recognized compilation strategy. That's true, but it's not a good choice for teaching how to write a compiler because the C compile
by lisper 5y ago
> Outputting to C is a well-recognized compilation strategy.
That's true, but it's not a good choice for teaching how to write a compiler because the C compiler does too much of the heavy lifting for you while at the same time imposing unnecessary constraints. Compiling to C allows you to entirely side-step the question of how to compile arithmetic and function calls, which is like 90% of the battle. Also, if you want your source language to, for example, be able to do non-local transfers of control or have automatic memory management then you will have to do extra work that you would not have to do if you were compiling to assembler or an abstract RTL.
Finally, compiling to C gives the student the impression that there is something holy and immutable about C, that C is The Way and the Truth and the Light and no man comes to the lambda nature but through C. It's not true, and, to paraphrase Dykstra, the psychological damage caused by indoctrinating people into this worldview is hard to undo.
- lebuffon 5y agoEverybody knows that CPU stands for: 'C' Processing Unit ;-)
- lebuffon 5y agoKrenshaw's classic version is available in C. https://github.com/lotabout/Let-s-build-a-compiler https://github.com/lotabout/Let-s-build-a-compiler
- thelittlenag 5y agoI agree. Compiling to C as your target just feels like a let down. I guess if you have to compromise on the hard parts why not target something like WASM or JVM bytecode? At least then you learn two interesting things.
- rualca 5y agoI thought that the technical term for parsing a high-level programming language to output another high-level programming language was transcompiling. https://en.wikipedia.org/wiki/Source-to-source_compiler https://en.wikipedia.org/wiki/Source-to-source_compiler
- dom96 5y agoYes, but most of the time C is considered to be low-level (and usually is lower level than the language that compiles to it). So “transpiling” is not correct.
- IncRnd 5y agoTranspiling is source language to source language, regardless of whether either of those languages is considered high livel. In this case, transpiler is the correct term.
- kryptiskt 5y agoIt's not the correct choice. For one, transpiling is an unnecessary word, there is no information value in it and the domain experts don't use it and never have. For another, C isn't the target, C is an intermediary language used to produce the binaries. The C produced by these compilers is highly unidiomatic and not meant for human consumption.
- rualca 5y ago> For one, transpiling is an unnecessary word, No, not really. Transpiring is an entirely different problem, and one which is not limited to programming languages. Transpiring involves parsing a high-level language down to an intermediate language, and afterwards encode it back to a high-level language. Therefore problems involving semantics enter into play, instead of just transforming high-level constructs down to primitives.
- chrisseaton 5y ago> the domain experts don't use it and never have Not true! You see it used in the academic literature I think as far back as the 1960s. I also use it myself (PhD in languages and practising professionally ever since - take that as being an expert or not up to you) and I think most of my peers would use it and understand it as well. To me transpiling implies translation from one source language that a human would normally expect to use directly to another. That's extra information over 'compiler'.
- hutzlibu 5y agoWell, but it is targeted at beginners to compiling, right? So step by step, because ... the topic is kind of complicated. I just skimmed over it and it looked clear enough. And C is not the holy grail of anything, but it is a very useful target. Your emotional response of "indoctrinating" I found a bit odd.
- justin66 5y ago> compiling to C gives the student the impression that there is something holy and immutable about C Your compiler is almost certainly going to end up relying on the OS's libc anyway. That is when the student is going to come to the realization that it's C all the way down, if they haven't done so already.