0% found this document useful (0 votes)
13 views22 pages

Lec 10-Kleens Theorem NFA

Uploaded by

alishbalatif398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views22 pages

Lec 10-Kleens Theorem NFA

Uploaded by

alishbalatif398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Kleene’s Theorem and NFA

Proof of Part 3: Converting Regular Expressions into


FAs
• We prove this part by recursive definition and constructive
algorithm at the same time.

– We know that every regular expression can be built up from the letters
of the alphabet ∑ and Λ by repeated application of certain rules: (i)
addition, (ii) concatenation, and (iii) closure.

– We will show that as we are building up a regular expression, we could


at the same time building up an FA that accepts the same language.

• Slides 3 - 30 below show the proof of part 3.

2
• Before we proceed, let’s have a quick review of the formal definition of
regular expressions.
• The set of regular expressions is defined by the following rules:
• Rule 1: Every letter of the alphabet ∑ can be made into a regular
expression by writing it in boldface: Λ itself is a regular expression.
• Rule 2: If r1 and r2 are regular expressions, then so are:

(i) (r1)
(ii) r1r2
(iii) r1 + r2
(iv) r1*

• Rule 3: Nothing else is a regular expression.

We now present proof of part 3 recursively.


3
Rule 1

• There is an FA that accepts any particular


letter of the alphabet.

• There is an FA that accepts only the word Λ.

4
Proof of rule 1

• If letter x is in ∑, then the following FA accepts


only the word x.

• The following FA accepts only λ:

5
Rule 2

• If there is an FA called FA1 that accepts the


language defined by the regular expression
r1, and there is an FA called FA2 that accepts
the language defined by the regular
expression r2, then there is an FA that we
shall call FA3 that accepts the language
defined by the regular expression (r1 + r2).

6
Proof of Rule 2

• We shall show that FA3 exists by presenting an


algorithm showing how to construct FA3.
• Algorithm:
– Starting with two machines, FA1 with states x1; x2;
x3;…, and FA2 with states y1; y2; y3; …, we construct
a new machine FA3 with states z1; z2; z3; … where
each zi is of the form xsomething or ysomething.
– The combination state xstart or ystart is the start
state of the new machine FA3.
– If either the x part or the y part is a final state,
then the corresponding z is a final state. 7
Algorithm (cont.)

– To go from one state z to another by reading a


letter from the input string, we observe what
happens to the x part and what happens to
the y part and go to the new state z
accordingly. We could write this as a formula:

znew after reading letter p = (xnew after reading


letter p on FA1) or (ynew after reading letter p
on FA2)
8
Remarks

• The new machine FA3 constructed by the above algorithm will


simultaneously keep track of where the input would be if it were running
on FA1 alone, and where the input would be if it were running on FA2
alone.

• If a string traces through the new machine FA3 and ends up at a final state,
it means that it would also end at a final state either on machine FA1 or on
machine FA2. Also, any string accepted by either FA1 or FA2 will be
accepted by this FA3. So, the language FA3 accepts is the union of the
languages accepted by FA1 and FA2, respectively.

• Note that since there are only finitely many states x’s and finitely many
states y’s, there can be only finitely many possible states z’s.

• Let us look at an example illustrating how the algorithm works.

9
Example

• Consider the following two FAs:

• FA1 accepts all words with a double a in them.


• FA2 accepts all words ending with b.
• Let’s follow the algorithm to build FA3 that
accepts the union of the two languages. 10
Combining the FAs
• The start (-) state of FA3 is z1 = x1 or y1.
• In z1, if we read an a, we go to x2 (observing FA1), or we go to y1 (observing
FA2).
Let z2 = x2 or y1.
• In z1, if we read a b, we go to x1 (observing FA1), or to y2 (observing FA2).
Let z3 = x1 or y2. Note that z3 must be a final state since y2 is a final state.

11
• In z2, if we read an a, we go to x3 or y1. Let z4 =
x3 or y1. z4 is a final state because x3 is.
• In z2, if we read a b, we go to x1 or y2, which is
z3.

12
• In z3, if we read an a, we go to x2 or y1, which is z2.
• In z3, if we read a b, we go to x1 or y2, which is z3.
• In z4, if we read an a, we go to x3 or y1, which is z4. Hence, we
have an a-loop at z4.
• In z4, if we read a b, we go to x3 or y2. Let z5 = x3 or y2. Note
that z5 is a final state because x3 (and y2) are.
• In z5, if we read an a, we go to x3 or y1, which is z4.
• In z5, if we read a b, we go to x3 or y2, which is z5. Hence, there
is a b-loop at z5.
• The whole machine looks like the following:

13
• This machine accepts all words that have a
double a or that end with b.
• The labels z1 = x1 or y1, z2 = x2 or y1, etc. can be
removed if you want. 14
Example

• Consider the following two FAs:

• FA1 accepts all words that end in a.

• FA2 accepts all words with an odd number of letters (odd length).

• Can you use the algorithm to build a machine FA3 that accepts all words
that either have an odd number of letters or end in a?

15
• Using the algorithm, we can produce FA3 that accepts all words that either
have an odd number of letters or end in a, as follows:

• The only state that is not a + state is the - state. To get back to that start
state, a word must have an even number of letters and end in b.

16
Rule 3

If there is an FA1 that accepts the language


defined by the regular expression r1, and
there is an FA2 that accepts the language
defined by the regular expression r2, then
there is an FA3 that accepts the language
defined by the (concatenation) regular
expression (r1r2), i.e. the product language.

17
• We shall show that such an FA3 exists by
presenting an algorithm showing how to
construct it from FA1 and FA2.
• The idea is to construct a machine that starts
out like FA1 and follows along it until it enters
a final state at which time an option is
reached. Either we continue along FA1, waiting
to reach another +, or else we switch over to
the start state of FA2 and begin circulating
there. 18
Algorithm

• First, create a state z for every state of FA1 that we may go


through before arriving at a final state.
2. For each final state xfinal of FA1, add a state z = xfinal or y1,
where y1 is the start state of FA2.
3. From the states added in step 2, add states

4. Label every state z that contains a final state from FA2 as a


final state.
19
Example

• Old States Reading at a Reading at b


z1-≡q0 q1≡z2 q1≡z2
z2≡q1 q3≡z3 (q2,p0)≡z4
z3≡q3 q3≡z3 q3≡z3
z4 ≡ (q2,p0) (q2,p0,p1)≡z5 (q2,p0,p1)≡z5
z5+≡(q2,p0,p1) (q2,p0,p1,p1)=Z5 (q2,p0,p1,p1)=
(q2,p0,p1)≡z5 (q2,p0,p1)≡z5 20
21
x

• This machine accepts all words that both begin and end with the letter b,
which is what the product of the two languages (defined by FA1 and FA2
respectively) would be.

• If you multiply the two languages in opposite order (i.e. first FA2 then FA1),
then the product language will be different. What is that language? Can
you build a machine for that product language

22

You might also like