Lexical Analysis Part 2 Professor Yihjia Tsai Tamkang University
2 Regular Expressions and FA are Equivalent Regular expressions NFA DFA For every regular expression, there is a deterministic finite-state machine that defines the same language, and vice versa
3 Closure Operator TheClosure operator is defined as Closure(s) = { s } U states reachable from s using transitions. Example: Closure(1) = {1,3} b a a/b a start
4 Subset Construction for NFA-> DFA 1.Compute A = Closure(start) 2.Compute the set of states reachable from A on transition a, call this new set S ’ 3.Compute Closure(S ’ ) – this is the new state and label it with the next available label 4.Continue for all possible transitions from the current state for all applicable elements of 5.Repeat steps 2-4 for each new state
5 Comparison from Last Time a b c NFA: D A C B a b b c c DFA:
6 Example: aa*b | aab* NFA: 3 1 a b 2 a 5 4 a a b
7 Example: aa*b | aab* DFA State NFA Set of States ab A{1}B B{2,4}CD C{2,5}EF D{3} E{2}ED F{3,5}G G{5}G
8 Resultant DFA D A a b B a C E a b b b F G b a How is this result different than before?
9 Generalized NFA Generalized Nondeterministic Finite Automaton M=(Q, , , q start, q accept ) with Q finite set of states the input alphabet q start the start state q accept the accept state :(Q\{q accept }) (Q\{q start }) R the transition function ( R is the set of regular expressions over )
10 Example GNFA qSqS qAqA 01* 0 0* |
11 Observation: This GNFA: recognizes the language L(R) Characteristics of GNFA ’ s :(Q\{q accept }) (Q\{q start }) R The interior Q\{q accept,q start } is fully connected by From q start only ‘outgoing transitions’ To q accept only ‘ingoing transitions’ Impossible q i q j transitions are “ (q i,q j ) = ” qSqS qAqA RRRR
12 DFA M Equivalent GNFA M ’ Let M have k states Q={q 1,…,q k } - Add two states q accept and q start qSqS q1q1 - Connect q start to earlier q 1 : qiqi qjqj - Complete missing transitions by qAqA qjqj - Connect old accepting states to q accept - Join multiple transitions : qiqi 0 qjqj 1 becomes qiqi 0|1 qjqj
13 Remove Internal state of GNFA If the GNFA M has more than 2 states, rip internal q rip to get equivalent GNFA M’ by: - Removing state q rip : Q’=Q\{q rip } - Changing the transition function by ’(q i,q j ) = (q i,q j ) | ( (q i,q rip )( (q rip,q rip ))* (q rip,q j )) for every q i Q’\{q accept } and q j Q’\{q start } qiqi R 4 |(R 1 R 2 *R 3 ) qjqj qiqi R2R2 qjqj R4R4 q rip R1R1 R3R3 =
14 Try at Home What is the regular expression for –Binary numbers, multiples of 2 –Strings of a’s and b’s with a at start and finish –Strings of a’s and b’s, no consecutive a’s
15 Try at Home What is the NFA for the following RE? ((a|b)*c) | (a b c*)
16 References Aho, A.V., R. Sethi, and J.D. Ullman, Compilers Principles, Techniques and Tools, Addison-Wesley, ISBN Appel, A., Modern Compiler Implementation In Java (2 nd Ed), Cambridge University Press, ISBN X.