Computation Theory: Expressions Languages Grammar
Computation Theory: Expressions Languages Grammar
Computation Theory: Expressions Languages Grammar
Chapter 3
1
Chap3: Regular expressions, Regular languages and Regular Grammar
Operations on formal languages (1)
Purpose : We define operators that allow us to combine elementary
languages to create more complex languages.
• RL = Ø RE is Ø (rule 1)
• RL = {} RE = (rule 2)
• RL = {a} RE = a (rule 3)
• RL = L1 L2 RE = (r1 + r2) (rule 4)
• RL = L1 L2 RE = (r1r2) (rule 5)
• RL = L1* RE = (r1*) (rule 6)
(01* + 1) 0101
(a + λ)b b
(ab)*a* λ
(a + b)(ab) bb
Chap3: Regular expressions, Regular languages and Regular Grammar 11
Kleene’s theorem
(1909-1994)
NDFA regular
expression
Kleene’s
DFA Theorem part 2
:
{λ} :
M1
λ
λ
λ M2
λ
λ M1
a b
λ
R3 = ab q0 q1 q2 q3
a b
R3 = ab q0 q2 q3
b
3) Closure under union: R3 = ab a
q0 q1 q2
b
R2 = b q3 q4
R4 = R3 U R2 a b
= ab U b λ q0 q1 q2 λ
q5
λ b q6
λ
q3 q4
a b
R4 = R3 U R2 q5 q1
= ab U b
b q6
b q2
a a
λ q3 q1
λ - transition elimination :
q2q3 q1 : q2 q1 : a b
q2q3 q2 : q2 q2 : b a b q2
b
q3 q1
b
b q2
R5 = (ab U b)*
L5 = {λ, ab, b, abab, abb, bab, bb,
ababab, ababb,…}
(a+b) a*
b ab*a
a
b ab*b
a
b
b ab*b
a
a ab*a
Chap3: Regular expressions, Regular languages and Regular Grammar 24
Kleene’s Theorem part2(5)
NDFA Regular expression
▪ The key step is removing states and re-labeling transitions with
regular expressions. Here are some examples of how to do this.
Remove state 2
b λ b λ
(a+b)
a* b λ a*b (a+b)*
Regular expressions
describe
accept
DFA NDFA Regular languages
generate
Regular grammars
S → bS | aA | λ
A → bA | aB | λ
B → bB | aC | λ
C → bC | λ
Chap3: Regular expressions, Regular languages and Regular Grammar 35
Example 2 (1)
• Given a grammar, you should be able to say
what language it generates
• Use set notation to define the language
generated by the following grammar
S → aaSB | λ
B → bB | b
▪ Construction:
• For each variable Vi in the grammar there will be a state in
the automaton labeled Vi.
a b
V0 V1 Vf
b a
a a a
q0 q1 q2 qf
q0 a q1 a q2 a qf
P = {} initially.
Add to P a rule for each transition in the NDFA:
q0 → aq1
q1 → aq2
q2 → bq2
q2 → aqf
Since qf is in F, add to P the production:
qf → λ Chap3: Regular expressions, Regular languages and Regular Grammar 48
Theorem 2(6)
DFA G
q0 a q1 a q2 a qf
▪ Proof:
Combine our definition of regular grammars,
which includes the statement, “A regular grammar is
right-linear, with theorem 2.