0% found this document useful (0 votes)
27 views

ACT CH2 Regular Expressions and Languages

The document contains information about regular expressions and regular languages including: - Regular expressions are used to describe languages accepted by finite automata and the languages they describe are called regular languages. - The syntax of regular expressions is defined including operators for union, concatenation, and Kleene closure. Examples of regular expressions over various alphabets are provided. - There is a connection between regular expressions and regular languages. For every regular expression there is a corresponding regular language it generates, and vice versa. Methods for constructing finite automata from regular expressions are discussed.

Uploaded by

kenbonhundara
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)
27 views

ACT CH2 Regular Expressions and Languages

The document contains information about regular expressions and regular languages including: - Regular expressions are used to describe languages accepted by finite automata and the languages they describe are called regular languages. - The syntax of regular expressions is defined including operators for union, concatenation, and Kleene closure. Examples of regular expressions over various alphabets are provided. - There is a connection between regular expressions and regular languages. For every regular expression there is a corresponding regular language it generates, and vice versa. Methods for constructing finite automata from regular expressions are discussed.

Uploaded by

kenbonhundara
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/ 59

WOLKITE UNIVERSITY

College of Computing & Informatics


Department of Computer Science

Regular Program

Regular Expresions & Regular Languages


Automata and Complexity Theory (CoSc3101)
Chapter two
Regular Expression and Regular languages
1
outline
 Regular expressions
 Connection between regular expression and regular
languages
 Regular grammar
 Pumping lemma and non-regular language grammars

Regular Expresions & Regular Languages


2
Regular expressions

• The language accepted by finite automata(FA) can be described by simple expressions called regular
expressions.
– It is the declarative way to represent any language.
– it is a sequence of pattern that defines some sets of string.

• The languages accepted by some regular expression are referred to as Regular languages.

• String searching algorithm used this pattern to find the operations on a string.
– Example: Search commands such as the UNIX grep and Lexical analyzer generators, such as Lex or Flex.

Regular Expresions & Regular Languages


3
Regular expressions...
• If ∑ is an alphabet then the regular expression(RE) over ∑ can be denoted by the following rules:

1. Any symbol from ∑, ε, Φ are regular expressions

If r1 and r2 are two regular expressions then

2. The union of these represented as r1 U r2 or r1+r2 is also a regular expression

3. The concatenation of these represented as r1r2 is also a regular expression

Regular Expresions & Regular Languages


4. The Kleene closure of r1 is denoted by r1* is also a regular expression

5. Then (r1) is also a regular expression

6. RE obtained by applying rules 1 – 5 above, once or more are also regular expressions

• In a RE, x* means zero or more occurrence of x. It can generate {ε, x, xx, xxx, xxxx, .....}

• In a RE, x+ means one or more occurrence of x. It can generate {x, xx, xxx, xxxx, .....}
4
Examples of Regular expressions
• Examples: If ∑={a, b}, then

1) a is regular expression ..... (Using rule 1)

2) b is regular expression .... (using rule 1)

3) a + b is regular expression .... (using rule 2)

4) b* is regular expression .... (using rule 4)

5) ab is regular expression .... (using rule 3)

Regular Expresions & Regular Languages


6) ab+b* is regular expression .... (using rule 6)
• The empty set ∅ is a regular expression denoting the language containing no strings.
• The empty string ε is a regular expression denoting the language containing only the empty string.

5
Examples of Regular expressions...
• Example 1: 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,
– (a + b)* shows any combination with a and b even a null string.

• Example 2 : 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.

Regular Expresions & Regular Languages


– The r.e. is as follows: R = 1 (0+1)* 0

6
Examples of Regular expressions...
• Example 3: 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}*

• Example 4: 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)*

Regular Expresions & Regular Languages


7
Exercise
• 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.

• Write the regular expression for the language over ∑ = {0} having even length of the string.

• Write the regular expression for the language having a string which should have atleast one 0 and alteast one
1.

• Describe the language denoted by following regular expression

Regular Expresions & Regular Languages


r.e. = (b* (aaa)* b*)*

• Write the regular expression for the language L over ∑ = {0, 1} such that all the string do not contain the
substring 01.

• Obtain RE to accept a language consisting of strings of a’s and b’s of even length

8
Connection between regular expression and regular
languages
• Regular expressions provide a convenient and intuitive way to specify patterns within
regular languages.
• For every regular language, there exists a regular expression that generates that
language, and vice versa
– For each regular expression E, there is a regular language L(E).

Regular Expresions & Regular Languages


9
.

Regular Expresions & Regular Languages


10
.

Regular Expresions & Regular Languages


11
Languages of Regular Expressions
• L(r) : language of regular expression r
• Example

L(a  b  c) *   , a, bc, aa, abc, bca,...

Regular Expresions & Regular Languages


12
Example
 Regular expression:
a  b   a *
La  b   a *  La  b  La *
 La  b  La *
 La   Lb  La *
 a b a*
 a, b , a, aa, aaa,...
 a, aa, aaa,..., b, ba, baa,...
13
Example

 Regular expression r  a  b * a  bb 
Lr   a, bb, aa, abb, ba, bbb,...

r  aa * bb * b
Lr   {a 2n 2m
b b: n, m  0}

14
Example
 Regular expression r  (0  1) * 00 (0  1) *

L(r ) = { all strings with at least


two consecutive 0 }

15
Relationship between FA and RE

Regular Expresions & Regular Languages


16
Constructing FA for the given RE
• Two methods of conversion from RE to NFA
 Direct method
 Subset method

• Here we will focus on the first method in the next slides ...

Regular Expresions & Regular Languages


17
Case 1 − For a regular expression ‘a’,
we can construct the following FA −

18
Case 2 − For a regular expression ‘ab’, we can
construct the following FA −

19
Case 3 − For a regular expression (a+b), we can
construct the following FA −

20
Case 4 − FA For a regular expression a*

FA for the RE a*

21
Case 5 − For a regular expression (a+b)*, we can
construct the following FA

22
Case 6− For a regular expression (ab)*
a

q0 q1
b

• FA for the regular expression: (ab)*

23
. Problem :Convert the following RE into its equivalent DFA:
1 (0 + 1)* 0
Solution
We will concatenate three expressions "1", "(0 + 1)*" and "0"

It is an NDFA corresponding to the RE: 1 (0 + 1)* 0. If you


want to convert it into a DFA, simply apply the method of
converting NDFA to DFA.

24
Example: Construct the FA for regular expression 0*1 + 10.

Regular Expresions & Regular Languages


1 0

25
Exercise: Convert the following RE to FA
1) a*b ( a+ b)*

2) ( a + b )c

3) 1(00)*1+10*1

Regular Expresions & Regular Languages


26
DFA to Regular Expression
.

We will focus on Arden’s Method


27
DFA to Regular Expression

Arden’s Theorem-

Arden’s Theorem is popularly used to convert a given DFA to its regular expression.
It states that-
• Let P and Q be two regular expressions over ∑.
• If P does not contain a null string ∈, then-
• R = Q + RP has a unique solution i.e. R = QP*

Conditions-
To use Arden’s Theorem, following conditions must be satisfied-
• The transition diagram must not have any ∈ transitions.
• There must be only a single initial state.

28
DFA to Regular Expression

Step-01:
• Form an equation for each state considering the transitions
which comes towards that state.
• Add ‘∈’ in the equation of initial state.

Step-02:
• Bring final state in the form R = Q + RP to get the required
regular expression.

29
Conti…

Important Notes:
• Arden’s Theorem can be used to find a regular expression for both
DFA and NFA.
• If there exists multiple final states, then-Write a regular expression
for each final state separately.
• Add all the regular expressions to get the final regular expression.

30
Converting DFA to Regular Expression-
Solution

31
Example 2: Find regular expression for the following DFA using Arden’s Theorem-
Solution: Step1 Step 2:
Form a equation for each state- Bring final state in the form R = Q + RP

q1 = ∈ ……(1)
q2 = q1.a …(2)
q3 = q1.b + q2.a + q3.a .(3)
Step3:

Using (1) and (4) in (3), we get-


q3 = q1.b + q2.a + q3.a
q3 = ∈.b + a.a + q3.a
q3 = (b + a.a) + q3.a …….(5)
Using Arden’s Theorem in (5), we get-
q3 = (b + a.a)a* :
Thus, Regular Expression for the given DFA = (b + aa)a*
32
Example 3: Find regular expression for the following DFA using Arden’s Theorem
Step-01:
Form a equation for each state-
q1 = ∈ + q1.a + q3.a ……(1)
q2 = q1.b + q2.b + q3.b ……(2)
q3 = q2.a …….(3)
Step-02:
• Bring final state in the form R = Q + RP.
• Using (6) in (1), we get-
• Using (3) in (2), we get-
q1 = ∈ + q1.a + q1.b.(b + a.b)*.a.a
q2 = q1.b + q2.b + q2.a.b q1 = ∈ + q1.(a + b.(b + a.b)*.a.a) …….(7)
q2 = q1.b + q2.(b + a.b) …….(4) • Using Arden’s Theorem in (7), we get-
• Using Arden’s Theorem in (4), we get- q1 = ∈.(a + b.(b + a.b)*.a.a)*
q2 = q1.b.(b + a.b)* …….(5) q1 = (a + b.(b + a.b)*.a.a)*
• Using (5) in (3), we get- Thus, Regular Expression for the given DFA is
(a + b(b + ab)*aa)*
q3 = q1.b.(b + a.b)*.a …….(6)

33
Regular grammar
• A third way of describing regular languages is by means of certain
grammars
• Formal Definition of Grammar:
– Any Grammar can be represented by four tuples
<V, T, P, S>
– V – Finite Non-Empty Set of Non-Terminal Symbols.
– T – Finite Set of Terminal Symbols.
– P – Finite Non-Empty Set of Production Rules.
– S – Start Symbol (Symbol from where we start producing our
sentences or strings).

34
Regular Expresions & Regular Languages
35
Left leaner Regular grammar

Regular Expresions & Regular Languages


36
Regular Expresions & Regular Languages
A regular grammar is always linear, but not all linear grammars are regular

37
Regular grammars and regular languages
• For every regular language there is a regular grammar.
• We can generate a sentence from a given grammar.
• Example: Consider the grammar G=( {S, A},{a,b}, S, P) with P given by: S aS|bA

AbA|ε
• Then S⇒aS ⇒aaS⇒aabA⇒aabbA⇒ aabbε=aabb

so we can write S ⇒ aabb

Regular Expresions & Regular Languages


• We say that the string aabb is a sentence or is derived from the grammar

38
Regular grammars from FA

Regular Expresions & Regular Languages


39
All have the same power and can be converted to other

Regular Expresions & Regular Languages


40
Example for Grammar
• Consider the language: L = {w ∈ {a, b}* : |w| is even}.
• DFA M accepts L:

• The corresponding RE is ((aa) ∪ (ab) ∪ (ba) ∪ (bb))*.


• The following regular grammar G also defines L:

Regular Expresions & Regular Languages


S→ε T→a
S → aT T→b
S → bT T → aS
T → bS

41
Regular Expresions & Regular Languages
42
Regular Expresions & Regular Languages
43
Regular Expresions & Regular Languages
44
Regular Expresions & Regular Languages
45
FA from Regular grammars

Regular Expresions & Regular Languages


46
FA from Regular grammars...

Regular Expresions & Regular Languages


47
FA from Regular grammars

• Example: Construct a finite automaton that accepts the language generated by the grammar
– V0 → aV1,
– V1 → abV0|b, where V0 is the start variable.

• We start the transition graph with vertices V0, V1, and Vf. The first production rule creates
an edge labeled a between V0 and V1.
• For the second rule, we need to introduce an additional vertex so that there is a path labeled
ab between V1 and V0.

Regular Expresions & Regular Languages


• Finally, we need to add an edge labeled b between V1 and Vf,

48
Regular Expresions & Regular Languages
49
Regular Expresions & Regular Languages
50
Regular Expresions & Regular Languages
51
Regular Expresions & Regular Languages
52
Pumping lemma
• What languages aren’t regular?
• The method used for proving that a language is not regular is the Pumping Lemma
• Let 𝐿 be a regular language. Then there exists a constant 𝑛 (which depends on 𝐿) such that for every 𝑤 ∈ 𝐿,
where |𝑤| ≥ 𝑛, there exists strings 𝑥, 𝑦, and 𝑧 such that
i. 𝑤 = 𝑥𝑦𝑧,
ii. |𝑥𝑦 | ≤ 𝑛,
iii. |𝑦| ≥ 1, and
iv. for all 𝑘 ≥ 0, 𝑥𝑦 𝑘𝑧 ∈ L

Regular Expresions & Regular Languages


• This means that if a string y is ‘pumped’, i.e., if y is inserted any number of times, the resultant string still
remains in L.

53
Pumping lemma
• In simple terms, this means that if a string y is ‘pumped’, i.e., if y is inserted any number of times, the
resultant string still remains in L.
• If a language is regular, it always satisfies pumping lemma.
• If there exists at least one string made from pumping which is not in L, then L is surely not regular.
– The opposite of this may not always be true. That is, if Pumping Lemma holds, it does not mean that the language is
regular.

Regular Expresions & Regular Languages


54
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

Regular Expresions & Regular Languages


Assign the remaining string to z.
 oSelect k such that the resulting string is not in L

55
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

Regular Expresions & Regular Languages


56
Example
• Let L = {0k1k : k ∈ N}. We prove that L is not regular.

• [step 1]
 By way of contradiction, suppose L is regular.

• [step 2]
 Let n be as in the Pumping Lemma.

• [step 3]
• Let x = 0n1n , Then x ∈ L [definition of L] and |x| = 2n ≥ n.

Regular Expresions & Regular Languages


• [step 4]
• By Pumping Lemma, there are strings u, v, w such that

(i) x = uvw,
(ii) v ε ,
(iii) |uv| ≤ n,
(iv) uvkw ∈ L for all k ∈ N.
57
Example...
• Let y be the prefix of x with length n. i.e., y is the first n symbols of x.
• By our choice of x, y = 0n
• By (i) and (iii), uv = 0j for some j ∈ N with 0 ≤ j ≤ n.
• Combining with (ii), v = 0j for some j ∈ N with 0 < j ≤ n.
• By (iv), uv2w∈L. ... (#)
• Aside: We are picking k = 2. Indeed, any k 1 will do here.

Regular Expresions & Regular Languages


• However, uv2w = uvvw = 0n+j1n , L, [definition of L; since j > 0, n + j n] which contradicts (#).
• Therefore L is not regular.

58
D !!
e E N
T h

59
Regular Expresions & Regular Languages

You might also like