A compiler is a program that translates human readable source code into computer executable machine code. To do this successfully the human readable code must comply with the syntax rules of whichever programming language it is written in. The compiler is only a program and cannot fix your programs for you. If you make a mistake, you have to correct the syntax or it won't compile.
A compiler's complexity depends on the syntax of the language and how much abstraction that programming language provides. A C compiler is much simpler than
Here is what happens when you compile code.
Lexical Analysis
This is the first process where the compiler reads a stream of characters (usually from a source code file) and generates a stream of lexical tokens. For example the C++ codeint C= (A*B)+10;might be analysed as these tokens:
type "int"
variable "C"
equals
leftbracket
variable "A"
times
variable "B"
rightbracket
plus
literal "10"
This output from Lexical Analyzer goes to the Syntactical Analyzer part of the compiler. This uses the rules of grammar to decide whether the input is valid or not. Unless variables A and B had been previously declared and were in scope, the compiler might say
- 'A' : undeclared identifier.
Had they been declared but not initialized. the compiler would issue a warning
- local variable 'A' used without been initialized.
You should never ignore compiler warnings. They can break your code in weird and unexpected ways.
- Always fix compiler warnings!
Some languages have been written so that a compiler can get away with reading the source code once and generating the machine code. Pascal is one such language. Many compilers require at least two passes. Why is this?
Sometimes it is because of
In C++ a class can be declared but not defined until later. The compiler will be unable to work out how much memory the class needs until it has compiled the body of the class. Then it must reread the code before generating correct code.
Assuming that the compiler has successfully completed these stages
- Lexical Analysis.
- Syntactical Analysis.
The speed of the compiled executable should be as fast as possible and can vary enormously according to
- The quality of the generated code.
- How much optimization has been requested.
The compiler writer faces challenges when writing a code generator. Many processors speed up processing by using
- Instruction Pipelining.
- Internal caches.
Most CPUs have a prefetch queue where the CPU reads in instructions into the cache prior to executing them. If a conditional branch happens then the CPU has to reload the queue. So code should be generated to minimize this.
Many CPUs have separate parts for
- Integer Arithmetic
- Floating Point Arithmetic
Compilers typically generate code into object files which are then linked together by a Linker program.
- Compilers and Interpreters - Some further reading.

