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
Union
Concatenation
Kleene Closure
Positive Closure
Range Closure
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 in the NFA, the epsilon-closure of refers to the set of states reachable from by epsilon () transitions:
This means the set of states that can be reached from by taking zero or more epsilon transitions.
For a set of states in the NFA, the epsilon-closure of refers to the set of all states that can be reached from any state in by epsilon transitions:
This means the set of all states that can be reached from any state in through zero or more epsilon transitions.
For a set of states 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 is the epsilon-closure of the set of states that can be reached by taking the transition labeled with from any state in .
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 in the DFA corresponds to a set of states in the NFA, then the transition from state on a character is found as follows:
Let be the set of states in the NFA that can be reached by following a transition labeled a from any of the states in . (This set may be empty.)
Let be the set of states in the NFA reachable from some state in by following zero or more epsilon transitions.
The state in the DFA transitions on a to a DFA state corresponding to the set of states .
DFA Optimization: Merging Equivalent States
For two nodes of the same type, and , the condition for merging them is:
Hopcroft Partitioning Algorithm:
The Hopcroft partitioning algorithm iteratively divides the state set of a DFA (Deterministic Finite Automaton) into subsets to minimize the DFA.
Initially, the state set is divided into two subsets: accepting states and non-accepting states .
The algorithm continues to partition the states until no further splits can be made.
The
Split(s)
function checks if a set of states 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