Chapter 2
Chapter 2
Lexical Analysis
Lexical Analysis
• What is the goal?
if (i ==0)
z=0;
else
z=1;
• An accepting state
a
• A transition
Example
• A finite state automata accepting any number of 1’s followed by a
single 0
Epsilon moves
• Another kind of transition: ε-moves
NFA DFA
Reg to FA
• Some additional notations in Reg ex
• Union A+B = A|B
• Option (zero or one): A+ ε = A?
• Range ‘a’+’b’+…+’z’ = [a-z]
• Excluded range: complement of [a-z]= [^a-z]
• Two ways of implementing.
• Regular expression => NFA = > DFA => Table-driven implementation
• Can be done intuitively
First method
• For each kind of rexp, define an NFA notation
• For ε
• For input a
• For AB
• For A+B
• For A*
Example
• Perform the following for the regExp -> NFA
• (1+0)*1
NFA to DFA
• Each state of DFA is a non-empty state of states in NFA
• Start state
• Set of NFA states reachable through ε-moves from NFA start state
• Add a transition S S’ ato DFA iff
• S’ is the set of NFA states reachable from any state in S after seeing the input
a, considering ε-moves as well
• Note that NFA may be in many states at any time.
Example
Second method
• What does the following rexp represent
• Digit = 0|1|…|9
• Digits = digit+
• Digits(.digits)?(e[+|-]?digits)?
• Perform the DFA imperically
Solution
digit
digit digit
digit *
digit . digit E +|- Other
1 2 3 4 5 6 7 8
digit
E
Reading assignment
• Error recovery
• Buffered I/O for token detection and Buffered I/O with Sentinels
• 2D table implementation of a DFA