I'm wondering, if compilers are written in human readable way, then how come the machine could read the compiler?

If machine could read the compiler - which is by itself is not a machine language, why don't the machine just read the high level programming language directly? As it is known, machine only can understand in binaries. If the compiler was written in binaries, in such a way that these binaries can fetch the high level language and translate it back to machine language, it would make more sense, but compilers are not in binaries, so there should be something else.

If a language needs a compiler, then how come a compiler is invented, since this compiler is a language and it will a compiler, see its confusing now!

A compiler is binary, so its been executed, so what compiled the compiler! And the compiler compiler's in what language was written?

I'm sure there are experts here who knows the answer. Waiting your reply.

I'm wondering, if compilers are written in human readable way, then how come the machine could read the compiler?

If machine could read the compiler - which is by itself is not a machine language, why don't the machine just read the high level programming language directly? As it is known, machine only can understand in binaries. If the compiler was written in binaries, in such a way that these binaries can fetch the high level language and translate it back to machine language, it would make more sense, but compilers are not in binaries, so there should be something else.

If a language needs a compiler, then how come a compiler is invented, since this compiler is a language and it will a compiler, see its confusing now!

A compiler is binary, so its been executed, so what compiled the compiler! And the compiler compiler's in what language was written?

I'm sure there are experts here who knows the answer. Waiting your reply.

Simple answer: When a program is written in C, Fortran, Pascal, Algol, Ada or any of a number of other languages, it must be compiled into machine-readable language before the machine can run it. The same goes for almost every compiler; it must be compiled into machine language before a machine can run it. Only a compiler written in machine language would need no translation and could be executed directly. And yes, this does mean that the first compilers, the first assemblers, the first OSen were written directly in machine language. (Actually, they might've been written in an assembler-like pseuo-language, assembled by hand and toggled into the machine using the front-panel switches. Look at pics of most any computer from before 1975 or so; you'll find that pert near all of them had front-panel switches for toggling in data and programs, and for running programs and stepping through memory.)

A compiler *is* a binary machine-language program; it translates a high-level language program into machine (binary) language. A well-written compiler is often used to compile itself. This, of course, introduces the 'chicken (compiler binary) or egg (compiler source)' quandary; but the clear answer is that the chicken must come first. There *must* be an initial compiler to translate the compiler source into machine language; once that boostrap process is complete, the compiler can prepare its own future versions (if it is well-written).

Take the C compiler. Originally, it was written in assembler which is easily translated to machine language. When it was (largely) rewritten in C, the 'assembler' compiler was used to compile it. At some point, the 'C' version became good enough to compile its own source. About that time, the assembler-source version was retired.

As a real-world example, in a class many years ago, I had to write a VAX-11 assembler. Unbeknownst to the professor, I had access to a VAX where I worked at a co-op job. [Note, I no longer remember what I wrote it in, but I'll say VAX-11 Macro (assembler) for the purpose of this illustration.] When it was time to turn it in, it was producing *exactly* the output listing that DEC's assembler produced. A couple more weeks and I would've been able to generate proper binary output suitable for linking. Had I gotten to that point and had my assembler actually produced correct binary, I would have been able to use my assembler (binary) to assemble, you guess it, my assembler (source). And the two versions (Macro-32-assembled and MyOwn-assembled) would have produced identical output *if* I programmed it correctly. (FWIW, even though my assembler produced no binary output, I still got an 'A' on the project because my assembler's output listing showed that it correctly handled all of the labels, instructions and addressing modes available on the VAX; from there, producing linkable binary would've been trivial.)

Prolly more than you wanted to know, but I hope I adequately answered your question.

Well Answered....MANY THANKS! Even though, i did not understand fully the extra part you were talking about, but i got the gist....Good explaination

You're referring to the compiler bootstrapping process, which is a sort of chicken and egg scenario. The gist of it is that early compilers/assemblers were written in machine code in a very basic form, then used to compile more complex compilers. Nowadays there are usually more sophisticated methods for bootstrapping compilers, such as using a different platform with an existing compiler to cross-compile to your target platform. Because of this, you'll find that a lot of compilers are actually written in the language that they compile (for example, ghc is written almost entirely in Haskell). You can read more about this on the web:

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

compiler high level language and machine language is like: English man Swahili man and one who knows both swahili and english. The one writes a paper in english and the compiler: who now knows both language takes that written and translate it all into swahili language. Then when took to any swahili speaking man can read that script. that english is HIGH LEVEL LANGUAGE and swahili is MACHINE LANGUAGE and translator of script is COMPILER

Hope you can understand now!

Hi,

While the above responses are correct, I think they have missed one signifcant point. The reason for compilers is speed. Speed for both programming, and speed for for program execution.

If all programs were written in machine code using binary, they would take a very long time to write, as the programmer would have to do the translation themself. While effective, this, of course, would be incredibly inefficient from a time and/or resource cost perspective.

Further, while it is possible for the computer to read the source code directly, and therefore translate the source code into executable code, this is not the only function of the compiler. The compiler also checks the source code for obvious errors which would prevent the program from running, such as incorrect syntax, spelling errors (of control and/or command words), ensuring all if statements, especially nested ifs, have the correct end, etc., which the programmers often overlook, despite vigilant proofreading. I know this well, as I have programmed since the early 1970's in a number of languages, and I have proofread not only my own programs and documentation, from business analyses to business/program specifications to source code to user manuals, but those of others as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.