160 likes | 329 Views
Lexical Analysis Part 2. CMSC 431 Shon Vick. Regular Expressions and FA are Equivalent. For every regular expression, there is a deterministic finite-state machine that defines the same language, and vice versa. NFA. Regular expressions. DFA. e Closure Operator.
E N D
Lexical Analysis Part 2 CMSC 431 Shon Vick
Regular Expressions and FA are Equivalent For every regular expression, there is a deterministic finite-state machine that defines the same language, and vice versa NFA Regular expressions DFA
eClosure Operator • The eClosure operator is defined as eClosure(s) = { s } U states reachable from s using e transitions. • Example: eClosure(1) = {1,3} a start 1 5 3 a a/b b 2 4
Subset Construction for NFA-> DFA • Compute A = eClosure(start) • Compute the set of states reachable from A on transition a, call this new set S’ • Compute eClosure(S’) – this is the new state and label it with the next available label • Continue for all possible transitions from the current state for all applicable elements of S • Repeat steps 2-4 for each new state
e a b e e c 1 6 3 2 5 4 e b b a A C B D c c Comparison from Last Time NFA: DFA:
Example: aa*b | aab* NFA: a a b 2 1 3 b a a 4 5
a b B A D b a a C a E b b b F G Resultant DFA How is this result different than before?
Generalized NFA • Generalized Nondeterministic Finite Automaton M=(Q, , , qstart, qaccept) with • Q finite set of states • the input alphabet • qstart the start state • qaccept the accept state • :(Q\{qaccept})(Q\{qstart}) R the transition function • (R is the set of regular expressions over )
Example GNFA 0110 0 qS qA 01* 0* | 11
Characteristics of GNFA’s • :(Q\{qaccept})(Q\{qstart}) R The interior Q\{qaccept,qstart} is fully connected by From qstart only ‘outgoing transitions’ To qaccept only ‘ingoing transitions’ Impossible qiqj transitions are “(qi,qj) = ” Observation: This GNFA:recognizes the language L(R) RR qS qA
qj DFA M Equivalent GNFA M’ Let M have k states Q={q1,…,qk} - Add two states qaccept and qstart q1 qS - Connect qstart to earlier q1: qj qA - Connect old accepting states to qaccept - Complete missing transitions by qi - Join multiple transitions: 1 becomes 0|1 qj qi qj qi 0
Remove Internal state of GNFA If the GNFA M has more than 2 states, ripinternal qrip to get equivalent GNFA M’ by: - Removing state qrip: Q’=Q\{qrip} - Changing the transition function by’(qi,qj) = (qi,qj) | ((qi,qrip)((qrip,qrip))*(qrip,qj)) for every qiQ’\{qaccept} and qjQ’\{qstart} R1 qrip R2 R4|(R1R2*R3) qj qi qi = R3 qj R4
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
Try at Home • What is the NFA for the following RE? ((a|b)*c) | (a b c*)
References • Introduction to Programming Languages and Compilers Compilers Principles, Techniques and Tools, Aho, Sethi, and Ullman • Karen Tomko’s Compiler Theory Course, UC, http://www.ececs.uc.edu/~ktomko/Compilers/Compilers.htm Introduction To the Theory of Computation, M. Sipser, PWS Publishing, 1997 (http://www.cs.berkeley.edu/~vandam/CS172/week2m.ppt)