tags:

views:

306

answers:

5

I wonder what is compiling, what happens when you compile? I mean yes you press compile or type in in the console but what does it actually do in the "background"?

+6  A: 

See this

Basically, magic elves and fairies turn human readable code into machine code.

Tom
+1  A: 

A very brief overfiew would be a compiler parses your code checking for errors and then transforms it into byte or machine code.

For a better overview I would read the wikipedia article on compilers:

http://en.wikipedia.org/wiki/Compiler

Mike Lowen
A: 

Compiling is translating source code to machine code. Usually a compiler (or interpreter) will generate an intermediate code sometimes called byte code which runs on a virtual machine (this is how java is compiled). The byte code is translated by the vm to machine specific code thats runs on the particular architecture you are targeting. This whole process can be considered "compiling"

ennuikiller
The target often is, but does not have to be, machine code. The compilers I work on compile to C code - we then use the native C compiler to finish the job of compiling to machine code. Another of them generates a p-code (pseudo machine code - where do the hyphens go?); the result is then interpreted by an interpreter which uses regular C (machine code) functions to execute the p-code instructions. A Java compiler generates byte code for the JVM - that is not machine code either.
Jonathan Leffler
+1  A: 

Or this.

The Dragon Book, the original source for building compilers.

The 'or' is mildly misleading - compared with what? Your suggested reading is quite good, but you should make your answer stand more nearly on its own. Then you'll get the up-votes...
Jonathan Leffler
It was in response to the first answer I found for this question, which was simply "This." as a link to Wikipedia's article on compilers.
+15  A: 
  1. First, the compiler "lexes" the source. This means that it transforms the source into a sequence of "tokens." Tokens are sequences of letters, numbers and symbols that have meaning to the compiler.

  2. Next, the compiler "parses" the sequence of tokens from step one. This means that the compiler checks to ensure that the source conforms to rules (the grammar) of the programming language.

  3. Next, the compiler performs syntactic analysis to create a representation of the source to determine the semantical meaning of the source. This is the step where the compiler will build a syntax tree.

  4. Finally, the compiler will generate output that captures the semantic meaning of the source in the target representation (be it machine code, an intermediate language such as Microsoft's CIL, or another programming language).

For the brief details see Wikipedia. For the gory details see the dragon book (every student of computer science should study this book).

Jason
thank you, "(every student of computer science should study this book)." I was a student of computer science .. I never really knew about that book and I was more focused on programming .. data structures, objects bla bla .. never really wondered "beyond"
c0mrade
+1 for Dragon Book
Pwninstein
Very nice explanation. +1
Alex
Dragon book" is not as gory as it could be. Last time I checked, it mentioned garbage collection no more than couple of times and provided absolutely no information about exception handling. Or I miss them?
maykeye
Don't know what is included in the dragon book, but Modern Compiler Implementation in Java (http://www.cs.princeton.edu/~appel/modern/java/) has all you mentioned and then some. It was what we used in our course at college.
Miha Hribar