CSC376 · 2081 · 5 marks
Explain different phases of compiler in brief.
Explain different phases of compiler in brief. [5]
Quick answer
A compiler operates in phases, where each phase takes a program in one representation and produces output in another representation. There are two major phases of compilation: The source program is read left-to-right and grouped into tokens; Tokens are sequences of characters with a collective meaning (e; Input.
Phases of a Compiler
A compiler operates in phases, where each phase takes a program in one representation and produces output in another representation. There are two major phases of compilation:
1. Analysis Phase (Front End)
In the analysis part, an intermediate representation is created from the given source program. It consists of the following phases:
i) Lexical Analysis (Scanning)
- The source program is read left-to-right and grouped into tokens.
- Tokens are sequences of characters with a collective meaning (e.g., constants, operators, reserved words).
- Input: Source program
- Output: Stream of tokens
ii) Syntax Analysis (Parsing)
- The tokens produced by the lexical analyzer are grouped into grammatical phrases using the grammar of the programming language.
- A parse tree or syntax tree is produced to show the hierarchical structure of the token stream.
- Input: Stream of tokens
- Output: Parse tree / Syntax tree
iii) Semantic Analysis
- Checks whether the parse tree follows the rules of the language (e.g., type checking, scope resolution).
- Ensures that the program is semantically correct.
- Input: Parse tree
- Output: Annotated parse tree
iv) Intermediate Code Generation
- An intermediate representation (e.g., three-address code) of the source program is generated.
- This code is easy to produce and easy to translate into target code.
- Input: Annotated parse tree
- Output: Intermediate code
2. Synthesis Phase (Back End)
In the synthesis part, the equivalent target program is created from the intermediate representation. It consists of the following phases:
i) Code Optimization
- Improves the intermediate code so that the output program runs faster and takes less space.
- Removes unnecessary lines of code and rearranges statements to speed up execution.
- Example:
t2 = id3 * 3.0;
id1 = id2 + t2;
ii) Code Generation
- The final phase that translates the optimized intermediate code into target machine code (or assembly code).
- Memory locations are assigned to variables and intermediate results.
- Input: Optimized intermediate code
- Output: Target machine code
Supporting Component: Symbol Table
Throughout all phases, the symbol table is used. It is a data structure that holds information about source-program constructs (identifiers), such as:
- Type
- Position in storage
- Other relevant information
The analysis phase collects information into the symbol table, and the synthesis phase uses it to generate target code.
Summary Diagram
Source Program
|
Lexical Analysis |
| | Analysis
Syntax Analysis | Phase
| | (Front End)
Semantic Analysis |
|
Intermediate Code Gen |
| |
Code Optimization | Synthesis
| | Phase
Code Generation | (Back End)
|
Target Program
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.