CS-352 - Spring 2024 - Lec1
CS-352 - Spring 2024 - Lec1
Spring 2024
Compiler Design
CS-352
Lecture (1)
1
Textbook
2
3
Outline
• Compilers
• Compilers VS. Interpreters
• Compiler Architecture
• Compiler Phases
• Translation of an Assignment Statement
4
Compilers
5
Compilers (Cont.)
• A program that reads a source program and
translates it into a semantically equivalent target
program.
6
Compilers VS. Interpreters
7
Compilers VS. Interpreters
• “Compilation”
– Translation of a program written in a source
language into a semantically equivalent
program written in a target language.
– Oversimplified view:-
Input
Source Target
Compiler
Program Program
• “Interpretation”
– Performing the operations implied by the
source program.
– Oversimplified view:-
Source
Program
Interpreter Output
Input
Error messages
9
Compilers VS. Interpreters (Cont.)
10
Compiler Architecture
11
Compiler Architecture (Cont.)
12
Compiler Phases
13
Lexical Analysis (Scanner)
Code
Optimizer
14
Lexical Analysis (Scanner)
15
Lexical Analysis (Scanner)
Input: result = a + b * c / d
• Tokens:
‘result’, ‘=‘, ‘a’, ‘+’, ‘b’, ‘*’, ‘c’, ‘/’, ‘d’
Identifiers
Operators
16
Syntax Analysis (Parser)
tokens Syntactic
Scanner Parser structure Semantic Code
Source Target
language
(lexical (syntax Analysis Generator
language
analysis) analysis) (IC generator)
Code
Optimizer
17
Syntax Analysis (Parser)
Input: result = a + b * c / d
ID ID
18
Semantic Analysis
Syntactic/semantic
Syntactic structure
Scanner Parser structure Semantic Code
Source Target
language
(lexical (syntax Analysis Generator
language
analysis) analysis) (IC generator)
Syntactic/semantic
structure
Code
Optimizer
• “Meaning”
• Type/Error Checking
• Intermediate Code Generation –
abstract machine Symbol
Table
19
Semantic Analysis (Cont.)
20
Code Optimization
21
Code Generation
Syntactic/semantic
structure
Scanner Parser Semantic Code
Source Target
language
(lexical (syntax Analysis Generator language
analysis) analysis) (IC generator)
Syntactic/semantic
Code structure
Optimizer
22
Translation of an Assignment
Statement
24
Translation of an Assignment
Statement (Cont.)
Symbol table
1 position … The symbol table stores information
about each identifier such as: its
2 initial … name and type.
3 rate …
… … …
25
Translation of an Assignment
Statement (Cont.)
26
Translation of an Assignment
Statement (Cont.)
27
Translation of an Assignment
Statement (Cont.)
28
Translation of an Assignment
Statement (Cont.)
29
Translation of an Assignment
Statement (Cont.)
30