NFA to Lecture 3 Sections 3.5-3.9 Hmpden-Sydney College Fri, Jn 23, 2009
Outline NFA to 1 2 NFA to 3 4 5
Building from Regulr Expressions NFA to A regulr expression consists of symbols, b, c,... ; opertors,, nd ; prentheses; nd. We describe recursive method of building stte digrm from regulr expression.
Building The bsic cses. For, build NFA to For ech symbol Σ, build
Building For the regulr expression r s: NFA to r s
Building For the regulr expression r s: NFA to r s
Building For the regulr expression rs: NFA to r s
Building For the regulr expression rs: NFA to r s
Building For the regulr expression r : NFA to r
Building NFA to For the regulr expression r : r
Building NFA to Applying these rules builds n NFA representing the regulr expression. Note tht ech digrm hs unique strt nd ccepting sttes. Note lso tht generous use ws mde of -moves. This fcilittes joining them together without ny complictions.
Exmple NFA to Exmple (Building Digrm) Build stte digrm from the regulr expression b ( ). Applying the rules rigorously produces the following. b
Exmple NFA to Exmple (Building Digrm) Build stte digrm from the regulr expression b ( ). Applying the rules rigorously produces the following. b
Exmple NFA to Exmple (Building Digrm) Build stte digrm from the regulr expression b ( ). Applying the rules rigorously produces the following. b
NFA to NFA to Let Q be the sttes of the NFA. The -closure of stte q in the NFA is the set of ll sttes tht re rechble from q (including q itself) through sequences of -moves. Define the sttes of the to be P(Q), i.e., sets of sttes in the NFA.
NFA to NFA to For every stte A P(Q) nd every symbol x Σ, the trnsition δ(a, x) is the -closure of ll sttes in the NFA tht re reched from sttes in A by reding x. Tht is, First find ll sttes reched from A by following x-moves. Then find the -closure of tht set of sttes.
Exmple NFA to Exmple ( NFA to ) Consider the NFA of the regulr expression b ( ). Number the sttes 1 through 14. 9 10 b 1 2 3 4 5 6 7 8 13 14 11 12
Exmple NFA to Exmple ( NFA to ) -cl() -cl() 1 {1, 2} 8 {8, 9, 11, 12, 13, 14} 2 {2} 9 {9} 3 {3, 4, 5, 7, 8, 9, 11, 12, 13, 14} 10 {10, 13, 14} 4 {4, 5, 7, 8, 9, 11, 12, 13, 14} 11 {11, 12, 13, 14} 5 {5} 12 {12, 13, 14} 6 {5, 6, 7, 8, 9, 11, 12, 13, 14} 13 {13, 14} 7 {7, 8, 9, 11, 12, 13, 14} 14 {14} Find the -closure of ech stte. The strt stte of the is -cl(1). From there, follow the rule for the trnsitions of the.
Exmple NFA to Exmple ( NFA to ) The result is cl(1) cl(3) cl(10) b cl(6) b
Exmple NFA to Exmple ( NFA to ) The result is 1 2 3 b 4 b
NFA to To minimize is to reduce the number of sttes to minimum without chnging the lnguge ccepted by the. Two sttes p nd q re equivlent if for every string w Σ, δ(p, w) nd δ(q, w) re either both ccepting sttes or both rejecting sttes.
Exmple NFA to Exmple ( ) Minimize the of regulr expression b ( ). First, dd ded stte to mke the fully defined. 1 2 3 b b, b 5 4, b b
Exmple NFA to Exmple ( ) The initil prtition is {1, 5}, {2, 3, 4}. Apply the trnsitions by nd b:
Exmple NFA to Exmple ( ) The second prtition is {1}, {2, 4}, {3}, {5}. This is the finl prtition. We see tht nd b do not distinguish 2 nd 4. Therefore, sttes 2 nd 4 re equivlent nd should be merged. Also, remove the ded stte.
Exmple NFA to Exmple ( ) The minimized is b 1 2 3
NFA to There re two bsic methods of implementing. Use switch sttements. Use trnsition tble.
Using switch ments NFA to Using switch sttements. The min function contins switch sttement whose cses re the different sttes, including the ded stte. Ech cse contins switch sttement whose cses re the different input symbols.
Using switch ments NFA to Using switch ments switch (stte) { cse STATE_0: }. cse STATE_1:. cse DEAD_STATE: stte = DEAD_STATE;
Using switch ments NFA to Using switch ments cse STATE_0: switch (symbol) { cse SYMBOL_A: stte =... }. cse SYMBOL_B: stte =.... defult: stte = DEAD_STATE;
Using Trnsition Tble NFA to Using Trnsition Tble The progrm uses 2-dimensionl rry to store the trnsitions. Rows represent sttes. Columns represent symbols. The progrms lex, flex, nd JLex ll use the tbles.
Using Trnsition Tble NFA to Using Trnsition Tble int trnsition[num_states][num_symbols] = { {STATE_4, STATE_2,..., STATE_2}, {STATE_0, STATE_2,..., STATE_3},. {DEAD_STATE,..., DEAD_STATE} };. stte = trnsition[stte][symbol];
NFA to Homework Red Sections 3.5-3.9. Exercises 6, 7, pge 147.