2

Possible Duplicate:
Bootstrapping a language

Compilers are themselves written in high level languages. Then how does the computer understand the compilers? They convert the HLLs into machine level codes, but how are they themselves understood by the computer?

Community
  • 1
  • 1
Pooja
  • 528
  • 2
  • 6
  • 14

3 Answers3

3

One compiler was written in machine code. The rest can just use it or other already compiled compiler (including future versions of itself, that can use the already existing version to recompile the new version.)

P.S. Note that most compilers do NOT compile the "high level code" into a machine code, they have 2 components, front-end and back-end. The front-end compiles the code to an intermediate middle language, and the back-end compiles the middle language to machine code.

This ensures that if you have n languages and m architectures, you only need n+m components and not n*m if you had a compiler per language/machine

amit
  • 175,853
  • 27
  • 231
  • 333
  • While the front-end/back-end separation is indeed vital for practical compiler construction, I don't think OP's phrasing is contradicting that: The compiler, as a whole package, *does* translate from one language to another. The IRs only matter when you gut the beast and look at internals. –  Feb 01 '13 at 19:04
1

One version needs to be written in assembler code. Then, you can use this compiler to compile other programs including other compilers.

However, an interesting paper / observation is that compilers can learn. That is, you can remove parts of the source code and still keep the functionality ;) One example is the parsing of the \ncharacter. The following paper shows this very nicely! http://cm.bell-labs.com/who/ken/trust.html

Dan
  • 1,539
  • 12
  • 23
1

A compiler for the language X, written in the language Y (X and Y may, or may not be the same language) must be compiled with a compiler for the language Y into machine code (or if Y is meant to be interpreted, then the compiler must be executed by a Y interpreter). There is no way around this, really. Unless Y is assembly. i.e. you write the X compiler in assembly.

If you want to write a compiler in language X for language X, then you could do it in this way:

  1. You write a compiler for the X language in another language, say Y, for which a compiler already exists (or in assembly)

  2. Then you write your X compiler in X, and compile it with the X compiler written in language Y (the one you wrote in step 1).

  3. If the new X compiler is complete enough, it should be able to compile copies of itself (because they are after all, X source).

This is called bootstrapping.

ArjunShankar
  • 23,020
  • 5
  • 61
  • 83