4

I am trying to learn the very details of programming, and I am now learning about compilers. I found my self in a chicken and egg situation. We need a compiler to run a code or a program, and a compiler in its self is a program, then which language is used to write compilers?

Thank you in advance

  • possible duplicate of [How was the first compiler written?](http://stackoverflow.com/questions/1653649/how-was-the-first-compiler-written) – nawfal Jul 21 '14 at 11:23

4 Answers4

1

I found my self in a chicken and egg situation. We need a compiler to run a code or a program.

This logic is not actually correct. You don't need a compiler to run code or a program. A compiler is used to convert human readable code into executable code. There is more than one way to create executable code, and you don't have to start with human readable source code.

Which language is used to write compilers?

Just about any language can be used to create a compiler. The compiler can output assembly code which is then assembled into an executable, create the executable directly, or an intermediate language which is either interpreted or converted to an executable.

Also, you can write the first compiler for a language in another language and then through bootstrapping, create the compiler for the language in the new language.

See: Implementing a compiler in “itself”

For more details see Basics of Compiler Design and in particular Chapter 13: Bootstrapping a compiler.

Community
  • 1
  • 1
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
0

The first compilers were written in Assembly Language.

See http://en.wikipedia.org/wiki/History_of_compiler_construction

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
0

The first compilers were built with assembly, but after that, assembly compilers could be used to compile other compilers in different languages. I vaguely remember The C Programming Language mentioning that a C compiler was written in C after the assembly compilers were written. So, now compilers can be written in any language, because we have compilers that turn those compilers into machine instructions. In the end, its all memory, so everything becomes binary.

trifork
  • 57
  • 1
  • 10
0

CPUs execute what is known as machine code. Machine code (or assembly) come in different variations, the instruction set (language) depends on the CPU architecture.

Of course any programs written for a new totally new instruction set needs to be written directly in assembly, this is also the case for compilers. The initial compiler for each instruction set has to be written in assembly. When you have a working compiler for that instruction set it's possible to rewrite the compiler in C or what ever language the assembly compiler supports.

The GNU Compiler Collection(GCC) is arguably one of the most used compilers to date, it's written in C++.

In short though, the first compiler for a new instruction set is written in assembly.

EDIT: Your questions made me curious about how to implement a compiler for a new language. Turns out this is done via a process called bootstrapping, I don't fully understand it myself yet, but the general idea is writing the compiler for your new language in the language itself. I know it's a little weird. I found some pretty nice information on this.

Thank you for a really nice question by the way.

Community
  • 1
  • 1
Hugo Tunius
  • 2,869
  • 24
  • 32