Are compilers in different languages?
Here's a couple of examples:
In general, compilers can be written in any language that is actually powerful enough to write a compiler in. This obviously includes any Turing-complete language. But it might even be possible to write a compiler in a non-Turing-complete language. (For example, I don't see any obvious reason why a compiler couldn't be a total function, but total functions are obviously not Turing-complete.)
In practice, however, compilers are mostly written in three specific classes of languages with different pros and cons:
All of these are essentially tradeoffs: writing the compiler in the same language makes it easier to understand, because you don't have to learn another language. It can also make it harder to understand because the language isn't actually very good at writing compilers. (Imagine, for example, writing a SQL compiler in SQL.) It might even be impossible to write a compiler, for example (for a pretty loose definition of "language" and "compiler") it is impossible to write a CSS compiler in CSS or an HTML compiler in HTML.
On the opposite side: writing the compiler in a specialized compiler-writing language probably makes it easier to understand, but at the same time it requires you to learn a new language.
Note that the three classes are not disjoint: a compiler can fall into more than one class. For example, a compiler for a specialized compiler-writing language, written in itself falls both into category 1 (written in itself) and 3 (written in a language good at writing compilers).
In some cases, you are actually able to hit the sweet spot. For example, F# is a native language with native speed on the CLI, and it is very good at writing compilers. So, writing the F# compiler in F# gives you #1 (writing in itself), #2 (writing in a native, fast language) and #3 (writing in a language that is good for writing compilers). The same applies to Scala.
A compiler could probably be written in any language. In its most basic form, a compiler merely converts code from one language to another. In the sense that most people use the term "compiler" today, they are referring to something that takes in source code of some higher level language and converts it to either assembly or some low level intermediate language (CIL).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With