nfa example
play

NFA Example Input: a a - PowerPoint PPT Presentation

NFA Example Input: a a B. Ward Spring 2014 1 NFA Example current state Input: a a


  1. NFA Example ��� � � � � � � � � ������� Input: a a B. Ward — Spring 2014 � 1

  2. NFA Example ��� � � � � � � � � ������� current state Input: a a Epsilon transition : � current input character Can transition from State 1 to State 2 without consuming any input. B. Ward — Spring 2014 � 2

  3. NFA Example ��� � � � � � � � � ������� current state Input: a a Regular transition : � current input character Can transition from State 2 to State 3 , which consumes the first ‘ a ’. B. Ward — Spring 2014 � 3

  4. NFA Example ��� � � � � � � � � ������� current state Input: a a Epsilon transition : � current input character Can transition from State 3 to State 2 without consuming any input. B. Ward — Spring 2014 � 4

  5. NFA Example ��� � � � � � � � � ������� current state Input: a a Regular transition : � current input character Can transition from State 2 to State 3 , which consumes the second ‘ a ’. B. Ward — Spring 2014 � 5

  6. NFA Example ��� � � � � � � � � ������� current state Input: a a Epsilon transition from State 3 to 4 : � current input character End of input reached, but the NFA can still carry out epsilon transitions. B. Ward — Spring 2014 � 6

  7. NFA Example ��� � � � � � � � � ������� current state Input: a a Input Accepted: � current input character There exists a sequence of transitions such that the NFA is in a final state at the end of input . B. Ward — Spring 2014 � 7

  8. Equivalent DFA Construction • Constructing a DFA corresponding to a RE. � ➡ In theory, this requires two steps . ‣ From a RE to an equivalent NFA . ‣ From the NFA to an equivalent DFA . • To be practical, we require a third optimization step. � ➡ Large DFA to minimal DFA . B. Ward — Spring 2014 � 8

  9. Example � ��������� ������� ������� � � � ��� � � � � ��������� � NFA to DFA � � 0*1(1|0)* � ����������������� ��� RE to NFA � � � Optimization �� � � ��������������������� � � � � � � ��� ��� � � � ��������������������� � � � � ������� � � Final DFA � �� � ���� � �� � 9 B. Ward — Spring 2014

  10. Step 1: RE ➔ NFA • Every RE can be converted to a NFA by repeatedly applying four simple rules. � ➡ Base case : a single character. ➡ Concatenation : joining two REs in sequence. ➡ Alternation : joining two REs in parallel. ➡ Kleene Closure : repeating a RE. (recall the definition of a RE) B. Ward — Spring 2014 � 10

  11. The Four NFA Construction Rules Rule 1—Base case: ‘a’ S a B. Ward — Spring 2014 � 11

  12. The Four NFA Construction Rules Rule 1—Base case: ‘a’ S a Simple two-state NFA (even DFA, too). B. Ward — Spring 2014 � 12

  13. The Four NFA Construction Rules Rule 2—Concatenation: AB S S followed by A B S AB B. Ward — Spring 2014 � 13

  14. The Four NFA Construction Rules Not just two states, but any NFA with a single final state . Rule 2—Concatenation: AB S S followed by A B S AB B. Ward — Spring 2014 � 14

  15. The Four NFA Construction Rules Rule 3--Alternation: “A|B” S S or A B A ε ε S B ε ε A|B B. Ward — Spring 2014 � 15

  16. The Four NFA Construction Rules Notice the epsilon transitions. Rule 3--Alternation: “A|B” S S or A B A ε ε S B ε ε A|B B. Ward — Spring 2014 � 16

  17. The Four NFA Construction Rules Rule 4—Kleene Closure: “A*” S A ε ε ε S A ε A* B. Ward — Spring 2014 � 17

  18. The Four NFA Construction Rules Notice the epsilon transitions. Rule 4—Kleene Closure: “A*” S A ε ε ε S A ε A* B. Ward — Spring 2014 � 18

  19. The Four NFA Construction Rules Rule 4—Kleene Closure: “A*” S zero occurrences A ε ε ε S A one occurrence ε repetition A* B. Ward — Spring 2014 � 19

  20. S Overview a • Four rules: � S A B ➡ Create two-state NFAs for individual A ε ε symbols, 
 S e.g., ‘a’ . B ε ε ➡ Append consecutive NFAs, e.g., AB . ε ➡ Alternate choices in ε ε parallel, e.g., A|B . S A ➡ Repeat Kleene Star , 
 ε e.g., A* . B. Ward — Spring 2014 � 20

  21. NFA Construction Example Regular expression: (a|b)(c|d)e* Apply Rule 1: � ��� � ��� � ��� � ��� � � � � ������� ������� ������� ������� Apply Rule 3: ε ε A ε ε B ��� ��� � � � � � � � � � � � � � � ������� � � ������� ��� ��� � � � � a|b c|d B. Ward — Spring 2014 � 21

  22. NFA Construction Example Regular expression: (a|b)(c|d)e* ��� ��� � � � � � � � � � � � � � � ������� � � ������� ��� ��� � � � � a|b c|d Apply Rule 2: A B ��� ��� � � � � � � � � � �� � � � � ������� � ��� ��� � �� � � (a|b)(c|d) B. Ward — Spring 2014 � 22

  23. NFA Construction Example Regular expression: (a|b)(c|d)e* � ��� Apply Rule 1: � ������� Apply Rule 4: ε ε ε S A ��� ε � � � � � � � � ������� e* B. Ward — Spring 2014 � 23

  24. NFA Construction Example Regular expression: (a|b)(c|d)e* ��� ��� ��� � � � � � � � � � � � � � � �� � � � � � � ������� � � ��� ��� ������� � �� � � (a|b)(c|d) e* Apply Rule 2: A B ��� ��� �� �� � ��� � � � � � � � � � � � �� � �� � � � ������� � � ��� ��� � �� � � (a|b)(c|d)e* B. Ward — Spring 2014 � 24

  25. Step 2: NFA ➔ DFA • Simulating NFA requires exploration of all paths. � ➡ Either in parallel (memory consumption!). ➡ Or with backtracking (large trees!). ➡ Both are impractical . • Instead, we derive a DFA that encodes all possible paths. � ➡ Instead of doing a specific parallel search each time that we simulate the NFA, we do it only once in general . • Key idea: for each input character, find sets of NFA states that can be reached. � ➡ These are the states that a parallel search would explore. ➡ Create a DFA state + transitions for each such set. ➡ Final states : a DFA state is a final state if its corresponding set of NFA states contains at least one final NFA state . B. Ward — Spring 2014 � 25

  26. NFA-to-DFA-CONVERSION: � � � todo: stack of sets of NFA states. � � � � push {NFA start state and all epsilon-reachable states} onto todo � � � � � � while (todo is not empty): � � � � curNFA: set of NFA states � curDFA: a DFA state � � � � curNFA = todo.pop � mark curNFA as done � � curDFA = find or create DFA state corresponding to curNFA � � reachableNFA: set of NFA states � � reachableDFA: a DFA state � � for each symbol x for which at least one state in curNFA has a transition: � � � � � reachableNFA = find each state that is reachable from a state in curNFA � via one x transition and any number of epsilon transitions � � � � � � if (reachableNFA is not empty and not done ): � push reachableNFA onto todo � � � � � reachableDFA = find or create DFA state corresponding to reachableNFA � add transition on x from curDFA to reachableDFA � end for � end while B. Ward — Spring 2014 � 26

  27. DFA Conversion Example Regular expression: (a|b)(c|d)e* ��� ��� �� �� � ��� � � � � � � � � � � � �� � �� � � � � ������� � ��� ��� � �� � � continues… � � ���������������� ������������ � � ��������� � ������������ � � ������� � ������������ ��������������� � B. Ward — Spring 2014 � 27

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend