Automata and Complexity Theory Chapter 3
Automata and Complexity Theory Chapter 3
Cont’d …
• An expression is regular if:
o ɸ is a regular expression for regular language ɸ.
Automata and Complexity Theory
o ɛ is a regular expression for regular language {ɛ}.
o If a ∈ Σ (Σ represents the input alphabet), a is regular expression with language
Chapter 3: Regular Expression and Regular Language {a}.
o If a and b are regular expression, a + b is also a regular expression with
language {a,b}.
o If a and b are regular expression, ab (concatenation of a and b) is also regular.
Instructor: Bayisa G.
o If a is regular expression, a* (0 or more times a) is also regular.
Cont’d …
Regular Expressions and Regular Languages
2. Regular Grammar
1. Regular Expressions • A grammar is regular if it has rules of form A -> a or A -> aB or A -> ɛ
• Regular Expressions are used to denote regular languages. where ɛ is a special symbol called NULL.
3. Regular Languages :
• The language accepted by finite automata can be easily described by
• A language is regular if it can be expressed in terms of regular expression.
simple expressions called regular expressions.
Note :
• It is most effective way to represent any language.
• Two regular expressions are equivalent if languages generated by them are
• The languages accepted by some regular expressions are referred to as
same.
regular languages.
• For example, (a+b*)* and (a+b)* generate same language.
• A regular expressions can also be described as a sequence of pattern that
• Every string which is generated by (a+b*)* is also generated by (a+b)* and
defines a string.
12/21/2024 Compiled by Bayisa G. 2 vice versa.
12/21/2024 Compiled by Bayisa G. 4
12/21/2024
Cont’d … Cont’d …
• For instance: Example 1: Write regular expression for the language accepting all strings
• Regular expression R.E x* means zero or more occurrence of x. containing any number of a’s and b’s.
• It can generate {ε, x, xx, xxx, xxxx, …..} Solution: R.E = (a+b)*
• Regular expression R.E x+ means one or more occurrence of x. • This will give the set as L= {ε, a, aa, ab, b, aba, ba, bb, …} any combination of
• It can generate {x, xx, xxx, xxxx, …..} a and b.
Operations on regular • The (a+b)* shows any combination with a and b even ε.
1. union: if L and M are two regular languages their union L U M is also regular Example 2: Write regular expression for the language accepting all strings which
L U M = {S | S is in L or S is in M} starting with 1 and ending with 0 over ∑ = {0,1}.
2. Intersection: if L and M are two regular languages their intersection L ո M is also Solution: the starting symbol is 1 and ending symbol is 0.
regular R.E = 1(0+1) * 0
L ո M = {ST | S is in L and T isCompiled
12/21/2024 in M} by Bayisa G. 5 12/21/2024
L= {10, 100, 1110, 110, 1010, …..}
Compiled by Bayisa G. 7
Cont’d … Cont’d …
Example 3: Write regular expression for the language accepting all strings
• Kleen Closure: if L is regular languages then its kleen closure L* also is
containing any number of a’s and b’s except empty string.
regular language.
Solution: R.E = (a+b)+
L* = zero or more occurrences of language L.
• This will give the set as L= {a, aa, ab, b, aba, ba, bb, …} any combination of a
• Kleen Plus: if L is regular languages then its kleen plus L+ also is regular
and b.
language.
• The (a+b)+ shows any combination with a and b even ε.
L+ = one or more occurrences of language L.
Example 4: Write regular expression for the language starting with a, but not
having consecutive b’s.
Solution L = {a, ab, aab, aba, aaa, abab, ….. }
R.E = a(a+ab)*
12/21/2024 Compiled by Bayisa G. 6 12/21/2024 Compiled by Bayisa G. 8
12/21/2024
Cont’d …
Example 5: Write R.E for the language over ∑ = {0}having Even length of ab
• Example1: ab + ac
the string
Solution: L={ε, 00, 0000, 000000, ….}
ac
R.E = (00)*
a b
• This will give the set as L={ε, 00, 0000, 000000, ….} even number of
a(b + c)
zeros. This NFA b/c two next
a states for input a
Example 6: Write R.E for the language over ∑ = {0,1}such that all the c
b
strings do not contain the substring 01.
a
Solution: L={ε, 0, 1,, 00, 10,100, ….} Now this is DFA
12/21/2024
R.E = (1*0*) Compiled by Bayisa G. 9 12/21/2024
c
Compiled by Bayisa G. 11
R2
2. Concatenation: R1.R2
R1 R2 q2
a b
a b
q0 q1 qf
3. Closure: R1* R1
Cont’d …
• Example 3: (aba)*
aba Step2:
qf
a q1
qf
b
a
q2 Step3:
Cont’d …
Method 2 Step4:
• To convert RE to FA, we use a method called subset method.
Step1: Design a transition diagram for a given RE using NFA with ε- moves.
Step2: Convert NFA with ε- moves to NFA without ε- moves.
Step3: Convert the obtained to equivalent DFA.
Example1 : Design FA from the given R.E 10+(0+11)0* NFA-ε
Step1: Step5:
Cont’d …
Cont’d …
• Now we have got NFA without ε. • After q1 and q2 are eliminated, DFA will be look like below:
• Now convert NFA to equivalent DFA
• First write transition table for NFA DFA q0
1
NFA δ 0 1 q1q2
Cont’d …
Finite Automata(FA) to Regular Expression(RE)
• Now finally construct DFA transition diagram
State Elimination Method
q2 • There are the following rules which are applied for State Elimination Method
q0 q1
1 1. There is no incoming edge to initial state
q1q2 • We create new initial state
0 new initial state
0 • Example: ɛ
0 1 1 q0 q1 qi q0 q1
qf
q3 1
2. There is no outgoing edge from final state.
0
q0 q1 q2 q0 q1 q2
• From the above diagram we observe that state q1 and q2 are unreachable, so we
3. There is only one final state.
ignore those two states.
q1
12/21/2024 Compiled by Bayisa G. 18 12/21/2024 Compiled by Bayisa G. 20
12/21/2024
Cont’d … Cont’d …
a
3. There is only one final state. Example 1: b
• Many final states are converted to one final q0 q2
as given below FA c
solution: RE = (a + b + c)
q1 q1
ɛ Example 2:
q0 q2 ɛ b
q0 q2 qf
ɛ a c ab*c
q0 q1 q2 q0 q2
q2 q2
Cont’d … Cont’d …
0
Note: the following rules are also used. Example 3:
q0 q1
1. Union 2. Concatenation 1
Cont’d …
b c Conversion of Regular Grammars to Finite Automata(FA)
Example 4: d • Number of states in the automata will be equal to number of non-terminals plus one.
q0 q1 q2
a • Each state in automata represents each non-terminal in regular grammar.
1. Using the rule 1 add initial state • Additional state will be Final State.
b c
Transition of Automata
qi ɛ d q2
q0 q1
a 1. For every production A → aB, make ƃ(A, a) = B { make an edge labelled a from A to B }
• Example: a
2. Eliminate state q0 as follow: A B
c 2. For every production A → a, make ƃ(A, a) = Final State(FS)
d
qi b q1 q2 a
• Example: A FS
ab* 3. For every production A → ɛ, make ƃ(A, ɛ) = A will be Final State.
3. Combining c and ab *
(c+ab)* • Example:
d A
qi b q1 q2
12/21/2024 Compiled by Bayisa G. 25 12/21/2024 Compiled by Bayisa G. 27
Cont’d …
Cont’d …
4. Eliminate state q1 Example 1: Consider the following grammar
S → 0A | 1B | 0 | 1
A → 0S | 1B | 1
B → 0A | 1S
b(c+ab)*d
qi q2 • Solution: using the above given 3 rules, we can construct the follow FA.
0 0,1
So, RE = b(c+ab)*d
S
1
A
1
1
0, 1
C
12/21/2024 Compiled by Bayisa G. 26 12/21/2024 Compiled by Bayisa G. 28
12/21/2024
Cont’d …
Conversion of Finite Automata to Regular Grammar
Example 2: Consider the following grammar. Trace the transition to accept the
• Converting a DFA, defined by M = (Q, Σ, δ, q0, F), to a regular grammar, defined
string abb. • Solution: using the above given 3 rules, we can construct
by G = (V, T, S, P) is straight forward.
the follow FA.
S → abA • The rules are summarized below:
S→B
b
1. The start symbol of the grammar is q0, the non-terminal corresponding to the
S → baB ab
S A
S→ɛ start state of the DFA.
A → bS 2. For each transition from state qi to state qj on some symbol ‘a’, create a
a ba ɛ b
B → aS ɛ
A→ b production rule of the form: qi → aqj
B C 3. For each state qi of the DFA which is a final state, create a production rule of the
form: qi → ɛ.
Cont’d … Cont’d …
Example 3: Consider the following grammar • Example 1: Convert the following FA to Regular Grammar.
S → 0S | 1A | 1 Solution:
A → 0A | 1A | 0 |1 a, b
• Solution: using the above given 3 rules, we can construct the follow FA. S → aA
a
S A B → aB | bB | ɛ
0
Cont’d … Cont’d …
• Example 3: Convert the following FA to Regular Grammar.
a Example 2: r=(aa)*(bb)*b
Solution:
b • L(r) = {b, aab, bbb, aabbb, aaaabbbbb }
b a S → bR
S R B FS
R → aR • denotes the set of all strings with an even number of a’s followed by an odd
• Note : FS indicates Final State R → aB number of b’s.
• Example 4: Convert the following FA to Regular Grammar. B → b
• L(r) = {a2nb2m+1 : n≥0,m≥0}
A → 0B
1 0,1 A → 1A Example 3: For ∑ = {0, 1} give regular expressions corresponding to regular
0 B 0 0 B → 0C
A C D B → 1A language.
1 C → 0D
C→0 • L(r) = {w ϵ ∑*: w has at least one pair of consecutive zeros}
1 C → 1A
D → 0D
• L = {00, 100,00111, 101000, 101001100, …..}
• Solution: D→0 • r = (0+1)*00(0+1)*
D → 1D
12/21/2024 Compiled by Bayisa G. 33 12/21/2024 Compiled by Bayisa G. 35
D→1
Connection Between Regular Expression and Regular Languages The Pumping Lemma and Non-Regular Languages
• Regular Languages: A language is regular if it can be expressed in terms of regular Theorem
expression. • Let L be a regular language. Then there exists a constant 'c' such that for every string
w in L :
• Regular language is a language that can be defined by regular expression.
|w| ≥ c
• Regular expression (a+b)* denotes the language the set of all strings over the alphabet{a,
• We can break w into three strings, w = xyz, such that −
b}.
• |y| > 0
• Example 1: r= (a+b)*(a+bb)
• |xy| ≤ c
L( r) = {a, bb, aa, abb, ba, bbb, …}
• For all k ≥ 0, the string xykz is also in L.
• The first part, (a+b)*, stands from any string of a’s and b’s.
• The second part, (a+bb) represents either an a or a double b.
• Consequently, L(r) is the set of all strings on(a, b), terminated by either an a or a double b.
12/21/2024 Compiled by Bayisa G. 34 12/21/2024 Compiled by Bayisa G. 36
12/21/2024
Cont’d … Cont’d …
Applications of Pumping Lemma • By pumping lemma, let w = xyz, where |xy| ≤ n.
• Pumping Lemma is to be applied to show that certain languages are not • Let x = ap, y = aq, and z = arbn, where p + q + r = n, p ≠ 0, q ≠ 0, r ≠ 0.
regular. • Thus |y| ≠ 0.
• It should never be used to show a language is regular.
• Let k = 2. Then xy2z = apa2qarbn.
If L is regular, it satisfies Pumping Lemma.
• Number of as = (p + 2q + r) = (p + q + r) + q = n + q
If L does not satisfy Pumping Lemma, it is non-regular.
• Hence, xy2z = an+q bn. Since q ≠ 0, xy2z is not of the form anbn.
• Method to prove that a language L is not regular:
• Thus, xy2z is not in L.
At first, we have to assume that L is regular.
• Hence L is not regular.
So, the pumping lemma should hold for L.
Use the pumping lemma to obtain a contradiction :
12/21/2024 Compiled by Bayisa G. 37 12/21/2024 Compiled by Bayisa G. 39
Cont’d … Applications of RE
o Select w such that |w| ≥ c
• Regular expressions are useful in a wide variety of text processing tasks, and more
o Select y such that |y| ≥ 1
generally string processing, where the data need not be textual.
o Select x such that |xy| ≤ c • Common applications include :
o Assign the remaining string to z. • lexical analysis and syntax analysis
o Select k such that the resulting string is not in L. • data validation