2. Phases of Compiler
2
Lexical Analyzer (Scanner)
reads a stream of characters and puts them
together into some meaningful (with respect to
the source language) units called tokens.
Lexical Analyzer
(Scanner)Character
Stream
Token
Stream
CD-Course-FSH 1st Term 2017-2018
4. Phases of Compiler
4
Syntax Analyzer (Parsing)
• Takes a stream of tokens from the scanner,
and
• Determines if the syntax (structure) of the
program is correct according to the
(context-free) grammar of the source
language.
Syntax Analyzer
Token Syntax Tree
CD-Course-FSH 1st Term 2017-2018
5. Phases of Compiler
5
Syntax Analyzer (Parsing) continue
• It generates a data structure, called a
parse tree or an abstract syntax tree,
which describes the syntactic structure of
the program.
CD-Course-FSH 1st Term 2017-2018
6. Phases of Compiler
6
Syntax Analyzer (Parsing) continue
Ex:
Statement:
id=id op id
Statement:
id=id [num]
Statement:
if (id op num) { } / if(xy==20) { }
CD-Course-FSH 1st Term 2017-2018
7. Phases of Compiler
7
Semantic Analyzer
Statement:
id op id op id
x = y + z (semantically correct)
But if
Int = float + fn (semantically incorrect)
CD-Course-FSH 1st Term 2017-2018
8. Phases of Compiler
8
Intermediate Code Generator
CD-Course-FSH 1st Term 2017-2018
Intermediate CodeSource Code Machine Code
Intermediate Code
C
Intel
Pascal
Basic
Mac
9. Phases of Compiler
9
Code Optimizer
Code optimizing means
Faster,
Shorter,
Target code consumes less power.
CD-Course-FSH 1st Term 2017-2018
10. Phases of Compiler
10
Example:
position := initial + rate * 60
would be grouped into the following tokens;
1. The identifier position
2. The assignment symbol :=
3. The identifier initial
4. The plus sign
5. The identifier rate.
6. The multiplication sign.
7. The number 60
LexicalAnalyzer
Syntax Analyzer
CD-Course-FSH 1st Term 2017-2018