10
$\begingroup$

I'm a computer science student. I want to create my own programming language (A basic language with few instructions).

I know how to do a syntactic analyser, I already did it in Perl. In an article, I read something about the compiler, a compiler is done in itself.

For example the C compiler is written in C. How it's possible? I can make my own language but I don't know how I could execute it? Any idea?

It's really a good question and I can write a blog the project.

$\endgroup$
  • $\begingroup$ People have explained how to bootstrap but why do you need to? There's no reason you can't compile your language using a compiler written in C, Perl or anything else. Sure, it would be nice to have a compiler for your language written in itself but that would be a lot of work -- you'd have to write at least two compilers to get that (one in C/Perl/whatever, one in your language). $\endgroup$ – David Richerby Oct 20 '13 at 8:34
  • $\begingroup$ Hmm.. I think I will write my first compiler in C and write a seconde in my language. It's really interesting to create a little programming language, we can learn a lot of the computer science $\endgroup$ – BaptisteL Oct 20 '13 at 8:38
14
$\begingroup$

The trick is bootstrapping. You first write a compiler for your language (or a subset thereof) in some other language. Then you write a compiler for your language (or a large subset of the one you can already handle) in your language. You use the former compiler to compile the new compiler, and then the new compiler can compile itself.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ So, I can create my first compiler using C and next time I will compile my compiler v2 with the v1 ? But there is a problem, how could I know the compiler doesn't have any problem ? I need to convert my source code to assembler ? Or in other thing ? $\endgroup$ – BaptisteL Oct 20 '13 at 8:19
  • 2
    $\begingroup$ How could I know the compiler doesn't have any problem? More generally, you wrote a program; how do you know it has no bugs? You don't. You write some tests and hope for the best. Do I need to convert my source code to assembly? Definitely not. You can trust the C compiler. You are doing the same thing, but with assembly replaced with C (or any other language of your choice). $\endgroup$ – Yuval Filmus Oct 20 '13 at 18:12
  • $\begingroup$ Okay I have to trust in my skills ^^ I wanted to ask the process do the compiler. I really don't understand what a compiler do, syntaxic and lexical analyzer okay but next ?? $\endgroup$ – BaptisteL Oct 20 '13 at 21:34
  • $\begingroup$ Then you generate code. If you don't want to get into the details of generating machine code, you can always generate code in some other language, say C code, and use an external compiler to compile that into machine code. (Or you could use an interpreted language and run an interpreter.) $\endgroup$ – Yuval Filmus Oct 21 '13 at 1:06
  • $\begingroup$ Hummm No I prefere do it like that : My Language in enter --> Machine Code. I don't want to convert my language in another (same for first time compile) It is possible ? $\endgroup$ – BaptisteL Oct 21 '13 at 2:57
3
$\begingroup$

A compiler which can compile its own sources is called a self-hosting compiler. Early compilers were written in another language. For example, the first C compiler was probably written in assembler. The whole trick in using a former lower level compiler is called bootstrapping.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.