Unit Iii Context-Free Grammar and Languages: 3.1.1. Definition
Unit Iii Context-Free Grammar and Languages: 3.1.1. Definition
Unit Iii Context-Free Grammar and Languages: 3.1.1. Definition
UNIT III
CONTEXT-FREE GRAMMAR AND LANGUAGES
Context-Free Grammar (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.
Ex.1:
A CFG, G = (V, T, P, S) whose productions are given by,
A → Ba
B→b
Soln:
A → Ba
→ ba ∴ which produces the string ba.
PROBLEMS:
Ex.1:
Obtain the derivation for L, with production for the string 01c10.
E→c
E → 0E0
E → 1E1
Soln:
E → 0E0
→ 01E10 [ E → 1E1 ]
→ 01c10 [E→c]
Ex.2:
Obtain the derivation for L, with production for the strings aaababbb and
abbaab.
S → aSb | aAb
A → bAa | ba
61
Soln:
(i) S → aSb
→ aaSbb [ S → aSb ]
→ aaaAbbb [ S → aAb ]
→ aaababbb [ A → ba ]
(ii) S → aAb
→ abAab [ A → bAa ]
→ abbaab [ A → ba ]
Ex.1:
Let G = ( V, T, P, S ), V = {E}, T = {+, *, id}, S = E, P is given by,
E → E + E | E * E | id
Construct leftmost derivation for id+id*id.
Soln:
lm
E ⇒ E+E
⇒ id+E [E → id]
⇒ id+E*E [E → E*E]
⇒ id+id*E [E → id]
⇒ id+id*id [E → id]
Rightmost Derivation:
If at each step in a derivation a production is applied to the rightmost
variable, then it is called rightmost derivation.
Ex.1:
Let G = ( V, T, P, S ), V = {E}, T = {+, *, id}, S = E, P is given by,
E → E + E | E * E | id
Construct rightmost derivation for id+id*id.
Soln:
62
rm
E ⇒ E*E
⇒ E*id [E → id]
⇒ E+E*id [E → E+E]
⇒ E+id*id [E → id]
⇒ id+id*id [E → id]
Ex.1:
Construct a parse tree for the grammar, E → E + E | E * E | (E) | id, for
the string id*id+id
Soln:
Derivation: Parse Tree:
Ex.2:
Construct a parse tree for the grammar, P → ε | 0 | 1 | 0P0 | 1P1, A parse
tree showing the derivation of a string 0110.
63
Soln:
Derivation: Parse Tree:
Ex.1:
For the grammar G is defined by the production,
S→A|B
A → 0A | ε
B → 0B | 1B | ε
Find the parse tree for the yields (i) 1001 (ii) 00101
Soln:
(i) 1001 (ii) 00101
Derivation: Derivation:
S⇒ B S⇒ B
⇒ 1B [B→1B] ⇒ 0B [B→0B]
⇒ 10B [B→0B] ⇒ 00B [B→0B]
⇒ 100B [B→0B] ⇒ 001B [B→1B]
⇒ 1001B [B→1B] ⇒ 0010B [B→0B]
⇒ 1001 [B→ε] ⇒ 00101B [B→1B]
⇒ 00101 [B→ε]
64
Proof:
Suppose there is a parse tree with root S and yield α, then there is a
leftmost derivation,
*
S ⇒ α in G.
lm
*
To prove, S ⇒ α in G. Let us prove this theorem by induction on height of the
lm
tree.
Basis:
If the height of parse tree is ‘1’ then the tree must be of the form given in
figure below with root ‘S’ and yield ‘α’.
This means that ‘S’ has only leaves and no subtrees. This is possible
only with the production.
S ⇒ α in G
S ⇒ α is an one step leftmost derivation.
lm
65
Induction:
If the height of the parse tree is in the parse tree must look like in.
α = α1α2 ……… αk. Where X1, X2, ….. Xk are all the subtrees of S.
*
Assume there exist a leftmost derivation S ⇒ α for every parse tree of height
less than ‘n’. Consider a parse tree of height ‘n’. Let the leftmost derivation be,
*
S ⇒ X1X2, ….. Xk
lm
S ⇒ X1X2, ….. Xk
lm
Therefore,
*
S ⇒ α1α2 ……… αi-1 Xi Xi+1 ….. Xk
lm
*
⇒ α1α2 ……… αi-1 w1 Xi+1 ….. Xk
lm
*
⇒ α1α2 ……… αi-1 w2 Xi+1 ….. Xk
lm
66
*
⇒ α1α2 ……… αi-1 αi Xi+1 ….. Xk
lm
Thus proved.
PROBLEMS:
Ex.1:
Construct ambiguous grammar for the grammar, E → E + E | E * E | (E) |
id, and generate a string id+id*id.
Soln:
Derivation1: Derivation2:
E ⇒ E+E E ⇒ E*E
⇒ id+E [E → id] ⇒ E+E*E [E → E+E]
⇒ id+E*E [E → E*E] ⇒ id*E+E [E → id]
⇒ id+id*E [E → id] ⇒ id*id+E [E → id]
⇒ id+id*id [E → id] ⇒ id*id+id [E → id]
67
Ex.2:
Construct ambiguous grammar for the grammar,
E → I | E+E | E*E | (E)
I → a | b | Ia | Ib | I0 | I1
and generate a string a+a*a.
Soln:
Derivation1: Derivation2:
E ⇒ E+E E ⇒ E*E
⇒ I+E [E → I] ⇒ E*I [E → I]
⇒ a+E [I → a] ⇒ E*a [I → a]
⇒ a+E*E [E → E*E] ⇒ E+E*a [E → E+E]
⇒ a+I*E [E → I] ⇒ E+I*a [E → I]
⇒ a+a*E [I → a] ⇒ E+a*a [I → a]
⇒ a+a*I [E → I] ⇒ I+a*a [E → I]
⇒ a+a*a [I → a] ⇒ a+a*a [I → a]
68
3.3.2. Unambiguous:
If each string has atmost one parse tree in the grammar, then the
grammar is unambiguous.
Ex:
Construct ambiguous grammar for the grammar,
E → I | E+E | E*E | (E)
I → a | b | Ia | Ib | I0 | I1
and generate two leftmost derivation for a string a+a*a.
Soln:
Derivation1: Derivation2:
rm rm
E ⇒ E+E E ⇒ E*E
⇒ I+E [E → I] ⇒ E+E*E [E →
E+E]
⇒ a+E [I → a] ⇒ I+E*E [E → I]
⇒ a+E*E [E → E*E] ⇒ a+E*E [I → a]
⇒ a+I*E [E → I] ⇒ a+I*E [E → I]
⇒ a+a*E [I → a] ⇒ a+a*E [I → a]
⇒ a+a*I [E → I] ⇒ a+a*I [E → I]
⇒ a+a*a [I → a] ⇒ a+a*a [I → a]
69
Ex:
Show that the language is inherent ambiguous L = {anbncmdm | n≥1,
m≥1} ∪ { anbmcmdn | n≥1, m≥1} then the production P is given by,
S → AB | C
A → aAb |ab C → aCd |aDd
B → cBd | cd D → bDc | bc
Soln:
L is a context free language. It separate sets of productions to generate
two kinds of strings in L. This grammar is ambiguous For ex, the string
aabbccdd has two leftmost derivations.
Derivation1: Derivation2:
rm rm
S ⇒ AB S⇒ C
⇒ aAbB ⇒ aCd
⇒ aabbB ⇒ aaDdd
⇒ aabbcBd ⇒ aabDcdd
⇒ aabbccdd ⇒ aabbccdd
Ex:
Mathematical model of a PDA for the language, L = {wwR | w is in
(0+1)* }, then PDA for L can be described as, P = ({q0, q1, q2}, {0, 1}, {0, 1, Z0},
δ, q0, Z0, {q2}), where δ is defined by the following rules;
1. δ(q0, 0, Z0) = {(q0, 0Z0)} and δ(q0, 1, Z0) = {(q0, 1Z0)}. One of these
rules applies initially, when we are in state q0 and we see the start symbol
Z0 at the top of the stack. We read the first input, and push it onto the
stack, leaving Z0 below to mark the bottom.
2. δ(q0, 0, 0) = {(q0, 00)}, δ(q0, 0, 1) = {(q0, 01)}, δ(q0, 1, 0) = {(q0, 10)}
and δ(q0, 1, 1) = {(q0, 11)}. These four similar rules allow us to stay in
state q0 and read inputs, pushing each onto the top of the stack and
leaving the pervious top stack symbol alone.
3. δ(q0, ε, Z0) = {(q1, Z0)}, δ(q0, ε, 0) = {(q1, 0)}, and δ(q0, ε, 1) = {(q1, 1)}.
These three rules allow P to go from state q0 to state q1 spontaneously
(on ε input), leaving intact whatever symbol is at the top of the stack.
4. δ(q1, 0, 0) = {(q1, ε)}, and δ(q1, 1, 1) = {(q1, ε)}. Now in state q1we can
match input symbols against the top symbols on the stack, and pop when
the symbols match.
5. δ(q1, ε, Z0) = {(q2, Z0)}. Finally, if we expose the bottom-of-stack
marker Z0 and we are in state q1, then we have found an input of the form
wwR. We go to state q2 and accept.
PROBLEMS:
Ex.1:
Construct PDA on the input strings 001010c010100, 001010c011100.
PDA can be described as, P = ({q1, q2}, {0, 1, c}, {R, B, G}, δ, q1, R, {q2}),
where δ is,
δ(q1, 0, R) = (q1, BR) δ(q1, c, R) = (q2, R)
δ(q1, 1, R) = (q1, GR) δ(q1, c, B) = (q2, B)
δ(q1, 0, B) = (q1, BB) δ(q1, c, G) = (q2, G)
δ(q1, 1, B) = (q1, GB) δ(q2, 0, B) = (q2, ε)
δ(q1, 0, G) = (q1, BG) δ(q2, 1, G) = (q2, ε)
73
• w2 = 001010c011100
(q1, 001010c011100, R) ├ (q1, 01010c011100, BR)
├ (q1, 1010c011100, BBR)
├ (q1, 010c011100, GBBR)
├ (q1, 10c011100, BGBBR)
├ (q1, 0c011100, GBGBBR)
├ (q1, c011100, BGBGBBR)
├ (q2, 011100, BGBGBBR)
├ (q2, 11100, GBGBBR)
├ (q2, 1100, BGBBR)
∴ There is no transition for (q2, 1, B). So the string is not accepted.
We must show that ‘w’ is in L(PF) if and only if ‘w’ is in N(PN). The
moves of the PDA PF to accept a string ‘w’ can be written as,
(P0, w, X0) ├ (q0, w, Z0X0) ├* (q0, ε, X0) ├ (Pf, ε, ε).
PF PF PF
Thus PF accepts ‘w’ by final state.
Suppose (Q0, w, Z0) ├* (q, ε, α) for some accepting state ‘q’ and stack
PF
string α. Then PN can do the following:
(P0, w, X0) ├ (q0, w, Z0X0) ├* (q, ε, αX0) ├* (P, ε, ε).
PN PN PN
PROBLEMS:
Ex.1:
Construct a PDA that accepts the given language, L = {xmyn | n<m}.
Soln:
Language L accepted by the strings are, L = {xxy, xxxy, xxxyy,
xxxxyy,……}
First find the grammar for that language. The grammar for the language
can be,
76
S → xSy | xS | x
The corresponding PDA for the above grammar is,
P = (Q, Σ, Γ, δ, q0, Z0, F)
where, Q = {q}
Σ = {x, y}
Γ = {S, x, y}
q0 = {q}
Z0 = {S}
F =ф
and δ is defined as,
δ(q, ε, S) = {(q, xSy), (q, xS), (q, x)}
δ(q, x, x) = (q, ε)
δ(q, y, y) = (q, ε)
To prove the string xxxyy is accepted by PDA,
(q, xxxyy, S) ├ (q, xxxyy, xSy) ├ (q, xxyy, Sy) ├ (q, xxyy, xSyy) ├ (q, xyy,
Syy)
├ (q, xyy, xyy) ├ (q, yy, yy) ├ (q, y, y) ├ (q, ε, ε)
Hence the string is accepted.
Ex.2:
Construct a PDA that accepts the given language, L = {0n1n | n≥1}.
Soln:
Language L accepted by the strings are, L = {01, 0011, 000111,
00001111,……}
First find the grammar for that language. The grammar for the language
can be,
S → 0S1 | 0A1
A → 01 | ε
The corresponding PDA for the above grammar is,
P = (Q, Σ, Γ, δ, q0, Z0, F)
where, Q = {q}
Σ = {0, 1}
Γ = {S, A, 0, 1}
q0 = {q}
Z0 = {S}
F =ф
and δ is defined as,
77
Γ = {S, a, b}
q0 = {q}
Z0 = {S}
F =ф
and δ is defined as,
δ(q, ε, S) = {(q, aSbb), (q, abb)}
δ(q, a, a) = (q, ε)
δ(q, b, b) = (q, ε)
Ex.2:
Construct a PDA equivalent to the CFG,
S → aABB | aAA
A → aBB | a
B → bBB |A
Soln:
PDA – P is defined as follows:
P = (Q, Σ, Γ, δ, q0, Z0, F)
Where, Q = {q}
Σ = {a, b}
Γ = {S, A, B, a, b}
q0 = {q}
Z0 = {S}
F =ф
and δ is defined as,
δ(q, ε, S) = {(q, aABB), (q, aAA)}
δ(q, ε, A) = {(q, aBB), (q, a)}
δ(q, ε, B) = {(q, bBB), (q, A)}
δ(q, a, a) = (q, ε)
δ(q, b, b) = (q, ε)
79
Ex.3:
Construct a PDA equivalent to the CFG,
E → I | E+E | E*E | (E)
I → a | b | Ia | Ib | I0 | I1
Soln:
PDA – P is defined as follows:
P = (Q, Σ, Γ, δ, q0, Z0, F)
Where, Q = {q}
Σ = {a, b, 0, 1, +, *, (, )}
Γ = {E, I, B, a, b, 0, 1, +, *, (, )}
q0 = {q}
Z0 = {E}
F =ф
and δ is defined as,
δ(q, ε, E) = {(q, I), (q, E+E), (q, E*E), (q, (E))}
δ(q, ε, I) = {(q, a), (q, b), (q, 0), (q, 1), (q, +), (q, *), (q, ( ), (q, ))}
δ(q, a, a) = (q, ε)
δ(q, b, b) = (q, ε)
δ(q, 0, 0) = (q, ε)
δ(q, 1, 1) = (q, ε)
δ(q, +, +) = (q, ε)
δ(q, *, *) = (q, ε)
δ(q, (, ( ) = (q, ε)
δ(q, ), ) ) = (q, ε)
*
In CFG, [qj, Bj, qj+1] ⇒ yj
The original move,
(q, ay, A) ├ (q1, y, B1B2……Bn)
(q, ay1y2 ……. yn, A) ├ (q1, y1y2……yn, B1B2……..Bn)
CFG is,
[q, A, p] ⇒ a[q1, B1, q2] [q2, B2, q3] …….. [qn, Bn, qn+1]
*
[q, A, p] ⇒ ay1y2 ……. yn
*
[q, A, p] ⇒ ay
*
[q, A, p] ⇒ x iff (q, x, A) ├* (p, ε, ε), where qn+1 = p.
PROBLEMS:
Ex.1:
Construct a CFG for the PDA, P = ({q0, q1}, {0, 1}, {S, A}, δ, q0, S,
{q1}), where δ is,
δ(q0, 1, S) = {(q0, AS)} δ(q0, 0, A) = {(q1, A)}
δ(q0, ε, S) = {(q0, ε)} δ(q1, 1, A) = {(q1, ε)}
δ(q0, 1, A) = {(q0, AA) δ(q1, 0, S) = {(q0, S)}
Soln:
82
Since [q1, A, q0], [q1, A, q1] does not have any productions we can leave them.
After eliminating the unwanted productions,
S → [q0, S, q0]
83
Ex.2:
Construct a CFG for the PDA, P = ({q0, q1}, {0, 1}, {X, Z0}, δ, q0, Z0,
{q1}), where δ is,
δ(q0, 0, Z0) = {(q0, XZ0)} δ(q1, 1, X) = {(q1, ε)}
δ(q0, 0, X) = {(q0, XX)} δ(q1, ε, X) = {(q1, ε)}
δ(q0, 1, X) = {(q1, ε) δ(q1, ε, Z0) = {(q1, ε)}
Soln:
CFG, G is defined as, G = (V, T, P, S)
Where, V = {[q0, X, q0], [q0, X, q1], [q1, X, q0], [q1, X, q1], [q0, Z0, q0],
[q0, Z0, q1],
[q1, Z0, q0], [q1, Z0, q1]}
T = {0, 1}
S = {S} [Start stack symbol]
To find production, P;
(1) Production for S,
S → [q0, Z0, q0]
S → [q0, Z0, q1] [q0 – Start state, Z0 – Initial stack
symbol]
Ex.3:
Construct a CFG for the PDA, P = ({q0, q1}, {0, 1}, {X, Z0}, δ, q0, Z0,
{q1}), where δ is,
δ(q0, b, Z0) = {(q0, ZZ0)} δ(q0, ε, Z0) = {(q0, ε)}
δ(q0, b, Z) = {(q0, ZZ)} δ(q0, a, Z) = {(q1, Z)}
δ(q1, b, Z) = {(q1, ε) δ(q1, a, Z0) = {(q0, Z0)}
Soln:
CFG, G is defined as, G = (V, T, P, S)
Where, V = {[q0, Z0, q0], [q0, Z0, q1], [q1, Z0, q0], [q1, Z0, q1], [q0, Z, q0],
[q0, Z, q1],
[q1, Z, q0], [q1, Z, q1]}
T = {a, b}
S = {S} [Start stack symbol]
To find production, P;
(1) Production for S,
S → [q0, Z0, q0]
S → [q0, Z0, q1] [q0 – Start state, Z0 – Initial stack
symbol]
For q1, [q0, Z0, q1] → b[q0, Z, q0] [q0, Z0, q1]
[q0, Z0, q1] → b[q0, Z, q1] [q1, Z0, q1]