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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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