Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a language's compiler be written in that language? [duplicate]

Tags:

Possible Duplicates:
implementing a compiler in “itself”
Bootstrapping a language

How can you write a compiler in the same language as the language you're writing that compiler for? Isn't that sort of recursive?

Edit: This may be deleted, but otherwise... :

How to bootstrap:

  • Writing a compiler in its own language
  • Bootstrapping a language

Why to bootstrap:

  • Bootstrapping a compiler: why?
like image 426
froadie Avatar asked Jun 08 '10 15:06

froadie


People also ask

How can compiler of a language be written in the same language?

If the compiler is written in the same language as the one it's compiling, the compiler is called self-hosting. If a language is new, then no compiler exists for it already, which means that its first compiler cannot be self-hosting (otherwise, how would that first compiler be compiled?).

Can you write a compiler in any language?

Show activity on this post. A compiler could probably be written in any language. In its most basic form, a compiler merely converts code from one language to another.

What language is compilers written in?

compiler, computer software that translates (compiles) source code written in a high-level language (e.g., C++) into a set of machine-language instructions that can be understood by a digital computer's CPU. Compilers are very large programs, with error-checking and other abilities.

How can a language be written in itself?

Yes it's called Bootstrapping. In computer science, bootstrapping is the process of writing a compiler (or assembler) in the target programming language which it is intended to compile. Applying this technique leads to a self-hosting compiler.


2 Answers

Generally the first version of the compiler is written in a different language, and then each subsequent version is written in that language and compiled with the older version. Once you've compiled version x with version x-1, you can use the newly built version x to recompile itself, taking advantage of any new optimizations that version introduces; GCC does its releases that way

like image 80
Michael Mrozek Avatar answered Jan 18 '23 23:01

Michael Mrozek


It is. You usually need a bootstrap version of the language either compiled or interpreted from another language.

And to bend your mind a little more, years ago I read the history of a Pascal compiler written as a grad student project. It written in Pascal and compiled with the system's built-in Pascal compiler. Eventually, it was good enough to replace the system's built-in Pascal compiler. Unfortunately, they found a bug in code generation, but the fix for the code generator triggered the bug in the compiler, generating a bad compiler. To fix it required hand-patching the binaries from the installed compiler to then apply the patch to the source to replace itself.

like image 22
plinth Avatar answered Jan 19 '23 01:01

plinth