Java compiler in java [duplicate]
Possible Duplicate:
implementing a compiler in “itself”
I was reading about the Java compiler and realized that it was written in Java. How can that be possible?
I mean, isnt this like "Chicken and egg" problem? How can we write the compiler in the same language?
The original was written in C. Then you can write the next version in Java. :)
The problem you are addressing is really a problem with any compiler. For example, many compilers are written in C. Well...how do you compile a C compiler without having a C compiler without...etc, etc, etc.
The process you are looking for is called bootstrapping. From Wikipedia (and see the article for more information and links to other articles):
Bootstrapping is a term used in computer science to describe the techniques involved in 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.
A large proportion of programming languages are bootstrapped, including BASIC, C, Pascal, Factor, Haskell, Modula-2, Oberon, OCaml, Common Lisp, Scheme, Python and more.
The article also addresses the chicken and the egg problem that you pointed out. I could quote it all here, but, hopefully this get you started in understanding it. Great question!
Once you get enough facilities in a programming language, you can write it's own compiler by simply treating the compiler like a program that takes in "certain formatted" input and writes out "certain formatted" output.
In other words, there is no dependency on the language the compiler is implemented in and the language that it compiles. By no dependency, this includes no need for it to be the same AND no need for it to be different.
The original compiler was obviously not written in Java. It was written in C; however, it is possible to have strong documentation and just write the "compiled code" using a hex editor if you really want to side step the technique of bootstrapping. While it is possible to sidestep bootstrapping, it is advisable to bootstrap because writing object code in a hex editor is incredible time-expensive and error prone.
精彩评论