ACT CH2 Regular Expressions and Languages
ACT CH2 Regular Expressions and Languages
Regular Program
• 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.
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
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.
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)*
• 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.
• 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 expression r a b * a bb
Lr a, bb, aa, abb, ba, bbb,...
r aa * bb * b
Lr {a 2n 2m
b b: n, m 0}
14
Example
Regular expression r (0 1) * 00 (0 1) *
15
Relationship between FA and RE
• Here we will focus on the first method in the next slides ...
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
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"
24
Example: Construct the FA for regular expression 0*1 + 10.
25
Exercise: Convert the following RE to FA
1) a*b ( a+ b)*
2) ( a + b )c
3) 1(00)*1+10*1
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:
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
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
AbA|ε
• Then S⇒aS ⇒aaS⇒aabA⇒aabbA⇒ aabbε=aabb
38
Regular grammars from FA
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
• 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.
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
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.
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
• [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.
(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.
58
D !!
e E N
T h
59
Regular Expresions & Regular Languages