cse443 compilers

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - PowerPoint PPT Presentation

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Announcements HW 1 posted PR 1 posted One student still not part of a team (will need to join a 3-person team) Team meeting scheduling - please respond to Doodle poll!


  1. CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall

  2. Announcements HW 1 posted PR 1 posted One student still not part of a team (will need to join a 3-person team) Team meeting scheduling - please respond to Doodle poll!

  3. Lexical Phases of structure a compiler Figure 1.6, page 5 of text

  4. Process of building lexical analyzer 5) The minimal DFA is character our lexical analyzer stream language regex NFA DFA DFA token stream lexical analyzer

  5. Focus last time regex NFA

  6. focus today NFA DFA

  7. (a|b) * abb first we construct an NFA from this regular expression

  8. (a|b) * abb a

  9. (a|b) * abb a b

  10. (a|b) * abb a ℇ ℇ b ℇ ℇ

  11. (a|b) * abb ℇ a ℇ ℇ ℇ ℇ ℇ b ℇ ℇ

  12. (a|b) * abb ℇ a ℇ ℇ a ℇ ℇ ℇ b ℇ ℇ

  13. (a|b) * abb ℇ a ℇ ℇ a b ℇ ℇ ℇ b ℇ ℇ

  14. (a|b) * abb ℇ a ℇ ℇ a b b ℇ ℇ ℇ b ℇ ℇ

  15. (a|b) * abb ℇ a 2 3 ℇ ℇ a b b ℇ 0 1 6 8 7 9 10 ℇ ℇ b ℇ ℇ 4 5

  16. Operations ℇ -closure(t) is the set of states reachable from state t using only ℇ -transitions. ℇ -closure(T) is the set of states reachable from any state t ∈ T using only ℇ - transitions. move(T,a) is the set of states reachable from any state t ∈ T following a transition on symbol a ∈ ∑ .

  17. NFA -> DFA algorithm (set of states construction - page 153 of text) INPUT: An NFA N = (S, ∑ , 𝛆 , s 0 , F) OUTPUT: A DFA D = (S', ∑ , 𝛆 ', s 0 ', F') such that ℒ (D)= ℒ (N) ALGORITHM: Compute s 0 ' = ℇ -closure(s 0 ), an unmarked set of states Set S' = { s 0 ' } while there is an unmarked T ∈ S' mark T for each symbol a ∈ ∑ let U = ℇ -closure(move(T,a)) if U ∉ S', add unmarked U to S' add transition: 𝛆 '(T,a) = U F' is the subset of S' all of whose members contain a state in F .

  18. NFA -> DFA algorithm (set of states construction - page 153 of text) S 0 ' = { A = {0,1,2,4,7} } Pick an unmarked set from S 0 ', A, mark it, and ∀ x ∈ ∑ let U = ℇ -closure(move(A,x)), if U ∉ S', add unmarked U to S' and add transition: 𝛆 '(A,x) = U S 1 ' = { A ✔ , B = {1,2,3,4,6,7,8} , C = {1,2,4,5,6,7}} 𝛆 '(A,a) = B 𝛆 '(A,b) = C Pick an unmarked set from S 1 ', B, mark it, and ∀ x ∈ ∑ let U = ℇ -closure(move(B,x)), if U ∉ S', add unmarked U to S' and add transition: 𝛆 '(B,x) = U S 2 ' = { A ✔ , B ✔ , C , D = {1,2,4,5,6,7,9}} 𝛆 '(B,a) = B 𝛆 '(B,b) = D Pick an unmarked set from S 2 ', C, mark it, and ∀ x ∈ ∑ let U = ℇ -closure(move(C,x)), if U ∉ S', add unmarked U to S' and add transition: 𝛆 '(C,x) = U S 3 ' = { A ✔ , B ✔ , C ✔ , D } 𝛆 '(C,a) = B 𝛆 '(C,b) = C

  19. NFA -> DFA algorithm (set of states construction - page 153 of text) Pick an unmarked set from S 3 ', D, mark it, and ∀ x ∈ ∑ let U = ℇ -closure(move(D,x)), if U ∉ S', add unmarked U to S' and add transition: 𝛆 '(D,x) = U S 4 ' = { A ✔ , B ✔ , C ✔ , D ✔ , E = {1,2,4,5,6,7,10} } 𝛆 '(D,a) = B 𝛆 '(D,b) = E Pick an unmarked set from S 4 ', E, mark it, and ∀ a ∈ ∑ let U = ℇ -closure(move(E,a)), if U ∉ S', add unmarked U to S' and add transition: 𝛆 '(E,a) = U S 5 ' = { A ✔ , B ✔ , C ✔ , D ✔ , E ✔ } 𝛆 '(E,a) = B 𝛆 '(E,b) = C Since there are no unmarked sets in S 5 ' the algorithm has reached a fixed point. STOP. F' is the subset of S' all of whose members contain a state in F: {E}

  20. The original NFA ℇ a 2 3 ℇ ℇ a b b ℇ 0 1 6 8 7 9 10 ℇ ℇ b ℇ ℇ 4 5

  21. The resulting DFA DFA = ( {A, B, C, D, E}, {a, b}, A, 𝛆 ', {E}), where b 𝛆 '(A,a) = B 𝛆 '(A,b) = C b C 𝛆 '(B,a) = B 𝛆 '(B,b) = D b a a 𝛆 '(C,a) = B b b 𝛆 '(C,b) = C A B D E 𝛆 '(D,a) = B a 𝛆 '(D,b) = E a a 𝛆 '(E,a) = B 𝛆 '(E,b) = C

  22. Process of building lexical analyzer 5) The minimal DFA is character our lexical analyzer stream language regex NFA DFA DFA token stream lexical analyzer

  23. focus above: NFA to DFA conversion NFA DFA

  24. next step: DFA minimization DFA DFA

  25. NFA for (a|b) * abb ℇ a 2 3 ℇ ℇ a b b ℇ 0 1 6 8 7 9 10 ℇ ℇ b ℇ ℇ 4 5

  26. DFA for (a|b) * abb b b C b a a b b A B D E a a a

  27. Minimization Algorithm

  28. DFA -> minimal DFA algorithm INPUT: An DFA D = (S, ∑ , 𝛆 , s 0 , F) OUTPUT: A DFA D' = (S', ∑ , 𝛆 ', s 0 ', F') such that S' is as small as possible, and ℒ (D)= ℒ (D') ALGORITHM: 1. Let π = { F , S-F } 2. Let π ' = π . For every group G of π : partition G into subgroups such that two states s and t are in the same subgroup iff for all input symbols a, states s and t have transitions on a to states in the same group of π Replace G in π ' by the set of all subgrops formed 3. if π '= π let π "= π , otherwise set π = π ' and repeat 2. 4. Choose one state in each group of π " as a representative for that group. a) The start state of D' is the representative of the group containing the start state of D b) The accepting states of D' are the representatives of those groups that contain an accepting state of D c) Adjust transitions from representatives to representatives.

  29. ORIGINAL DFA D = ( S, ∑ , s 0 , 𝛆 , F) S = {A, B, C, D, E} ∑ = {a, b} s 0 = A 𝛆 = {(A,a)->B, (A,b)->C, (B,a)->B, (B,b)->D, (C,a)->B, (C,b)->C, (D,a)->B, (D,b)->E, (E,a)->B, (E,b)->C} F = {E}

  30. Finding the minimal set of distinct sets of states 𝞺 0 = { F , S-F } = { {E}, {A,B,C,D} } Pick a non-singleton set X = {A,B,C,D} from 𝞺 0 and check behavior of states on all transitions on symbols in ∑ (are they to states in X or to other groups in the partition?) (A,a)->B, (B,a)->B, (C,a)->B, (D,a)->B (A,b)->C, (B,b)->D, (C,b)->C, (D,b)->E D behaves differently, so put it in its own partition.

  31. Finding the minimal set of distinct sets of states 𝞺 1 = { {E}, {A, B, C}, {D} } Pick a non-singleton set X = {A,B,C} from 𝞺 1 and check behavior of states on all transitions on symbols in ∑ (are they to states in X or to other groups in the partition?) (A,a)->B, (B,a)->B, (C,a)->B (A,b)->C, (B,b)->D, (C,b)->C B behaves differently, so put it in its own partition.

  32. Finding the minimal set of distinct sets of states 𝞺 2 = { {E}, {A, C}, {B}, {D} } Pick a non-singleton set X = {A,C} from 𝞺 2 and check behavior of states on all transitions on symbols in ∑ (are they to states in X or to other groups in the partition?) (A,a)->B, (C,a)->B (A,b)->C, (C,b)->C A and C both transition outside the group on symbol a, to the same group (the one containing B). Therefore A and C are indistinguishable in their behaviors, so do not split this group.

  33. Finding the minimal set of distinct sets of states 𝞺 3 = { {E}, {A, C}, {B}, {D} } = 𝞺 2 We have reached a fixed point! STOP

  34. Pick a representative from each group 𝞺 FINAL = { {E}, {A, C}, {B}, {D} }

  35. MINIMAL DFA D' = ( S', ∑ , s' 0 , 𝛆 ', F') S' = {B, C, D, E} -> the representatives ∑ = {a, b} -> no change s' 0 = C -> the representative of the group that contained D's starting state, A 𝛆 = (on next slide) F = {E} -> the representatives of all the groups that contained any of D's final states (which, in this case, was just {E})

  36. The new transition function 𝛆 ' For each state s ∈ S', consider its transitions in D, on each a ∈ ∑ . if 𝛆 (s,a) = t, then 𝛆 '(s,a) = r, where r is the representative of the group containing t.

  37. 𝛆 = { (B,a)->B, (B,b)->D, (C,a)->B, (C,b)->C, (D,a)->B, (D,b)->E, (E,a)->B, (E,b)->C }

  38. Minimal DFA for (a|b) * abb b b } a C a , A { b b B D E a a

  39. Non- minimized DFA for (a|b) * abb b b C b a a b b A B D E a a a

Recommend


More recommend