Lexical Analysis
In this lecture, we start from using regular expressions to specify the language. In order to parse the regular expressions, we need to construct the finite automaton corresponding to the regular expressions.
Regular Expressions
a
x=a
[ab]
x∈{a,b}
[a−z]
x∈{a,…,z}
[a−zA−Z]
x∈{a,…,z,A,…,Z}
.
x∈Σ
a^
x∈Σ∖{a}
ε
x∈∅
a?
x=a or x=ε
Union
S∣T
x∈S∪T
Concatenation
ST
x∈{st∣s∈S,t∈T}
Kleene Closure
S∗
x∈{s0…sn∣∀0≤i≤n,si∈S,0≤n<∞}
Positive Closure
S+
x∈{s0…sn∣∀0≤i≤n,si∈S,1≤n<∞}
Range Closure
S{min,max}
x∈{s0…sn∣∀0≤i≤n,si∈S,min≤n<max}
Precedence: Closure > Concatenation > Union.
Lexical Specification
Handling conflicts in recognition.
Keywords vs Identifiers: Keywords have a higher priority than identifiers. For example, the string
"if"should be recognized as<IF>, not as<ID (if)>.When multiple matching patterns exist, choose the longest match. For example, "
<=" should be recognized as<LTE>, and not as two separate tokens<LT><EQ>. The string "ifabc" should be recognized as<ID (ifabc)>, and not as<IF>and<ID (abc)>.
Lexical Parsing
Motivation: We need to convert regular expressions to finite automatons.
Thompson's Construction

Below is an example of converting the regular expression of UNUM to NFA:

Gap: Using an NFA for lexical analysis is impractical due to the numerous possible transition paths.
Converting NFA to DFA
Given an input string, we can simulate an NFA with a DFA by defining the states in the DFA as the reachable states in the NFA.
For a single state si in the NFA, the epsilon-closure of si refers to the set of states reachable from si by epsilon (ϵ) transitions:
This means the set of states sj that can be reached from si by taking zero or more epsilon transitions.
For a set of states S in the NFA, the epsilon-closure of S refers to the set of all states that can be reached from any state in S by epsilon transitions:
This means the set of all states q′ that can be reached from any state q in S through zero or more epsilon transitions.
For a set of states S in the NFA, the transition for an input symbol α refers to the epsilon-closure of the set of all states that can be reached after reading the symbol α:
This means that the transition on symbol α for a set of states S is the epsilon-closure of the set of states that can be reached by taking the transition labeled with α from any state q in S.
Powerset Construction
The start state in the DFA corresponds to the start state of the NFA, plus all states reachable via ϵ-transitions.
If a state q in the DFA corresponds to a set of states S in the NFA, then the transition from state q on a character a is found as follows:
Let S′ be the set of states in the NFA that can be reached by following a transition labeled a from any of the states in S. (This set may be empty.)
Let S′′ be the set of states in the NFA reachable from some state in S′ by following zero or more epsilon transitions.
The state q in the DFA transitions on a to a DFA state corresponding to the set of states S′′.
DFA Optimization: Merging Equivalent States
For two nodes of the same type, di and dj, the condition for merging them is:
Hopcroft Partitioning Algorithm:

The Hopcroft partitioning algorithm iteratively divides the state set D of a DFA (Deterministic Finite Automaton) into subsets to minimize the DFA.
Initially, the state set D is divided into two subsets: accepting states Dac and non-accepting states D∖Dac.
The algorithm continues to partition the states until no further splits can be made.
The
Split(s)function checks if a set of states s can be split based on input characters from the alphabet Σ. If a split occurs, it returns two new subsets; otherwise, it returns the original set.
Regular Language
Two regular expressions are considered equivalent if, and only if, they generate the same regular set (language).
Pumping Lemma:
If a language consists of a finite set of lexemes, it is guaranteed to be a regular language.
For languages with an infinite set of lexemes, the Pumping Lemma can be used to demonstrate that the language is not regular.
Last updated