1 / 20

Regular Expressions

Regular Expressions. Lecture 12 Section 1.3 Mon, Sep 17, 2007. Regular Expressions. Regular expressions are like algebraic expressions except that they describe regular languages. Regular Expressions. The basic regular expressions are The symbols of the alphabet,  , .

conor
Télécharger la présentation

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. Regular Expressions Lecture 12 Section 1.3 Mon, Sep 17, 2007

  2. Regular Expressions • Regular expressions are like algebraic expressions except that they describe regular languages.

  3. Regular Expressions • The basicregular expressions are • The symbols of the alphabet, • , • .

  4. Regular Expressions • For a regular expression R, let L(R) be the language of R. • L(a) = {a}. • L() = {}. • L() =.

  5. Regular Operators • The regular operators are union, concatenation, and star. • If R1 and R2 are regular expressions, then so are • R1R2, • R1R2, • R1*

  6. Regular Operators • For the regular operators, • L(R1R2) = L(R1) L(R2). • L(R1R2) = L(R1) L(R2). • L(R1*) = (L(R1))*.

  7. The Empty String • The regular expression  is not essential because L(*) =  . • But we will use it because is so convenient.

  8. Examples • If  = {a, b}, then L(ab) = . • Furthermore, (a b)* represents all strings. • We will call this * for short.

  9. Examples • Write regular expressions for the following regular sets. • All strings ending with a. • All strings containing aaa. • All strings containing aaa or bbb. • All strings not containing aaa. • See more examples on p. 65.

  10. Examples • Let D = {0, 1, 2, …, 9} • Let L = {A, B, …, Z, a, b, …, z} • Write regular expressions for • C++ identifiers • C++ ints • C++ floats

  11. Regular Languages and Regular Expressions • Theorem: A language is regular if and only if it is the language of some regular expression. • Proof (): • The basic languages L(a), L(), and L() are regular. • Union, concatenation, and star of regular languages is regular.

  12. Regular Languages and Regular Expressions • Proof (): • Let L be a regular language. • We need to construct a regular expression R that describes it. • We will use a “GNFA,” a Generalized NFA

  13. Generalized Nondeterministic Finite Automata • A GNFA is like an NFA except that the labels are regular expressions. • To make a transition to another state, we must read a string described by the regular expression that is the label.

  14. Converting a DFA to a Regular Expression • Now begin with a transition diagram for L. • Replace each symbol label with the equivalent regular expression label. • Use the language L = {x | x has an even no. of a’s} to illustrate the proof.

  15. Converting a DFA to a Regular Expression • Add a new start state that has no transitions into it, but it has one -move to the original start state. • Add a new accept state that has no transitions out of it, but has -moves into it from all of the original accept states. • Remove all states from which the accept state is inaccessible.

  16. Converting a DFA to a Regular Expression • Let k be the number of states in the NFA. • Note that k 3. • We will reduce the number of states down to 2, at which point we will have the regular expression.

  17. Converting a DFA to a Regular Expression • Choose a non-initial, non-final state q to be removed. • For every state p with a transition into q and for every state r with a transition from q, • Create a transition from p to r.

  18. Converting a DFA to a Regular Expression • Let • R1 be the label on the transition p q. • R2 be the label on a loop q  q, if there is one. • R3 be the label on the transition from q  r. • Apply the label R1(R2)*R3 to the new transition.

  19. Converting a DFA to a Regular Expression • After doing this for every combination of transitions into q and out of q, remove state q and all of its transitions. • Repeat this process until only the initial and final states remain. • The label on that transition is the regular expression.

  20. Example • Find a regular expression for the language of all strings containing a string with an even number of a’s followed by a string with an even number of b’s.

More Related