Damascus University
Automata theory and formal languages
Closure Properties & Regular
Languages
Dr. Mohammad Ahmad
Closure properties
Example
• The language L of strings that end in 101 is regular
(0+1)*101
• How about the language L of strings that do not end
in 101?
Example
• Hint: w does not end in 101 if and only if it ends in:
000, 001, 010, 011, 100, 110 or 111
or it has length 0, 1, or 2
• So L can be described by the regular expression
(0+1)*(000+001+010+010+100+110+111)
+ e + (0 + 1) + (0 + 1)(0 + 1)
Complement
• The complement L of a language L is the set of all
strings that are not in L
• Examples (S = {0, 1})
– L1 = all strings that end in 101
– L1 = all strings that do not end in 101
= all strings end in 000, …, 111 or have length 0, 1, or 2
– L2 = 1* = {e, 1, 11, 111, …}
– L2 = all strings that contain at least one 0
= (0 + 1)*0(0 + 1)*
Example
• The language L of strings that contain 101 is regular
(0+1)*101(0+1)*
• How about the language L of strings that do not
contain 101?
You can write a regular expression,
but it is a lot of work!
Closure under complement
If L is a regular language, so is L.
• To argue this, we can use any of the equivalent
definitions for regular languages:
regular
NFA DFA
expression
• The DFA definition will be most convenient
– We assume L has a DFA, and show L also has a DFA
Arguing closure under complement
• Suppose L is regular, then it has a DFA M
accepts L
• Now consider the DFA M’ with the accepting and
rejecting states of M reversed
accepts strings not in L
this is exactly L
Food for thought
• Can we do the same thing with an NFA? NO!
0, 1
1 0
q0 q1 q2 (0+1)*10
0, 1
1 0
q0 q1 q2 (0+1)*
Intersection
• The intersection L L’ is the set of strings that are
in both L and L’
• Examples:
L = (0 + 1)*11 L’ = 1* L L’ = 1*11
L = (0 + 1)*10 L’ = 1* L L’ = ∅
• If L, L’ are regular, is L L’ also regular?
Closure under intersection
If L and L’ are regular languages, so is L L’.
• To argue this, we can use any of the equivalent
definitions for regular languages:
regular
NFA DFA
expression
Suppose L and L’ have DFAs, call them M and M’
Goal: Construct a DFA (or NFA) for L L’
An example
M 1 1 0 0
M’
0 1
r0 r1 s0 s1
0 1
L = even number of 0s L’ = odd number of 1s
1
r0, s0 r0, s1
1
0 0 0 0
1
r1, s0 r1, s1
1
L L’ = even number of 0s and odd number of 1s
Closure under intersection
M and M’ DFA for L L’
states Q = {r1, ..., rn} Q × Q’ = {(r1, s1), (r1, s2), ...,
Q’ = {s1, ..., sn’} (r2, s1), ..., (rn, sn’)}
start state ri for M (ri, sj)
sj for M’
accepting F for M F × F’ = {(ri, sj): ri F, sj F’}
states F’ for M’
Whenever M is in state ri and M’ is in state sj,
the DFA for L L’ will be in state (ri, sj)
Closure under intersection
M and M’ DFA for L L’
a
transitions ri rj in M ri, si’ a rj, sj’
a
si’ sj’ in M’
Reversal
• The reversal wR of a string w is w written backwards
w = cave wR = evac
• The reversal LR of a language L is the language
obtained by reversing all its strings
L = {cat, dog} LR = {tac, god}
Reversal of regular languages
• L = all strings that end in 101 is regular
(0+1)*101
• How about LR?
• This is the language of all strings beginning in 101
• Yes, because it is represented by
101(0+1)*
Closure under reversal
If L is a regular language, so is LR.
• How do we argue?
regular
NFA DFA
expression
Arguing closure under reversal
• Take a regular expression E for L
• We will show how to reverse E
• A regular expression can be of the following types:
– The special symbols and e
– Alphabet symbols like a, b
– The union, concatenation, or star of simpler expressions
Proof of closure under reversal
regular expression E reversal ER
e e
a (alphabet symbol) a
E1 + E2 E1R + E2R
E1E2 E2RE1R
E1* (E1R)*
A question
LDUP = {ww: w L} Ex. L = {cat, dog}
LDUP = {catcat, dogdog}
If L is regular, is LDUP also regular?
regular
expression
?
NFA DFA
A question
• Let’s try with regular expression:
L = {a, b}
L DUP = LL LDUP = {aa, bb}
LL = {aa, ab, ba, bb}
• Let’s try with NFA:
q0 e NFA for L e NFA for L e q1
An example
L = 0*1 is regular
LDUP = {1, 01, 001, 0001, ...}
LDUP = {11, 0101, 001001, 00010001, ...}
= {0n10n1: n ≥ 0}
• Let’s try to design an NFA for LDUP
An example
0 0 0 0
1 1 1 1
1 01 001 0001
LDUP = {11, 0101, 001001, 00010001, ...}
= {0n10n1: n ≥ 0}
For regular language L1and L2
Union: L1 L2
Concatenation: L1L2
Are regular
Star: L1 *
Languages
Reversal: R
L1
Complement: L1
Intersection: L1 L2
24
2
5
Regular Grammars
2
6 Regular Grammars
Grammars is generative description of a language
A regular grammar G is a quadruple (V, S, R, S), where:
● V is the rule alphabet (Variables), which contains
non-terminals,
● S (the set of terminals).
● R (the set of rules) is a finite set of rules of the form:
X → Y,
● S (the start symbol) is a nonterminal.
2
7 Regular Grammars
In a regular grammar, all rules in R must:
● have a left hand side that is a single nonterminal
● have a right hand side that is:
● e, or
● a single terminal, or
● a terminals followed by a single nonterminal.
Legal: S → a, S → e, and T → aS
Not legal: S → aSa and aSa → T
Regular Grammars
A regular grammar is any right-linear or left-
linear grammar
Examples:
G1 G2
S → abS S → Aab
S →a A → Aab | B
B→a
28
Observation
Regular grammars generate regular languages
Examples: G2
G1 S → Aab
S → abS A → Aab | B
S →a B→a
L(G1 ) = (ab) * a L(G2 ) = aab(ab) *
29
3
0 Regular Grammar Example
L = {w {a, b}* : |w| is even}
FSM:
3
1 Regular Grammar Example
L = {w {a, b}* : |w| is even}
Regular expression: ((aa) (ab) (ba) (bb))*
FSM:
Regular grammar: G = (V, S, R, S), where
V = {S, T }, S = {a, b}, R = { S→e
S → aT
S → bT
T → aS
T → bS
}
In General
a
For any transition: q p
Add production: q → ap
variable terminal variable
32
3
3 Regular Languages and Regular Grammars
Theorem: The class of languages that can be defined with
regular grammars is exactly the regular languages.
3
4 Regular Languages and Regular Grammars
Regular grammar → FSM:
grammar to fsm (G = (V, S, R, S)) =
1. Create in M a separate state for each nonterminal in V.
2. Start state is the state corresponding to S .
3. If there are any rules in R of the form X → w, for some
w S, create a new state labeled #.
4. For each rule of the form X → w Y, add a transition from
X to Y labeled w.
5. For each rule of the form X → w, add a transition from X
to # labeled w.
6. For each rule of the form X → e, mark state X as
accepting.
7. Mark state # as accepting.
FSM → Regular grammar: Similarly.
3
5 Example 1 - Even Length Strings
S→e
S → aT
S → bT
T → aS
T → bS
3
6 Example 1 - Even Length Strings
S→e
S → aT
S → bT
T → aS
T → bS
3
7 Strings that End with aaaa
L = {w {a, b}* : w ends with the pattern aaaa}.
S → aS
S → bS
S → aB
B → aC
C → aD
D→a
3
8 Strings that End with aaaa
L = {w {a, b}* : w ends with the pattern aaaa}.
S → aS
S → bS
S → aB
B → aC
C → aD
D→a
3
9 Example 2 – One Character Missing
S→e A → bA C → aC
S → aB A → cA C → bC
S → aC A→e C→e
S → bA B → aB
S → bC B → cB
S → cA B→e
S → cB
4
0 Example 2 – One Character Missing
S→e A → bA C → aC
S → aB A → cA C → bC
S → aC A→e C→e
S → bA B → aB
S → bC B → cB
S → cA B→e
S → cB
4
1 Regular Languages and Regular Grammars
FSM → Regular grammar:
Show by construction that, for every FSM M there exists a
regular grammar G such that L(G) = L(M).
1. Make M deterministic → M’ = (Q, S, , s, F).
Construct G = (V, S, R, S) from M’.
2. Create a nonterminal for each state in the M’.
V = Q.
3. The start state becomes the starting nonterminal.
S = s.
4. For each transition (T, a) = U, make a rule of the form
T → aU.
5. For each accepting state T, make a rule of the form
T → e.
4
2 Strings that End with aaaa
L = {w {a, b}* : w ends with the pattern aaaa}.
4
3 Strings that End with aaaa
L = {w {a, b}* : w ends with the pattern aaaa}.
S → aS
S → bS
S → aB
B → aC
C → aD
D→a