Regular Expressions and DFAs
Regular Expressions and DFAs
and DFAs
Regular Expression
• Let ∑ be a finite set of symbols and let L, L1, and L2 be sets of strings
from ∑*. L1L2 is the set {xy | x is in L1, and y is in L2}
• L1 = {10, 1}, L2 = {011, 11}, L1L2 = ?
Answer
• Write RE for
• All strings of 0’s and 1’s
• All strings of 0’s and 1’s with at least 2
consecutive 0’s
• All strings of 0’s and 1’s beginning with 1 and not having two consecutive 0’s
Answer
• (0+1)*
All strings of 0’s and 1’s
• (0+1)*00(0+1)*
All strings of 0’s and 1’s with at least 2
consecutive 0’s
• (1+10)* (1+10)
All strings of 0’s and 1’s beginning with 1
and not having two consecutive 0’s
More Exercises
• 1) (0+1)*011
• 2) 0*1*2*
• 3) 00*11*22*
More Exercises (Answers)
1) (0+1)*011
Answer: all strings of 0’s and 1’s ending in 011
2) 0*1*2*
• Answer: any number of 0’s followed by
any number of 1’s followed by any number
of 2’s
• 3) 00*11*22*
Answer: strings in 0*1*2 with at least one of
each symbol
Using Regular Expressions
• ab*a:
Problem 2
a
b
b
q0 a q2 q3
a
Solution
• a(a|b)*a
a
b
b
q0 a q2 q3
a
DFA to RE: State Elimination
• If there are n accepting states, we must repeat the above steps for
each accepting states to get n different regular expressions, R1, R2, …
Rn.
• For each repeat we turn any other accepting state to non-accepting.
• The desired regular expression for the automaton is then the union
of each of the n regular expressions: R1 U R2… U RN
DFA->RE Example
Eliminate State 1:
Answer: (0+10)*11(0+1)*
Second Example
• Eliminate state 2:
Second Example (2)
Resulting R.E.:
(0+1)*1(0+1)+
(0+1)*1(0+1)(0+1) or
(0+1)*1(0+1)(ϵ+0+1)
35