CT-4212 Theory of Automata and Computations
CT-4212 Theory of Automata and Computations
CT-4212 Theory of Automata and Computations
College of Engineering
Bishoftu, Ethiopia
Department
of
Computer and Information Technology
LAB MANUAL
for
Theory of Automata and
Computation
CT-4212 Theory of Automata and Computation 2
TABLE OF CONTENTS
Introduction 3
INTRODUCTION
Post Activity 5%
(if not given 20% is given to lab activity)
1.3 Expectations
Learning Outcomes:
Background
The input mechanism can move only from left to right and reads exactly one symbol on each
step. The transitions from one internal state to another are governed by the transition
function δ.
Language of DFA
Example:
Construct the DFA M for the language L over the alphabet = {0, 1}.
Construct a DFA for the following languages over the alphabet = {0, 1}.
a. The set of strings ending in ‘00’.
b. The set of strings with three consecutive 0’s.
Lab Activity
Problem:
Given the following regular languages:
Requirement:
Construct the Deterministic Finite Automata using JFLAP 7.0 and submit the file using the
filename:
a. DFA_M1.jff
b. DFA_M2.jff
Hand – in:
Submit the following DFA in paper with their 5–tuple notation, transition graph and transition
table.
L(M) = {all strings over the alphabet = {0, 1}, where the strings contain the
substring ‘0101’}
Submit the transition graph of DFA with 5-tuple notation and transition table.
Learning Outcomes:
Background
The transition function δ takes a state in Q and an input symbol in Σ as arguments and
returns a subset of Q. Notice that the only difference between NFA and a DFA is in the type
of value that δ returns: as set of states in the case of an NFA and a single state in the case
of a DFA.
Language of NFA
Example:
L(M) = { w {0, 1}* | w has a ‘1’ in the third position from the end }
Lab Activity
Problem:
Given the regular languages L1 and L2:
a. L(M1) = {a* b* a* a}
b. L(M2) = {a} {ab*}
Requirement:
Construct the Non Deterministic Finite Automata (M) equivalent. The following filename
should be followed.
Hand – in:
Submit the following NFA in white paper:
a. Transition graph of NFA with 5 – tuple notation.
b. Transition table of NFA
Problem:
Given the regular language L(M) = {00* 11*}
Requirement:
Construct the Non Deterministic Finite Automata (M) equivalent.
Hand – in:
Submit the following NFA in white paper:
c. Transition graph of NFA with 5 – tuple notation.
d. Transition table of NFA
Learning Outcomes:
Background
L(M1) = L(M2)
Both DFA and NFA recognize the same class of languages. It is important to note that every
NFA has an equivalent DFA.
Example:
δ a b
Construct the DFA M’ equivalent to the NFA M accepting the language L = {a* b*}
Lab Activity
Problem:
Given the NFA below:
Requirement:
Construct the equivalent Deterministic Finite Automaton (M’). The following filename should
be followed.
Hand – in:
Submit the following DFA in white paper:
a. Transition graph of NFA with 5 – tuple notation
b. Transition table of NFA
c. Transition table of DFA. (note: rename your states using A, B, C …)
d. Transition graph of DFA with 5 – tuple notation
Requirement:
Construct the equivalent Deterministic Finite Automaton (M2) by providing the following:
a. Transition graph of NFA with 5 – tuple notation M1.
b. Transition table of NFA
c. Transition table of DFA. (note: rename your states using A, B, C …)
d. Transition graph of DFA with 5 – tuple notation M2.
Hand – in:
Submit all the requirements above in white paper.
Learning Outcomes:
Background
M = (Q, , δ, q0 , F ) where
This NFA allows the symbol ε, empty string, as the second argument of δ. This means that
the NFA can make a transition without consuming an input symbol.
Example:
Construct the ε – NFA for the given transition table.
a b c ε
p {p} {q} {r}
q {q} {r} {p}
*r {r} {p} {q}
Informally, to get the ε-closure of a state is to follow all transitions out of that state labeled ε
and following all states that can be reached along path whose arcs are all labeled ε.
a b c ε
p {p} {q} {r}
q {q} {r} {p}
*r {r} {p} {q}
δ' A b c
p {p} {q} {r}
q {p, q} {q, r} {r}
*r {p, q, r} {q, r} {p, r}
a b c ε
p {q} {r} {q , r}
q {p} {r} {p , q}
*r
Lab Activity
Problem:
Requirement:
Hand – in:
Problem:
Requirement:
Hand – in:
Learning Outcomes:
Background
Regular Expressions
Another way to describe regular languages, other than finite automata, is via notation of
regular expression. This notation involves a combination of strings of symbols from some
alphabet , parentheses, and the operators +, •, and *. If E is a regular expression, then
L(E) is the language it defines.
The language L(r) denoted by any regular expression r is defined by the following
rules:
1. is a regular expression denoting the empty set, { } or ,
2. ε is a regular expression denoting { ε },
3. for every a ∊ , a is a regular expression denoting {a}.
If r1 and r2 are regular expressions, then
4. L(r1 + r2) = L(r1) L(r2,
5. L(r1 • r2) = L(r1) L(r2,
6. L((r1)) = L(r1),
7. L(r1*) = (L(r1))*.
Precedence of Operators
Examples:
L(01) = {01}.
L(01+0) = {01, 0}.
L(0(1+0)) = {01, 00}.
L(0*) = {ε, 0, 00, 000,… }.
Lab Activity
Activity 1.
Problem:
Given the following languages:
a. L1 = {anbm | n ≥ 3 , m is even}
b. L2 = {anbm | n < 4 , m ≤ 3}
c. Complement of L2 (L3)
Requirement:
Find the regular expression equivalent to the above regular languages.
Activity 2.
Problem:
Given the regular language L:
Requirement:
Provide the regular expressions for the following languages.
Hand – in:
Learning Outcomes:
Background
The following figures are the equivalent ε-NFA for the regular languages described by a
regular expression.
ε a
ε R ε
ε ε
S
(d) R + S
R ε S
(e) R • S
ε ε
R
ε
(f) R*
Example:
Lab Activity
Problem:
Requirement:
Provide the same requirements above given the regular expression L(a* + b*)
Learning Outcomes:
Background
Any DFA defines a unique language, but the converse is not true. For a given language,
there are many DFAs that accept it. There may be a considerable difference in the number
of states of such equivalent automata.
Definition:
for all w ∈ *. If, on the other hand, there exists some string w ∈ * such that
or vice versa, then the states p and q are said to be distinguishable by a string w.
If two states are said to be indistinguishable then the states are equivalent.
The table – filling algorithm shows the equivalence of the states in a given DFA. The
table shows that if there is an X, it indicates that the pair of states is distinguishable, and the
blank squares indicate those pairs of states that are found equivalent.
Minimization of DFAs
An important consequence of the test for equivalence of states is to minimize a DFA. That
is, for each DFA we can find an equivalent DFA that has few number of states. The
algorithm is as follows:
1. Use the table – filling algorithm to find all the pairs of equivalent states.
2. Partition the set of states into blocks of mutually equivalent states.
3. Construct the minimum – state equivalent DFA.
B X
C X X
D X X X
E X X X
F X X X X
G X X X X X X
H X X X X X X
A B C D E F G
Block of equivalent states are: [A, E], [B, H], [D, F], [C], [G]
M’ = ({[A, E], [B, H], [D, F], [C], [G]}, {0, 1}, δ, [A, E], {[C]})
δ 0 1
B A
A
B A C
C D B
*D D A
E D F
F G E
G F G
H G D
Lab Activity
Problem:
Given the transition table below
δ 0 1
B E
A
B C F
*C D H
D E H
E F I
*F G B
G H B
H I C
*I A E
Requirement:
Construct the DFA with minimal states.
Hand – in:
Submit the following in white paper.
Problem:
M = ({q0, q1, q2, q3, q4, q5}, {0, 1}, δ, q0, {q3, q5})
Step 1: Use the table filling algorithm to get the block of states that are mutually
equivalent.
Step 2: Remove all the states (block of states) that are unreachable by the start
state.
Requirement:
Hand – in:
Learning Outcomes:
Background
A Context – Free Grammar (CFG) is a set of recursive rewriting rules (or productions) used
to generate patterns of strings (or language).
S aSb | S SS | S λ
A xB,
A x,
where A, B V, and x T*. A grammar is said to be left-linear if all productions are of the
form
A Bx,
A x,
A grammar is one that is either right-linear or left-linear. Note that in a grammar there is at
most one variable appears on the right side of any production. Furthermore, that variable
must consistently be either the rightmost or leftmost symbol of the right side of any
production.
Example:
S abS | a
is right-linear. The grammar G2 = ({S, S1, S2} , {a, b} ,P2, S), with productions P2 as
S S1ab
S1 S1ab | S2
S2 a
We derive strings in the language of a CFG by starting with the start symbol, and repeatedly
replacing some variable A by the right side of one of its productions. That is, the
“productions for A” are those that have A on the left side of the .
Example: Derive the string 000111, given the grammar G = ({S}, {0,1}, P, S) with productions
P as
S 01
S 0S1
Language of a Grammar
Example:
Given the grammar G = ({S}, {0, 1}, P, S) with P as
S 0S1
Sλ
L(G) = { 0n1n | n≥ 0}
Lab Activity
Activity 1:
Find the context – free grammar for the following languages (with n ≥ 0, m ≥ 0)
a. L(G1) = { anbm | n ≠ m – 1}
b. L(G2) = { anbm | n ≠ 2m}
Activity 2:
Give the right – linear (G1) and left linear (G2) grammar of G = ({S, S1, A, B}, {am b}, P, S)
with P as
S AS1 | S1B
S1 aS1b | λ
A aA | a
B bB | b
Problem:
Given the grammar G = ({ S,A, B}, {a, b}, P, S) with P as
S abB
B bbAa
A aaBb | λ
Requirement:
Learning Outcomes:
Background
In a Context Free Grammar (CFG), it may not be necessary to use all the symbols in V T,
or all the production rules in P while deriving sentences. Let us try to eliminate symbols and
productions in G which are not useful in deriving sentences.
A x1 B x2.
B y1 | y 2 | … | y n
Substitution Rule
A production A x1Bx2 can be eliminated from a grammar if we put in its place the set of
productions in which B is replaced by all strings it derives in one step. In this result, it is
necessary that A and B are different variables.
A a | aaA | abBc
B abbA | b.
Using the suggested substitution rule for the variable B, we get the grammat G’ with
productions
Notice that, in the example given, the variable B (in G’) and its associated productions are
still in the grammar even though they can no longer play a part in any derivations.
the production S A does not play any role because A cannot be transformed into a
terminal string. ‘A’ can occur in a string derived from S, this can never lead to a sentence.
Hence this production rule can be removed, which does not affect the language.
A variable A ∈ V is said to be “useful” if and only if there is at least one w ∈ L(G) such that
S =>* xAy =>* w
with x, y in (V T)*. In other words, a variable is useful if and only if it occurs in at least one
derivation. A variable that is not useful is called useless. A production is useless if it
involves any useless variable.
Here the variable B is said to be “useless” and so is the production B bA. Although B can
derive a terminal string, there is no way to achieve S =>* xBy.
The examples above illustrate the two reasons why a variable is useless:
Either because it cannot derive a terminal string (or not generating any string), or
Because it cannot be reached from the start symbol
The procedure for removing useless variables and productions is based on recognizing
these two situations.
Theorem: Let G = (V, T, P, S) be a CFG. Then there exist an equivalent grammar G’ = (V’,
T’, P’, S) that does not contain any useless variables or productions.
Proof: The grammar G’ can be generated from G by an algorithm consisting of two parts. In
the first part we construct an intermediate G 1 = (V1, T2, P1, S) such that V1 contains only
variables A for which
A =>* w ∈ T*
Add A to V1.
In the second part of the construction, we get the final answer G’ from G 1. We draw the
variable dependency graph of G1. Dependency graph shows that a variable is useful
only if there is a path from the vertex labelled S to the vertex labelled with that
variable. Using this dependency graph, we will find all variables that cannot be reached from
S and these are removed from the variable set, as are the productions involving them. The
result is the grammar G’ = (V’, T’, P’, S).
Example: Eliminate useless symbols and productions from G = ({S, A, B, C}, {a, b}, P, S)
with P as
S aS | A | C
Aa
B aa
C aCb
Using the first part of the theorem, we can find that C is not terminating, therefore can be
considered as a useless variable and its production is also useless. After the first part, the
grammar will be:
S aS | A
Aa
B aa
Using the second part, we will notice that B is variable unreachable by the start symbol
therefore a useless variable and its production also useless. After the second part, the
grammar which has no more useless productions is:
S aS | A
Aa
Removing λ – Productions
One kind of production that is sometimes undesirable is one which the right side is the empty
string.
Aλ
A=>* λ
A grammar may generate a language not containing λ, yet have some λ-production or
nullable variables. In such cases, the λ-productions can be removed.
S aS1b
S1 aS1b | λ
S aS1b | ab
S1 aS1b | ab
This new grammar generates the same language as the original one.
Theorem. Let G be any context – free grammar with λ not in L(G). Then there exists an
equivalent grammar G’ having no λ-productions.
Proof: We first find the set Vn of all nullable variables of G, using the following steps,
Once the set Vn has been found, we can construct P’. To do so, we look at all productions
in P of the form
A x1x2 ...xm, m≥ 1,
where each xi ∈ V T. For each such production of P, we put into P’ that production as well
as all those generated by replacing nullable variables with λ in all possible combinations.
For example, if xi and xj are both nullable, there will be one production on P’ with x i, replaced
with λ, one in which xj is replaved with λ, and one in which both xi and xj are replaced with λ.
There is one exception: if all xi are nullable, the production A λ is not put into P’.
Example: Find a context – free grammar without λ – productions equivalent to the grammar
defined by
S ABaC
A BC
Bb|λ
CD|λ
Dd
From the first step of the construction in the above theorem, we find that the nullable
variables are A, B, C. Then, following the second step of the construction, we get
Productions in which both sides are a single variable are at time undesirable.
AB
S Aa | B
B A | bb
A a | bc | B
First create a dependency graph for the unit productions. Using a dependency graph, we
see that
Note that the removal of the unit – productions has made B and the associated productions
useless.
We can all put these results together to show that grammars for context – free languages
can be made free of useless productions, λ – productions and unit productions.
Theorem. Let L be a context – free language that does not contain λ. Then there exists a
context – free grammar that generates L and that does not have any useless productions, λ
– productions and unit productions.
1. Remove λ – productions
2. Remove unit productions
3. Remove useless productions
A BC
or
Aa
Examples:
Obtain a grammar in Chomsky Normal Form (CNF) equivalent to the grammar G 1 with
productions P given by
S ABa
A aab
B Ac
not in the proper form. Hence, we assume new variables D1 and D2 and the new
productions.
S AD1
D1 BBa
A BaD2
D2 BaBb
Thus the grammar in Chomsky Normal Form (CNF) is G2 given by the productions given by
S AD1
D1 BBa
A BaD2
D2 BaBb
B ABc
Ba a
Bb b
Bc c
S a | b | cSS
Lab Activity
S a | aA | B | C
A aB | λ
B Aa
C cCD
D ddd
S abSb | a | aAb
A bS | aAAb
Learning Outcomes:
At the end of this laboratory session, the student will be able to:
Understand the concept and application of pushdown automata
Differentiate pushdown automata from finite automata
Construct pushdown automata
Define the language generated by pushdown automata
Background
Nondeterministic PDA
M = (Q, , , δ, q0, Z, F)
where
Q = Finite set of internal states of the control unit
= input alphabet
= Finite set of symbols called “stack alphabet”
δ : Q x ( {λ}) x finite subsets of Q x * is the transition function
q0 ∊ Q = initial state of the control unit
Z is the stack start symbol
F Q = Set of final states
The arguments of δ are the current state of the control unit, the current input symbol, and the
current symbol on the top of the stack. The result is a set of pairs (q, x) where
The “stack” is an additional component available as part of PDA. The stack increases its
memory.
δ is now a function of three arguments. The first two arguments are the same as before
(NFA):
i. the state,
ii. either λ, or a symbol from the input alphabet.
The third argument is the symbol on top of the stack. Just as the input symbol is
“consumed” when the function is applied, the stack symbol is also “consumed” (removed
from the stack).
Note that while the second argument may be λ, rather than a member of the input alphabet
( so that no input symbol is consumed), there is no option for the third argument.
δ always consumes a symbol from the symbols from the stack, no move is possible if the
stack is empty.
There may also be a λ – transition, where the second argument may be λ, which means that
a move that does not consume an input symbol is possible. No move is possible if the stack
is empty.
If at any time the control unit is in state q1, the input symbol read is ‘a’, and the symbol on
the top of the stack is ‘b’, then one of the following two cases can occur:
a. the control unit tends to go into the state q2 and the string ‘cd’ replaces ‘b’ on top
of the stack;
b. the control unit goes into state q3 with the symbol b removed from the top of the
stack.
In the deterministic case, when the function δ is applied, the automaton moves to a new
state q ∊ Q and pushes a new string of symbols x ∊ * onto the result of applying δ is a
finite set of (q, x) pairs.
Drawing NPDAs
NPDAs are not usually drawn. However, with a few minor extensions, we can draw a NPDA
similar to the way we draw an NFA.
Instead of labeling an arc with an element of , we can label arcs with a / x, y where a ∊ , x
∊ and y ∊ *.
M = ({q0, q1, q2, q3}, {a, b}, {0, 1}, δ, q0, 0, {q3})
where:
δ(q0, a, 0) = {(q1, 10), (q3, λ)}
δ(q0, λ, 0) = {(q3, λ)}
δ(q1, a, 1) = {(q1, 11)}
δ(q1, b, 1) = {(q2, λ)}
δ(q2, b, 1) = {(q2, λ)}
δ(q2, λ, 0) = {(q3, λ)}
Please note that the top of the stack is considered to the left, so that for example, if we get
an ‘a’ from the starting position, the stack changes from ‘0’ to ‘10’.
Execution of NPDA
Assume that someone is in the middle of stepping through a string with a DFA, and we need
to take over and finish the job. There are two things that are required to be known:
But if the automaton is an NPDA we need to know one more… the contents of the stack.
Let the symbol “├” denote a move of the NPDA, and suppose that δ(q1, a, x) = {(q2, y), …},
then the following is possible:
where W indicates the rest of the string following ‘a’ and z indicates the rest of the stack
contents underneath the x.
This notation tells that in moving from state q1 to state q2, an ‘a’ is consumed from the input
string aW, and the x at the top of the stack xz is replaced with y, leaving yz on the stack.
M = (Q, , , δ, q0, z, F)
(q0, w, z)
where
q0 = start state
w = entire string to be processed, and
z = start stack symbol
Starting with this instantaneous description, make zero or more moves. There are two kinds
of moves that can be made:
a. λ-transition. If you are in state q1, x is the top symbol in the stack, and
then you can replace the symbol x with the string w2 and move to q2.
b. Nonempty transitions. If you are in state q1, ‘a’ is the next unconsumed input
symbol, x is the top of the stack, and
the you can remove the ‘a’ from the input string, replace the symbol x with the string
w2 and move to state q2.
If you are in the final state when you reach the end of the string, (and may be make some λ-
transitions after reaching the end), then the string is accepted by the NPDA. It does not
matter what is on the stack.
Acceptance
(q0, w, z) ├ * (p, λ, λ)
Languages of a NPDA
or
where δ is given by
δ(q0, a, z0) = {(q0, az0) }
δ(q0, a, a) = {(q0, aa)}
δ(q0, b, a) = {(q1, a)}
δ(q1, b, a) = {(q1, a)}
δ(q1, a, a) = {(q1, λ)}
δ(q1, λ, z0) = {(q1, λ)}
Lab Activity
where δ is given by
δ(q0, a, z0) = {(q0, z1z0)}
δ(q0, a, z1) = {(q0, z1z1)}
δ(q0, b, z1) = {(q1, λ)}
δ(q1, b, z1) = {(q1, λ)}
δ(q1, c, z0) = {(q1, z0)}
δ(q1, λ, z0) = {(q1, λ)}
RESOURCES / TOOLS
REFERENCE MATERIALS:
Peter Linz, “An Introduction to Formal Languages and Automata”, 3rd Edition,
Jones and Bartlett Publishers Inc., 2001