I've always been curious about how compilers and interpreters work, especially considering they're written in programming languages themselves. If they are programs, how was the first one ever created? Also, how is it possible to write a compiler in the same language it compiles?
5 Answers
Check out the concept of a 'bootstrap compiler.' The very first compilers were often written in different languages. They start with the basics needed to compile programs in a new language. Once that's up and running, further iterations can be developed using the new language itself. For interpreters, they usually originate from different programming languages than the ones they interpret. Believe it or not, the first compiler was essentially an Assembler, created in machine code that was manually translated from Assembly!
When it comes to compilers, the first one typically gets written in another language. For example, the C compiler was originally crafted in B. After that, it can compile a bootstrap compiler made in its own language, allowing it to function independently. However, for interpreters, things are trickier. They don't create binary executables, so you can't bootstrap them in the same way as a compiler.
The original compiler was usually built using machine instructions. Most modern compilers can bootstrap from existing code, sometimes switching between different languages. For instance, you might compile a Python interpreter using a C compiler. Some earlier versions of compilers can also build upon each other, like using an older GCC version to compile a newer one.
Creating a new compiler often starts with writing it in a high-level language or assembly. Afterward, it's possible to reimplement it in the language it compiles—this is referred to as 'self-hosting'. Interpreted languages are different since they don't create an executable; they need an existing interpreter to run and can't be rewritten just using the language they interpret. The first compiler was crafted either in assembly or directly in binary machine code. It's fascinating how far we've come since then!
If you're confused about the creation of compilers and interpreters, it helps to consider what a program is at a fundamental level. They are just rules and structures—nothing stops you from writing a compiler following the same rules even if it’s initially written in the language it processes. Just like teaching someone English using a shared language, compilers can bootstrap themselves once you grasp the basics!

Thanks for the info! Now I'm intrigued about bootstrap compilers. I might check out some resources to dive deeper!