From: | torbenm@pc-003.diku.dk (Torben =?iso-8859-1?Q?=C6gidius?= Mogensen) |
Newsgroups: | comp.compilers |
Date: | Wed, 20 May 2009 12:25:39 +0200 |
Organization: | Department of Computer Science, University of Copenhagen |
References: | 09-05-080 |
Keywords: | practice |
Posted-Date: | 20 May 2009 10:01:07 EDT |
armencho@gmail.com writes:
> What is the importance with a compiler that is able to compile itself?
The short answer is "it's not". As long as you write your compiler in
a language that allows you to run it on the platforms you need, you
should choose the implementation language for ease of writing a
compiler rather than for being the same as the language that your
compiler compiles. This is especially true if the lnaguage is new --
the first compiler or interpreter needs to be written in another
language.
That said, there are some reasons why it is sometimes useful to write
a compiler in its own subject language:
- If the compiler evaluates constant expressions, it can be easier to
ensure that these compile-time evaluations yield the same result as
run-time evaluations.
- As others have mentioned, self-application is a large nontrivial
test that you don't need to write new programs for and where the
result is easy to check. It is, however, not a rigorous and
certainly not an exhaustive test.
- Once your compiler is complete, you don't have to rely on other
people's compilers. There is, certainly, a personal satisfaction
to this, and you are less likely to be hurt if the vendor of the
compiler you use suddenly folds or changes things that make your
compiler stop working.
But there are, certainly, disadvantages too:
- The language you want to compile may be horrible for writing
compilers. C is a good example of this (in spite of the many
bootstrapped compilers for C).
- Using the compiler itself for compiling your compiler may make
subtle bugs be undetected. For example, if you have misunderstood
a certain language feature that you use in your compiler, the
compiler will compile it as you expect it to work, so you get the
result you expect. Using another compiler will give you an
unexpected result, so you will discover your misunderstanding
earlier. Ideally, you should compile your compiler using several
different existing compilers.
- If you get a wrong result when applying your compiler on a
particular language construct, the error might be in either the
part of your compiler that handles this language construct or in
the part of the compiler that compiles this part of the compiler.
- A newly designed language needs, initially, a compiler or
interpreter written in an existing language. You can make this
without optimisation and with missing features, and use this to
perform the bootstrap of a compiler written in its own language,
but it is extra work.
Torben
Return to the
comp.compilers page.
Search the
comp.compilers archives again.