Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if.

Similar presentations


Presentation on theme: " 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if."— Presentation transcript:

1  2004 SDU Lecture4 Regular Expressions

2  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if R is 1.a, for some a in the alphabet  2. , 3. , 4.(R 1  R 2 ) where R 1 and R 2 are regular expressions 5.(R 1  R 2 ) where R 1 and R 2 are regular expressions 6.(R 1 * ) where R 1 is a regular expression The value of a regular expression is a language.  The value of ((0  1)*) is { s | s is any string of 0 and 1}  Operation order: parenthesis, star, concatenation, union  Note that this is an inductive definition!

3  2004 SDU 3 Language described by a regular expression Notation: L(r), or r, denotes the language described by regular expression r, define L(  ) =  L(a) = {a} L(  ) = {  } L(    ) = L(  )  L(  ) L(    ) = L(  )  L(  ) L(  * ) = (L(  )) *

4  2004 SDU 4 Examples of regular expressions Note: We drop parentheses when not required.  Example: (a  b) is written a  b Let  = {a,b} The following are regular expressions:  , a, b   *, a *, b *, ab, a  b, a *  //  is dropped  (a  b) *, a * b *, (ab) *  (a  b) * ab, etc. Are the following regular expressions?    a= , * a, a *b See page 65 example 1.27

5  2004 SDU 5 A language is regular if and only if some regular expression describes it. Theorem:  (a) If a language is described by a regular expression, then it is regular.  (b) If a language is regular, then it is described by a regular expression. Regular expression vs. Regular language

6  2004 SDU 6 Regular expression  NFA --proof of a R: L(R): NFA that recognizes L(R) 1.a, {a} 2. , {  } 3. ,  4.(R 1  R 2 ) L(R 1 )  L(R 2 ) 5.(R 1  R 2 ) L(R 1 )  L(R 2 ) 6.(R 1 * ) (L(R 1 )) * a Proof by induction!

7  2004 SDU 7 Example: NFA for (a  b) * b a b a b   a b abab

8  2004 SDU 8 Example: NFA for (a  b) * b a b   (a  b) *   

9  2004 SDU 9 Example: NFA for (a U b) * b a b      (a  b) * b b   

10  2004 SDU 10 DFA  regular expression --proof of b Easier to do:  DFA  GNFA  regular expression. GNFA (Generalized NFA)  A GNFA G = (Q, , , q start, q accept ): 1.Q is the finite set of states, 2.  is the input alphabet, 3.  : (Q-{q accept })  (Q-{q start })  R is the transition function, 4.q start is the start state, and 5.q accept is the accept state, which is different from q start. where R is the collection of regular expressions over .

11  2004 SDU 11 DFA  regular expression --proof of b A GNFA accepts a string w in  * if w = w 1 w 2 …w k, where each w i is in  * and a sequence of states q 0,q 1,…,q k exists such that 1.q 0 =q start is the start state 2.q k =q accept is the accept state 3.For each i, we have w i  L(R i ), where R i =  (q i-1, q i ), i.e., R i is the expression on the arrow from q i-1 to q i.  From a DFA D to a GNFA M:  Add an extra start state s and an extra accept state t  From s to the old start state add an  arrow  From the old accept states to t add  arrows  Multiple labels or arrows are replaced by single arrow labeled by the union of the labels  Add arrows labeled  between states with no arrows  D and M recognize the same language.

12  2004 SDU 12 12 a a,b b DFA 12 a abab b s   t GNFA Note that the  arrows are omitted since a transition with  arrow will never be used.

13  2004 SDU 13 DFA  regular expression (contd.) Idea:  Convert DFA  special GNFA.  Convert GNFA  regular expression: –Eliminate all states, except start and accept state, one state at a time. –Output the label on the single transition left at the end.

14  2004 SDU 14 Eliminating state q {rip} R4R4 R3R3 R1R1 (R 1 )(R 2 )* (R 3 )  (R 4 ) R2R2 qiqi q rip qjqj qiqi qjqj For each in-point q i to each out-point q j, do

15  2004 SDU 15 Constructing regular expression. DFA L = {w in {a, b}* | w has odd number of b's } b b q0 q1 a a

16  2004 SDU 16 Constructing regular expression (contd.) Added: a new start state and a new final state with empty transitions b b q0 q1 a a q2q3 

17  2004 SDU 17 Constructing regular expression (contd.) q rip We take out the q rip state and it leaves us with a 3 state Finite Automata Before we take out q rip, we have to see all the paths through the q rip state and all possible transitions. Eliminate states one-by-one b q0 b q1 a a q2q3  q2 a*b q1 a  (ba*b) q3 

18  2004 SDU 18 Constructing regular expression (contd.) q rip We take out the q rip state and we are left with the regular expression q2 a*b q1 a  (ba*b) q3  q2q3 a*b(a  (ba*b))*

19  2004 SDU 19 Regular language vs regular expression Conclusion: Regular expressions exactly describe the class of regular languages.  E.g., –(0  1)*1  strings ending with 1 –(00)*  (000)*  strings of multiple of 2 or 3 0’s –((0  1)1)*  strings in which every even position is 1 –(0  1)*111 ((0  1)*  strings that contain 111 as substring –…–…

20  2004 SDU 20 More Exercises: 1.18, 1.19


Download ppt " 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if."

Similar presentations


Ads by Google