CS-352 - Spring 2024 - Lec2
CS-352 - Spring 2024 - Lec2
Spring 2024
Compiler Design
CS-352
Lecture (2)
1
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
2
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
3
Structure of a Compiler
4
Today !
5
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
6
Basic Terminologies
Token
Pattern
Lexeme
It is a sequence of characters in the source program that matches the pattern for a
token and is identified by the lexical analyzer as an instance of that token.
7
Basic Terminologies (Cont.)
8
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
9
Role of Lexical Analyzer
Lexical
Source Program Token Stream
Analyzer
10
Role of Lexical Analyzer (Cont.)
Scanning
Lexical Analyzer
11
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
12
Interactions between Scanner and
Parser
• Commonly, the interaction is implemented by having the
parser call the lexical analyzer. The call, suggested by the
getNextToken command, causes the lexical analyzer to read
characters from its input until it can identify the next lexeme
and produce for it the next token, which it returns to the
parser.
13
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
15
Main Components of Lexical
Analyzer
RE
Thompson
Construction
NFA
Subset
Construction
DFA
Minimization
Minimized
DFA
DFA Simulation Scanner
Generator
Program 16
Main Components of Lexical
Analyzer (Cont.)
17
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
18
Operations on Languages
19
Operations on Languages (Cont.)
20
Operations on Languages (Cont.)
• Example: Let L be the set of letters {A, B, ... , Z, a, b, ... , z} and let D
be the set of digits {0, 1, ... , 9}.
– We may think of L and D in two, essentially equivalent, ways. One way is that L and
D are, respectively, the alphabets of uppercase and lowercase letters and of digits.
The second way is that L and D are languages, all of whose strings happen to be of
length one.
– Here are some other languages that can be constructed from languages L and D,
using the operators on languages:-
21
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
22
Regular Expressions
23
Regular Expressions (Cont.)
24
Regular Expressions (Cont.)
25
Regular Expressions (Cont.)
26
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
27
Regular Definitions
28
Regular Definitions (Cont.)
29
Regular Definitions (Cont.)
30
Outline
• Structure of a Compiler
• Basic Terminologies
• Role of Lexical Analyzer
• Interactions between Scanner and Parser
• Main Components of Lexical Analyzer
• Operations on Languages
• Regular Expressions
• Regular Definitions
• Extensions of Regular Definitions
31
Extensions of Regular Definitions
32
Extensions of Regular Definitions
(Cont.)
33
Extensions of Regular Definitions
(Cont.)
34