Presentation is loading. Please wait.

Presentation is loading. Please wait.

NFAs, DFAs, and regular expressions

Similar presentations


Presentation on theme: "NFAs, DFAs, and regular expressions"— Presentation transcript:

1 NFAs, DFAs, and regular expressions
The Chinese University of Hong Kong Fall 2010 CSCI 3130: Automata theory and formal languages NFAs, DFAs, and regular expressions Andrej Bogdanov

2 Three ways of doing it L = {x Î S*: x ends in 01} S = {0, 1} (0+1)*01
1 0, 1 q0 q1 q2 qe 1 q0 q1 q00 q10 q01 q11 (0+1)*01 regular expression NFA DFA

3 They are all the same NFA regular expression DFA regular languages

4 Road map regular expression NFA NFA without e DFA

5 Examples: regular expression → NFA
q0 q1 R1 = 0 R2 = 01 1 q0 q1 q2

6 Examples: regular expression → NFA
q1 q2 q0 q6 R3 = q3 q4 q5 1 q0’ q1’ R4 = (0 + 01)* NFA3

7 Regular expressions → NFA
In general, how do we convert a regular expression to an NFA? A regular expression over S is an expression formed using the following rules: The symbol Æ is a regular expression The symbol e is a regular expression For every a  S, the symbol a is a regular expression If R and S are regular expressions, so are R+S, RS and R*.

8 General method Æ e regular expr NFA q0 q0 q0 q1 a a  S RS q0 q1 
NFAR NFAS

9 General method continued
regular expr NFA NFAR q0 q1 R + S NFAS q0 q1 R* NFAR

10 Road map regular expression NFA NFA without e DFA

11 Road map regular expression NFA NFA without e DFA

12 Road map regular expression NFA NFA without e DFA 2-state GNFA GNFA

13 Generalized NFAs A generalized NFA is an NFA whose transitions are labeled by regular expressions, like moreover It has exactly one accept state, different from its start state No arrows come into the start state No arrows go out of the accept state 0*1 e+10* 0*11 q0 q1 q2 01

14 Converting a DFA to a GNFA
regular expression NFA NFA without e DFA 2-state GNFA GNFA q1 q3 q5 q0 qf e

15    Conversion example
1 q0 e q3 1 q1 q2 It has exactly one accept state, different from its start state No arrows come into the start state No arrows go out of the accept state

16 GNFA state reduction regular expression NFA NFA without e DFA 2-state GNFA GNFA We will eliminate every state but the start and accept states

17 State elimination e+10* 0*11 0*1 q0 q1 q2 01 q0 q2 01

18 State elimination – general method
To eliminate state qk, for every pair of states (qi, qj) remember to do this even when qi = qj! R2 R1 R3 Replace qi qk qj R4 R1R2*R3 + R4 by qi qj

19       Road map NFA without e NFA regular expression DFA GNFA
2-state GNFA GNFA q0 q1 R A 2-state GNFA is the same as a regular expression R!

20 Conversion example Eliminate q1: Eliminate q2: Check: 0*1(00*1+1)* = 1
1 e 1 e q0 q1 q2 q3 00*1+1 0*1 e Eliminate q1: q0 q2 q3 0*1(00*1+1)* Eliminate q2: q0 q3 q1 q2 1 ? Check: 0*1(00*1+1)* =

21 Check your answer! 0*1(00*1+1)* 0*1(0*1)* 011001000101
q1 q2 1 All strings that end in 1 (0 + 1)*1 0*1(00*1+1)* Always ends in 1 = Does every string that ends in 1 have this form? 0*1(0*1)* Yes!

22 Design Analyze Convert What you need to know regular expression NFA
DFA regular languages Design Analyze Convert


Download ppt "NFAs, DFAs, and regular expressions"

Similar presentations


Ads by Google