Regular and Nonregular Languages
Regular and Nonregular Languages
Languages
Chapter 8 1
Languages: Regular or Not?
a*b* is regular.
{anbn: n 0} is not.
2
How Many Regular Languages?
Theorem: There is a countably infinite number of regular
languages.
Proof:
● Upper bound on number of regular languages:
number of FSMs (or regular expressions).
{a},{aa},{aaa},{aaaa},{aaaaa},{aaaaaa},…
3
Regular And Nonregular
Languages?
There is a countably infinite number of regular languages.
4
Showing that a Language is Regular
Theorem: Every finite language is regular.
s1 s2 … sn
So it too is regular.
5
Showing that a Language is Regular
Example:
L=
Is L1 regular?
Is L2 regular?
Is L regular?
6
Finiteness - Theoretical vs. Practical
Any finite language is regular. The size of the language doesn't
matter.
7
Regular Does Not Always Mean Tractable
8
Showing That L is Regular
1. Show that L is finite.
9
Closure Properties of Regular Languages
● Union
● Kleene star
● Complement
● Intersection
● Difference
● Reverse
● Letter substitution 10
Closure of Regular Languages
Under Complement
11
Closure of Regular Languages
Under Complement
What about:
On input bbb?
12
Closure of Regular Languages
Under Complement
What about:
On input a?
13
A Complement Example
14
The Complement Procedure
Given: an FSM M,
Construct a new machine to accept L(M):
15
Closure of Regular Languages
Under Intersection
L1 L2 =
L1 L2
● Union
● Concatenation
● Kleenestar
● Complementation
16
Closure of Regular Languages
Under Intersection
L1 L2 =
(L1 L2)
L1 L2
17
Closure of Regular Languages
Under Difference
L1 - L2 =
18
Closure of Regular Languages
Under Difference
L1 - L2 = L1 L2
19
Letter Substitution
● Let 1 and 2 be alphabets.
Example:
sub(a) = 0, and
sub(b) = 11.
20
Letter Substitution
letsub is a letter substitution function iff:
Example:
sub(a) = 0, and
sub(b) = 11.
21
Divide-and-Conquer
Let L = {w {a, b}* : w contains an even number of a’s
and an odd number of b’s and all a’s come in runs of
three}.
L = L1 L2, where:
• L1 = {w {a, b}* : w contains an even number of a’s
and an odd number of b’s}, and
22
L1 is Regular
23
L2 is Regular
24
Don’t Try to Use Closure Backwards
One Closure Theorem:
L = L1 L2
L = L1 L2
25
Don’t Try to Use Closure Backwards
One Closure Theorem:
L = L1 L2
L = L1 L2
26
Don’t Try to Use Closure Backwards
One Closure Theorem:
L = L1 L2
L = L1 L2
L = L1 L2
L = L1 L2
28
Don’t Try to Use Closure Backwards
Another Closure Theorem:
L = L1 L2
L = L1 L2
{abanbn : n 0} = {ab} {anbn : n 0}
29
Don’t Try to Use Closure Backwards
Another Closure Theorem:
L = L1 L2
L = L1 L2
Not
{abanbn : n 0} = {ab} {anbn : n 0}
Regular
{aaa*} = {a*} {ap: p is prime}
Regular 30
Showing that a Language is Not Regular
Example:
{anbn, n 0} is not regular
31
Showing that a Language is Not Regular
The only way to generate/accept an infinite language with
a finite description is to use:
• Kleene star (in regular expressions), or
• cycles (in automata).
Example:
ab*a generates aba, abba, abbba, abbbba, etc.
Example:
{an : n 1 is a prime number} is not regular.
32
How Long a String Can Be Accepted?
37
The Pumping Theorem for Regular Languages
So, k 1
( x, y, z (w = xyz,
|xy| k,
y , and
q 0 (xyqz is in L)))).
38
Example: {anbn: n 0} is not Regular
k is the number from the Pumping Theorem.
Choose w to be ak/2bk/2 (“long enough”).
1 2
aaaaa…aaaaabbbb …bbbbbb
x y z
Case III
We show that there is no x, y, z with the required properties:
|xy| k,
y ,
q 0 (xyqz is in L).
Three cases to consider:
● Case I: y falls in region 1:
● Case II: y falls across regions 1 and 2:
● Case III: y falls in region 2: 39
Example: {anbn: n 0} is not Regular
k is the number from the Pumping Theorem.
Choose w to be ak/2bk/2 (“long enough”).
1 2
aaaaa…aaaaabbbb …bbbbbb
x y z
40
Example: {anbn: n 0} is not Regular
Second try:
Choose w to be akbk (We get to choose any w).
1 2
aaaaa…aaaaabbbb …bbbbbb
x y z
44
PalEven = {wwR : w {a, b}*}
Again, a w in L can be very complicated, but we only pick one.
45
{anbm: n > m}
Sometimes we need to pump out.
y = ap for some p.
q 0 (a|x| + |z| +q|y| must be in L). So |x| + |z| +q|y| must be prime.
47
L = {an: n is prime}
Let w = aj, where j is the smallest prime number > k+1.
y = ap for some p.
q 0 (a|x| + |z| +q|y| must be in L). So |x| + |z| +q|y| must be prime.
● To choose q:
● Try letting q be either 0 or 2.
● If that doesn’t work, analyze L to see if there is some
other specific value that will work.
49
Using the Closure Properties
• Intersection
• Complement
50
Using the Closure Properties
L = L _______
51
L = {aibj: i, j 0 and i j}
52
L = {aibj: i, j 0 and i j}
53
L = {aibj: i, j 0 and i j}
An easier way:
54
L = {aibj: i, j 0 and i j}
An easier way:
55
L = {aibjck: i, j, k ≥ 0 and (if i = 1 then j = k)}
But is L regular?
56
L = {aibjck: i, j, k ≥ 0 and (if i = 1 then j = k)}
57
L = {aibjck: i, j, k ≥ 0 and (i 1 or j = k)}
Every string in L of length at least 1 is pumpable:
•If i = 0 then: if j 0, let y be b; otherwise, let y be c. Pump
in or out. Then i will still be 0 and thus not equal to 1, so
the resulting string is in L.
58
L = {aibjck: i, j, k ≥ 0 and (i 1 or j = k)}
59
L = {aibjck: i, j, k ≥ 0 and (i 1 or j = k)}
An Alternative
If L is regular, then so is LR:
LR = {ckbjai : i, j, k ≥ 0 and (i 1 or j = k)}
Use Pumping to show that L is not regular:
60
{abanbn : n 0}
Choose w = abakbk
k
abaaaaaaaaaabbbbbbbbbbb
x y z
(, a)
(, ab)
(, aba+)
(a, b)
(a, ba+)
(aba*, a+)
61
A Different Approach to abanbn?
L = ab || {anbn: n 0}
? regular not regular
62
There is a Closure Theorem that Helps
{abanbn : n 0}
63
Exploiting Problem-Specific Knowledge
L = {w {0, 1, 2, 3, 4 ,5, 6, 7}*:
w is the octal representation of a
nonnegative integer that is divisible by 7}
64
Exploiting Problem-Specific Knowledge
Let = {w, h, q, e, x, r, l }.
Let L = {w : w represents a song in 4/4 time}.
65
Is English Regular?
Is English finite?
66
In the event that the Purchaser defaults in the payment of any installment of purchase price, taxes,
insurance, interest, or the annual charge described elsewhere herein, or shall default in the
performance of any other obligations set forth in this Contract, the Seller may: at his option: (a)
Declare immediately due and payable the entire unpaid balance of purchase price, with accrued
interest, taxes, and annual charge, and demand full payment thereof, and enforce conveyance of
the land by termination of the contract or according to the terms hereof, in which case the
Purchaser shall also be liable to the Seller for reasonable attorney's fees for services rendered by
any attorney on behalf of the Seller, or (b) sell said land and premises or any part thereof at public
auction, in such manner, at such time and place, upon such terms and conditions, and upon such
public notice as the Seller may deem best for the interest of all concerned, consisting of
advertisement in a newspaper of general circulation in the county or city in which the security
property is located at least once a week for Three (3) successive weeks or for such period as
applicable law may require and, in case of default of any purchaser, to re-sell with such
postponement of sale or resale and upon such public notice thereof as the Seller may determine,
and upon compliance by the Purchaser with the terms of sale, and upon judicial approval as may
be required by law, convey said land and premises in fee simple to and at the cost of the Purchaser,
who shall not be liable to see to the application of the purchase money; and from the proceeds of
the sale: First to pay all proper costs and charges, including but not limited to court costs,
advertising expenses, auctioneer's allowance, the expenses, if any required to correct any
irregularity in the title, premium for Seller's bond, auditor's fee, attorney's fee, and all other
expenses of sale occurred in and about the protection and execution of this contract, and all
moneys advanced for taxes, assessments, insurance, and with interest thereon as provided herein,
and all taxes due upon said land and premises at time of sale, and to retain as compensation a
commission of five percent (5%) on the amount of said sale or sales; SECOND, to pay the whole
amount then remaining unpaid of the principal of said contract, and interest thereon to date of
payment, whether the same shall be due or not, it being understood and agreed that upon such
sale before maturity of the contract the balance thereof shall be immediately due and payable;
THIRD, to pay liens of record against the security property according to their priority of lien and to
the extent that funds remaining in the hands of the Seller are available; and LAST, to pay the
remainder of said proceeds, if any, to the vendor, his heirs, personal representatives, successors 67 or
assigns upon the delivery and surrender to the vendee of possession of the land and premises, less
costs and excess of obtaining possession.
Is English Regular?
● The rat ran.
● The rat that the cat saw ran.
● The rat that the cat that the dog chased
saw ran.
Let:
A = {cat, rat, dog, bird, bug, pony}
V = {ran, saw, chased, flew, sang, frolicked}.