SlideShare a Scribd company logo
1 of 87
UNIT III
CONTEXT FREE GRAMMAR &
LANGUAGE
Mrs.D.Jena Catherine Bel,
Assistant Professor,CSE,
Velammal Engineering College.
CONTEXT FREE
GRAMMAR & LANGUAGE
CFG – Parse Trees – Ambiguity in Grammars and Languages – Definition of the
Pushdown Automata – Languages of a Pushdown Automata – Equivalence of
Pushdown Automata and CFG, Deterministic Pushdown Automata.
Disadvantage of FA
• Memory
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
3
CONTEXT FREE GRAMMAR
• A CFG is a way of describing languages by recursive rules
called productions
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
4
CFG – Formal Definition
A context-free grammar (CFG) consisting of a finite
set of grammar rules is a quadruple (V, T, P, S)
where
• V is a set of non-terminal symbols.
• T is a set of terminals where V ∩ T = NULL.
• P is a set of rules, P: V → (V ∪ T)*, i.e., the left-
hand side of the production rule P does have any
right context or left context.
• S is the start symbol.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
5
Derivation
• Beginning with the start symbol, derive the terminal strings by
repeatedly replacing the variable by the body of some
production with that variable in the head
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
6
Derivation
• Leftmost derivation:
• In each step of derivation, if the leftmost variable is replaced then
it is called leftmost derivation
• Rightmost derivation:
• In each step of derivation, if the rightmost variable is replaced
then it is called rightmost derivation
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
7
• Sentential Forms
• Any step in a derivation is a string of variables and or terminals.
Such strings are called sentential forms.
• If the derivation is leftmost then the string is a left sentential
form
• If the derivation is rightmost then the string is a right sentential
form
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
8
Parse Tree
• Tree representation of derivation is called parse tree or
derivation tree
• Interior nodes are labelled with variables and leaf nodes are
labelled with terminal or ε
• For each interior node, there must be a production such that
the head of the production is the label of the node and the
label of its children read from left to right form the body of
the production
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
9
rightmost derivation –
example contd..,
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
10
Derivation / yield of a 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
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
11
Tree representation
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
12
leftmost derivation – example
contd..,
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
13
Ambiguous grammar
• If a context free grammar G has more than one derivation tree
for some string w ∈ L(G), it is called an ambiguous grammar.
There exist multiple right-most or left-most derivations for
some string generated from that grammar.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
14
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
15
Why PDA
Design FA for accepting a language {an | n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
16
Why PDA
Design FA for accepting a language {an | n>=1}
L = {a, aa, aaa, aaaa, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
17
Why PDA
Design FA for accepting a language {an | n>=1}
L = {a, aa aaa, aaaa, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
18
Why PDA
Design FA for accepting a language {anbm | m,n>=0}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
19
Why PDA
Design FA for accepting a language {anbm | m,n>=0}
L= {ε,a,b,ab,aa,bb,aaa,aab,abb,bbb,aabb,aaabbb,aaaabbbb,
......}
Constraints/limitations
1. a followed by b
2. N >=0
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
20
Why PDA
Design FA for accepting a language {anbm | m,n>=0}
L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb,
aaaabbbb, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
21
Why PDA
Design FA for accepting a language {an | n>=1}
L = {a, aa aaa, aaaa, ......}
Design FA for accepting a language {anbm | m,n>=0}
L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb,
aaaabbbb, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
22
Why PDA
Design FA for accepting a language {anbn | n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
23
why
Design FA for accepting a language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
24
why
Design FA for accepting a language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >=0
3.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
25
why
Design FA for accepting a language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >0
3. Number of (a ) == Number of (b)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
26
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
27
Why PDA
is it Possible
Design FA for accepting a language {anbn | n>=1}
Design FA for accepting a language {anb2n | n>=1}
Design FA for accepting a language {anbm cn|m. n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
28
Pushdown Automata
Schematic PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
29
PDA Components
Input tape: The input tape is divided in many cells or symbols. The input head is read-only and may only move from left to right,
one symbol at a time.
Finite control: The finite control has some pointer which points the current symbol which is to be read.
Stack: The stack is a structure in which we can push and remove the items from one end only. It has an infinite size. In PDA, the
stack is used to store the items temporarily.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
30
Formal Definition of PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
31
Moves of PDA And Types
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
32
Pushdown Automata Acceptance
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
33
Pushdown Automata Acceptance
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
34
Representation of State
Transition
Delta Function ( ) is the transition function, the use of which will become more clear by
taking a closer look at the Three Major operations done on Stack :-
1. Push
2. Pop
3. Skip /No operation
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
35
Representation of State
Transition
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
36
Representation of State
Transition
Representation of Push in a PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
37
Representation of State
Transition
Representation of Pop in a PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
38
Representation of State
Transition
Representation of Ignore in a PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
39
Moves of PDA
Push
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
40
Moves of PDA
Pop Skip/ no operation
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
41
Pushdown Automata
Define the pushdown automata for language {anbn |
n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
42
Pushdown Automata
Define the pushdown automata for language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >0
3. Number of (a ) == Number of (b)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
43
Pushdown Automata
Define the pushdown automata for language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
1. While (a)
Push a;
While (b)
Pop ;
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
44
Pushdown Automata
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
45
Pushdown Automata
Define the pushdown automata for language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
46
Pushdown Automata
Define the pushdown automata for language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
1. While (a)
Push a;
While (b)
Pop ;
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
47
PDA for the Language {anbn | n>=1}
PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf})
δ:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (qf, ε)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
48
Pushdown Automata
2. Design PDA for accepting a language {anb2n | n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
49
Pushdown Automata
Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
Constraints/limitations
1. a followed by b
2. N >0
3. Number of (b ) == 2 * Number of (a)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
50
Pushdown Automata
Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
1. While (a)
push(aa);
While(b)
Pop ;
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
51
Pushdown Automata
Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
Logic 1:
While (a)
push(aa);
While(b)
Pop ;
Logic 2:
While (a)
push(a);
While(bb)
Pop;
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
52
PDA for the Language {anb2n | n>=1}
Logic 1:
PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})
δ:
δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q2, ε, z) = (q2, ε)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
53
PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
54
PDA for the Language {anb2n | n>=1}
Logic 2:
PDA = ({q0, q1, q2, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf})
δ:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, a)
δ(q1, b, a) = (q2, ε)
δ(q2, b, a) = (q1, a)
δ(q2, ε, Z) = (qf, Z)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
55
PDA foracceptingalanguage {0n1m0n |m,n>=1}
3. Design a PDA for accepting a language {0n1m0n | m, n>=1}.
L = { 010, 0110, 01110, 001100, 0001000, 00001110000, ...... }
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
56
PDAfor acceptingalanguage {0n1m0n |m,n>=1}
Design a PDA for accepting a language {0n1m0n | m, n>=1}.
L = { 010, 0110, 01110, 001100, 0001000, 00001110000, ...... }
• Push all 0's onto the stack on encountering first 0's.
• read 1, just do nothing.
• read 0, and on each read of 0, pop one 0 from the stack.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
57
PDA foracceptingalanguage{0n1m0n |m,n>=1}.
PDA = ({q0, q1, q2}, {0, 1}, {0, Z}, δ, q0, Z, {q2})
δ:
δ(q0, 0, Z) = δ(q0, 0Z)
δ(q0, 0, 0) = δ(q0, 00)
δ(q0, 1, 0) = δ(q1, 0)
δ(q1, 1, 0) = δ(q1, 0)
δ(q1, 0, 0) = δ(q2, ε)
δ(q2, 0, 0) = δ(q2, ε)
δ(q2, 0, z) = δ(q2, Z) (ACCEPT state)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
58
PDA foracceptingalanguage{0n1m0n |m,n>=1}.
simulate this PDA for the input string "0011100".
δ(q0, 0011100, Z) ⊢ δ(q0, 011100, 0Z)
⊢ δ(q0, 11100, 00Z)
⊢ δ(q0, 1100, 00Z)
⊢ δ(q1, 100, 00Z)
⊢ δ(q1, 00, 00Z)
⊢ δ(q2, 0, 0Z)
⊢ δ(q2, ε, Z)
⊢ δ(q2, Z)
ACCEPT
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
59
Instantaneous Description (ID)
A ID is a triple (q, w, α), where:
1. q is the current state.
2. w is the remaining input.
3.α is the stack contents, top at the left.
(p, b, T) ⊢ (q, w, α)
⊢ sign describes the turnstile notation and represents one move.
⊢* sign describes a sequence of moves.
Problem – Design a non deterministic
PDA for accepting the language L = { anb2n
: m>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ)
⊢ δ(q0, abbbbbb, aaaaZ)
⊢ δ(q0, bbbbbb, aaaaaaZ)
⊢ δ(q1, bbbbb, aaaaaZ)
⊢ δ(q1, bbbb, aaaaZ)
⊢ δ(q1, bbb, aaaZ)
⊢ δ(q1, bb, aaZ)
⊢ δ(q1, b, aZ)
⊢ δ(q1, ε, Z)
⊢ δ(q2, ε)
ACCEPT
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
60
PushDown Automata
Find the language accepted by PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
61
PushDown Automata
Find the language accepted by PDA
PDA for accepting the language L = { anb m+n cm|| m,n ≥ 1}
The strings of given lanugage will be: L = {abbc, abbbcc, abbbcc, aabbbbcc, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
62
PDA for the Language {anbn | n>=1}
PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf})
δ:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (qf, ε)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
63
PDA for the Language {anbn | n>=1}
PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf})
δ:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (qf, ε)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
64
PushDown Automata
Answer the following
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
65
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
66
L={w?{a,b}*}
Problem – Design a non deterministic PDA for accepting the language L ={w?{a,b}* | w contains equal no. of
a’s and b’s}, i.e.,
L = {ε,ab, aabb, abba, aababb, bbabaa, baaababb, .......}
If ‘a’ comes first then push it in stack and if again ‘a’ comes then also push it.
Similarly, if ‘b’ comes first (‘a’ did not comes yet) then push it into the stack and if
again ‘b’ comes then also push it.
Now, if ‘a’ is present in the top of the stack and ‘b’ comes then pop the ‘a’ from the
stack. And if ‘b’ present in the top of the stack and ‘a’ comes then pop the ‘b’ from
the stack.
So, at the end if the stack becomes empty then we can say that the string is accepted
by the PDA.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
67
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
68
Deterministic pushdown
Automata
Construct a PDA for language L = {wcw’ | w={0, 1}*}
where w’ is the reverse of w
L = {1c1, 0c0, 01c10, 001c100, 010c010 ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
69
Non Deterministic pushdown Automata
Design a non deterministic PDA for
accepting the language L = {wwR w ∈
(a, b)*},
i.e.,
L = {aa, bb, abba, aabbaa, abaaba,
......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
70
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
71
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
72
PDA–Applications
● For designing the parsing phase of a compiler (Syntax
Analysis).
● For implementation of stack applications.
● For evaluating the arithmetic expressions.
● For solving the Tower of Hanoi Problem.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
73
S.NO PUSHDOWN AUTOMATA FINITE AUTOMATA
1.
For Type-2 grammar we
can design pushdown
automata.
For Type-3 grammar we
can design finite
automata.
2.
Non – Deterministic
pushdown automata has
more powerful than
Deterministic pushdown
automata.
Non-Deterministic Finite
Automata has same
powers as in
Deterministic Finite
Automata.
3.
Not every Non-
Deterministic pushdown
automata is transformed
into its equivalent
Deterministic pushdown
Every Non-Deterministic
Finite Automata is
transformed into an
equivalent Deterministic
Finite Automata
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
74
Construct a PDA
Construct a PDA for language L = {0n1m| n >= 1, m >= 1, m > n+2}
● Step-1: On receiving 0 push it onto stack. On receiving 1, ignore it and goto next state
● Step-2: On receiving 1, ignore it and goto next state
● Step-3: On receiving 1, pop a 0 from top of stack and go to next state
● Step-4: On receiving 1, pop a 0 from top of stack. If stack is empty, on receiving 1 ingore it and
goto next state
● Step-5: On receiving 1 ignore it. If input is finished then goto last state
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
75
Construct a PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
76
Exercises
• Construct Pushdown Automata for given languages
• L = {0(n+m)1m2n | m, n ≥ 0}
• L = {0n1m2(n+m) | m,n ≥ 0}
• L = {a(2*m)c(4*n)dnbm | m,n ≥ 0}
• L = {0n1m2m3n | m,n ≥ 0}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
77
L={0(n+m)1m2n | m,n≥0}
Input: NULL String
Output: Accepted
Input: 0000011122
Output: Accepted
Input: 00000112222
Output: Not Accepted
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
78
L={0n1m2(n+m) | m,n≥0}
Input: 00001112222222
Output: Accepted
Input: 00011112222
Output: Not Accepted
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
79
Exercises
• NPDA for accepting the language L = {am b(2m) | m>=1}
• NPDA for accepting the language L = {ambncn | m,n ≥ 1}
• NPDA for accepting the language L = {amb(m+n)cn | m,n ≥ 1}
• NPDA for accepting the language L = {am bn cp dq | m+n=p+q ; m,n,p,q>=1}
• NPDA for accepting the language L = {a(m+n)bmcn | m,n ≥ 1}
• NPDA for accepting the language L = {anb(2n) | n>=1} U {anbn | n>=1}
• NPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1}
• NPDA for accepting the language L = {anbm | n,m ≥ 1 and n ≠ m}
• NPDA for accepting the language L = {amb(2m+1) | m ≥ 1}
• NPDA for accepting the language L = {an bn | n>=1}
• NPDA for accepting the language L = {a2mb3m | m ≥ 1}
• NPDA for accepting the language L = {an bn cm | m,n>=1}
• NPDA for accepting the language L = {ambnc(m+n) | m,n ≥ 1}
• NPDA for accepting the language L = {wwR | w ∈ (a,b)*}
• DFA for accepting the language L = {an bm | n+m=odd}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
80
Thank YOU
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
81
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
82
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
83
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
84
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
85
NFA Vs PDA
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
86
Definition
Alphabet
● Definition: An alphabet is any finite set of symbols.,
● Example: Σ = {a, b, c, d} is an alphabet set where ‘a’, ‘b’, ‘c’, and ‘d’ are symbols.,
String
1. Definition: A string is a finite sequence of symbols taken from Σ.,
2. Example: ‘cabcad’ is a valid string on the alphabet set Σ = {a, b, c, d}
Length of a String
1. Definition : It is the number of symbols present in a string. (Denoted by |S|).,
2. Examples:,
3. If S=‘cabcad’, |S|= 6
If |S|= 0, it is called an empty string (Denoted by λ or ε)
Kleene Star
1. Definition: The Kleene star, Σ* , is a unary operator on a set of symbols or strings, Σ, that gives the
infinite set of all possible strings of all possible lengths over Σ including λ.
2. Representation: Σ* = Σ0 U Σ1 U Σ2 U……. where Σp is the set of all possible strings of length p.
3. Example: If Σ = {a, b}, Σ*= {λ, a, b, aa, ab, ba, bb,………..}
4. Kleene Closure / Plus
1. Definition: The set Σ+ is the infinite set of all possible strings of all possible lengths over Σ excluding
λ.
2. Representation: Σ+ = Σ1 U Σ2 U Σ3 U……. Σ+ = Σ* − { λ }
3. Example: If Σ = { a, b } , Σ+ ={ a, b, aa, ab, ba, bb,………..}
5. Language
1. Definition : A language is a subset of Σ* for some alphabet Σ. It can be finite or infinite.
2. Example : If the language takes all possible strings of length 2 over Σ = {a, b}, then L = { ab, bb, ba, bb}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
87

More Related Content

What's hot

Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Srimatre K
 
Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Srimatre K
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
Deciability (automata presentation)
Deciability (automata presentation)Deciability (automata presentation)
Deciability (automata presentation)Sagar Kumar
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automatadeepinderbedi
 
Theory of computing
Theory of computingTheory of computing
Theory of computingRanjan Kumar
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problemsJyotsna Suryadevara
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Abhimanyu Mishra
 
4.3 techniques for turing machines construction
4.3 techniques for turing machines construction4.3 techniques for turing machines construction
4.3 techniques for turing machines constructionSampath Kumar S
 

What's hot (20)

Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Deciability (automata presentation)
Deciability (automata presentation)Deciability (automata presentation)
Deciability (automata presentation)
 
Lesson 05
Lesson 05Lesson 05
Lesson 05
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
FInite Automata
FInite AutomataFInite Automata
FInite Automata
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Backtracking
BacktrackingBacktracking
Backtracking
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
 
4.3 techniques for turing machines construction
4.3 techniques for turing machines construction4.3 techniques for turing machines construction
4.3 techniques for turing machines construction
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Ch2 finite automaton
Ch2 finite automatonCh2 finite automaton
Ch2 finite automaton
 
Compiler construction
Compiler constructionCompiler construction
Compiler construction
 

More from Jena Catherine Bel D (7)

Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
 
Compiler Design Unit 4
Compiler Design Unit 4Compiler Design Unit 4
Compiler Design Unit 4
 
Compiler Design Unit 3
Compiler Design Unit 3Compiler Design Unit 3
Compiler Design Unit 3
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
Compiler Design Unit 1
Compiler Design Unit 1Compiler Design Unit 1
Compiler Design Unit 1
 
Theory of Computation Unit 4
Theory of Computation Unit 4Theory of Computation Unit 4
Theory of Computation Unit 4
 
Automata
AutomataAutomata
Automata
 

Recently uploaded

BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 

Recently uploaded (20)

BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

Theory of Computation Unit 3

  • 1. UNIT III CONTEXT FREE GRAMMAR & LANGUAGE Mrs.D.Jena Catherine Bel, Assistant Professor,CSE, Velammal Engineering College.
  • 2. CONTEXT FREE GRAMMAR & LANGUAGE CFG – Parse Trees – Ambiguity in Grammars and Languages – Definition of the Pushdown Automata – Languages of a Pushdown Automata – Equivalence of Pushdown Automata and CFG, Deterministic Pushdown Automata.
  • 3. Disadvantage of FA • Memory Mrs.D.Jena Catherine Bel,AP/CSE, VEC 3
  • 4. CONTEXT FREE GRAMMAR • A CFG is a way of describing languages by recursive rules called productions Mrs.D.Jena Catherine Bel,AP/CSE, VEC 4
  • 5. CFG – Formal Definition A context-free grammar (CFG) consisting of a finite set of grammar rules is a quadruple (V, T, P, S) where • V is a set of non-terminal symbols. • T is a set of terminals where V ∩ T = NULL. • P is a set of rules, P: V → (V ∪ T)*, i.e., the left- hand side of the production rule P does have any right context or left context. • S is the start symbol. Mrs.D.Jena Catherine Bel,AP/CSE, VEC 5
  • 6. Derivation • Beginning with the start symbol, derive the terminal strings by repeatedly replacing the variable by the body of some production with that variable in the head Mrs.D.Jena Catherine Bel,AP/CSE, VEC 6
  • 7. Derivation • Leftmost derivation: • In each step of derivation, if the leftmost variable is replaced then it is called leftmost derivation • Rightmost derivation: • In each step of derivation, if the rightmost variable is replaced then it is called rightmost derivation Mrs.D.Jena Catherine Bel,AP/CSE, VEC 7
  • 8. • Sentential Forms • Any step in a derivation is a string of variables and or terminals. Such strings are called sentential forms. • If the derivation is leftmost then the string is a left sentential form • If the derivation is rightmost then the string is a right sentential form Mrs.D.Jena Catherine Bel,AP/CSE, VEC 8
  • 9. Parse Tree • Tree representation of derivation is called parse tree or derivation tree • Interior nodes are labelled with variables and leaf nodes are labelled with terminal or ε • For each interior node, there must be a production such that the head of the production is the label of the node and the label of its children read from left to right form the body of the production Mrs.D.Jena Catherine Bel,AP/CSE, VEC 9
  • 10. rightmost derivation – example contd.., Mrs.D.Jena Catherine Bel,AP/CSE, VEC 10
  • 11. Derivation / yield of a 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 Mrs.D.Jena Catherine Bel,AP/CSE, VEC 11
  • 13. leftmost derivation – example contd.., Mrs.D.Jena Catherine Bel,AP/CSE, VEC 13
  • 14. Ambiguous grammar • If a context free grammar G has more than one derivation tree for some string w ∈ L(G), it is called an ambiguous grammar. There exist multiple right-most or left-most derivations for some string generated from that grammar. Mrs.D.Jena Catherine Bel,AP/CSE, VEC 14
  • 16. Why PDA Design FA for accepting a language {an | n>=1} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 16
  • 17. Why PDA Design FA for accepting a language {an | n>=1} L = {a, aa, aaa, aaaa, ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 17
  • 18. Why PDA Design FA for accepting a language {an | n>=1} L = {a, aa aaa, aaaa, ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 18
  • 19. Why PDA Design FA for accepting a language {anbm | m,n>=0} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 19
  • 20. Why PDA Design FA for accepting a language {anbm | m,n>=0} L= {ε,a,b,ab,aa,bb,aaa,aab,abb,bbb,aabb,aaabbb,aaaabbbb, ......} Constraints/limitations 1. a followed by b 2. N >=0 Mrs.D.Jena Catherine Bel,AP/CSE, VEC 20
  • 21. Why PDA Design FA for accepting a language {anbm | m,n>=0} L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb, aaaabbbb, ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 21
  • 22. Why PDA Design FA for accepting a language {an | n>=1} L = {a, aa aaa, aaaa, ......} Design FA for accepting a language {anbm | m,n>=0} L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb, aaaabbbb, ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 22
  • 23. Why PDA Design FA for accepting a language {anbn | n>=1} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 23
  • 24. why Design FA for accepting a language {anbn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 24
  • 25. why Design FA for accepting a language {anbn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} Constraints/limitations 1. a followed by b 2. N >=0 3. Mrs.D.Jena Catherine Bel,AP/CSE, VEC 25
  • 26. why Design FA for accepting a language {anbn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} Constraints/limitations 1. a followed by b 2. N >0 3. Number of (a ) == Number of (b) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 26
  • 28. Why PDA is it Possible Design FA for accepting a language {anbn | n>=1} Design FA for accepting a language {anb2n | n>=1} Design FA for accepting a language {anbm cn|m. n>=1} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 28
  • 30. PDA Components Input tape: The input tape is divided in many cells or symbols. The input head is read-only and may only move from left to right, one symbol at a time. Finite control: The finite control has some pointer which points the current symbol which is to be read. Stack: The stack is a structure in which we can push and remove the items from one end only. It has an infinite size. In PDA, the stack is used to store the items temporarily. Mrs.D.Jena Catherine Bel,AP/CSE, VEC 30
  • 31. Formal Definition of PDA Mrs.D.Jena Catherine Bel,AP/CSE, VEC 31
  • 32. Moves of PDA And Types Mrs.D.Jena Catherine Bel,AP/CSE, VEC 32
  • 35. Representation of State Transition Delta Function ( ) is the transition function, the use of which will become more clear by taking a closer look at the Three Major operations done on Stack :- 1. Push 2. Pop 3. Skip /No operation Mrs.D.Jena Catherine Bel,AP/CSE, VEC 35
  • 37. Representation of State Transition Representation of Push in a PDA Mrs.D.Jena Catherine Bel,AP/CSE, VEC 37
  • 38. Representation of State Transition Representation of Pop in a PDA Mrs.D.Jena Catherine Bel,AP/CSE, VEC 38
  • 39. Representation of State Transition Representation of Ignore in a PDA Mrs.D.Jena Catherine Bel,AP/CSE, VEC 39
  • 41. Moves of PDA Pop Skip/ no operation Mrs.D.Jena Catherine Bel,AP/CSE, VEC 41
  • 42. Pushdown Automata Define the pushdown automata for language {anbn | n>=1} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 42
  • 43. Pushdown Automata Define the pushdown automata for language {anbn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} Constraints/limitations 1. a followed by b 2. N >0 3. Number of (a ) == Number of (b) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 43
  • 44. Pushdown Automata Define the pushdown automata for language {anbn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} 1. While (a) Push a; While (b) Pop ; Mrs.D.Jena Catherine Bel,AP/CSE, VEC 44
  • 46. Pushdown Automata Define the pushdown automata for language {anbn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 46
  • 47. Pushdown Automata Define the pushdown automata for language {anbn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} 1. While (a) Push a; While (b) Pop ; Mrs.D.Jena Catherine Bel,AP/CSE, VEC 47
  • 48. PDA for the Language {anbn | n>=1} PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf}) δ: δ(q0, a, Z) = (q0, aZ) δ(q0, a, a) = (q0, aa) δ(q0, b, a) = (q1, ε) δ(q1, b, a) = (q1, ε) δ(q1, ε, Z) = (qf, ε) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 48
  • 49. Pushdown Automata 2. Design PDA for accepting a language {anb2n | n>=1} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 49
  • 50. Pushdown Automata Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e., L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......} Constraints/limitations 1. a followed by b 2. N >0 3. Number of (b ) == 2 * Number of (a) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 50
  • 51. Pushdown Automata Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e., L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......} 1. While (a) push(aa); While(b) Pop ; Mrs.D.Jena Catherine Bel,AP/CSE, VEC 51
  • 52. Pushdown Automata Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e., L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......} Logic 1: While (a) push(aa); While(b) Pop ; Logic 2: While (a) push(a); While(bb) Pop; Mrs.D.Jena Catherine Bel,AP/CSE, VEC 52
  • 53. PDA for the Language {anb2n | n>=1} Logic 1: PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2}) δ: δ(q0, a, Z) = (q0, aaZ) δ(q0, a, a) = (q0, aaa) δ(q0, b, a) = (q1, ε) δ(q1, b, a) = (q1, ε) δ(q2, ε, z) = (q2, ε) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 53
  • 55. PDA for the Language {anb2n | n>=1} Logic 2: PDA = ({q0, q1, q2, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf}) δ: δ(q0, a, Z) = (q0, aZ) δ(q0, a, a) = (q0, aa) δ(q0, b, a) = (q1, a) δ(q1, b, a) = (q2, ε) δ(q2, b, a) = (q1, a) δ(q2, ε, Z) = (qf, Z) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 55
  • 56. PDA foracceptingalanguage {0n1m0n |m,n>=1} 3. Design a PDA for accepting a language {0n1m0n | m, n>=1}. L = { 010, 0110, 01110, 001100, 0001000, 00001110000, ...... } Mrs.D.Jena Catherine Bel,AP/CSE, VEC 56
  • 57. PDAfor acceptingalanguage {0n1m0n |m,n>=1} Design a PDA for accepting a language {0n1m0n | m, n>=1}. L = { 010, 0110, 01110, 001100, 0001000, 00001110000, ...... } • Push all 0's onto the stack on encountering first 0's. • read 1, just do nothing. • read 0, and on each read of 0, pop one 0 from the stack. Mrs.D.Jena Catherine Bel,AP/CSE, VEC 57
  • 58. PDA foracceptingalanguage{0n1m0n |m,n>=1}. PDA = ({q0, q1, q2}, {0, 1}, {0, Z}, δ, q0, Z, {q2}) δ: δ(q0, 0, Z) = δ(q0, 0Z) δ(q0, 0, 0) = δ(q0, 00) δ(q0, 1, 0) = δ(q1, 0) δ(q1, 1, 0) = δ(q1, 0) δ(q1, 0, 0) = δ(q2, ε) δ(q2, 0, 0) = δ(q2, ε) δ(q2, 0, z) = δ(q2, Z) (ACCEPT state) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 58
  • 59. PDA foracceptingalanguage{0n1m0n |m,n>=1}. simulate this PDA for the input string "0011100". δ(q0, 0011100, Z) ⊢ δ(q0, 011100, 0Z) ⊢ δ(q0, 11100, 00Z) ⊢ δ(q0, 1100, 00Z) ⊢ δ(q1, 100, 00Z) ⊢ δ(q1, 00, 00Z) ⊢ δ(q2, 0, 0Z) ⊢ δ(q2, ε, Z) ⊢ δ(q2, Z) ACCEPT Mrs.D.Jena Catherine Bel,AP/CSE, VEC 59
  • 60. Instantaneous Description (ID) A ID is a triple (q, w, α), where: 1. q is the current state. 2. w is the remaining input. 3.α is the stack contents, top at the left. (p, b, T) ⊢ (q, w, α) ⊢ sign describes the turnstile notation and represents one move. ⊢* sign describes a sequence of moves. Problem – Design a non deterministic PDA for accepting the language L = { anb2n : m>=1}, i.e., L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......} δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ) ⊢ δ(q0, abbbbbb, aaaaZ) ⊢ δ(q0, bbbbbb, aaaaaaZ) ⊢ δ(q1, bbbbb, aaaaaZ) ⊢ δ(q1, bbbb, aaaaZ) ⊢ δ(q1, bbb, aaaZ) ⊢ δ(q1, bb, aaZ) ⊢ δ(q1, b, aZ) ⊢ δ(q1, ε, Z) ⊢ δ(q2, ε) ACCEPT Mrs.D.Jena Catherine Bel,AP/CSE, VEC 60
  • 61. PushDown Automata Find the language accepted by PDA Mrs.D.Jena Catherine Bel,AP/CSE, VEC 61
  • 62. PushDown Automata Find the language accepted by PDA PDA for accepting the language L = { anb m+n cm|| m,n ≥ 1} The strings of given lanugage will be: L = {abbc, abbbcc, abbbcc, aabbbbcc, ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 62
  • 63. PDA for the Language {anbn | n>=1} PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf}) δ: δ(q0, a, Z) = (q0, aZ) δ(q0, a, a) = (q0, aa) δ(q0, b, a) = (q1, ε) δ(q1, b, a) = (q1, ε) δ(q1, ε, Z) = (qf, ε) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 63
  • 64. PDA for the Language {anbn | n>=1} PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf}) δ: δ(q0, a, Z) = (q0, aZ) δ(q0, a, a) = (q0, aa) δ(q0, b, a) = (q1, ε) δ(q1, b, a) = (q1, ε) δ(q1, ε, Z) = (qf, ε) Mrs.D.Jena Catherine Bel,AP/CSE, VEC 64
  • 65. PushDown Automata Answer the following Mrs.D.Jena Catherine Bel,AP/CSE, VEC 65
  • 67. L={w?{a,b}*} Problem – Design a non deterministic PDA for accepting the language L ={w?{a,b}* | w contains equal no. of a’s and b’s}, i.e., L = {ε,ab, aabb, abba, aababb, bbabaa, baaababb, .......} If ‘a’ comes first then push it in stack and if again ‘a’ comes then also push it. Similarly, if ‘b’ comes first (‘a’ did not comes yet) then push it into the stack and if again ‘b’ comes then also push it. Now, if ‘a’ is present in the top of the stack and ‘b’ comes then pop the ‘a’ from the stack. And if ‘b’ present in the top of the stack and ‘a’ comes then pop the ‘b’ from the stack. So, at the end if the stack becomes empty then we can say that the string is accepted by the PDA. Mrs.D.Jena Catherine Bel,AP/CSE, VEC 67
  • 69. Deterministic pushdown Automata Construct a PDA for language L = {wcw’ | w={0, 1}*} where w’ is the reverse of w L = {1c1, 0c0, 01c10, 001c100, 010c010 ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 69
  • 70. Non Deterministic pushdown Automata Design a non deterministic PDA for accepting the language L = {wwR w ∈ (a, b)*}, i.e., L = {aa, bb, abba, aabbaa, abaaba, ......} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 70
  • 73. PDA–Applications ● For designing the parsing phase of a compiler (Syntax Analysis). ● For implementation of stack applications. ● For evaluating the arithmetic expressions. ● For solving the Tower of Hanoi Problem. Mrs.D.Jena Catherine Bel,AP/CSE, VEC 73
  • 74. S.NO PUSHDOWN AUTOMATA FINITE AUTOMATA 1. For Type-2 grammar we can design pushdown automata. For Type-3 grammar we can design finite automata. 2. Non – Deterministic pushdown automata has more powerful than Deterministic pushdown automata. Non-Deterministic Finite Automata has same powers as in Deterministic Finite Automata. 3. Not every Non- Deterministic pushdown automata is transformed into its equivalent Deterministic pushdown Every Non-Deterministic Finite Automata is transformed into an equivalent Deterministic Finite Automata Mrs.D.Jena Catherine Bel,AP/CSE, VEC 74
  • 75. Construct a PDA Construct a PDA for language L = {0n1m| n >= 1, m >= 1, m > n+2} ● Step-1: On receiving 0 push it onto stack. On receiving 1, ignore it and goto next state ● Step-2: On receiving 1, ignore it and goto next state ● Step-3: On receiving 1, pop a 0 from top of stack and go to next state ● Step-4: On receiving 1, pop a 0 from top of stack. If stack is empty, on receiving 1 ingore it and goto next state ● Step-5: On receiving 1 ignore it. If input is finished then goto last state Mrs.D.Jena Catherine Bel,AP/CSE, VEC 75
  • 77. Exercises • Construct Pushdown Automata for given languages • L = {0(n+m)1m2n | m, n ≥ 0} • L = {0n1m2(n+m) | m,n ≥ 0} • L = {a(2*m)c(4*n)dnbm | m,n ≥ 0} • L = {0n1m2m3n | m,n ≥ 0} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 77
  • 78. L={0(n+m)1m2n | m,n≥0} Input: NULL String Output: Accepted Input: 0000011122 Output: Accepted Input: 00000112222 Output: Not Accepted Mrs.D.Jena Catherine Bel,AP/CSE, VEC 78
  • 79. L={0n1m2(n+m) | m,n≥0} Input: 00001112222222 Output: Accepted Input: 00011112222 Output: Not Accepted Mrs.D.Jena Catherine Bel,AP/CSE, VEC 79
  • 80. Exercises • NPDA for accepting the language L = {am b(2m) | m>=1} • NPDA for accepting the language L = {ambncn | m,n ≥ 1} • NPDA for accepting the language L = {amb(m+n)cn | m,n ≥ 1} • NPDA for accepting the language L = {am bn cp dq | m+n=p+q ; m,n,p,q>=1} • NPDA for accepting the language L = {a(m+n)bmcn | m,n ≥ 1} • NPDA for accepting the language L = {anb(2n) | n>=1} U {anbn | n>=1} • NPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1} • NPDA for accepting the language L = {anbm | n,m ≥ 1 and n ≠ m} • NPDA for accepting the language L = {amb(2m+1) | m ≥ 1} • NPDA for accepting the language L = {an bn | n>=1} • NPDA for accepting the language L = {a2mb3m | m ≥ 1} • NPDA for accepting the language L = {an bn cm | m,n>=1} • NPDA for accepting the language L = {ambnc(m+n) | m,n ≥ 1} • NPDA for accepting the language L = {wwR | w ∈ (a,b)*} • DFA for accepting the language L = {an bm | n+m=odd} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 80
  • 87. Definition Alphabet ● Definition: An alphabet is any finite set of symbols., ● Example: Σ = {a, b, c, d} is an alphabet set where ‘a’, ‘b’, ‘c’, and ‘d’ are symbols., String 1. Definition: A string is a finite sequence of symbols taken from Σ., 2. Example: ‘cabcad’ is a valid string on the alphabet set Σ = {a, b, c, d} Length of a String 1. Definition : It is the number of symbols present in a string. (Denoted by |S|)., 2. Examples:, 3. If S=‘cabcad’, |S|= 6 If |S|= 0, it is called an empty string (Denoted by λ or ε) Kleene Star 1. Definition: The Kleene star, Σ* , is a unary operator on a set of symbols or strings, Σ, that gives the infinite set of all possible strings of all possible lengths over Σ including λ. 2. Representation: Σ* = Σ0 U Σ1 U Σ2 U……. where Σp is the set of all possible strings of length p. 3. Example: If Σ = {a, b}, Σ*= {λ, a, b, aa, ab, ba, bb,………..} 4. Kleene Closure / Plus 1. Definition: The set Σ+ is the infinite set of all possible strings of all possible lengths over Σ excluding λ. 2. Representation: Σ+ = Σ1 U Σ2 U Σ3 U……. Σ+ = Σ* − { λ } 3. Example: If Σ = { a, b } , Σ+ ={ a, b, aa, ab, ba, bb,………..} 5. Language 1. Definition : A language is a subset of Σ* for some alphabet Σ. It can be finite or infinite. 2. Example : If the language takes all possible strings of length 2 over Σ = {a, b}, then L = { ab, bb, ba, bb} Mrs.D.Jena Catherine Bel,AP/CSE, VEC 87