It's really a good question and I can write a blog the project. Improve this question. Bartosz Przybylski 1, 1 1 gold badge 11 11 silver badges 19 19 bronze badges. BaptisteL BaptisteL 1 1 silver badge 6 6 bronze badges. There's no reason you can't compile your language using a compiler written in C, Perl or anything else. I think I will write my first compiler in C and write a seconde in my language. Add a comment. Active Oldest Votes. Improve this answer. Yuval Filmus Yuval Filmus k 25 25 gold badges silver badges bronze badges.
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? 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. I really don't understand what a compiler do, syntaxic and lexical analyzer okay but next??
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. Yes, that's exactly what that statement means, and I hope you can see now how that statement is true.
It means exactly that. First of all, some things to consider. There are four objects we need to look at:. Now, it should be obvious that you can use the generated assembly - the executable - of the CoffeScript compiler to compile any arbitrary CoffeScript program, and generate the assembly for that program.
Now, the CoffeScript compiler itself is just an arbitrary CoffeScript program, and thus, it can be compiled by the CoffeScript compiler.
It seems that your confusion stems from the fact that when you create your own new language, you don't have a compiler yet you can use to compile your compiler. This surely looks like an chicken-egg problem , right? Introduce the process called bootstrapping. Now you need to add new features.
Say you have only implemented while -loops, but also want for -loops. This isn't a problem, since you can rewrite any for -loop in such a way that it is a while -loop. This means you can only use while -loops in the source code of your compiler, since the assembly you have at hand can only compile those. But you can create functions inside your compiler that can pase and compile for -loops with it. Then you use the assembly you already have, and compile the new compiler version.
And now you have an assembly of an compiler that can also parse and compile for -loops! You can now go back to the source file of your compiler, and rewrite any while -loops you don't want into for -loops.
And then you are in the situation CoffeScript is in now: The compiler compiles itself. There is much literature out there. Reflections on Trusting Trust is a classic everyone interested in that topic should read at least once.
Here the term compiler glosses over the fact that there are two files involved. One is an executable which takes as input files written in CoffeScript and produces as its output file another executable, a linkable object file, or a shared library.
The other is a CoffeeScript source file which just happens to describe the procedure for compiling CoffeeScript. You apply the first file to the second, producing a third which is capable of performing the same act of compilation as the first possibly more, if the second file defines features not implemented by the first , and so may replace the first if you so desire. Since the Ruby version of the CoffeeScript compiler already existed, it was used to create the CoffeeScript version of the CoffeeScript compiler.
This is known as a self-hosting compiler. It's extremely common, and usually results from an author's desire to use their own language to maintain that language's growth.
It's not a matter of compilers here, but a matter of expressiveness of the language, since a compiler is just a program written in some language. These languages are called universal languages. In order to be able to understand this, think about a metal lathe. It is a tool used to shape metal. It is possible, using just that tool, to create another, identical tool, by creating its parts.
Thus, that tool is a universal machine. Of course, the first one was created using other means other tools , and was probably of lower quality. But the first one was used to build new ones with higher precision.
A 3D printer is almost a universal machine. You can print the whole 3D printer using a 3D printer you can't build the tip that melts the plastic. But the first version of the compiler written in X must be compiled by a compiler for X that is written in a language other than X. This step is called bootstrapping the compiler. Compilers take a high-level specification and turn it into a low-level implementation, such as can be executed on hardware. Therefore there is no relationship between the format of the specification and the actual execution besides the semantics of the language being targeted.
Cross-compilers move from one system to another system, cross-language compilers compile one language specification into another language specification. Basically compiling is a just translation, and the level is usually higher-level of language to lower-level of language, but there are many variants.
Bootstrapping compilers are the most confusing, of course, because they compile the language they are written in. Don't forget the initial step in bootstrapping which requires at least a minimal existing version that is executable.
Many bootstrapped compilers work on the minimal features of a programming language first and add additional complex language features going forward as long as the new feature can be expressed using the previous features. If that were not the case it would require to have that part of the "compiler" be developed in another language beforehand. While other answers cover all the main points, I feel it would be remiss not to include what may be the most impressive example known of a compiler which was bootstrapped from its own source code.
Using paper and pen, he wrote out source code for a simple TMG compiler Now, if only he had a TMG interpreter, he could use it to run his TMG compiler on its own source code, and then he would have a runnable, machine-language version of it.
It was a slow one, but since the input was small, it would be fast enough. Doug ran the source code on that paper on the TMG interpreter behind his eye sockets, feeding it the very same source as its input file. As the compiler worked, he could see the tokens being read from the input file, the call stack growing and shrinking as it entered and exited subprocedures, the symbol table growing After the compiler finished execution and exited successfully, Doug brought the resulting hand-written assembly listings to a computer terminal, typed them in, and his assembler converted them into a working compiler binary.
So this is another practical??? Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How can a compiler compile itself? Ask Question. Asked 5 years, 5 months ago. Active 1 year, 2 months ago. Viewed 10k times. Another term for a compiler that can compile itself is a self-hosting compiler. See programmers. Why shouldn't a compiler be able to compile itself?
There are at least two copies of the compiler involved. A pre-existing one compiles a new copy. The new one may or may not be identical to the old one.
Compilers provide many options and settings that you can use depending on what properties you want the compiled program to have e. These have to do with optimizations, specific computer architectures, etc There are many compilers for C, but we will focus on a free open source version called the Gnu C compiler. It will run under Linux or Windows. The most recent version of the compiler can be found here: Gnu Web Page.
Compiling Compiling is the process of transforming a high level language into a low level langauge. Compiling a C Program Compiling is the transformation from Source Code human readable into machine code computer executable.
0コメント