Transition Diagrams Lecture 3 Wed Jan 21 2004

  • Slides: 21
Download presentation
Transition Diagrams Lecture 3 Wed, Jan 21, 2004

Transition Diagrams Lecture 3 Wed, Jan 21, 2004

Building Transition Diagrams from Regular Expressions l l A regular expression consists of symbols

Building Transition Diagrams from Regular Expressions l l 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.

Building Transition Diagrams l The basic cases. l For , build : l For

Building Transition Diagrams l The basic cases. l For , build : l For each symbol a , build a: a

Building Transition Diagrams l The recursive cases. l For the expression r | s,

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

Building Transition Diagrams l For the expression rs, build rs: l r s For

Building Transition Diagrams l For the expression rs, build rs: l r s For the expression r*, build r*: r

Building Transition Diagrams l l Applying these rules builds an NFA representing the regular

Building Transition Diagrams l l 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.

Example: Building a Transition Diagram l l Build a transition diagram from the regular

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

Converting an NFA to a DFA l l l Let Q be the states

Converting an NFA to a DFA l l l 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.

Converting an NFA to a DFA l For every state A (Q) and every

Converting an NFA to a DFA l 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.

Example: A DFA from an NFA l l Consider the NFA of the regular

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

Example: A DFA from an NFA l Find the -closure of each state. l

Example: A DFA from an NFA l Find the -closure of each state. l l l l -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}.

Example: A DFA from an NFA l l l l -cl(8) = {8}. -cl(9)

Example: A DFA from an NFA l l l l -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.

Example: A DFA from an NFA l The result is a 1 2, 3,

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

Minimizing a DFA l l To minimize a DFA is to reduce the number

Minimizing a DFA l l 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.

Example: Minimizing a DFA l l Minimize the DFA of regular expression ab*(a |

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

Example: Minimizing a DFA l l The initial partition is {1, 5}, {2, 3,

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

Example: Minimizing a DFA l The second partition is {1}, {2, 4}, {3}, {5}.

Example: Minimizing a DFA l The second partition is {1}, {2, 4}, {3}, {5}. l l 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.

Example: Minimizing a DFA l The minimized DFA is b 1 a 2 a

Example: Minimizing a DFA l The minimized DFA is b 1 a 2 a 3

Programming a DFA l There are two basic methods of programming a DFA. l

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

Using Switch Statements l l l The main function contains a switch statement whose

Using Switch Statements l l l 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

Using a Transition Table l l The program uses a 2 -dimensional array to

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