0% found this document useful (0 votes)
16 views

Unit 4 ContextFreeLanguage

Jhhj

Uploaded by

hub9004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Unit 4 ContextFreeLanguage

Jhhj

Uploaded by

hub9004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

CSO-322 Theory of Computation

Unit-4: Context Free Languages

Dr. Lavanya Selvaganesh

Dr. Lavanya Selvaganesh CSO322 1 / 60


Context Free Grammar (CFG)
A simple recursive method of specifying grammar rules by which
strings in a language can be generated.

Formal Definition
A CFG is a 4-tuple, G = (VN , Σ, S, P), where
VN and Σ : Disjoint sets called set of non-terminal (or variables)
and terminal (or alphabets) symbols.
S : Start symbol
P : Grammar rules or productions, a finite set of formulas of the
form A → α, where A ∈ VN and α ∈ (VN ∪ Σ)∗ .

The language generated by G is

L(G) = {x ∈ G|S ⇒∗G x},

where ⇒∗G is the transitive closure of the relation ” ⇒ ” in the grammar


G, used to derive x from S.
Dr. Lavanya Selvaganesh CSO322 2 / 60
CFG

α ⇒G β means that the string β can be obtained from string α by


replacing some variable that appears on the left side of a
production in G by the corresponding right side.
In general, we write α ⇒∗G β, if α derives β in zero or more steps.
In other words, either α = β or there exists an integer k ≥ 1 and
strings α0 , α1 , . . . , αk with α0 = α and αk = β so that αi ⇒G αi+1
for every i, 0 ≤ i ≤ k − 1.

Context Free Language - CFL


A language L is a CFL, if there is a CFG G so that L = L(G).

L(G) = {x ∈ Σ∗ |S ⇒∗G x}

Dr. Lavanya Selvaganesh CSO322 3 / 60


CFG Examples

1 L = {an bn |n ≥ 0, Σ = {a, b}}


G = (VN = {S}, Σ = {a, b}, S, P = {S → aSb|ϵ})
2 Language "pal" of Palindromes over {a, b}.
▶ ϵ, a, b ∈ pal
▶ For any S ∈ pal, aSa and bSb are in pal.
▶ No other strings in pal.
▶ S → ϵ|a|b|aSa|bSb.
3 Compliment of Palindrome
▶ aAb and bAa are in N, for any A ∈ (a, b)∗
▶ For any S ∈ N, aSa and bSb are in N.
▶ No other strings are in N.
▶ S → aAb|bAa|aSa|bSb
A → ϵ|Aa|Ab.
4 Construct a CFG for L = (011 + 1)∗ (01)∗ .

Dr. Lavanya Selvaganesh CSO322 4 / 60


Exercise I
Construct a CFG for L = {x|n0 (x) = n1 (x)} over {0, 1}, where ni (x) =
number of i’s in the string x.
1 Clearly ϵ ∈ L.
2 Given a string x ∈ L, we can add a ’0’ and a ’1’. We can add a one
at each end to get either ”0x1” or ”1x0”
=⇒ S → ϵ|0S1|1S0.
3 But not every string can be obtained from these productions
because some elements of L begin and end with the same
symbols, that is, 0110, 10001101, . . ..
S → SS, helps to get expressions of the form 0010111100 by
taking 001011 and 1100.
4 Consider, d(x) = n0 (x) − n1 (x), then x ∈ L(G), if d(x) = 0.
We prove by induction the above claim.
5 Induction hypothesis: For k ≥ 0 and for any y with |y | ≤ k and
d(y ) = 0, then y ∈ L(G).

Dr. Lavanya Selvaganesh CSO322 5 / 60


Exercise II
6 Induction step: Let x be a string with |x| = k + 1.
7 If x begins with 0 and ends with 1, then x = 0y 1 for some y
satisfying d(y ) = 0. =⇒ By induction hypothesis, y ∈ L(G).
8 Therefore, S ⇒∗G y , we can derive x with the production S → 0S1
and continue to derive y from the second S.
9 Similarly, if S begins with 1 and ends with 0, S → 1S0 can be
used to begin the derivation.
10 The remaining case is the one in which x begins and ends with
the same symbol. Since d(x) = 0, x has length atleast 2; suppose
x = 0y 0, for some string y .
11 Claim: Let us show that x = wz, w and z are shorter strings that
can be derived from S, i.e., x has a prefix w such that
0 < |w| < |x| and d(w) = 0.
12 Shortest non-null orefeix is 0 and d(0) = 1;
13 Longerst prefix shorter than x is 0y and d(0y ) = −1.
Dr. Lavanya Selvaganesh CSO322 6 / 60
Exercise III

14 Furthermore, d-value of a prefix changes by 1 each time an extra


symbol is added. Therefore, it follows that there must be a prefix
w, longer than 0 and shorter than 0y with d(w) = 0, which proves
the claim
15 Similarly, for x = 1y 1.
16 Therefore, S → SS will be used to derive the required w and z.

Dr. Lavanya Selvaganesh CSO322 7 / 60


Theorem
If L1 and L2 are CFLs, then the languages L1 ∪ L2 , L1 .L2 and L∗1 are
also CFLs.
Proof:
Let us consider two grammars G1 = (V1 , Σ, S1 , P1 ) and
G2 = (V2 , Σ, S2 , P2 ) generating L1 and L2 respectively.
We show how to construct a new CFG for each of these three
cases.

Dr. Lavanya Selvaganesh CSO322 8 / 60


1 Grammar Gu = (Vu , σ, Su , Pu ) generating L1 ∪ L2 .
▶ Let Vu = V1 ∪ V2 ∪ {Su }, where Su is a new symbol not in V1 or V2 .
▶ Pu = P1 ∪ P2 ∪ {Su → S1 |S2 }.
▶ On one hand, if x is in either L1 or L2 , then Su =⇒ ∗ x in the
grammar Gu , because we can start a derivation with either Su → S1
or Su → S2 and continue with the derivation of x in G1 or G2 .

L1 ∪ L2 ⊆ L(Gu ) (1)

▶ On other hand, if x is derivable from Su in Gu , the first step in any


derivation must be Su =⇒ S1 or Su =⇒ S2 . From S1 , all the
productions must be in G1 , because no variables in V2 are involved
and x ∈ L1 . From S2 , x ∈ L2 .

L(Gu ) ⊆ L1 ∪ L2 (2)

▶ From equation (1) and (2), L(Gu ) = L1 ∪ L2 .

Dr. Lavanya Selvaganesh CSO322 9 / 60


2 Grammar Gc = (Vc , Σ, Sc , Pc ) to generate L1 .L2

Vc = V1 ∪ V2 ∪ {Sc }
Pc = P1 ∪ P2 ∪ {Sc → S1 .S2 }

2 Grammar G∗ = (V , Σ, S, P) to generate L∗1

V = V1 ∪ {S} where S ∈
/ V1
P = P1 ∪ {S → S1 S|ϵ}

Corollary
Every regular language is a CFL.

Exercise
Construct L = {0i 1j 0k : j > i + k }.

Dr. Lavanya Selvaganesh CSO322 10 / 60


Derivation Trees or Parse Trees

A natural way to exhibit the structure of a derivation.


Root of Tree: variable with which the derivation begins.
Interior Nodes: variables that appear in derivation.
Children of a Node A represent symbols in a string α for which the
production A → α is used in the derivation.
Leaf Nodes: Labeled using terminal or empty symbols.
Yield of the tree is the string of terminals obtained by concatenating,
left to right, the labels of leaves.

Dr. Lavanya Selvaganesh CSO322 11 / 60


Example
Language of Algebraic Expressions:
S → S + S|S − S|S ∗ S|S/S|(S)|a
Consider the derivation of algebraic expression: a+(a*a)
S =⇒ S + S =⇒ a + S =⇒ a + (S) =⇒ a + (S ∗ S) =⇒
a + (a ∗ S) =⇒ a + (a ∗ a)

In general, any derivation of a string in a CFL has a corresponding


derivation tree (exactly one).
Dr. Lavanya Selvaganesh CSO322 12 / 60
Notations of Parse/Derivation Tree
1 Figure (a) is a parse tree for each α ∈ Σ. The single node of this
parse tree is both the root and leaf. Yield of this tree is ′ α′ .
2 If A → ϵ is a rule in P, then Figure (b) is a parse tree; its root is the
node labelled A, its only leaf is the node labelled ϵ. Yield is ’ϵ’.
A
A
A1 A2 An A1 A2 An
α ϵ T1 T2 . . . Tn T1 T2 . . . Tn
(a) (b) (c) Trees (d)

3 If (c) are parse trees with the roots labelled at A1 , A2 , . . . , An with


yields y1 , y2 , . . . , yn and A → A1 A2 · · · An is a rule in P, then (d) ia a
parse tree with new root A; its leaves are the leaves of the trees
T1 , T2 , . . . , Tn , its yield is y1 y2 · · · yn .
4 Nothing else is a parse tree.

Dr. Lavanya Selvaganesh CSO322 13 / 60


Parse Trees
It is a way of representing derivations of strings in L(G) so that the
superficial differences between derivations owing to the order of
application of rules are suppressed i.e. Parse trees represent
equivalence classes of derivations.
Let G = (V , Σ, S, P) be a CFG. Let
D = x1 =⇒ x2 =⇒ ... =⇒ xn and
′ ′ ′ ′
D = x1 =⇒ x2 =⇒ ... =⇒ xn

be two derivations in G, where xi , xi ∈ (V ∪ Σ)∗ , 1 < i < n,
′ ′
x1 , x1 ∈ V and xn , xn ∈ Σ∗ .
Define D precedes D’, denoted by D ≺ D ′ , if n > 2 and there is
an integer k , 1 < k < n, such that
▶ for all i ̸= k , we have xi = xi′ .
▶ xk −1 = xk′ −1 = uAvBw, where u, v , w ∈ (V ∪ Σ)∗ , A, B ∈ V .
▶ xk = uyvBw, where A → y ∈ P.
▶ xk′ = uAvzw, where B → z ∈ P.
▶ xk +1 = xk′ +1 = uyvzw.

Dr. Lavanya Selvaganesh CSO322 14 / 60


Example- Parse Tree Derivation

Grammar for generating Balanced Parenthesis

G = ({S}, Σ = {(, )}, S, P = {S → ϵ|SS|(S)})

1 S =⇒ SS =⇒ S(S) =⇒ S((S)) =⇒ S(()) =⇒ (S)(()) =⇒


()(())
2 S =⇒ SS =⇒ (S)S =⇒ ()S =⇒ ()(S) =⇒ ()((S)) =⇒
()(())
Both derivations have same strings but have different derivation due to
the order of application of rules.

Dr. Lavanya Selvaganesh CSO322 15 / 60


Example- Parse Tree Derivation
1 D1 = S =⇒ SS =⇒ (S)S =⇒ ((S))S =⇒ (())S =⇒
(())(S) =⇒ (())()
2 D2 = S =⇒ SS =⇒ (S)S =⇒ ((S))S =⇒ ((S))(S) =⇒
(())(S) =⇒ (())()
3 D3 = S =⇒ SS =⇒ (S)S =⇒ ((S))S =⇒ ((S))(S) =⇒
((S))() =⇒ (())()
Here, D1 ≺ D2 and D2 ≺ D3 , but D1 ̸≺ D3 , since D1 and D3 differ in
more than one intermediate string, but they all have same parse trees.

S
S S
( S ) ( S )
( S ) ϵ
ϵ

Dr. Lavanya Selvaganesh CSO322 16 / 60


Two derivations are said to be similar if the pair (D,D’) belongs in the
reflexive, symmetric and transitive closure of ≺. Hence, similarity is
an equivalence relationship.
Each equivalence class of derivation under similarity (each parse
tree) contains a derivation that is maximal under ≺; i.e. it is not
preceded by any other derivation. This derivation is called a
leftmost derivation.
A leftmost derivation exists in every parse tree:
Starting from the label of the root A, repeatedly replace the
leftmost non-terminal in the current string according to the rule
suggested by the Parse tree.
Similarly, a rightmost derivation is one that does not precede
any other derivation, i.e. it is obtained from the parse tree by
always expanding the right most non-terminal in the current string.
Each parse tree has exactly one leftmost derivation and exactly one
rightmost derivation.

Dr. Lavanya Selvaganesh CSO322 17 / 60


Examples

1 Construct 0100110 from the Grammar given below, using the


leftmost and rightmost derivations:
S → 0S|1AA, A → 0|1A|0B, B → 1|0BB
2 Construct abbbb from the Grammar given below, using the
leftmost and rightmost derivations:
S → aAB, A → bBb, B → A|ϵ

Dr. Lavanya Selvaganesh CSO322 18 / 60


Dr. Lavanya Selvaganesh CSO322 19 / 60
We write
L
x =⇒ y if and only if x = wAβ and y = wαβ,

where w ∈ Σ∗ , A ∈ V , α, β ∈ (V ∪ Σ)∗ and A → α is a production of G.


Thus, if x1 ⇒ x2 ⇒ · · · ⇒ xn is a leftmost derivation, then
L L L
x1 =⇒ x2 =⇒ · · · =⇒ xn
R
Rightmost derivation is denoted by x =⇒ y

Theorem
Let G = (V , Σ, S, P) be a context-free grammar and let A ∈ V and
w ∈ Σ∗ . Then th following statements are equivalent:
1 A ⇒∗ w
2 There exists a parse tree with root A and yield w.
L
3 There is a leftmost derivation A ⇒∗ w.
R
4 There is a rightmost derivation A ⇒∗ w.

Dr. Lavanya Selvaganesh CSO322 20 / 60


Ambiguity in Grammar

Ambiguous CFG
A CFG is called ambiguous, if there is atleast one string in L(G) having
two or more distinct derivation trees (or equivalently two or more
distinct leftmost derivations.)

For example:
Consider a + a ∗ a in the grammar of algebraic expressions.

S ⇒S+S ⇒a+S ⇒a+S∗S ⇒a+a∗S ⇒a+a∗a


or

S ⇒S∗S ⇒S+S∗S ⇒a+S∗S ⇒a+a∗S ⇒a+a∗a

Dr. Lavanya Selvaganesh CSO322 21 / 60


S
C
S a C d
S + S a D d
a D d b D c
b D c b c

Dr. Lavanya Selvaganesh CSO322 22 / 60


Example

Consider

L = {an bn c m d m | n ⩾ 1, m ⩾ 1} ∪ {an bm c m d n | n ≥ 1, m ≥ 1} .

L has a context-free grammar G = (V , Σ, S, P) where


P = {S → AB | C, A → aAb | ab, B → cBd | cd,
C → aCd | aDd, D → bDC | bc}
This grammar is ambiguous.
For instance:
w = aabbccdd
L L L L L
(i) S =⇒ AB =⇒ aAbB =⇒ aabbB =⇒ aabbcBd =⇒ aabbccdd
L L L L L
(ii) S =⇒ C =⇒ aCd =⇒ aaDdd =⇒ aabDcdd =⇒ aabbccdd

Dr. Lavanya Selvaganesh CSO322 23 / 60


For instance: aabbccdd
(i) S ⇒L AB ⇒L aAbB ⇒L aabbB ⇒L aabbcBd ⇒L aabbccdd
(ii) S ⇒L C ⇒ aCd ⇒L aaDdd ⇒ aabDcdd ⇒L aabbccdd

S
C
S a C d
A B a D d
a A b c B d b D c
a b c d b c

Since the are two distinct leftmost derivations for the same string, we
can see that all grammars for L must be ambiguous. Any string with
equal number of a, b, c and d can be generated by two different ways:
One in which the a’s and b’s are generated to be equal and the c ’s
and d ’s are generated to be equal, while in a second way, where the a
’s and d ’s are generated to be equal and likewise the b’s and c’s.
Dr. Lavanya Selvaganesh CSO322 24 / 60
An unambiguous cfg for algebraic expressions
Theorem:
For each grammar G = (V , Σ, S, P) and string in w ∈ Σ∗ , w has two
distinct parse trees if and only if w has two leftmost derivations from S.

Consider:
G1 : S1 → S1 + T |T
T →T ∗F |F
F → (S1 ) | a.

Theorem:
Let G be a context-free grammar with productions
S → S + S | S ∗ S | (S) | a and let G1 be the grammar as above. Then
L(G) = L(G1 ).

The above result claims that both the grammar are equivalent.
To prove this, we first show L(G) ⊆ L (G1 ) and next L (G1 ) ⊆ L(G).
Dr. Lavanya Selvaganesh CSO322 25 / 60
Proof I
Claim 1: To show L(G) ⊆ L (G1 ).
Proof is by induction on the length of a string in L (G1 ). The basis step
is to show that a ∈ L(G), which is true.
In the induction step, we assume that k ≥ 1 and that every y ∈ L (G1 ),
satisfying |y | ≤ k is in L(G).
To show: x ∈ L (G1 ) and |x| = k + 1, then x ∈ L(G).
Since x ̸= a, any derivation of x ∈ G1 must begin in one of the three
ways:
S1 ⇒ S1 + T
S1 ⇒ T ⇒ T ∗ F
S1 ⇒ T ⇒ F ⇒ (S1 )
If x has a derivation beginning S1 ⇒ S1 + T , then x = y + z, where
S1 ⇒∗G1 y and T ⇒∗G1 z. Since S1 ⇒∗G1 T , it follows that S1 ⇒∗G1 z.
Therefore, since |y | and |z| must be ≤ k , the induction hypothesis
implies that y and z are both in L(G). Since G contains the production
S −→ S + S, the string y + z is derivable from S in G, and hence
x ∈ L(G).
Dr. Lavanya Selvaganesh CSO322 26 / 60
Proof II
Claim 2: To show that L(G) ⊆ L(G1 ).
Induction on |x|: Let k ≥ 1 and that for every y ∈ L(G) with |y | ≤ k ,
y ∈ L(G1 );
To show that if x ∈ L(G) and |x| = k + 1, then x ∈ L(G1 ).
Simplest case is when x has a derivation in G beginning S → (S).
In this case, x = (y ), for some y in L(G) and it follows from the
inductive hypothesis that y ∈ L (G1 ).
Therefore, we can derive x in G1 , by the derivation
S1 ⇒ T ⇒ F ⇒ (S1 ) and then derive y from S1
1 Suppose x has a derivation in G that begins S → S + S. Then just
as before, the induction hypothesis tells that x = y + z, where y
and z are both in L(G1 ).
Now to conclude x ∈ L(G1 ), we need z to be derivable from T ;
In other words, we would like z to be a single term, the last of the
terms whose sum is x. Let x = x1 + x2 + · · · + xn , where each
xi ∈ L(G1 ) and n ≥ 2 is as large as possible.
Dr. Lavanya Selvaganesh CSO322 27 / 60
Proof III
Hence, none of the xi ’s can have a derivation in G1 that begins
S1 ⇒ S1 + T ; therefore, every xi can be derived from T in G1 . Let
y = x1 + x2 + · · · + xn−1 , z = xn .
Then y can be derived from S1 , since S1 ⇒∗G1 T + · · · + T and z can
| {z }
(n−1terms)
be derived from T .
It follows that x ∈ L (G1 ), since we can start with the productions
S1 → S1 + T ,
2 Finally, suppose that every derivation of x in G begins with
S ⇒ S ∗ S. Then for some y and z in L(G), x = y ∗ z. This time
we let x = x1 ∗ x2 ∗ . . . ∗ xn , where each xi ∈ L(G) and n is as large
as possible. Then by the inductive hypothesis, each xi ∈ L (G1 ).
2 Suppose xi has a derivation in G1 , that begins S1 ⇒ T ⇒ T ∗ F . If
this were true, xi would be of the form yi ∗ zi for some
yi , zi ∈ L (G1 ), since we know α (G1 ) ⊆ L(G), this would contradict
the maximality of the number.
Dr. Lavanya Selvaganesh CSO322 28 / 60
Suppose that some xi had a derivation in G1 , beginning S1 ⇒ S1 + T .
Then xi = yi + zi , for some yi , zi ∈ L (G1 ) ⊆ L(G). In this case,

x = x1 ∗ x2 ∗ . . . xi−1 ∗ yi + zi ∗ xi+1 ∗ . . . ∗ xn

This is impossible too.


If we let u and v be the substrings before and after the + , respectively,
we have u, v ∈ L(G), and hence x = u + v . This means we could
derive x in G using a derivation that begins S ⇒ S + S, and we have
assumed that this is not the case.
∴ We may conclude that each xi is derivable from F in G1 . Then as in
the previous case, we let y = x1 ∗ x2 ∗ . . . ∗ xn−1 , z = xn .
The string y is derivable from T in G1 , since F ∗ F ∗ . . . ∗ F (n − 1
factors) is. Therefore, we may derive x from S1 in G1 by starting the
derivation S1 ⇒ T ⇒ T ∗ F , and so x ∈ L (G1 ).

Dr. Lavanya Selvaganesh CSO322 29 / 60


To show the grammar G1 is unambiguous I

Theorem
S1 → S1 + T | T
The CFG G1 with productions T → T ∗ F | F is unambiguous.
F → (S1 ) | a.

Proof: Claim: String in L (G1 ) has only one left most derivation from
S1 .
The proof will be by induction on |x|, and it will actually be easier to
prove something apparently stronger:
For any x derivable from one of the variables S1 , T or F , x has only
one leftmost derivation from that variable.
Basic step: ’ a ’ can be derived from any of the three variables and
that in each case there is only one derivation.

Dr. Lavanya Selvaganesh CSO322 30 / 60


To show the grammar G1 is unambiguous II
Induction Step: Let k ≥ 1 and that for every y derivable from S1 , T or
F for which |y | ≤ k , y has only one leftmost derivations from that
variable. Let x with |x| = k + 1.
Case 1: Consider first the case in which x contains at least one + not
within parenthesis Since the only +’s in strings derivable from T or F
are with in parenthesis. Therefore, any leftmost derivation of x from S1
has the form
S1 ⇒ S1 + T ⇒∗ y + T ⇒∗ y + z,
where the last two steps represent left most derivations of y from S,
and z from T , resp, and the + is still the last one not within
parentheses.
The induction hypothesis tells us that y has only one leftmost
derivation from S1 .
Case 2: Next consider the case in which x contains no + outside
parenthesis but at least one ∗ outside parentheses. This time x can be

Dr. Lavanya Selvaganesh CSO322 31 / 60


To show the grammar G1 is unambiguous III
derived only from S1 or T ; any derivation from S1 must begin
S1 ⇒ T ⇒ T ∗ F ; and any derivation from T must begin T ⇒ T ∗ F .
In either case, the ∗ must be the last one in x that is not within
parentheses.
As in the first case, the subsequent steps of any leftmost derivation
must be T ∗ F ⇒∗ y ∗ F ⇒∗ y ∗ z consisting first of a leftmost derivation
of y from T and then of a leftmost derivation of z from F . By induction
hypothesis, we have only one leftmost derivation of x from S1 or T .
Case 3: Finally, suppose x contains no + ’s or ∗’s outside parentheses.
Then x can be derived from any of the variables, but the only derivation
from S1 begins S1 ⇒ T ⇒ F ⇒ (S1 ) and the only derivation from T or
F begins the same way with the first one or two steps omitted.
∴ x = (y ), where S1 ⇒ y .
By the induction hypothesis, y has only one leftmost derivation from S1
and it follows that x has only one from each of the three variables.

Dr. Lavanya Selvaganesh CSO322 32 / 60


Simplied forms and Normal Forms
1 Ambiguity is one undesirable property of a CFG that we wish
to eliminate.
2 In this section, we discuss some straight forward ways of
improving a grammar, by eliminating ϵ-productions (of the form
A → ϵ) and unit productions (where one variable is replaced by
another variable).

Nullable Variable
A nullable variable in a CFG G = (V , Σ, S, P) is defined as
Any variable A for which P contains the production A → ϵ is
nullable.
If P contains the production of the form A → B1 B2 . . . Bn , where
each B1 , B2 , . . . Bn are nullable variables, then A is nullable.
No other variable is nullable.

Dr. Lavanya Selvaganesh CSO322 33 / 60


Eliminating ϵ productions from a CFG
Let G be a CFG with productions
S → ABCBCDA, A → CD, B → Cb, C → a | ϵ, D → bD | ϵ
In this example, we cannot simply throw away ϵproductions, as without
D → ϵ nothing can be derived.
Let S → ABCBCDA be written as S → ABC1 BC2 DA where
C1 , C2 , D all being ϵ productions and each can also be used ot
produce non-null strings.
In a derivation, we may replace none, any or all of these by ϵ.
Without ϵ productions, we need to allow for all these options by
adding productions of the for S → α, where α is a string obtained
from ABCBCDA by deleting some or all of {C1 , C2 , D},
that is, we will need at least the productions
S →ABBC2 DA | ABC1 BDA | ABC1 BC2 A | ABBDA
| ABBC2 A | ABC1 BA | ABBA
along with other original productions.
Dr. Lavanya Selvaganesh CSO322 34 / 60
Repeating the same with other productions from A:
We can leave out C or D but not both, otherwise we will have
A → ϵ.
Now considering S → A1 BC1 BC2 DA2 , we need to consider the
productions by leaving out some subset of {A1 , A2 , C1 , C2 , D},⇒
32 other possible productions needed to be added to the grammar.
Applying same reasoning to each of the original productions,
where ϵ can be derived, then we add all the productions of the
form X → α′ , where α′ is obtained from α by deleting some of
these occurrences.
In general, this procedure might produce new ϵ productions or of
the form X → X , these can be ignored as they contribute nothing
to the grammar.
Finally our CFG has 40 productions with 32 from S, along with

A → CD | C | D, B → Cb | b, C → a, D → bD | b

.
Dr. Lavanya Selvaganesh CSO322 35 / 60
Algorithm: FindNULL
Finding the nullable variables in a CFG G = (V , Σ, S, P) .
N0 = {A ∈ V : P contains the production A → ϵ}.
i = 0;
do
{i = i + 1;

Ni = Ni−1 ∪ {A : P contains A → α, for some α ∈ Ni−1 }
}
while Ni ̸= Ni−1 ;
Ni is the set of nullable variables.
By applying the algorithm, to the previous example, we get

N0 = {C, D}
N1 = {A, C, D}

Since no other productions have right sides in {A, C, D}∗ , these three
are only the nullable variables.
Dr. Lavanya Selvaganesh CSO322 36 / 60
Finding an equivalent CFG with no ϵ-productions

Given a CFG G = (V , Σ, S, P) construct a CFG G1 = (V , Σ, S, P1 ) with


no ϵ-productions as follows:
Step 1: Initialize P1 to be P.
Step 2: Find all the nullable variable in V , using the FindNULL
algorithm.
Step 3: For every production X → α in P, add to P1 every
production that can be obtained by deleting from α one or
more occurrences of nullable variables in α.
Step 4: Delete all ϵ productions from P1 . Also remove any
duplicates, as well as productions of the form X → X .

Dr. Lavanya Selvaganesh CSO322 37 / 60


Theorem
Let G = (V , Σ, S, P) be a context-free grammar and let
G1 = (V , Σ, S, P1 ) be the grammar obtained from G by the above
algorithm. Then G1 has no ϵ-productions and L(G1 ) = L(G) \ {ϵ}.

Proof: G1 has no ϵ-productions is immediate to follow. We show a


statement that is stronger than that in the theorem:

For any variable A ∈ V , and any non-null string x ∈ Σ∗

A ⇒∗G x if and only if A ⇒∗G1 x.

A ⇒kG x means there is a k -step derivation of x from A in G.


We first show that for any n ≥ 1, if A ⇒nG x, then A ⇒∗G1 x.
Proof is by mathematical induction on n.

Dr. Lavanya Selvaganesh CSO322 38 / 60


Proof-I
For basic step: Suppose A ⇒G x, then A → x is a production in P.
Since x ̸= ϵ, this production is also in P1 and so A ⇒∗G1 x.
In the induction step, we assume that for k ≥ 1 and that any string
other than ϵ is derivable from A in k or fewer steps is derivable
from A in G1 .
We next show that if x ̸= ϵ and A ⇒kG+1 x then A ⇒∗G1 x.
Suppose that the first step in the k + 1-step derivation of x in G is
A → X1 X2 · · · Xm , where each Xi is a variable or terminal.
Then x = x1 x2 · · · xm , where each xi is equal to Xi or is derivable
from Xi in k or less steps in G.
Any Xi for which the corresponding xi is ϵ is a nullable variable.
If we delete these Xi ’s from the string X1 X2 · · · Xm , then there will
be some non-empty string left, since x ̸= ϵ and the resulting
production is an element of P1 .

Dr. Lavanya Selvaganesh CSO322 39 / 60


Proof - II
Further, the induction hypothesis implies that for each Xi
remaining in the right side of this production,

Xi ⇒∗G1 xi .

Therefore, A ⇒∗G1 x.
Next, we show the converse, that for any n ≥ 1, if A ⇒nG1 x, then
A ⇒∗G x by induction on n.
For basic step: If A ⇒G1 x, then A → x is a production in P1 . This
means that A → α is a production in P, where x is obtained from
α by deleting one or more nullable variables.
It follows that A ⇒∗G x, because we can begin a derivation with a
production A → α and proceed by deriving ϵ from each nullable
variable that was deleted.
In the induction step, we assume that for k ≥ 1 and that any string
other than ϵ is derivable from A in k or fewer steps in G1 is
derivable from A in G.
Dr. Lavanya Selvaganesh CSO322 40 / 60
Proof - III
We next show that if x ̸= ϵ and A ⇒kG+1
1
x then A ⇒∗G x.
Suppose that the first step in the k + 1-step derivation of x in G1 is
A → X1 X2 · · · Xm , where each Xi is either a variable or a terminal.
Let x = x1 x2 · · · xm , where each xi is equal to Xi or is derivable
from Xi in k or less steps in G1 .
By induction hypothesis, Xi ⇒∗G xi , for each i.
By definition of G, there is a production A → α in P so that
X1 X2 · · · Xm can be obtained from α by deleting certain nullable
variables.
Since
A ⇒∗G X1 X2 · · · Xm ,
we can derive x from A in G, by first deriving X1 X2 · · · Xm and then
deriving each xi from the corresponding Xi .
Hence the proof.

Dr. Lavanya Selvaganesh CSO322 41 / 60


A-derivable variables

For any variable A, the set of A-derivable is as follows:


1 If A → B is a production, B is A-derivable.
2 If C is A-derivable, C → B is a production and B ̸= A, then B is
A-derivable.
3 No other variables are A-derivable.
Consider the CFG G with productions

S →S+T |T
T →T ∗F |F
F → (S) | a.

Here, S-derivables are T and F and


T -derivables are F .

Dr. Lavanya Selvaganesh CSO322 42 / 60


Finding a equivalent CFG with no unit productions
Given a CFG G = (V , Σ, S, P) with no ϵ-productions. Construct a
grammar G1 = (V1 , Σ, S.P1 ) having no unit productions as follows:
Step 1: Initialize P1 to be P.
Step 2: For each A ∈ V , find the set of A-derivable variable in V .
Step 3: For every pair (A, B) such that is B is A-derivable and
every non-unit production B → α in P, add to P1 the
production A → α, if it is not already present in P1 .
Step 4: Delete all unit productions from P1 .
In the previous example, by Step 3,

S → S + T | T ∗ F | (S) | a,
T → T ∗ F | (S) | a,
F → (S) | a.

Dr. Lavanya Selvaganesh CSO322 43 / 60


Theorem
Let G = (V , Σ, S, P) be a context-free grammar without ϵ-productions
and let G1 = (V , Σ, S, P1 ) be the grammar obtained from G by the
previous algorithm. Then G1 has no unit productions and
L(G1 ) = L(G).

If the grammar G is unambiguous, then the grammar G1 obtained from


the above algorithms (to remove ϵ and unit productions) is also
unambiguous.

Dr. Lavanya Selvaganesh CSO322 44 / 60


Useless Productions

Let A be a non-terminal in a CFG with start symbol S.


Then, we say A is a generating non-terminal, if A ⇒∗ w for some
string w of terminals.
A is a reachable non-terminal, if S ⇒ αAβ, for some strings α, β
of terminals and/or non-terminals.
Combining both, we say that A is a useful non-terminal, if there
are strings α, β of terminals and/or non-terminals, and a string w
of terminals such that S ⇒∗ αAβ ⇒∗ w.
A useless non-terminal is a non-terminal that is not useful.
A useless production is one that is never used in any derivation
of a terminal string.

Dr. Lavanya Selvaganesh CSO322 45 / 60


Eliminating Useless Nonterminals

Eliminating Nonreachable terminals


Let G = (V , Σ, S, P) be a CFG. To eliminate the non-reachable
non-terminals from G, we compute the set of all reachable
non-terminals iteratively by the following:
1 Mark S reachable.
2 If X ∈ V has been marked reachable, and X → αY β ∈ P, then
mark Y reachable.

You can show easily, by induction, that the set of reachable


symbols is correctly computed by the above marking scheme.
To eliminate the non-generating non-terminals, we assume that
L(G) ̸= ∅.
This assumption guarantees that the start symbol S is a
generating non-terminal.

Dr. Lavanya Selvaganesh CSO322 46 / 60


Computing generating non-terminals

We compute the set of generating non-terminals by the following


iterative scheme:
1 Mark ϵ and each terminal in Σ as generating.
2 If X → α ∈ P and each symbol in α is generating, then mark X to
be generating.

The useless symbols are then eliminated by first throwing away


non-generating symbols, and all productions wherever they occur, and
then by eliminating all symbols that are not reachable.

Dr. Lavanya Selvaganesh CSO322 47 / 60


Example
Consider the CFG with productions S → a | AB, A → b.
S is generating as S ⇒ a,
A is generating as A ⇒ b;
Terminals a, b are generating by default.
The only non-generating symbol is B.
We throw away the production S → AB updating the productions
to S → a, A → b.
Now, we see that the symbol A (as well as b) are not reachable.
Eliminating it, we are left with the only production S → a.
We have a CFG with no useless symbols.

Dr. Lavanya Selvaganesh CSO322 48 / 60


Elimination of Useless Productions
Compute the generating non-terminals and mark the
non-generating ones.
Delete from P all productions involving one or more of these
marked non-generating non-terminals.
Compute the reachable non-terminals and mark the
non-reachable ones.
Delete from P all productions involving one or more of these
marked non-reachable non-terminals.
Delete all terminals that do not occur in the updated productions.
Update the CFG G.

Lemma
Let G be a CFG such that L(G) ̸= ∅. Let G′ be the context-free
grammar as constructed above from the context-free grammar G by
eliminating the useless productions. Then L(G′ ) = L(G).

Dr. Lavanya Selvaganesh CSO322 49 / 60


Summary

Exercise
What happens if we first eliminate the non-reachable symbols and then
non-generating symbols? Will the resulting CFG be without useless
symbols?

[Hint: Carry out the steps on the CFG with productions S → a, A → b.]
Elimination of useless symbols is not only a matter of elegance;
It also helps in saving time while converting a CFG to its normal
forms.
The normal forms we will be discussing in the next section are not
only helpful for allowing nice progress indicators in a derivation,
but also they help in proving certain results in relatively shorter
way.

Dr. Lavanya Selvaganesh CSO322 50 / 60


Chomsky Normal Form
A Context-free grammar is in Chomsky Normal Form (CNF) if every
production is of these two types:

A → BC, A → a

where A, B, C are variable and a is a terminal symbol..

Conversion into CNF


It is done in 3 steps: Given a CFG G = (V , Σ, S, P)
First, apply Algorithm 1 and 2 to obtain a CFG G1 = (V1 , Σ, S, P1 )
having neither ϵ nor unit productions, so that L(G1 ) = L(G) \ ϵ.
Second step is to obtain a grammar G2 = (V2 , Σ, S, P2 )
generating the same language as G1 , so that every production in
P2 is either of the form
A →B1 B2 . . . Bk , where k ≥ 2, Bi ∈ V2
or A →a, for some a ∈ Σ.

Dr. Lavanya Selvaganesh CSO322 51 / 60


Construction of G2

Note that every production in P1 is of the form

A → a, A → α, α ∈ (V ∪ Σ)∗ , |α| ≥ 2

So consider, A → α:
For every a ∈ Σ appearing in α, introduce a new variable Xa and a
production Xa → a.
Replace a by Xa in every production it appears (except of the form
A → a).
For ex: A → aAb and B → ab
Replacing: A → Xa AXb , B → Xa Xb , and adding the productions
Xa → a and Xb → b.
The only production from Xa is a implies G2 is equivalent to G1 .

Dr. Lavanya Selvaganesh CSO322 52 / 60


Last step is to replace each production having more than two
variables on right by an equivalent set of productions, each one
having exactly two variables on the right.

Consider, A → BCDBCE
would be replaced by

A →BY1
Y1 →CY2
Y2 →DY3
Y3 →BY4
Y4 →CE

Theorem
For any CFG G = (V , Σ, S, P), there is a CFG G′ = (V ′ , Σ, S, P ′ ) in
Chomsky Normal Form so that L(G′ ) = L(G) \ {ϵ}.
Dr. Lavanya Selvaganesh CSO322 53 / 60
Example
Consider

S → AACD, A → aAb | ϵ, C → aC | a, D → aDa | bDb | ϵ

Eliminating ϵ-productions: Nullable Variables:= {A, D}


S →AACD | ACD | AAC | AC | CD | C
A →aAb | ab, C → aC | a D → aDa | bDb | aa | bb
Eliminating Unit Productions: Include S → aC | a and delete
S→C
Restricting the right side of the productions to single terminals or
strings of variables yields:
S →AACD | ACD | AAC | AC | CD | Xa C | a
A →Xa AXb | Xa Xb , C → Xa C | a
D →Xa DXa | Xb DXb | Xa Xa | Xb Xb
Xa →a and Xb → b
Dr. Lavanya Selvaganesh CSO322 54 / 60
Final step to CNF: Reducing long productions we get:

S →AT1 |AT2 |AT3 | AC | CD | Xa C | a


T1 →AT2
T2 →CD
T3 →AC
A →Xa T4 | Xa Xb
T4 →AXb
C →Xa C | a
D →Xa T5 | Xb T6 | Xa Xa | Xb Xb
T5 →DXa
T6 →DXb
Xa →a
Xb →b

Dr. Lavanya Selvaganesh CSO322 55 / 60


Simple grammar or s-grammar
Definition
A context-free grammar G = {V , T , S, P} is said to be a simple
grammar or s-grammar, if all its productions are of the form

A → ax,

where A ∈ V , a ∈ T , x ∈ V ∗ , and any pair (A, a) occurs at most once


in P.

Example
The grammar S → aS | bSS | c is an s-grammar.
The grammar S → aS | bSS | aSS | c is not an s-grammar because
the pair (S, a) occurs in the two productions S → aS and S → aSS.

While s-grammars are quite restrictive, they are of some interest:


Many features of common programming languages can be described
by s-grammars.
Dr. Lavanya Selvaganesh CSO322 56 / 60
Greibach Normal Form

Another useful grammatical form is the Greibach normal form.


Here, we put restrictions not on the length of the right sides of a
production, but on the positions in which terminals and variables
can appear.
Arguments justifying Greibach normal form are a little complicated
and not very transparent.
Similarly, constructing a grammar in Greibach normal form
equivalent to a given context-free grammar is tedious.

Definition
A context-free grammar is said to be in Greibach normal form if all
productions have the form A → ax, where a ∈ T and x ∈ V ∗ .

Dr. Lavanya Selvaganesh CSO322 57 / 60


We see that the form A → ax is common to both Greibach normal
form and s-grammars,
but GNF does not carry the restriction that the pair (A, a) occur at
most once.
This additional freedom gives GNF a generality not possessed by
s-grammars.
If a grammar is not in Greibach normal form, we may be able to
rewrite it in this form with some of the techniques encountered
above.

Dr. Lavanya Selvaganesh CSO322 58 / 60

You might also like