200 likes | 376 Views
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
E N D
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 • , • , • (R1R2) where R1 and R2 are regular expressions • (R1R2) 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!
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())*
Examples of regular expressions • Note: We drop parentheses when not required. • Example: (ab) is written ab • Let = {a,b} • The following are regular expressions: • , a, b • *, a*, b*, ab, ab, a* // is dropped • (ab)*, a*b*, (ab)* • (ab)*ab, etc. • Are the following regular expressions? • a= , *a, a*b • See page 65 example 1.27
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.
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!
a Example: NFA for (ab)*b a b b a ab b
Example: NFA for (a b)*b a (ab)* b
Example: NFA for (a U b)*b a (ab)*b b b
DFA regular expression--proof of b • Easier to do: • DFA GNFAregular 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 .
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.
a,b a b 1 2 DFA ab a b t s GNFA 1 2 Note that the arrows are omitted since a transition with arrow will never be used.
DFA regular expression (contd.) • Idea: • Convert DFA special GNFA. • Convert GNFAregular expression: • Eliminate allstates, except start and accept state, one state at a time. • Output the label on the single transition left at the end.
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
Constructing regular expression. a a b q1 q0 b DFA L = {w in {a, b}* | w has odd number of b's }
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
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
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
Regular language vs regular expression • Conclusion: Regular expressions exactly describe the class of regular languages. • E.g., • (0 1)*1strings 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 • …
More • Exercises: 1.18, 1.19