This document summarizes the six phases of compiler design: 1) lexical analysis, 2) syntax analysis, 3) semantic analysis, 4) intermediate code generation, 5) code optimization, and 6) code generation. It provides details on what each phase is responsible for, such as breaking down source code into tokens, building a syntax tree, type checking, generating intermediate code, optimizing code, and finally generating target machine code. The document uses examples and diagrams to illustrate the different phases in the compilation process.
5. Phase 1: Lexical Analyser
1. The lexical analysis phase is the first step in the compilation process.
2. Its main purpose is to analyse the source code and break it down into individual tokens
3. These tokens are then passed on to the next phase , which is syntax analysis phase
Code:
7. Phase 2: Syntax Analysis
The syntax analysis is responsible for analysing the structure of the code and ensuring that it
adheres to the rules of the programming language.
This phase uses a grammar to parse the tokens generated in the previous phase and build a
syntax tree(Parse tree).
If a>5 then y=10 else y=20
Expression Statement Statement
If Statement
Statement
8. Phase 3: Semantic Analysis
The semantic analysis phase is responsible for analysing the meaning of the
code and ensuring that it makes sense.
This phase checks for things like type compatibility, variable declaration and
function calls. If the code does not make sense errors are generated.
Task of semantic analyser:
1). Type checking
2). Scope checking
3).Handling errors
9. Cont…
In this semantic tree, I've annotated 'x' and 'y'
wi t h their respective types ('int’).
The '>' operator represents a comparison between
'x' and 5, which evaluates t o a boolean (true or
false) value.
The '=' operator represents an assignment o f 10
t o 'y' in t h e 'if' block and 20 t o 'y' in t h e 'else' block.
10. Phase 4: Intermediate Code Generator
Intermediate code generator phase is responsible for generating an
intermediate representation of the code that can easily translated into machine
code
This phase takes the syntax tree generated in previous phase and converts it
into a form that can be optimized and translated into machine code.
Intermediate code:
1: if x > 5 goto L4
2: y = 20
3: goto L5
4: y = 10
11. Phase 5: Code Optimizer
Code optimization is a program modification strategy that endeavours to enhance the
intermediate code, so a program utilizes the least potential memory.
It minimizes its CPU time and offers high speed.