UNIT III
CONTEXT FREE GRAMMAR &
LANGUAGE
UNIT III
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
3
CONTEXT FREE GRAMMAR
• A CFG is a way of describing languages by recursive rules
called productions
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.
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
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
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
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
9
rightmost derivation –
example contd..,
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
11
Tree representation
12
leftmost derivation – example
contd..,
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.
14
15
Why PDA
Design FA for accepting a language {an
| n>=1}
16
Why PDA
Design FA for accepting a language {an
| n>=1}
L = {a, aa, aaa, aaaa, ......}
17
Why PDA
Design FA for accepting a language {an
| n>=1}
L = {a, aa aaa, aaaa, ......}
18
Why PDA
Design FA for accepting a language {an
bm
| m,n>=0}
19
Why PDA
Design FA for accepting a language {an
bm
| 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
20
Why PDA
Design FA for accepting a language {an
bm
| m,n>=0}
L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb,
aaaabbbb, ......}
21
Why PDA
Design FA for accepting a language {an
| n>=1}
L = {a, aa aaa, aaaa, ......}
Design FA for accepting a language {an
bm
| m,n>=0}
L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb,
aaaabbbb, ......}
22
Why PDA
Design FA for accepting a language {an
bn
| n>=1}
23
why
Design FA for accepting a language {an
bn
| n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
24
why
Design FA for accepting a language {an
bn
| n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >=0
3.
25
why
Design FA for accepting a language {an
bn
| n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >0
3. Number of (a ) == Number of (b)
26
27
Why PDA
is it Possible
Design FA for accepting a language {an
bn
| n>=1}
Design FA for accepting a language {an
b2n
| n>=1}
Design FA for accepting a language {an
bm
cn
|m. n>=1}
28
Pushdown Automata
Schematic PDA 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.
30
Formal Definition of PDA
31
Moves of PDA And Types
32
Pushdown Automata Acceptance
33
Pushdown Automata Acceptance
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
35
Representation of State Transition
36
Representation of State Transition
Representation of Push in a PDA
37
Representation of State Transition
Representation of Pop in a PDA
38
Representation of State Transition
Representation of Ignore in a PDA
39
Moves of PDA
Push
40
Moves of PDA
Pop Skip/ no operation
41
Pushdown Automata
Define the pushdown automata for language {an
bn
|
n>=1}
42
Pushdown Automata
Define the pushdown automata for language {an
bn
| n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >0
3. Number of (a ) == Number of (b)
43
Pushdown Automata
Define the pushdown automata for language {an
bn
| n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
1. While (a)
Push a;
While (b)
Pop ;
44
Pushdown Automata
45
Pushdown Automata
Define the pushdown automata for language {an
bn
| n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
46
Pushdown Automata
Define the pushdown automata for language {an
bn
| n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
1. While (a)
Push a;
While (b)
Pop ;
47
PDA for the Language {an
bn
| 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, ε)
48
Pushdown Automata
2. Design PDA for accepting a language {an
b2n
| n>=1}
49
Pushdown Automata
Problem – Design a PDA for accepting the language L = { an
b2n
| 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)
50
Pushdown Automata
Problem – Design a PDA for accepting the language L = { an
b2n
| n>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
1. While (a)
push(aa);
While(b)
Pop ;
51
Pushdown Automata
Problem – Design a PDA for accepting the language L = { an
b2n
| 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;
52
PDA for the Language {an
b2n
| 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, ε)
53
PDA
54
PDA for the Language {an
b2n
| 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)
55
PDAforacceptinga language {0n
1m
0n
|m,n>=1}
3. Design a PDA for accepting a language {0n
1m
0n
| m, n>=1}.
L = { 010, 0110, 01110, 001100, 0001000, 00001110000, ...... }
56
PDAforacceptinga language {0n
1m
0n
|m,n>=1}
Design a PDA for accepting a language {0n
1m
0n
| 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.
57
PDAforacceptinga language {0n
1m
0n
|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)
58
PDAforacceptinga language {0n
1m
0n
|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
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 = { an
b2n
: 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
60
PushDown Automata
Find the language accepted by PDA
61
PushDown Automata
Find the language accepted by PDA
PDA for accepting the language L = { an
b m+n
cm
|| m,n ≥ 1}
The strings of given lanugage will be: L = {abbc, abbbcc, abbbcc, aabbbbcc, ......}
62
PDA for the Language {an
bn
| 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, ε)
63
PDA for the Language {an
bn
| 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, ε)
64
PushDown Automata
Answer the following
65
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.
67
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 ......}
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, ......}
70
71
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.
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.
Not every Non-
Deterministic pushdown
automata is transformed
Every Non-
Deterministic Finite
Automata is
74
Construct a PDA
Construct a PDA for language L = {0n
1m
| 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
75
Construct a PDA
76
Exercises
• Construct Pushdown Automata for given languages
• L = {0(n+m)
1m
2n
| m, n ≥ 0}
• L = {0n
1m
2(n+m)
| m,n ≥ 0}
• L = {a(2*m)
c(4*n)
dn
bm
| m,n ≥ 0}
• L = {0n
1m
2m
3n
| m,n ≥ 0}
77
L={0(n+m)
1m
2n
|m,n ≥ 0}
Input: NULL String
Output: Accepted
Input: 0000011122
Output: Accepted
Input: 00000112222
Output: Not Accepted
78
L={0n
1m
2(n+m)
|m,n ≥0}
Input: 00001112222222
Output: Accepted
Input: 00011112222
Output: Not Accepted
79
Exercises
• NPDA for accepting the language L = {am
b(2m)
| m>=1}
• NPDA for accepting the language L = {am
bn
cn
| m,n ≥ 1}
• NPDA for accepting the language L = {am
b(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)
bm
cn
| m,n ≥ 1}
• NPDA for accepting the language L = {an
b(2n)
| n>=1} U {an
bn
| n>=1}
• NPDA for accepting the language L = {ai
bj
ck
dl
| i==k or j==l,i>=1,j>=1}
• NPDA for accepting the language L = {an
bm
| n,m ≥ 1 and n ≠ m}
• NPDA for accepting the language L = {am
b(2m+1)
| m ≥ 1}
• NPDA for accepting the language L = {an
bn
| n>=1}
• NPDA for accepting the language L = {a2m
b3m
| m ≥ 1}
• NPDA for accepting the language L = {an
bn
cm
| m,n>=1}
• NPDA for accepting the language L = {am
bn
c(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}
80
Thank YOU
81
82
83
84
85
NFA Vs PDA
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
3. Definition : It is the number of symbols present in a string. (Denoted by |S|).,
4. Examples:,
5. 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,………..}
6. 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,………..}
7. 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}
87

unit 3.pptx-theory of computation unit 3 notes

  • 1.
    UNIT III CONTEXT FREEGRAMMAR & LANGUAGE
  • 2.
    UNIT III CONTEXT FREEGRAMMAR & 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.
  • 4.
    CONTEXT FREE GRAMMAR •A CFG is a way of describing languages by recursive rules called productions 4
  • 5.
    CFG – FormalDefinition 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. 5
  • 6.
    Derivation • Beginning withthe start symbol, derive the terminal strings by repeatedly replacing the variable by the body of some production with that variable in the head 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 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 8
  • 9.
    Parse Tree • Treerepresentation 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 9
  • 10.
  • 11.
    Derivation / yieldof 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 11
  • 12.
  • 13.
    leftmost derivation –example contd.., 13
  • 14.
    Ambiguous grammar • Ifa 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. 14
  • 15.
  • 16.
    Why PDA Design FAfor accepting a language {an | n>=1} 16
  • 17.
    Why PDA Design FAfor accepting a language {an | n>=1} L = {a, aa, aaa, aaaa, ......} 17
  • 18.
    Why PDA Design FAfor accepting a language {an | n>=1} L = {a, aa aaa, aaaa, ......} 18
  • 19.
    Why PDA Design FAfor accepting a language {an bm | m,n>=0} 19
  • 20.
    Why PDA Design FAfor accepting a language {an bm | 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 20
  • 21.
    Why PDA Design FAfor accepting a language {an bm | m,n>=0} L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb, aaaabbbb, ......} 21
  • 22.
    Why PDA Design FAfor accepting a language {an | n>=1} L = {a, aa aaa, aaaa, ......} Design FA for accepting a language {an bm | m,n>=0} L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb, aaaabbbb, ......} 22
  • 23.
    Why PDA Design FAfor accepting a language {an bn | n>=1} 23
  • 24.
    why Design FA foraccepting a language {an bn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} 24
  • 25.
    why Design FA foraccepting a language {an bn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} Constraints/limitations 1. a followed by b 2. N >=0 3. 25
  • 26.
    why Design FA foraccepting a language {an bn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} Constraints/limitations 1. a followed by b 2. N >0 3. Number of (a ) == Number of (b) 26
  • 27.
  • 28.
    Why PDA is itPossible Design FA for accepting a language {an bn | n>=1} Design FA for accepting a language {an b2n | n>=1} Design FA for accepting a language {an bm cn |m. n>=1} 28
  • 29.
  • 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. 30
  • 31.
  • 32.
    Moves of PDAAnd Types 32
  • 33.
  • 34.
  • 35.
    Representation of StateTransition 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 35
  • 36.
  • 37.
    Representation of StateTransition Representation of Push in a PDA 37
  • 38.
    Representation of StateTransition Representation of Pop in a PDA 38
  • 39.
    Representation of StateTransition Representation of Ignore in a PDA 39
  • 40.
  • 41.
    Moves of PDA PopSkip/ no operation 41
  • 42.
    Pushdown Automata Define thepushdown automata for language {an bn | n>=1} 42
  • 43.
    Pushdown Automata Define thepushdown automata for language {an bn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} Constraints/limitations 1. a followed by b 2. N >0 3. Number of (a ) == Number of (b) 43
  • 44.
    Pushdown Automata Define thepushdown automata for language {an bn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} 1. While (a) Push a; While (b) Pop ; 44
  • 45.
  • 46.
    Pushdown Automata Define thepushdown automata for language {an bn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} 46
  • 47.
    Pushdown Automata Define thepushdown automata for language {an bn | n>=1} L = {ab, aabb, aaabbb, aaaabbbb, ......} 1. While (a) Push a; While (b) Pop ; 47
  • 48.
    PDA for theLanguage {an bn | 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, ε) 48
  • 49.
    Pushdown Automata 2. DesignPDA for accepting a language {an b2n | n>=1} 49
  • 50.
    Pushdown Automata Problem –Design a PDA for accepting the language L = { an b2n | 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) 50
  • 51.
    Pushdown Automata Problem –Design a PDA for accepting the language L = { an b2n | n>=1}, i.e., L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......} 1. While (a) push(aa); While(b) Pop ; 51
  • 52.
    Pushdown Automata Problem –Design a PDA for accepting the language L = { an b2n | 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; 52
  • 53.
    PDA for theLanguage {an b2n | 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, ε) 53
  • 54.
  • 55.
    PDA for theLanguage {an b2n | 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) 55
  • 56.
    PDAforacceptinga language {0n 1m 0n |m,n>=1} 3.Design a PDA for accepting a language {0n 1m 0n | m, n>=1}. L = { 010, 0110, 01110, 001100, 0001000, 00001110000, ...... } 56
  • 57.
    PDAforacceptinga language {0n 1m 0n |m,n>=1} Designa PDA for accepting a language {0n 1m 0n | 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. 57
  • 58.
    PDAforacceptinga language {0n 1m 0n |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) 58
  • 59.
    PDAforacceptinga language {0n 1m 0n |m,n>=1}. simulatethis 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 59
  • 60.
    Instantaneous Description (ID) AID 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 = { an b2n : 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 60
  • 61.
    PushDown Automata Find thelanguage accepted by PDA 61
  • 62.
    PushDown Automata Find thelanguage accepted by PDA PDA for accepting the language L = { an b m+n cm || m,n ≥ 1} The strings of given lanugage will be: L = {abbc, abbbcc, abbbcc, aabbbbcc, ......} 62
  • 63.
    PDA for theLanguage {an bn | 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, ε) 63
  • 64.
    PDA for theLanguage {an bn | 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, ε) 64
  • 65.
  • 66.
  • 67.
    L={w?{a,b}*} Problem – Designa 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. 67
  • 68.
  • 69.
    Deterministic pushdown Automata Constructa PDA for language L = {wcw’ | w={0, 1}*} where w’ is the reverse of w L = {1c1, 0c0, 01c10, 001c100, 010c010 ......} 69
  • 70.
    Non Deterministic pushdownAutomata Design a non deterministic PDA for accepting the language L = {wwR w ∈ (a, b)*}, i.e., L = {aa, bb, abba, aabbaa, abaaba, ......} 70
  • 71.
  • 72.
  • 73.
    PDA–Applications ● For designingthe parsing phase of a compiler (Syntax Analysis). ● For implementation of stack applications. ● For evaluating the arithmetic expressions. ● For solving the Tower of Hanoi Problem. 73
  • 74.
    S.NO PUSHDOWN AUTOMATAFINITE 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. Not every Non- Deterministic pushdown automata is transformed Every Non- Deterministic Finite Automata is 74
  • 75.
    Construct a PDA Constructa PDA for language L = {0n 1m | 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 75
  • 76.
  • 77.
    Exercises • Construct PushdownAutomata for given languages • L = {0(n+m) 1m 2n | m, n ≥ 0} • L = {0n 1m 2(n+m) | m,n ≥ 0} • L = {a(2*m) c(4*n) dn bm | m,n ≥ 0} • L = {0n 1m 2m 3n | m,n ≥ 0} 77
  • 78.
    L={0(n+m) 1m 2n |m,n ≥ 0} Input:NULL String Output: Accepted Input: 0000011122 Output: Accepted Input: 00000112222 Output: Not Accepted 78
  • 79.
    L={0n 1m 2(n+m) |m,n ≥0} Input: 00001112222222 Output:Accepted Input: 00011112222 Output: Not Accepted 79
  • 80.
    Exercises • NPDA foraccepting the language L = {am b(2m) | m>=1} • NPDA for accepting the language L = {am bn cn | m,n ≥ 1} • NPDA for accepting the language L = {am b(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) bm cn | m,n ≥ 1} • NPDA for accepting the language L = {an b(2n) | n>=1} U {an bn | n>=1} • NPDA for accepting the language L = {ai bj ck dl | i==k or j==l,i>=1,j>=1} • NPDA for accepting the language L = {an bm | n,m ≥ 1 and n ≠ m} • NPDA for accepting the language L = {am b(2m+1) | m ≥ 1} • NPDA for accepting the language L = {an bn | n>=1} • NPDA for accepting the language L = {a2m b3m | m ≥ 1} • NPDA for accepting the language L = {an bn cm | m,n>=1} • NPDA for accepting the language L = {am bn c(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} 80
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
    Definition Alphabet ● Definition: Analphabet 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 3. Definition : It is the number of symbols present in a string. (Denoted by |S|)., 4. Examples:, 5. 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,………..} 6. 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,………..} 7. 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} 87