Definition[edit | edit source]
A compiler is a program that reads source language and translates it into another language. Typically, a compiler reads source code, translates it into machine language, and writes the machine language to binary (object) code that can be directly loaded and executed. Common compiled languages include C and C++.
An interpreter is similar in that it is a program that reads source code one statement at a time, translates that statement to machine language, executes the machine language statement, then continues with the next statement. Common interpreted languages include Java and PHP.
BASIC is sometimes interpreted and sometimes compiled. Although less convenient for programming, compilers are much faster than interpreters.
Compiler Structure[edit | edit source]
A compiler consists of many phases, each phase consisting of some of steps. Each phase take input from previous phase (except the first phase, which takes its input from the source code created by a programmer).
- Scanner Phase (Lexical Analyzer)
The input for this phase is a source code file, the output is set of tokens.
- Parser Phase (Syntax Analyzer)
The input for this phase are set of tokens , the output is syntax tree.
- Semantic Analyzer Phase
The input for this phase is syntax tree , the output is annotated tree.
- Source Code Optimizer Phase (Optional phase).
The input for this phase is annotated tree, the output is intermediate code.
- Code Generator Phase.
The input for this phase is intermediate code, the output is target code.
- Target Code Optimizer Phase.
The input for this phase is target code, the output is optimizing target code.