Java compiler in java [duplicate]

♀尐吖头ヾ 提交于 2019-12-10 09:28:08

问题


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?


回答1:


The original was written in C. Then you can write the next version in Java. :)




回答2:


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!




回答3:


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.



来源:https://stackoverflow.com/questions/4971372/java-compiler-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!