0% found this document useful (0 votes)
12 views112 pages

Theory of Computation

Uploaded by

bakrcyber
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)
12 views112 pages

Theory of Computation

Uploaded by

bakrcyber
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/ 112

THEORY OF

COMPUTATION
Mosul University/College of Computer and Math sciences / Department of Cyber Security
Subject: Theory of computation
Class: 2nd
Semester:1st
Year: 2023-2024
Prepared by
Dr. Ayad Alezzi
Outline
• Set, Strings, alphabets and language.
• Regular expression.
• Grammars: Definition, The Chomsky hierarchy of languages.
• The regular grammars and regular languages.
• closure properties of regular sets (union, catenation and kleene closure).
• Finite state automata ,deterministic and nondeterministic finite state automata.
• Equivalence between deterministic and nondeterministic finite state automata.
• Finite state automata with output(The Moore and Mealy Machine).
• Context-free grammars and languages, derivation trees.
• Simplification of context-free grammars, the Chomsky and Greibach normal
form.
• Pushdown Automata
Introduction to Set Theory

Vocabulary
• A set is any well define collection of Objects

• The elements of a set are the objects in a set

• Subsets consists of elements from the given set.

• Empty set/Null set: is the set that contains no elements

• Universal set : is the set of all possible elements


Ways of Describing Sets
• List the elements

A= 1,2,3,4,5,6
Give a verbal description
“A is the set of all integers from 1 to 6, inclusive”
• Give a mathematical inclusion rule

A= Integers x 1  x  6
Some Special Sets
• The Null Set or Empty Set. This is a set with no elements, often
symbolized by

 or {}
• The Universal Set. This is the set of all elements currently under
consideration, and is often symbolized by
U ‫ وﻏﺎﻟًﺑﺎ ﻣﺎ ﯾﺗم ﺗرﻣﯾزھﺎ ﺑـ‬،‫ھذه ھﻲ ﻣﺟﻣوﻋﺔ ﻛل اﻟﻌﻧﺎﺻر اﻟﺗﻲ ﯾﺗم اﻟﻧظر ﻓﯾﮭﺎ ﺣﺎﻟًﯾﺎ‬
U
Universal Sets
• The universal set is the set of all things pertinent to a given discussion
and is designated by the symbol U
Example: ‫ اﻟﻣﺟﻣوﻋﺔ اﻟﺷﺎﻣﻠﺔ ھﻲ ﻣﺟﻣوﻋﺔ ﻛل اﻷﺷﯾﺎء ذات اﻟﺻﻠﺔ ﺑﻣﻧﺎﻗﺷﺔ ﻣﻌﯾﻧﺔ وﯾﺗم اﻹﺷﺎرة إﻟﯾﮭﺎ ﺑﺎﻟرﻣز‬U
U = {all students at Brandeis}
Some Subsets:
A = {all Computer Technology students}
B = {freshmen students} ‫طﻼب اﻟﺳﻧﺔ اﻷوﻟﻰ‬
C = {sophomore students} ‫طﻼب اﻟﺳﻧﺔ اﻟﺛﺎﻧﯾﺔ‬
Find the Subsets
• What are all the subsets of {3, 4, 5}

{} or Ø

{3}, {4}, {5}

{3,4}, {3,5}, {4,5}

{3,4,5}
Venn Diagrams
• Venn diagrams show relationships between sets and their elements

Sets A & B

Universal Set
Venn Diagram Example
Set Definition
U={1, 2, 3, 4, 5, 6, 7, 8}
Set Complement

~A or A′
• “A complement,” or “not A” is the set of all elements not in A.

"‫اﻟﻣﻛﻣل" أو "ﻟﯾس أ" ھو ﻣﺟﻣوﻋﺔ ﻛل اﻟﻌﻧﺎﺻر ﻏﯾر اﻟﻣوﺟودة ﻓﻲ أ‬.


Practice:

Universal set U =
What is the complement of set A?
Combining Sets – Set Union

A B
Combining Sets – Set Intersection

• A B
• “A intersect B” is the set of all elements that are in
both A and B.
• This is similar to the logical “and”
Venn Diagrams
• Venn Diagrams use topological areas to stand for sets. I’ve done this one for
you.

A B

A B
Venn Diagrams

A B

A  B
Examples
A  {1, 2,3} B  {3, 4,5,6}

A  B  {3}

A  B  {1, 2,3, 4,5,6}


Homework
• Let P = {b, d, f, g, h}, M = {a, b, c, d, e, f, g, h, i, j},
N = {c, k}
P M
PM
PN
NM
PN
Alphabets, Strings, and language.
• Alphabet: A finite and nonempty set of symbols
denoted by Σ. The elements of an alphabet are
letters, but sometimes are named also symbols.
• Strings (words): is a finite ordered sequence of
symbols from a given Alphabet. Note that repetitions
are allowed. The words(strings) u = a1a2 . . . am and v
= b1b2 . . . bn are equal (i.e. u = v), if m = n and ai = bi,
i = 1, 2, . . . , n. ‫ﻣﺟﻣوﻋﺔ ﻣﺣدودة وﻏﯾر ﻓﺎرﻏﺔ ﻣن اﻟرﻣوز‬
‫ ﯾرﻣز ﻟﮭﺎ ﺑﺎﻟرﻣز‬Σ. ‫ﻋﻧﺎﺻر اﻷﺑﺟدﯾﺔ ھﻲ‬
‫ ﻋﺑﺎرة ﻋن ﺗﺳﻠﺳل ﻣرﺗب ﻣﺣدود ﻣن‬:(‫ﺿﺎ ﺑﺎﻟرﻣوز اﻟﺳﻼﺳل )اﻟﻛﻠﻣﺎت‬
ً ‫ وﻟﻛن ﻓﻲ ﺑﻌض اﻷﺣﯾﺎن ﯾﺗم ﺗﺳﻣﯾﺗﮭﺎ أﯾ‬،‫ﺣروف‬
‫ ﻻﺣظ أن اﻟﺗﻛرارات‬.‫اﻟرﻣوز ﻣن أﺑﺟدﯾﺔ ﻣﻌﯾﻧﺔ‬
‫ﻣﺳﻣوح ﺑﮭﺎ‬
Some Important Notations
• ϵ (Empty String): is a special string its length is 0, some text denoted empty string with (λ)
• Σ* : All strings composed from alphabet Σ with length >= 0.
• Σ+ : All strings composed from alphabet Σ with length > 0.
eg. Let Σ={0,1}
{0,1}* is all strings over (all strings composed from) {0,1}
{0,1}*={ϵ, 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, 100, 101, 110, 111, 0000,……..}
{0,1}+ ={0, 1, 00, 01, 10, 11, 000, 001, 010, 011, 100, 101, 110, 111, 0000,……..}
Σi : all strings over Σ with length i.
e.g : suppose Σ = {a,b,c} {a,b}2= {aa, ab , ba, bb}
Σ0 ={ ϵ } {a,b}3 ={ aaa , aab, aba, abb, baa, bab, bba, bbb}
Σ1 ={ a, b , c }
Σ2 ={ aa, ab, ac, ba , bb, bc, ca, cb, cc}
Σ3 ={ aaa, aab, aba, abb , baa, bab, bba, bbb , …………. }
Σ*= Σ0 Ս Σ1 ∪ Σ2 ∪ · · · ∪ Σn ∪ Σn+1 ∪ ……..
Σ =
+ Σ1 ∪ Σ2 ∪ · · · ∪ Σn ∪ Σn+1 ∪ ……..
• Length of string |s| : The length of a string is the number of symbols in the string, with repetitions
counted.
Eg. |a| is 1, |ab| is 2, |aba| is 3, |ϵ| is 0.
String Operations
• Concatenation: The concatenation (or product) of the words u = a1a2 . . . am and v =
b1b2 . . . bn is the word uv = a1a2 . . . amb1b2 . . . bn. |uv|=|u|+|v|.
• The reversal (or mirror image) of the word u = a1a2 . . . an is
u-1 = anan-1 . . . a1. The reversal of u sometimes is denoted by uR or ˜ u. It is clear
that u-1-1 = u and (uv)-1 = v-1u-1.
• Word v is a prefix of the word u if there exists a word z such that u = vz. If
z 6= ε then v is a proper prefix of u.
• Similarly v is a suffix of u if there exists a word x such that u = xv. The proper suffix can
also be defined.
• Word v is a subword of the word u if there are words p and q such that u = pvq. If pq ≠
ε then v is a proper subword.
Language
A subset L of Σ* is called a language over the alphabet Σ.
Sometimes this is called a formal language because the
words are here considered without any meanings. Note that
∅ is the empty language while {ε} is a language which
contains the empty word.
Operations on Languages
Specifying languages
Languages can be specified in several ways. For example a
language can be specified using:

1) the enumeration of its words,


2) a property, such that all words of the language have this
property but other word have not.
3) Regular Expression.
4) a grammar.
• Specifying languages by listing their elements
For example the following are languages
L1 = {ε, 0, 1},
L2 = {a, aa, aaa, ab, ba, aba}.
Even if we cannot enumerate the elements of an infinite set infinite languages can
be specified by enumeration if after enumerating the first some elements we can
continue the enumeration using a rule. The following is such a language
L3 = {ε, ab, aabb, aaabbb, aaaabbbb, . . .}.

Specifying languages by properties
The following sets are languages
L4 = {anbn | n = 0, 1, 2, . . .},
L5 = {uu-1 | u ∈ Σ*},
L6 = {u ∈ {a, b}* | |u|a = |u|b },
e.g baba , aabb, bbaa ,
where |u|a denotes the number of letters a in word u and |u|b the number of
letters b.
Regular Expression
• A regular expression is a formula, and the corresponding language is
a language over Σ. For example, if Σ = {a, b}, then a*, b*, a*+b* are
regular expressions over Σ which represent respectively languages
{a}*, {b}*, {a}* ∪ {b}*. The exact definition is the following.

Definition :Define recursively a regular expression over Σ and the


language it represent.
• ∅ is a regular expression representing the empty language.
• ε is a regular expression representing language {ε}.
• If a ∈ Σ, then a is a regular expression representing language {a}.
• If x, y are regular expressions representing languages X and Y
respectively, then (x + y), (xy), (x*) are regular expressions representing
languages X ∪ Y , XY and X* respectively.
Properties of regular expressions.
•x + y ≡ y + x
• (xy)z ≡ x(yz)
• (x+y)+z ≡ x+(y+z)
• x(y+z) ≡ xy+xz
• (x+y)* ≡ (x*y*)*
• (x*)* ≡ x*
• x*x ≡ xx*
• xx* + ϵ ≡ x*
Regular Expression: Examples
Consider Σ = {0,1} and w ϵ Σ, Find RE for each of the following languages:
1- {w| w contains a single 1}
Ans: 0* 1 0* eg: 0010 , 100 , 01, 1 , 0000100000,
2- {w| w has at least single 1}
Ans: (0+1)* 1 (0+1)* eg: 0 1 010 , 1 , 10 , 01 1 0 , 001 ,
111111111,
3- { w| w contains the string 001 as a substring}.
Ans: (0+1)* 001 (0+1)* eg: 100101, 0001, 100 , 1010010, 010101
4- {w| every 0 in w is followed by at least single 1 }
Ans: 1* (011*)* eg: 1101101111, 111, ϵ , 01 , 0010
5- {w| w is a string of even length}
Ans: (( 0 + 1)(0 + 1))* eg : 01, 1001, 11, 1100, 0000, 1111
6- {w| the length of w is a multiple of three}
Ans: (( 0 + 1)(0 + 1) (0 + 1) )*
7- {w| w starts and ends with the same symbol}.
Ans: (0(0+1)*0)+ (1(0+1)*1)+ 0 + 1
Specifying languages by grammars
Definition 1.1 A grammar is an ordered quadruple G = (N, T, S, P), where
• N is the alphabet of variables (or nonterminal symbols),
• T is the alphabet of terminal symbols, where N ∩ T = ∅,
• S ∈ N is the start symbol.
• P is a finite set of productions, each production of the following form:
u→v
Such that:
u ∈ (N ∪ T )*N(N ∪ T )*
v ∈ (N ∪ T )*
u is called a left hand side of the production and v is called the right-hand side of
the production. If for a grammar there are more than one production with the
same left-hand side, then these production:
u → v1 , u → v2 , ……. u → vr can be written as u → v1 | v2 | …..vr
Example:

G= ({A,B}, { 0.1} , A ,P)


Where P is
A → 0A1
A→B
B→#

P can be written as follows:


A → 0A1 | B
B→#
Derivation
The sequence of substitutions to generate a string (sentence) is called derivation.
Example: A derivation of string 000#111 in G is
G:
A ⇒ 0A1 ⇒ 00A11 ⇒ 000A111 ⇒ 000B111 ⇒ 000#111 A → 0A1 | B
B→#

Starting Sentential Sentence


Symbol forms
A sentential form is any string derivable from the start symbol.
A sentence is a sentential form consisting only of terminals
Example 2 : write a grammar for the following languages:
• L1 = {anbn | n>=0}
Ans:
G1: ( {A} , { a,b} , A , P1 )
Where P1 is
A→ aAb | ϵ
• L2 = { anbm | n,m >=1}
Ans:
G2: ( {S, A , B} , { a,b} , S , P2 )
Where P2 is
S→ AB
A→ aA|a
B → bB |b
Example 2 ……… Cont.
• L3 = all words over { a, b, c} such that each word start and end with a.
Ans:
G3= ( {S, A } , { a , b, c } , S , P3 )
Where P3 is
S→ aAa
A→ aA|bA|cA| ϵ
acbaa (S=>aAa=> acAa => acbAa=>acbaAa => acbaa )
• L4 = all word over {0,1} with each word contains an even number of 1’s
Ans:
G4= ({E,O}, {0,1}, E , P4}
Where P4 is
E → 1O | 0E | ϵ
O→ 1E |0O
01111 ( E => 0E=>01O=>011E=>0111O => 01111E =>01111)
Example 2 ……… Cont.

L5= all words over {0,1} such that each word contains an odd number of
0’s.
Ans:
G5= ({E,O}, {0,1}, E , P5}
Where P5 is
E → 0O | 1E
O → 0E |1O | ϵ
Example 2 ……… Cont.
L6= all words over {0,1} such that each word contains an even number of
1’s and an odd number of 0’s
Ans:
• Even Even eg ( 0110, 1001, 00 , 11}
• Even Odd eg (110, 01010, 0 , 01100)
• Odd Even eg (100, 10011, 1101101, 11100 , 1)
• Odd Odd eg (10 , 1101,001011)

G6= ({See, Seo , Soe , Soo}, {0,1}, See, P6}


Where P6 is
See → 1Soe | 0Seo
Seo → 1 Soo |0See | ϵ
Soe → 1 See |0Soo
Soo → 1 Seo |0Soe
Example 2 ……… Cont.
L7 = { w ∈ {a,b,c}* : w = anbncn, n ≥ 0 }
G7 = ({S, B , C,}, {a,b,c}, S, P7}
P7 is:
S → aSBC | ε
CB → BC
aB → ab
bB → bb
bC → bc
cC → cc
Here is a sample derivation:
S ⇒ aSBC ⇒ aaSBCBC ⇒ aaaSBCBCBC ⇒ aaaBCBCBC ⇒ aaaBBCCBC
⇒ aaaBBCBCC ⇒ aaaBBBCCC ⇒ aaabBBCCC ⇒ aaabbBCCC
⇒ aaabbbCCC ⇒ aaabbbcCC ⇒ aaabbbccC ⇒ aaabbbccc
From the start we generate a sentential form: an(BC)n
the rule CB → BC gives you the ability to change this to anBnCn
The remaining rules allow you to convert this form to: anbncn
The Chomsky hierarchy of languages.
Definition: Define for a grammar G = (N, T, P, S) the following four types.
Type 0 :
A grammar G is of type 0 (phrase-structure grammar) if there are no
restrictions on productions.
Type 1:
A grammar G is of type 1 (context-sensitive grammar) if all of its
productions are of the form αAγ → αβγ, where A ∈ N, α, γ ∈ (N ∪ T )*, β ∈
(N ∪ T )+. A production of the form S → ε can also be accepted if the start
symbol S does not occur in the right-hand side of any production.
The Chomsky hierarchy of languages………cont.
Type 2:
A grammar G is of type 2 (context-free grammar) if all of its productions are of the
form A → β, where A ∈ N, β ∈ (N ∪ T )+. A production of the form S → ε can also be
accepted if the start symbol S does not occur in the right-hand side of any
production.

Type 3:
A grammar G is of type 3 (regular grammar) if its productions are of the form A →
aB or A → a, where a ∈ T and A, B ∈ N. A production of the form S → ε can also be
accepted if the start symbol S does not occur in the right-hand side of any
production.

If a grammar G is of type i then language L(G) is also of type i.


Example: what is the type of each of the following grammars

1.
G1 = (N1, T1, P1, S1), where N1 = {S1, A, B, C} , T1 ={a, 0,1}.
Elements of P1 are:
S1 → ACA
AC → AACA | ABa |AaB
B → AB | A
A → 0|1
Example: …………….cont.

2.
G2 = (N2, T2, P2, S), where N2 = {S, A, B} , T2 ={a, +, *, (, )}.
Elements of P2 are:
S → S+A |A
A → A*B | B
B → (S) | a
Example: ……………cont.
3.
G3 = (N3, T3, P3, S), where N3 = {S, A, B} , T3 ={a, b}.
Elements of p3 are:
S →aA
A → aB | a
B → aB | bB | a | b
Finite State Automata
• Automata – What is it?
The term "Automata" is derived from the Greek word "αὐτόματα" which means "self-acting". An
automaton (Automata in plural) is an abstract self-propelled computing device which follows a
predetermined sequence of operations automatically.
An automaton with a finite number of states is called a Finite Automaton (FA) or Finite State Machine
(FSM).

Formal definition of a Finite Automaton


An automaton can be represented by a 5-tuple (Q, ∑, δ, q , F), where -
Q is a finite set of states.
∑ is a finite set of symbols, called the alphabet of the automaton.
δ is the transition function
q is the initial state from where any input is processed (q ∈ Q).
F is a set of final state/states of Q (F ⊆ Q).
Deterministic & Non-Deterministic Finite Automata

Finite Automaton can be classified into two types

• Deterministic Finite Automaton (DFA)


• Non-deterministic Finite Automaton (NDFA / NFA)
Deterministic Finite Automaton (DFA)
In DFA, for each input symbol, one can determine the state to which the
machine will move. Hence, it is called Deterministic Automaton. As it has
a finite number of states, the machine is called
Deterministic Finite Machine or Deterministic Finite Automaton.
Formal Definition of a DFA
A DFA can be represented by a 5-tuple (Q, ∑, δ, q , F) where -
Q is a finite set of states.
∑ is a finite set of symbols called the alphabet.
δ is the transition function where δ: Q × ∑ → Q
q is the initial state from where any input is processed (q ∈ Q).
F is a set of final state/states of Q (F ⊆ Q).
Graphical Representation of a DFA

A DFA is represented by digraphs called state diagram.
The vertices represent the states.
The arcs labeled with an input alphabet show the transitions.
The initial state is denoted by an empty single incoming arc.
The final state is indicated by double circles.
Example
Let a deterministic finite automaton be →
Q = {a, b, c},
∑ = {0, 1},
q = {a},
F = {c},
Transition function δ as shown by the following table -
Current State 0 1
a a b
b c a
c b c

Its graphical representation would be as follows -


Non-deterministic Finite Automaton
• Non-deterministic Finite Automaton
In NDFA, for a particular input symbol, the machine can move to any combination of the
states in the machine. In other words, the exact state to which the machine moves cannot
be determined. Hence, it is called Non-deterministic Automaton. As it has finite number
of states, the machine is called Non-deterministic Finite Machine or Non-deterministic
Finite Automaton.

Formal Definition of an NDFA
An NDFA can be represented by a 5-tuple (Q, ∑, δ, I , F) where -
Q is a finite set of states.
∑ is a finite set of symbols called the alphabets.
δ is the transition function where δ: Q × ∑ → 2Q
(Here the power set of Q (2 ) has been taken because in case of NDFA, from a state,
transition can occur to any combination of Q states)
I is a set of initial states (I⊆Q).
F is a set of final state/states of Q (F ⊆ Q).
Graphical Representation of an NDFA: (same as DFA)
An NDFA is represented by digraphs called state diagram.
The vertices represent the states.
The arcs labeled with an input alphabet show the transitions.
The initial state is denoted by an empty single incoming arc.
The final state is indicated by double circles.
Example
Let a non-deterministic finite automaton be →
Q = {a, b, c}
∑ = {0, 1}
q = {a}
F = {c}
The transition function δ
Current State 0 1
a a,b b
b c a,c
c b,c c
Acceptors, Classifiers, and Transducers
• Acceptor (Recognizer)
An automaton that computes a Boolean function is called an acceptor. All the states of an
acceptor is either accepting or rejecting the inputs given to it.

• Classifier
A classifier has more than two final states and it gives a single output when it terminates.

Transducer
An automaton that produces outputs based on current input and/or previous state is called a
transducer.
Transducers can be of two types -
Mealy Machine - The output depends both on the current state and the current input.
Moore Machine - The output depends only on the current state.
Examples (Finite Automata as an Accepter)
• Example 1:
Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and ends with 0.
Solution:
The FA will have a start state q0 from which only the edge with input 1 will go to the
next state.

In state q1, if we read 1, we will be in state q1, but if we read 0 at state q1, we will
reach to state q2 which is the final state. In state q2, if we read either 0 or 1, we will
go to q2 state or q1 state respectively. Note that if the input ends with 0, it will be in
the final state.
• Example 2:

Design a FA with ∑ = {0, 1} accepts the only input 101.


Solution:

• In the given solution, we can see that only input 101 will be accepted.
Hence, for input 101, there is no other path shown for other input.
• Example 3:
Design FA with ∑ = {0, 1} accepts even number of 0's and even number of 1's.
Solution:
This FA will consider four different stages for input 0 and input 1. The stages could
be:

Here q0 is a start state and the final state also. Note carefully that a symmetry of 0's
and 1's is maintained. We can associate meanings to each state as:
q0: state of even number of 0's and even number of 1's.
q1: state of odd number of 0's and even number of 1's.
q2: state of odd number of 0's and odd number of 1's.
q3: state of even number of 0's and odd number of 1's.
• Example 4:
Design FA with ∑ = {0, 1} accepts the set of all strings with three consecutive
0's.
Solution:
The strings that will be generated for this particular languages are 000, 0001,
1000, 10001, .... in which 0 always appears in a clump of 3. The transition
graph is as follows:
• Example 5:

Design a FA with ∑ = {0, 1} accepts the strings with an even number


of 0's followed by single 1.
Solution:
The DFA can be shown by a transition diagram as:
NDFA to DFA

• Problem Statement
Let X = (Q , ∑, δ , q , F ) be an NDFA which accepts
the language L(X). We have to design an
equivalent DFA Y = (Q , ∑, δ , q , F ) such that L(Y) =
L(X). The following procedure converts the
NDFA to its equivalent DFA -
• Algorithm
Input - An NDFA
Output - An equivalent DFA
Step 1 - Create state table from the given NDFA.
Step 2 - Create a blank state table under possible input alphabets for
the equivalent DFA.
Step 3 - Mark the start state of the DFA by q0 (Same as the NDFA).
Step 4 - Find out the combination of States {Q , Q ,... , Q } for each
possible input alphabet.
Step 5 - Each time we generate a new DFA state under the input
alphabet columns, we have to apply step 4 again, otherwise go to
step 6.
Step 6 - The states which contain any of the final states of the NDFA
are the final states of the equivalent DFA.
Example
Let us consider the NDFA shown in the figure below.
Step 1 - Create state table from the given NDFA. ∅

Current
State 0 1

a
b
c
d
e
Step 2 - Create a blank state table under possible input
alphabets for the equivalent DFA.
Step 3 - Mark the start state of the DFA by q0 (Same as the
NDFA).
Step 4 - Find out the combination of States {Q , Q ,... , Q } for
each possible input alphabet.
Step 5 - Each time we generate a new DFA state under the
input alphabet columns, we have to apply step 4 again,
otherwise go to step 6.
Step 6 - The states which contain any of the final states of the
Current NDFA are the final states of the equivalent DFA.
state 0 1
State 0 1
[a]
→a { a, b , c , d , e} {d , e}

b {c} {e}
c ∅ {b}
d {e} ∅
+ e ∅ ∅
state 0 1

[a] [a,b,c,d,e] [d,e]

+ [a,b,c,d,e] [a,b,c,d,e] [b,d,e]

+ [d,e] [e] ∅

+ [b,d,e] [c,e] [e]

+ [e] ∅ ∅

+ [c, e] ∅ [b]

[b] [c] [e]

[c] ∅ [b]
state 0 1

[a] [a,b,c,d,e] [d,e]


Current
State 0 1 [a,b,c,d,e] [a,b,c,d,e] [b,d,e]

[d,e] [e] ∅
a { a, b , c , d {d , e}
, e} [b,d,e] [c,e] [e]
b {c} {e}
[e] ∅ ∅
c ∅ {b} [c, e] ∅ [b]
d {e} ∅ [b] [c] [e]

e ∅ ∅ [c] ∅ [b]
Finite State Automata with Output
Till now we have seen the finite automata, DFA and NFA. Now we will see finite
automata with output.
It will be clear once you see one example

• There are two type of machines under this category

• Moore Machine : This machine has output on the states itself.


• Mealy Machine : This machine has output on the transitions
Note: Till now we have seen automata with no output those are language
accepters
Note: Now we will see language transducers means they will convert input to
output
Moore Machine

Moore machine shows output on state itself so if input ε then also we will get one
output.
Below are parameters for Moore machine:
Parameter Name Description

Q Finite Set of States

Σ Input Alphabet

q0 Initial State

Δ Output Alphabet

λ Output Function(λ): Q-> Δ

δ Transition Function: δ:Q X Σ -> Q


Example of Moore Machine
Mealy Machine
• Mealy machine shows output on transition.

• Below are parameters for mealy machine:

Parameter Name Description

Q Finite Set of States

Σ Input Alphabet

q0 Initial State

Δ Output Alphabet

λ Output Function(λ): Q X Σ-> Δ

δ Transition Function: δ:Q X Σ -> Q


Example of Mealy Machine
Design Moore machine for counting baa's in a
given string
Firstly we will design a DFA for accepting baa's then we will write output inside
states.
• Now we will assign the output inside states so that we can count baa's. For
accepting state D we will assign '1' and for other states we will assign '0'.

We have taken string 'baabaa'. So as you can see from the below diagram that we
will get '1' twice.
Example:
design mealy machine for 1's complement.
Example:
Design a mealy machine for 2's complement

Generally we take 2's complement as follows:


1. Take 1's complement of the input
1. Add 1 to step 1
• But here we are taking 2's complement in a different manner to design mealy machine.
The approach goes as follows:
Start from right to left
• Ignore all 0's
• When 1 comes ignore it and then take 1's complement of every digit
Example
1. Lets take 001 and we know that its 2's complement is (110+1 = 111)
2. So scan from right to left
3. On state A '1' came first to go to state B and in output write 1
4. On state B replace '0' with '1' and vice-versa
5. So finally we got 111 as output
6. Be aware that the output is also printed in right to left order
Context-Free Grammar

A grammar G is of type 2 (context-free grammar) if all of its


productions are of the form
A → β, where A ∈ N, β ∈ (N ∪ T )+. A production of the form S → ε
can also be accepted if the start symbol S does not occur in the
right-hand side of any production.
Generation of Derivation Tree
A derivation tree or parse tree is an ordered rooted tree that
graphically represents the semantic information a string
derived from a context-free grammar.
Representation Technique
Root vertex - Must be labeled by the start symbol.
Vertex - Labeled by a non-terminal symbol.
Leaves - Labeled by a terminal symbol or ε.
If S → x x …… x is a production rule in a CFG, then the parse tree /
derivation tree will be as follows:
There are two different approaches to draw a derivation tree –

Top-down Approach –
Starts with the starting symbol S
Goes down to tree leaves using productions

Bottom-up Approach -
Starts from tree leaves

Proceeds upward to the root which is the starting symbol S


Derivation Tree
• The derivation or the yield of a parse tree is the final string
obtained by concatenating the labels of the leaves of the tree
from left to right, ignoring the Nulls. However, if all the leaves are
Null, derivation is Null.
• Example

Let a CFG {N,T,P,S} be


N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb | ε
One derivation from the above CFG is “abaabb”
S → SS → aSbS → abS → abaSb → abaaSbb → abaabb
S → SS → aSbS → abS → abaSb → abaaSbb → abaabb
Leftmost and Rightmost Derivation of a String

Leftmost derivation - A leftmost derivation is
obtained by applying production to the leftmost
non-terminal in each step.

Rightmost derivation - A rightmost derivation is
obtained by applying production to the rightmost
non-terminal in each step.
Example
Let any set of production rules in a CFG be

X → X+X | X*X |X| a

over an alphabet {a}.

The leftmost derivation for the string "a+a*a" may be –

X → X+X → a+X → a + X*X → a+a*X → a+a*a

The stepwise derivation of the above string is shown as below -


• The rightmost derivation for the above string
"a+a*a" may be –

X → X*X → X*a → X+X*a → X+a*a → a+a*a

The stepwise derivation of the above string is


shown as below -
Left

and Right Recursive Grammars
In a context-free grammar G, if there is a production in the form
X → Xa where X is a non-terminal and ‘a’ is a string of terminals, it is
called a left recursive production. The grammar having a left
recursive production is called a left recursive grammar.

And if in a context-free grammar G, if there is a production is in the
form X → aX where X is a nonterminal and ‘a’ is a string of
terminals, it is called a right recursive production. The grammar
having a right recursive production is called a right recursive
grammar.
Chomsky Normal form
• A CFG is in Chomsky Normal Form if the Productions are in the following forms -
A→a
A → BC
S→ε
where A, B, and C are non-terminals and a is terminal.
Algorithm to Convert into Chomsky Normal Form -
Step 1 - If the start symbol S occurs on some right side, create a new start symbol S’ and a new
production S’→ S.

Step 2 - Remove Null productions.

In a CFG, a non-terminal symbol ‘A’ is a nullable variable if there is a production A → ε or there is a


derivation that starts at A and finally ends up with
ε: A → .......… → ε
Removal Procedure
1 − Find out nullable non-terminal variables which derive ε.
2 − For each production A → a, construct all productions A → x where x is obtained from ‘a’ by
removing one or multiple non-terminals from 1.
3 − Combine the original productions with the result of 2 and remove ε - productions.
Example
Remove null production from the following −
S → ASA | aB | b, A → B, B → b | ∈
Solution −
There are two nullable variables − A and B
At first, we will remove B → ε.
After removing B → ε, the production set becomes −
S→ASA | aB | b | a, A →ε |B, B → b
Now we will remove A → ε.
After removing A → ε, the production set becomes −
S→ASA | aB | b | a | SA | AS | S, A→ B, B→b
This is the final production set without null transition.
Step 3 - Remove unit productions.
Any production rule in the form A → B where A, B ∈ Non-
terminal is called unit production..
Removal Procedure −
1 − To remove A → B, add production A → x to the grammar
rule whenever B → x occurs in the grammar. [x ∈ Terminal, x
can be Null]
2 − Delete A → B from the grammar.
3 − Repeat from step 1 until all unit productions are removed.
Example
Remove unit production from the following −
S → XY, X → a, Y → Z | b, Z → M, M → N, N→a
Solution −
There are 3 unit productions in the grammar − (Y → Z, Z → M, and M → N)
At first, we will remove M → N.
As N → a, we add M → a, and M → N is removed.
The production set becomes
S → XY, X → a, Y → Z | b, Z → M, M → a, N → a
Now we will remove Z → M.
As M → a, we add Z→ a, and Z → M is removed.
The production set becomes
S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
Now we will remove Y → Z.
As Z → a, we add Y→ a, and Y → Z is removed.
The production set becomes
S → XY, X → a, Y → a | b, Z → a, M → a, N → a
Now Z, M, and N are unreachable, hence we can remove those.
The final CFG is unit production free −
S → XY, X → a, Y → a | b
Step 4 - Replace each production A → B …B where n > 2 with
A → B C where C → B …B .
Repeat this step for all productions having two or more symbols in the
right side.

Step 5 - If the right side of any production is in the form A → aB where a


is a terminal and A, B are non-terminal, then the production is replaced
by A → XB and X → a. Repeat this step for every production which is in
the form A → aB.
Example:
Convert the following CFG into CNF (S → ASA | aB, A → B | S, B → b | ε)

Solution
(1) Since S appears in R.H.S, we add a new state S’ and S’ →S is added to the
production set and it becomes -
S’ →S, S→ ASA | aB, A → B | S, B→b|∈

(2) Now we will remove the null productions -


B → ∈ and A → ∈
After removing B → ε, the production set becomes -
S’ →S, S→ ASA | aB | a, A → B | S | ∈, B → b
After removing A → ∈, the production set becomes –
S’ →S, S→ ASA | aB | a | AS | SA | S, A → B | S, B→b
(3) Now we will remove the unit productions.
After removing S’ → S, the production set becomes -
S’ →S, S→ ASA | aB | a | AS | SA, A → B | S, B → b

After removing S → S, the production set becomes -


S’ → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A → B | S, B → b

After removing A→ B, the production set becomes -


S’ → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A→S|b
B→b

After removing A→ S, the production set becomes -


S’ → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A → b |ASA | aB | a | AS | SA, B → b
(4) Now we will find out more than two variables in
the R.H.S Here, S’ → ASA, S → ASA, A→ ASA
violates two Non-terminals in R.H.S.

Hence we will apply step 4 and step 5 to get the


following final production set which is in CNF -
S’ → AX | aB | a | AS | SA
S→ AX | aB | a | AS | SA
A → b |AX | aB | a | AS | SA
B→b
X → SA
(5) We have to change the productions
S’ → aB, S→ aB, A→ aB
And the final production set becomes -
S’ → AX | YB | a | AS | SA
S→ AX | YB | a | AS | SA
A → b A → b |AX | YB | a | AS | SA
B→b
X → SA
Y→a
Pushdown
Automata
• A pushdown automaton (PDA) is an abstract model
machine similar to the FSA
• It has a finite set of states. However, in addition,
it has a pushdown stack. Moves of the PDA are as
follows:
• 1. An input symbol is read and the top symbol on the
stack is read.
• 2. Based on both symbols, the machine enters a new
state and writes zero or more symbols onto the
pushdown stack.
• 3. Acceptance of a string occurs if the stack is ever
empty or the PDA is in a final state.
Power of PDA
PDAs are more powerful than FSAs.
•anbn, which cannot be recognized by an FSA, can
easily be recognized by the PDA.
•Stack all a symbols and, for each b, pop an a off
the stack.
•If the end of input is reached at the same time that
the stack becomes empty, the string is accepted.

The languages accepted by PDAs are equivalent to the


context-free languages.
Formal definition of PDA
P=(Q, S, G, d(qi , a, tm )→ {(qk , tn ),…….}, q0 , t0, F )
Q: A finite set of states.
S : A finite set of input symbols.
G: A finite stack alphabet.
d: The transition function with input:
qi is a state in Q.
a is a symbol in S or a = e (the empty string).
tm is a stack symbol, tm  G.
and the output is a finite set of pairs:
qk the new state.
tn is the string of stack symbols that replaces tm at the top of the stack. If tn = e, then the stack
is popped.
q0: The start state.
t0 : Initially, the PDA’s stack consists this symbol and nothing else.
F : The set of accepting states.
 Two types of PDA transitions
1st Type:
δ(q, a, z) = {(p1,γ1), (p2,γ2),…, (pm,γm)}

 Current state is q
 Current input symbol is a
 Symbol currently on top of the stack z
 Move to state pi from q
 Replace z with γi on the stack (leftmost symbol on top)
 Move the input head to the next input symbol
 Two types of PDA transitions --------cont.
2nd Type
δ(q, ε, z) = {(p1,γ1), (p2,γ2),…, (pm,γm)}

 Current state is q
 Current input symbol is not considered
 Symbol currently on top of the stack z
 Move to state pi from q
 Replace z with γi on the stack (leftmost symbol on top)
 No input symbol is read

:
 Example1: 0n1n,
n>=0
M = ({q1, q2}, {0, 1}, {L, #}, δ, q1, #, Ø)
δ:
(1) δ(q1, 0, #) = {(q1, L#)} // stack order: L on top, then # below
(2) δ(q1, 1, #) = Ø // illegal, string rejected, When will it happen?
(3) δ(q1, 0, L) = {(q1, LL)}
(4) δ(q1, 1, L) = {(q2, ε)}
(5) δ(q2, 1, L) = {(q2, ε)}
(6) δ(q2, ε, #) = {(q2, ε)} //if ε read & stack hits bottom, accept
(7) δ(q2, ε, L) = Ø // illegal, string rejected
(8) δ(q1, ε, #) = {(q2, ε)} // n=0, accept
Goal: (acceptance)
 Read the entire input string
 Terminate with an empty stack
 Informally, a string is accepted if there exists a computation that uses up all
the input and leaves the stack empty.
PDA Example: Lwwr  {ww | w  (0  1) }
R *

The language, Lwwr, is the even-length palindromes


over alphabet {0,1}.

Lwwr is a Context-Free Language (CFL) generated by


the grammar:

S  0 S 0 | 1S1 | e
One PDA for Lwwr is given on the following slide...
Design a non-deterministic PDA for accepting the language L
= {wwR | w ∈ (a, b)*}
L = {aa, bb, abba, aabbaa, abaaba, ......}
δ(q0, a, z)=(q0, az)
δ(q0, a, a)=(q0, aa)
δ(q0, b, z)=(q0, bz)
δ(q0, b, b)=(q0, bb)
δ(q0, a, b)=(q0, ab)
δ(q0, b, a)=(q0, ba)
δ(q0, a, a)=(q1, ∈)
δ(q0, b, b)=(q1, ∈)
δ(q1, a, a)=(q1, ∈)
δ(q1, b, b)=(q1, ∈)
δ(q1, ∈, z)=(qf, z)
Where, q0 = Initial state
qf = Final state
∈ = indicates pop operation
A Graphical Notation for PDA’s
1. The nodes correspond to the states of the PDA.
2. An arrow labeled Start indicates the unique start state.
3. Doubly circled states are accepting states.
4. Edges correspond to transitions in the PDA as follows:
5. An edge labeled (ai, tm)/tn from state q to state p means
that d(q, ai, tm) contains the pair (p, tn), perhaps among
other pairs.
Graphical representation of PDA (wwR )
Design a deterministic PDA for accepting the language
L = { wcwR w ∈ (a, b)+}, i.e.,
L={aca, bcb, abcba, abbcbba, aacaa, bbcbb, .......}
δ(q0, a, z) = (q0, az)
δ(q0, a, a) = (q0, aa)
δ(q0, b, z) = (q0, bz)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)

δ(q0, c, a) = (q1, a)
δ(q0, c, b) = (q1, b)

δ(q1, a, a) = (q1, ∈)
δ(q1, b, b) = (q1, ∈)

δ(q1, ∈, z) = (qf, z)

Where, q0 = Initial state


qf = Final state
Example:
• Design a PDA for the language L ={w∈ {a,b}*| w contains equal no. of a's and b's}
• L={ , ab, ba , abba , aabb , abab,……………………………..}

• Design a PDA for accepting the language L = {an bm cn | m,n>=1}


Example

Design a PDA for accepting the language L = {an bm cn | m,n>=1}


Homework
• Design a PDA for accepting the language L = {a2mb3m | m ≥ 1}
• Design a PDA for accepting the language L = {ambnc(m+n) | m,n ≥ 1}
• Design a PDA for accepting the language L = {an bn cm | m,n>=1}
• Design a PDA for accepting the language L = {an bn | n>=1}
• Design a PDA for accepting the language L = {am b(2m) | m>=1}
• Design a PDA for accepting the language L = {am bn cp dq | m+n=p+q ;
m,n,p,q>=1}
Convert the following CFG to PDA
A->0A0
A->1A1 • Start P, Final Q,
Stack Input state
{0,1,c} , {0,1,c,S},e, 01c10 P
A->c transition function M A 01c10 Q
0A0 01c10 Q
M(P,e,e)->(Q,A) 0A 1c10 Q
M(Q,e,A)->(Q,0A0) 01A1 1c10 Q
01A c10 Q
M(Q,e,A)->(Q,1A1) 01c c10 Q
01 10 Q
M(Q,e,A)->(Q,c) 0 0 Q
M(Q,0,0)->(Q,e) Q

M(Q,1,1)->(Q,e)
M(Q,c,c)->(Q,e)
Convert the following CFG to PDA
• Start P, Final Q, {+,*,i,(,)} , {+,*,i,(,), E,T,A,F,B},
E->TA ɛ, transition function M
M(Q,+,+)->(Q, ɛ) Stack Input State
A->+TA M(P, ɛ, ɛ)->(Q,E)
M(Q,*,*)->(Q, ɛ)
i+i P
E i+iI Q
A->ɛ M(Q, ɛ,E)->(Q,TA) M(Q, i , i )->(Q, ɛ)
AT i+i Q
M(Q, ɛ,A)->(Q,+TA) M(Q,(,( )->(Q, ɛ) ABF i+i Q
T->FB M(Q, ɛ,A)->(Q, ɛ) M(Q,),) )->(Q, ɛ)
ABi i+i Q
AB +i Q
B->*FB M(Q, ɛ,T)->(Q,FB) A +i Q
AT+ +i Q
B-> ɛ M(Q, ɛ,B)->(Q,*FB) AT i Q
ABF i Q
F->i M(Q, ɛ,B)->(Q, ɛ)
ABi i Q
M(Q, ɛ,F)->( Q, i) AB Q
F->(E) M(Q, ɛ,F)->(Q, (E) )
A Q
Q
Thank you

You might also like