0% found this document useful (0 votes)
28 views37 pages

Automata Ch2

The document discusses regular expressions and regular languages. It provides examples of regular expressions that define languages containing combinations of characters. Regular expressions use operations like concatenation, union, closure and Kleene star to define languages. Regular languages have properties like closure under union, intersection, concatenation and complement. The document also introduces regular grammars and Chomsky normal form, defining a grammar as a 4-tuple with sets of variables, terminals, a start symbol and production rules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views37 pages

Automata Ch2

The document discusses regular expressions and regular languages. It provides examples of regular expressions that define languages containing combinations of characters. Regular expressions use operations like concatenation, union, closure and Kleene star to define languages. Regular languages have properties like closure under union, intersection, concatenation and complement. The document also introduces regular grammars and Chomsky normal form, defining a grammar as a 4-tuple with sets of variables, terminals, a start symbol and production rules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Automata and Complexity Theory

Chapter two
Regular Expression and Regular languages

02/02/2024 For third year CS student by Gezahiegn.T In 2022 1


Regular Expression
• The language accepted by finite automata can be easily described by
simple expressions called Regular Expressions. It is the most effective
way to represent any language.
• The languages accepted by some regular expression are referred to as
Regular languages.
• A regular expression can also be described as a sequence of pattern
that defines a string.
• Regular expressions are used to match character combinations in
strings. String searching algorithm used this pattern to find the
operations on a string.
02/02/2024 For third year CS student by Gezahiegn.T In 2022 2
Continue..

In a regular expression, means zero or x*


more occurrence of x. It can generate {e, x, xx,
xxx, xxxx, .....}
In a regular expression, + means one or more x
occurrence of x. It can generate {x, xx, xxx,
xxxx, .....}
02/02/2024 For third year CS student by Gezahiegn.T In 2022 3
Continue..
Regular Expressions are used to denote regular languages. An expression is
regular if:
ɸ is a regular expression for regular language ɸ.
ɛ is a regular expression for regular language {ɛ}.
If a ∈ Σ (Σ represents the input alphabet), a is regular expression with
language {a}.
If a and b are regular expression, a + b is also a regular expression with
language {a,b}.
If a and b are regular expression, ab (concatenation of a and b) is also regular.
If a is regular expression, a* (0 or more times a) is also regular.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 4


Continue..
Regular Expression Regular Languages

set of vovels (a∪e∪i∪o∪u) {a, e, i, o, u}


a followed by 0 or {a, ab, abb, abbb,
more b (a.b*) abbbb,….}

{ ε , a ,aou, aiou, b,
abcd…..} where ε
any no. of vowels v*.c* ( where v – represent empty string
followed by any no. of vowels and c – (in case 0 vowels and
consonants consonants) o consonants )

02/02/2024 For third year CS student by Gezahiegn.T In 2022 5


Continue..
Regular Languages : A language is regular if it can be expressed in
terms of regular expression
Properties of Regular Languages:
1. Union : If L1 and If L2 are two regular languages, their union L1 ∪ L2
will also be regular. For example, L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1 ∪ L2 = {an ∪ bn | n ≥ 0} is also regular.
2. Intersection : If L1 and If L2 are two regular languages, their intersection
L1 ∩ L2 will also be regular. For example,
L1= {am bn | n ≥ 0 and m ≥ 0} and L2= {am bn ∪ bn am | n ≥ 0 and m ≥ 0}
L3 = L1 ∩ L2 = {am bn | n ≥ 0 and m ≥ 0} is also regular.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 6


Continue..
3. Concatenation : If L1 and If L2 are two regular languages, their
concatenation L1.L2 will also be regular. For example,
L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1.L2 = {am . bn | m ≥ 0 and n ≥ 0} is also regular.

4. Kleene Closure : If L1 is a regular language, its Kleene closure L1*


will also be regular. For example,
L1 = (a ∪ b)
L1* = (a ∪ b)*

02/02/2024 For third year CS student by Gezahiegn.T In 2022 7


Continue..
5. Complement : If L(G) is regular language, its complement L’(G) will
also be regular. Complement of a language can be found by subtracting
strings which are in L(G) from all possible strings. For example,
L(G) = {an | n > 3}
L’(G) = {an | n <= 3}

Note : Two regular expressions are equivalent if languages generated by


them are same. For example, (a+b*)* and (a+b)* generate same
language. Every string which is generated by (a+b*)* is also generated
by (a+b)* and vice versa.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 8


Example1
1. Write the regular expression for the language accepting all
combinations of a's, over the set ∑ = {a}
• Solution:
• All combinations of a's means a may be zero, single, double and so on.
If a is appearing zero times, that means a null string. That is we expect
the set of {ε, a, aa, aaa, ....}. So we give a regular expression for this
as:
• R = a*
• That is Kleen closure of a.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 9


Example 2.
2. Write the regular expression for the language accepting all
combinations of a's except the null string, over the set ∑ = {a}
Solution:
• The regular expression has to be built for the language
• L = {a, aa, aaa, ....}
• This set indicates that there is no null string. So we can denote regular
expression as:
• R = a+

02/02/2024 For third year CS student by Gezahiegn.T In 2022 10


Example3.
3. Write the regular expression for the language accepting all the string
containing any number of a's and b's.
• Solution:
• The regular expression will be:
• r.e. = (a + b)*
• This will give the set as L = {ε, a, aa, b, bb, ab, ba, aba, bab, .....}, any
combination of a and b.
• The (a + b)* shows any combination with a and b even a null string.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 11


Example 4
4. Write the regular expression for the language accepting all
the string which are starting with 1 and ending with 0, over ∑
= {0, 1}.
Solution:
• In a regular expression, the first symbol should be 1, and the
last symbol should be 0. The r.e. is as follows:
• R = 1 (0+1)* 0

02/02/2024 For third year CS student by Gezahiegn.T In 2022 12


Example 5
5. Write the regular expression for the language starting and ending with
a and having any having any combination of b's in between.
• Solution:
• The regular expression will be:
• R = a b* b

02/02/2024 For third year CS student by Gezahiegn.T In 2022 13


Example 6
6. Write the regular expression for the language starting with a but not
having consecutive b's.
• Solution: The regular expression has to be built for the language:
• L = {a, aba, aab, aba, aaa, abab, .....}
• The regular expression for the above language is:
• R = {a + ab}*

02/02/2024 For third year CS student by Gezahiegn.T In 2022 14


Example 7
7. Write the regular expression for the language accepting all the string
in which any number of a's is followed by any number of b's is followed
by any number of c's.
• Solution: As we know, any number of a's means a* any number of b's
means b*, any number of c's means c*. Since as given in problem
statement, b's appear after a's and c's appear after b's. So the regular
expression could be:
• R = a* b* c*

02/02/2024 For third year CS student by Gezahiegn.T In 2022 15


Example 8
8. Write the regular expression for the language over ∑ = {0} having
even length of the string.
• Solution:
• The regular expression has to be built for the language:
• L = {ε, 00, 0000, 000000, ......}
• The regular expression for the above language is:
• R = (00)*

02/02/2024 For third year CS student by Gezahiegn.T In 2022 16


Example 9
9. Write the regular expression for the language L over ∑ = {0, 1} such
that all the string do not contain the substring 01.
• Solution:
• The Language is as follows:
• L = {ε, 0, 1, 00, 11, 10, 100, .....}
• The regular expression for the above language is as follows:
• R = (1* 0*)

02/02/2024 For third year CS student by Gezahiegn.T In 2022 17


Example 10
10. Write the regular expression for the language containing the string
in which every 0 is immediately followed by 11.
• Solution:
• The regular expectation will be:
• R = (011 + 1)*

02/02/2024 For third year CS student by Gezahiegn.T In 2022 18


Regular grammar
The theory of formal languages finds its applicability extensively in
the fields of Computer Science.
Noam Chomsky gave a mathematical model of grammar in 1956
which is effective for writing computer languages.
A grammar G can be formally written as a 4-tuple (N, T, S, P) where −
•N or VN is a set of variables or non-terminal symbols.
•T or ∑ is a set of Terminal symbols.
•S is a special variable called the Start symbol, S ∈ N
•P is Production rules for Terminals and Non-terminals. A production rule has the form α
→ β, where α and β are strings on VN ∪ ∑ and least one symbol of α belongs to VN.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 19


Example

• Grammar G1 −
• ({S, A, B}, {a, b}, S, {S → AB, A → a, B → b})
• Here,
• S, A, and B are Non-terminal symbols;
• a and b are Terminal symbols
• S is the Start symbol, S ∈ N
• Productions, P : S → AB, A → a, B → b

02/02/2024 For third year CS student by Gezahiegn.T In 2022 20


Example 2
• Grammar G2 −
• (({S, A}, {a, b}, S,{S → aAb, aA → aaAb, A → ε } )
• Here,
• S and A are Non-terminal symbols.
• a and b are Terminal symbols.
• ε is an empty string.
• S is the Start symbol, S ∈ N
• Production P : S → aAb, aA → aaAb, A → ε

02/02/2024 For third year CS student by Gezahiegn.T In 2022 21


Continue..

Regular Grammar : A grammar is regular if it has rules of form:


A -> a or
A -> aB or
A -> ɛ
where ɛ is a special symbol called NULL.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 22


The Regular Grammars are either left of right:

Right Regular Left Regular


Grammars: Grammars:
Rules of the forms Rules of the forms
A→ε
A→a A→ε
A → aB A→a
A → Ba
A,B: variables and
a: terminal A,B: variables and
a: terminal
02/02/2024 For third year CS student by Gezahiegn.T In 2022 23
Example..
S → aS | bA
A → cA | ε

This grammar produces the language produced by the regular expression


a*bc*

S → aS → aaS → … → a…aS →a…abA →a…abcA


→ a…abccA → … → a…abc…c

02/02/2024 For third year CS student by Gezahiegn.T In 2022 24


The Right Regular Grammars are producing
the Regular Languages
Proof: We will show that Right Regular Grammars are equivalent to NFAε
Two directions:
1. Given a Right Regular grammar construct an NFAε that recognizes the
same language with the Right Regular grammar.
2. Given an NFAε construct a Right Regular grammar that describes the
same language with the NFAε.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 25


1. Right Reg Grammar → NFAε
Suppose that I have a right regular grammar (V, Σ, R, S). I construct an
NFAε (Q, Σ, δ, S, {f}).
• The set of states Q will be the set VU{f}, where f is a new symbol denoting
the final state
• Productions in R have three possible forms:
• A → ε : add the transition δ(Α,ε) = f
• A → a : add the transition δ(A,a) = f
• A → aB : add the transition δ(Α,a) = B

02/02/2024 For third year CS student by Gezahiegn.T In 2022 26


2. NFAε → Right Regular Grammar

Suppose that I have an NFAε (Q, Σ, δ, q0, F,). I construct a right regular grammar
(Q, Σ, R, q0).
• For each transition δ(qi ,a) = qj, I construct the rule qi → aqj in R.
• Furthermore, for every state qi in F I add the rule qi → ε in R.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 27


Examples
1) Transform the following Right Regular grammar in an equivalent NFAε.
S → aS |bA
A → cA | ε

Solution:

02/02/2024 For third year CS student by Gezahiegn.T In 2022 28


Continue..
2) Transform the following DFA to a right regular grammar

Solution:
Q0 → aQ1 | bQ0
Q1 → aQ1 |bQ0 | ε

02/02/2024 For third year CS student by Gezahiegn.T In 2022 29


Continue..
• It can be proved that Left Regular Grammars also produce the Regular
Languages but this is not so straightforward.
• Actually, a Left Regular grammar produces the reverse of the language
produced by the Right Regular grammar in which we reversed the
rules A → Ba to A →aB.
• But the set of the reverse languages of all the Regular Languages is
exactly the set of the Regular Languages. So the Left Regular
Grammars produce the Regular Languages.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 30


Example
C → Bc
B → Ab
A→a
The derivation of abc is:
C → Bc → Abc → abc, or abc ← Abc ← Bc ← C
So I should start creating the string abc from right to left. But this is
equivalent with creating the reverse of cba.
C → cB → cbA → cba and then take the reverse.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 31


Continue..
The Right Regular grammar with the rules of the form A → Ba reversed
is
C → cB
B → bA
A→a
and it produces the reverse language.
So, just create the NFAε for the language produced by the Right Regular
grammar and then compute the reverse (change start with final state
and reverse the arrows). This is an NFAε for the Left Regular grammar.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 32


Pumping Lemma(Theorem)

Let L be a regular language. Then there exists a constant ‘c’ such that
for every string w in L −
|w| ≥ c
• We can break w into three strings, w = xyz, such that −
|y| > 0
|xy| ≤ c
For all k ≥ 0, the string xykz is also in L.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 33


Applications of Pumping Lemma
Pumping Lemma is to be applied to show that certain languages are
not regular. It should never be used to show a language is regular.

• If L is regular, it satisfies Pumping Lemma.

• If L does not satisfy Pumping Lemma, it is non-regular.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 34


Method to prove that a language L is not
regular
• At first, we have to assume that L is regular.
• So, the pumping lemma should hold for L.
• Use the pumping lemma to obtain a contradiction −
• Select w such that |w| ≥ c
• Select y such that |y| ≥ 1
• Select x such that |xy| ≤ c
• Assign the remaining string to z.
• Select k such that the resulting string is not in L.
• Hence L is not regular.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 35


Problem
• Prove that L = {aibi | i ≥ 0} is not regular.
• Solution −
• At first, we assume that L is regular and n is the number of states.
• Let w = anbn. Thus |w| = 2n ≥ n.
• By pumping lemma, let w = xyz, where |xy| ≤ n.
• Let x = ap, y = aq, and z = arbn, where p + q + r = n, p ≠ 0, q ≠ 0, r ≠ 0. Thus |y| ≠ 0.
• Let k = 2. Then xy2z = apa2qarbn.
• Number of as = (p + 2q + r) = (p + q + r) + q = n + q
• Hence, xy2z = an+q bn. Since q ≠ 0, xy2z is not of the form anbn.
• Thus, xy2z is not in L. Hence L is not regular.
02/02/2024 For third year CS student by Gezahiegn.T In 2022 36
ou !
nk y
T ha
tw o
p t e r
c ha
of
s e nd
i s i
Th
02/02/2024 For third year CS student by Gezahiegn.T In 2022 37

You might also like