| You are here: | About>Computing & Technology>C / C++ / C#> Getting Started> What is a Compiler? |
![]() | C / C++ / C# |
What is a Compiler?What actually Is A Compiler?: 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.
What happens When You Compile Code?: 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 AnalysisThis 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" Next is Syntactical Analysis: 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
Had they been declared but not initialized. the compiler would issue a warning
You should never ignore compiler warnings. They can break your code in weird and unexpected ways.
One Pass Or Two?: 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. Generating Machine Code: Assuming that the compiler has successfully completed these stages
The speed of the compiled executable should be as fast as possible and can vary enormously according to
Code Generation Is Challenging!: The compiler writer faces challenges when writing a code generator. Many processors speed up processing by using
Caches And Queues:
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
Compilers typically generate code into object files which are then linked together by a Linker program.
|
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | Print this Page | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


