1 / 20

Lecture4 Regular Expressions

Lecture4 Regular Expressions. Regular expressions. A third way to view regular languages. Say that R is a regular expression if R is a , for some a in the alphabet  ,  , (R 1 R 2 ) where R 1 and R 2 are regular expressions

miya
Download Presentation

Lecture4 Regular Expressions

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. Lecture4 Regular Expressions

  2. Regular expressions • A third way to view regular languages. • Say that R is a regular expression if R is • a, for some a in the alphabet  • , •  , • (R1R2) where R1 and R2 are regular expressions • (R1R2) where R1 and R2 are regular expressions • (R1*) where R1 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. 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. 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. Regular expression vs. Regular language • 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.

  6. a Regular expression  NFA--proof of a • R: L(R): NFA that recognizes L(R) • a, {a} • , {} • ,  • (R1 R2) L(R1) L(R2) • (R1 R2) L(R1)  L(R2) • (R1*)(L(R1))* Proof by induction!

  7. a Example: NFA for (ab)*b a b b a  ab b 

  8. Example: NFA for (a  b)*b  a   (ab)*  b 

  9. Example: NFA for (a U b)*b  a (ab)*b     b    b

  10. DFA  regular expression--proof of b • Easier to do: • DFA GNFAregular expression. • GNFA (Generalized NFA) • A GNFA G = (Q, , , qstart, qaccept): • Q is the finite set of states, •  is the input alphabet, • : (Q-{qaccept})  (Q-{qstart})R is the transition function, • qstart is the start state, and • qaccept is the accept state, which is different from qstart. where R is the collection of regular expressions over .

  11. DFA  regular expression--proof of b • A GNFA accepts a string w in * if w = w1w2…wk, where each wi is in * and a sequence of states q0,q1,…,qk exists such that • q0=qstart is the start state • qk=qaccept is the accept state • For each i, we have wi L(Ri), where Ri= (qi-1, qi), i.e., Ri is the expression on the arrow from qi-1 to qi. • 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. a,b a b 1 2 DFA ab a  b  t s GNFA 1 2 Note that the  arrows are omitted since a transition with  arrow will never be used.

  13. DFA  regular expression (contd.) • Idea: • Convert DFA special GNFA. • Convert GNFAregular expression: • Eliminate allstates, except start and accept state, one state at a time. • Output the label on the single transition left at the end.

  14. Eliminating state q{rip} R4 qi qj R3 R1 qrip R2 qi qj (R1 )(R2)* (R3)  (R4) For each in-point qito each out-point qj, do

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

  16. Constructing regular expression (contd.) a a b   q2 q1 q3 q0 b Added: a new start state and a new final state with empty transitions

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

  18. Constructing regular expression (contd.) a(ba*b) a*b  q2 q1 q3 qrip a*b(a(ba*b))* q2 q3 We take out the qrip state and we are left with the regular expression

  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. More • Exercises: 1.18, 1.19

More Related