1 / 21

Transition Diagrams

Transition Diagrams. Lecture 3 Wed, Jan 21, 2004. Building Transition Diagrams from Regular Expressions. A regular expression consists of symbols a, b, c, …, operators, parentheses, and . We describe a recursive method of building a transition diagram from a regular expression. . a.  :.

nibaw
Télécharger la présentation

Transition Diagrams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Transition Diagrams Lecture 3 Wed, Jan 21, 2004

  2. Building Transition Diagrams from Regular Expressions • A regular expression consists of symbols a, b, c, …, operators, parentheses, and . • We describe a recursive method of building a transition diagram from a regular expression.

  3. a : a: Building Transition Diagrams • The basic cases. • For , build • For each symbol a  , build

  4. r   r | s:   s Building Transition Diagrams • The recursive cases. • For the expression r | s, build

  5. r  s rs:    r r*:  Building Transition Diagrams • For the expression rs, build • For the expression r*, build

  6. Building Transition Diagrams • Applying these rules builds an NFA representing the regular expression. • Note that each diagram has unique start and accepting states. • Note also that generous use was made of -moves. • This facilitates joining them together without any complications.

  7. a        a b     Example: Building a Transition Diagram • Build a transition diagram from the regular expression ab*(a | ). • Applying the rules rigorously produces the following.

  8. Converting an NFA to a DFA • Let Q be the states of the NFA. • The -closure of a state q in the NFA is the set of all states that are reachable from q through sequences of -moves (including q itself). • Define the states of the DFA to be (Q), i.e., sets of states in the NFA.

  9. Converting an NFA to a DFA • For every state A  (Q) and every symbol x  , the transition (A, x) is the -closure of all states in the NFA that are reached from states in A by reading x.

  10. a    8 9     a b 2 12 1 3 4 5 6 7   10 11   Example: A DFA from an NFA • Consider the NFA of the regular expression ab*(a | ). • Number the states 1 through 12.

  11. Example: A DFA from an NFA • Find the -closure of each state. • -cl(1) = {1}. • -cl(2) = {2, 3, 4, 6, 7, 8, 10, 11, 12}. • -cl(3) = {3, 4, 6, 7, 8, 10, 11, 12}. • -cl(4) = {4}. • -cl(5) = {4, 5, 6, 7, 8, 10, 11, 12}. • -cl(6) = {6, 7, 8, 10, 11, 12}. • -cl(7) = {7, 8, 10, 11, 12}.

  12. Example: A DFA from an NFA • -cl(8) = {8}. • -cl(9) = {12}. • -cl(10) = {10, 11, 12}. • -cl(11) = {11, 12}. • -cl(12) = {12}. • The start state of the DFA is -cl(1). • From there, follow the rule for the transitions of the DFA.

  13. a a 1 2, 3, 6, 7, 8, 10, 11, 12 9, 12 b a b 4, 5, 6, 7, 8, 10, 11, 12 Example: A DFA from an NFA • The result is

  14. Minimizing a DFA • To minimize a DFA is to reduce the number of states to a minimum without changing the language accepted by the DFA. • Two states p and q are equivalent if for every string w  *, (p, w) and (q, w) are either both accepting states or both rejecting states.

  15. a a 1 2 3 b b a a | b 5 a | b b 4 Example: Minimizing a DFA • Minimize the DFA of regular expression ab*(a | ). • First, add a dead state to make the DFA fully defined.

  16. Example: Minimizing a DFA • The initial partition is {1, 5}, {2, 3, 4}. • Apply the transitions by a and b: • a distinguishes 1 and 5, and {2, 4} and 5. • b distinguishes {2, 4} and 5.

  17. Example: Minimizing a DFA • The second partition is {1}, {2, 4}, {3}, {5}. • a and b do not distinguish 2 and 4. • Therefore, this is the final partition. • States 2 and 4 are equivalent and should be merged. • Also, remove the dead state.

  18. b a a 1 2 3 Example: Minimizing a DFA • The minimized DFA is

  19. Programming a DFA • There are two basic methods of programming a DFA. • Use switch statements. • Use a transition table.

  20. Using Switch Statements • The main function contains a switch statement whose cases are the different states, including the dead state. • Each case contains a switch statement whose cases are the different symbols. • Example: DFASwitch.cpp

  21. Using a Transition Table • The program uses a 2-dimensional array to store the transitions. • Rows represent states. • Columns represent symbols. • Example: DFATable.cpp

More Related