Regular Expressions and Languages
Regular Expressions and Languages
Languages
1
4/11/2020
Regular Expressions vs. Finite Automata
2
4/11/2020
Regular Expressions
3
4/11/2020
Kleene Closure (the * operator)
4
4/11/2020
Regular Expressions
5
4/11/2020
Regular Expressions
6
4/11/2020
Regular Expressions
7
⚫ Examples
⚪ RE F = 1 “expresses” the language L(1) = {1}.
*
⚪ RE E = 1
4/11/2020
Regular Expressions
8
⚫ Examples
*
⚪ RE G = 01
4/11/2020
Regular Expressions
9
⚫ Examples
*
⚪ RE H = 1 + 01
4/11/2020
Regular Expressions
10
⚫ Examples
⚪ RE K = ε + a*
Language expressed by K ---
L = L(K) = L(ε + a*) = L(ε ) U L(a*)
= {ε} U {ε, a, aa, aaa, …}
= {ε, a, aa, aaa, …}
= L(a*)
That is, we have the following RE equalities:
ε + a* = a* = a* + ε
4/11/2020
Regular Expressions
11
⚫ Example
⚪ A RE defining a language of strings of alternating 0’s and 1’s
(including none) is one of the two below:
4/11/2020
Regular Expressions
12
Precedence of RE operators
⚪ Precedence
*
Highest --- (closure)
Next--- . (concatenation) (left to right)
Last--- + (union) (left to right)
Use parentheses anywhere to resolve ambiguity
4/11/2020
Regular Expressions
13
Precedence of RE operators
⚪ Example
4/11/2020
FA’s & RE’s
14
⚫ Important Theorems
⚪ Every language defined by a DFA is also defined by an RE.
4/11/2020
FA’s & RE’s
15
ε-NFA NFA
RE DFA
4/11/2020
FA’s & RE’s
16
4/11/2020
FA’s & RE’s
17
4/11/2020
FA’s & RE’s
18
4/11/2020
FA’s & RE’s
19
(k)
⚫ Meaning of R ---
ij
Basis:
⚪ when k = 0, since all state numbers ≥ 1, and so there is no
intermediate state in path i to j;
2 cases to consider:
(1) an arc (a transition) from i to j;
(2) a path from i to i itself.
4/11/2020
FA’s & RE’s
20
(k)
⚫ Meaning of R ---
ij
Basis (cont’d):
⚪ If i ≠ j, only (case 1) is possible:
no symbol for such a transition ⇒ Rij(0) = φ
one symbol a for the transition ⇒ Rij(0) = a
multiple symbls a1, a2, ..., am for the transition,
• ⇒ Rij(0) = a1 + a2 + ... + am
4/11/2020
FA’s & RE’s
21
(k)
⚫ Meaning of R ---
ij
Basis (cont’d) i ≠ j: qi qj
Rij(0) = φ
a
qi qj
Rij(0) = a
a1+…+am
(0)
qi qj
Rij = a1 + a2 + ... + am
4/11/2020
FA’s & RE’s
22
(k)
⚫ Meaning of R ---
ij
⚪ If i = j, only (case 2) is possible, which means there
exists at least a path ε from i to i itself, in addition to
the 3 cases:
no symbol for such a transition ⇒ Rij(0)= ε
one symbol a for the transition ⇒ Rij(0)= ε + a
multiple symbls a1, a2, ..., am for the transition,
• ⇒ Rij(0) = ε + a1 + a2 + ... + am
4/11/2020
FA’s & RE’s
(k) 23
⚫ Meaning of R ---
ij ε
Basis (cont’d) i = j: qi
Rij(0) = ε ε+a
Rij(0) = ε + a qi
qi
4/11/2020
FA’s & RE’s
24
R
Induction (to compute ij(k) ):
⚪ Suppose there is a path from i to j that goes through no state
numbered higher than k. Then, two cases should be
considered:
(1) the path does not go through k ⇒ Rij(k-1)
(2) the path goes through k at least once, then the path may be
broken into 3 pieces:
through i to k without passing k ⇒ Rik(k-1)
from k to k itself ⇒ (Rkk(k-1))* (recusive);
from k to j without passing k ⇒ Rkj(k-1).
4/11/2020
FA’s & RE’s
25
(Rkk(k-1))*
…… circulating zero
or more times
i … k … j
Rik (k-1)
Rkj(k-1)
4/11/2020
FA’s & RE’s
26
Induction (cont’d):
⚪ Combining (1) & (2), we get the RE defining “all the paths from
i to j that go through no state higher than k” as
Rij(k) = Rij(k-1) + Rik(k-1)(Rkk(k-1))*Rkj(k-1).
4/11/2020
FA’s & RE’s
27
⚫ Example
⚪ Convert the following DFA into an RE.
1 0, 1
0
start 1 2
⚪ Rij(0) may be constructed to be (details in the next page):
R11(0) ε+1
R12(0) 0
R21(0) φ
R22(0) (ε + 0 + 1)
4/11/2020
FA’s & RE’s
28
⚫ Example
1 0, 1
0
start 1 2
4/11/2020
FA’s & RE’s
29
⚫ Example (cont’d) 0, 1
1
0
start 1 2
4/11/2020
FA’s & RE’s
30
⚫ Example (cont’d)
⚪ There is only one final state 2, so only have to compute
R12(2) = R12(1) + R12(1)(R22(1))*R22(1).
4/11/2020
FA’s & RE’s
31
4. (ε + a)* = a* = (a + ε)*
4/11/2020
FA’s & RE’s
32
⚫ To compute
R12(2) = R12(1) + R12(1)(R22(1))*R22(1)
⚪ R12(1) = R12(0) + R11(0)(R11(0))*R12(0)
= 0 + (ε + 1)(ε + 1)*0 (by substitutions)
= 0 + (ε + 1)1*0 (by 4. in last slide )
= 0 + 1* 0 (by 5.)
= (ε + 1*)0 (by distributive law)
= 1*0 (by 4.)
4/11/2020
FA’s & RE’s
33
⚫ To compute
R12(2) = R12(1) + R12(1)(R22(1))*R22(1)
⚪ R22(1) = R22(0) + R21(0)(R11(0))*R12(0)
= (ε + 0 + 1) + φ(ε + 1)*0 (by substitutions)
= (ε + 0 + 1) + φ (by 1.)
=ε+0+1 (by 2.)
4/11/2020
FA’s & RE’s
34
⚫ To compute
R12(2) = R12(1) + R12(1)(R22(1))*R22(1)
⚪ Finally, R12(2)
= 1*0 +1*0(ε + 0 + 1)*(ε + 0 + 1) (by subst.)
= 1*0 +1*0(0 + 1)*(ε + 0 + 1) (by 4.)
= 1*0 +1*0(0 + 1)* (by 6.)
=1*0(ε + (0 + 1)*) (by distributive law)
= 1*0(0 + 1)* (by 4.)
4/11/2020
FA’s & RE’s
35
1 0, 1
0
start 1 2
4/11/2020