Re:Writing a C Compiler (2017)
by userbinator on Friday June 09, 2023 @09:33AM
(#36257587)
IMHO writing a compiler for a high-level language, in an even higher level language, somehow feels a bit "anachronistic" (for lack of better word).
[ Parent
| on HN ]
Most of the current major C implementations are written in C++.
Re:Writing a C Compiler (2017)
by WalterBright on Friday June 09, 2023 @10:57AM
(#36258786)
ImportC is written in D.
Re:Writing a C Compiler (2017)
by userbinator on Friday June 09, 2023 @03:34PM
(#36262965)
That's unfortunate.
Re:Writing a C Compiler (2017)
by avgcorrection on Friday June 09, 2023 @09:43AM
(#36257703)
For what reason?
Re:Writing a C Compiler (2017)
by userbinator on Friday June 09, 2023 @03:33PM
(#36262952)
It's backwards. Writing a C compiler in C or Asm makes sense, a Python compiler in C also does, but a C compiler in Python is an odd inversion of abstraction.
Why? The objective is to translate code in one language (C) to another (machine code or assembly or perhaps an intermediate representation). Why does it make sense to use C for that task and not Python or some other language? It's not like C provides facilities that specifically enable compiler writing or text parsing for itself that other languages are lacking.
Re:Writing a C Compiler (2017)
by avgcorrection on Friday June 09, 2023 @04:19PM
(#36263606)
I guess this harkens back to the days when you had to write a compiler in a low-level language because that’s all that the platform that you are targeting supports. Then it sounds weird to talk about writing a compiler in a high-level language in order to target a low-level one, because surely these high-level languages are more platform-dependent than the blessed (guaranteed on the platform) low-level one.
But these days we can access dozens of languages on many platforms. And we can use high-level languages that are good for writing compilers—languages with good string types and algebraic data types—instead of being limited to awfully imperative/procedural ones.
In other words: your perspective sounds way more anachronistic.
Weren't a lot of functional languages, like ML and it's descendants, created specifically to write parsers and compilers?
I'm not sure about specifically. A joyful synergy, maybe. The fact that compilers are, in essence, pure functions, maps from input programs to output programs, was recognized quite early on. I can't put my finger on the origin of the idea, but it may have been understood by Turing himself. Well-understood in the Lisp world by the late 1960s, before ML came along. Parsers and compiler data structures, translate extremely well into recursive functions, operating on algebraic data types. These are applicable to many other problems, but compilers have always had centre place in the functional showroom. Sometimes I do wonder if it's just navel-gazing, though -- look, functional languages are useful for implementing functional languages!
Re:Writing a C Compiler (2017)
by JonChesterfield on Friday June 09, 2023 @10:11AM
(#36258069)
I've seen it claimed that ML was originally written, in lisp, in order to have a better language to write compilers in.
ML was written as the language used by the theorem prover LCF. It was written in Lisp.
Re:Writing a C Compiler (2017)
by JonChesterfield on Friday June 09, 2023 @08:51PM
(#36266400)
I didn't know that link, searches seem to confirm. Thank you.
The LCF style provers rely on the host language type system. A theorem instance can only be created by the trusted kernel. Lisp doesn't obviously lend itself to that - you could represent the proof, but I'm not clear how you'd prevent forging one.
Re:Writing a C Compiler (2017)
by peterfirefly on Friday June 09, 2023 @11:15AM
(#36259087)
No. ML was the meta language for a theorem prover (LCF).
https://en.wikipedia.org/wiki/Logic_for_Computable_Functions