Difference between revisions of "Compiler"

From Conservapedia
Jump to: navigation, search
m (add category: Computer Science)
(more detail on phases within a compiler)
Line 1: Line 1:
 
A '''compiler''' is a computer program which translates source code written in a high-level [[programming language]] into executable machine code.  The act of doing this is called [[compilation]].
 
A '''compiler''' is a computer program which translates source code written in a high-level [[programming language]] into executable machine code.  The act of doing this is called [[compilation]].
  
Most compilers perform the following actions, sometimes in a single pass over the source code, and sometimes in several passes.  Program optimisation may take place before and during the 'code generation' stage.
+
==Outline of the compilation process==
 +
In the freely available online book "Basics of Compiler Design"<ref>{{cite web
 +
| url = http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/
 +
| title = Basics of Compiler Design
 +
| accessdate = 2012-03-19}}</ref>, Professor Torben Mogensen identifies seven phases within a compiler, with the various phases being performed in one or more passes over the program.
  
:'''Lexical analysis''': convert source code into sequence of tokens.
+
:'''Lexical analysis''': convert source code into sequence of tokens, such as variable names, keywords, numbers, and special symbols such as '+'.
  
:'''Syntactic analysis''': structure tokens into parse tree.
+
:'''Syntax analysis''' or '''parsing''': structure tokens into parse tree or syntax tree which represents the structure of the program.
  
:'''Semantic analysis''': annotate parse tree with semantic actions.
+
:'''Semantic analysis''': annotate parse tree with semantic actions, and perform various consistency checks.
  
:'''Code generation''': produce the final machine code.
+
:'''Intermediate code generation''': use the annotated parse tree to generate code in some simple machine-independent intermediate language.
 +
 
 +
:'''Register allocation''': map the symbolic names used in the intermediate code on to the registers available in the target machine code.
 +
 
 +
:'''Assembly code generation''': translate the intermediate code into assembly language for the target machine.
 +
 
 +
:'''Assembly and linking''': translate the assembly language into executable machine code.
 +
 
 +
==References==
 +
<references/>
  
 
[[Category:Computer Science]]
 
[[Category:Computer Science]]

Revision as of 16:03, March 19, 2012

A compiler is a computer program which translates source code written in a high-level programming language into executable machine code. The act of doing this is called compilation.

Outline of the compilation process

In the freely available online book "Basics of Compiler Design"[1], Professor Torben Mogensen identifies seven phases within a compiler, with the various phases being performed in one or more passes over the program.

Lexical analysis: convert source code into sequence of tokens, such as variable names, keywords, numbers, and special symbols such as '+'.
Syntax analysis or parsing: structure tokens into parse tree or syntax tree which represents the structure of the program.
Semantic analysis: annotate parse tree with semantic actions, and perform various consistency checks.
Intermediate code generation: use the annotated parse tree to generate code in some simple machine-independent intermediate language.
Register allocation: map the symbolic names used in the intermediate code on to the registers available in the target machine code.
Assembly code generation: translate the intermediate code into assembly language for the target machine.
Assembly and linking: translate the assembly language into executable machine code.

References

  1. Basics of Compiler Design. Retrieved on 2012-03-19.