Professional Documents
Culture Documents
CS-352 - Spring 2024 - Lec3
CS-352 - Spring 2024 - Lec3
Spring 2024
Compiler Design
CS-352
Lecture (3)
1
Outline
2
Outline
3
Main Components of Lexical
Analyzer
RE
Thompson
Construction
NFA
Subset
Construction
DFA
Minimization
Minimized
DFA
DFA Simulation Scanner
Generator
Program 4
Outline
5
Recognition of Tokens
• Now, must study how to take the patterns for all the needed
tokens and build a piece of code that examines the input
string and finds a prefix that is a lexeme matching one of the
patterns.
6
Recognition of Tokens (Cont.)
• The terminals of the grammar, which are if, then, else, relop,
id, and number, are the names of tokens as far as the lexical
analyzer is concerned.
• The patterns for these tokens are described using regular
definitions, as follows.
7
Recognition of Tokens (Cont.)
8
Recognition of Tokens (Cont.)
10
Transition Diagrams
11
Transition Diagrams (Cont.)
12
Transition Diagrams (Cont.)
13
Transition Diagrams (Cont.)
• The transition diagram for id's that we saw in the former Figure has
a simple structure. Starting in state 9, it checks that the lexeme
begins with a letter and goes to state 10 if so. We stay in state 10 as
long as the input contains letters and digits.
15
Transition Diagrams (Cont.)
16
Transition Diagrams (Cont.)
17
Transition Diagrams (Cont.)
19
Finite Automata
Have, for each state, and for each symbol of its input alphabet exactly
one edge with that symbol leaving that state.
20
Finite Automata (Cont.)
21
Outline
22
Nondeterministic Finite Automata
23
Nondeterministic Finite Automata
(Cont.)
• A nondeterministic finite automaton (NFA) consists of:-
1. A finite set of states S.
2. A set of input symbols Σ, the input alphabet. We assume that ε,
which stands for the empty string, is never a member of Σ.
3. A transition function that gives, for each state, and for each
symbol in Σ ⋃ {ε} a set of next states.
4. A state s0 from S that is distinguished as the start state (or initial
state).
5. A set of states F, a subset of S, that is distinguished as the
accepting states (or final states).
24
Nondeterministic Finite Automata
(Cont.)
• Example: The transition graph for an NFA recognizing the
language of regular expression (alb)*abb is shown in the
following Figure.
25
Outline
26
Transition Table
• The entry for a given state and input is the value of the
transition function applied to those arguments. If the
transition function has no information about that state-input
pair, we put Ø; in the table for the pair.
27
Transition Table (Cont.)
28
Outline
29
Acceptance of Input Strings by
Automata
• An NFA accepts input string x if and only if there is some path in
the transition graph from the start state to one of the accepting
states, such that the symbols along the path spell out x.
• Note that ε labels along the path are effectively ignored, since the
empty string does not contribute to the string constructed along the
path.
30
Acceptance of Input Strings by
Automata (Cont.)
• The following path is another path from state 0 labelled by the
string aabb. This path leads to state 0, which is not accepting.
31
Acceptance of Input Strings by
Automata (Cont.)
• The language defined (or accepted) by an NFA is the set of strings
labelling some path from the start to an accepting state. As was
mentioned, the NFA of the following Figure defines the same
language as does the regular expression (alb)*abb, that is, all
strings from the alphabet {a, b} that end in abb.
32
Acceptance of Input Strings by
Automata (Cont.)
• Example: The following transition graph is an NFA accepting
L(aa*|bb*). String aaa is accepted because of the path