views:

388

answers:

13

This question is just something that I have been thinking about lately. Can a programming language be written in that language as a second implementation? e.g. Java. Is it possible to rewrite the java programming language using the java programming language?

Apologies if this is a silly question but I need to know!

GF

+10  A: 

Always. Any Turing-Complete language is -- well -- a Turing-Complete language. If you can write the compiler in one complete language, you can write it in any equivalent language.

S.Lott
+3  A: 

Yes. As long as the language is Turing Complete, you can implement the language in itself.

Vivin Paliath
Turing-completeness is not strictly a requirement IIRC. But the existing Turing-incomplete languages (Regex, SQL and such) are still insufficient.
Seva Alekseyev
You can read a regex using regex, but I think it's impossible to create a regex implementation using it. The turing completeness is theory, in order to write a java compiler you need to manipulate bits and write to disk (Java can do this).
tovare
@tovare: Actually you cannot even [verify regex using regex](http://stackoverflow.com/questions/172303/is-there-a-regular-expression-to-detect-a-valid-regular-expression/172363#172363) (ie. the language we use to specify regular languages is not regular)!
BlueRaja - Danny Pflughoeft
@Seva I didn't mean to imply that it was a strict requirement; just that if a language is Turing Complete, then what the OP suggests is possible. :)
Vivin Paliath
@BlueRaja - Danny Pflughoeft: you're absolutely righ I didn't think about the fact that the regex laguage isn't regular with constructs such as the matching parenthesis.
tovare
+8  A: 

Yes, it is possible. Check out BootStrapping.

Moron
+1 I was racking my brain (and going on Google) to search for that term. I vaguely remembered something about C compilers being successively implemented in itself.
Vivin Paliath
+1  A: 

It not only can, it is. ecj (Eclipse's compiler) is one example, and I think the SDK itself comes with a pure Java compiler, though I could be wrong about that.

Mark Peters
+7  A: 

Yes for any Turing Complete language. Lisp comes to mind as one of the easiest languages to write an interpreter/compiler for itself.

Mandelbrot
+2  A: 

There are many practical examples of this, one example is the Oberon Language, which is of interest in this discussion because the compiler code is very readable it's in the book Project Oberon available for free:

http://www.oberon.ethz.ch/bibliography/publications

http://en.wikipedia.org/wiki/Bootstrapping_(compilers)

tovare
Interesting links.thanks.
+6  A: 

It can. A recent example is that python has pypy. A little more information is on the Wikipedia page and some good links.

Zakmobl
AFAIK Pypy does not interpret, it compile the the VM.
mathk
Pypy is Python interpreter written in a reduced subset of Python (RPython). However the project can construct a C version and even a tracing JITC by doing transformations on the RPython source.
Axel Gneiting
Yes but can pypy interpret the VM being design?
mathk
Of course it can. ./pypy-c pypy.py works.
Axel Gneiting
A: 

Check out 3-LISP

mathk
A: 

Sure. I've even seen someone write a COBOL compiler written in COBOL! (OK, not a full compiler ... but at least a parser.)

Stephen C
+2  A: 

Sure.

Many many years ago one of my first home computers, a Vic 20, came with a built-in BASIC interpreter but that was it. So I wrote the first version of an assembler for it in BASIC. Then I used my first primitive assembler to write a better assembler.

Jay
+2  A: 

The GCC compilers are written in C.

It has been a long time since anyone built any C compilers from assembly.

Zan Lynx
+1  A: 

write a java compiler in java - no problem at all. actually I think Sun's javac is written in java.

however, 'java' usually means more things than just the javac, so your question isn't very clear.

irreputable
+2  A: 

Not just possible, but for native-code compilers, this is the most common implementation technique. A good how-to guide is Andrew Appel's paper Axiomatic Bootstrapping: A Guide for Compiler Hackers.

Norman Ramsey