Chapter - 3 - Context Free Language - Part - 1
Chapter - 3 - Context Free Language - Part - 1
ANUJ GHIMIRE
DISCLAIMER
This document does not claim any originality and
cannot be used as a substitute for prescribed textbooks.
The information presented here is merely a collection
from various sources as well as freely available material
from internet and textbooks.
The ownership of the information lies with the
respective authors or institutions.
Chapter 3
Solution,
The set of palindrome string over {a,b} are:
{a, b, aa, bb,aba, baab, bbbaabbb, aaabbaaa, ababbaba………}
X1 X2 X3
a X3
Derivation Tree/ Parse Tree
Consider a grammar G as:
G=(V, ∑, R, S)
where, V= {S, A, B, a, b} are set of terminals
∑= {a, b} are set of terminals
R consists of:
S → aAS------------- (i)
S→ aS ------------- (ii)
S → b ------------------(iii)
A→ bB------------- (iv)
A →a ------------------(v)
B→ aA ------------------(vi)
B→ b ------------------(vii)
S= S is a start symbol
Derive a string w= abaaab using LMD draw parse tree
Derivation Tree/ Parse Tree
Left Most Derivation (LMD)
S → aAS------------rule (i)
→ abBS------------rule (iv)
→ abaAS------------rule (vi)
→ abaaS------------rule (v)
→ abaaaS------------rule (ii)
→ abaaab------------rule (iii)
Derivation Tree/ Parse Tree
Parse Tree for derivation
S
a S
A a
S
b B
A b
a
a
Derivation Tree/ Parse Tree
Consider a grammar G as:
G=(V, ∑, R, S)
where, V= {S, A, B, a, b} are set of terminals
∑= {a, b} are set of terminals
R consists of:
S → aA | bB
A→ b | bS | aAA
B → a | aS | bBB
S= S is a start symbol
For the string bbaababa find its LMD, RMD and draw
parse tree.
Derivation Tree/ Parse Tree
Left Most Derivation (LMD)
S → bB------------[ using S → bB ]
→ bbBB------------[ using B → bBB ]
→ bbaB------------[ using B → a]
→ bbaaS------------[ using B → aS ]
→ bbaabB------------[ using S → bB ]
→ bbaabaS------------[ using B → aS ]
→ bbaababB------------[ using S → bB ]
→ bbaababa------------[ using B → a ]
Derivation Tree/ Parse Tree
Right Most Derivation (RMD)
S → bB------------[ using S → bB ]
→ bbBB------------[ using B → bBB ]
→ bbBaS------------[ using B → aS]
→ bbBabB------------[ using S → bB ]
→ bbBabaS------------[ using B → aS ]
→ bbBababB------------[ using S → bB ]
→ bbBababa------------[ using B → a ]
→ bbaababa------------[ using B → a ]
Derivation Tree/ Parse Tree
S
b B
b B
B
a S
a
b B
a S
b B
Parse Tree of Derivation
a
Ambiguous Grammar
A grammar G = (V, ∑, R, S) is said to be ambiguous if
there is a string w Є L(G) for which we can derive two
or more distinct derivation tree rooted at S and
resulting the string w.
In other words, if there exist multiple leftmost
derivation or multiple rightmost derivation for the
same string for any grammar then it is ambiguous.
Ambiguous Grammar
Let G = (V, ∑, R, S) be a CFG with the production rule of
the form:
S → AB | aaB
A → a | Aa
B→b
Now, for string w=aab , we have two left most derivation
as:
S → AB
S → aaB
→ AaB
→ aab
→ aaB
→ aab
Ambiguous Grammar
The derivation tree can be:
S S
A B a a B
A a
b b
a
Since there are two parse tree for the same string w=aab,
this grammar is ambiguous.
Inherently Ambiguous
Sometimes when we have an ambiguous grammar we
can find an unambiguous grammar that generates the
same language.
Some context-free languages, however, can be
generated only by ambiguous grammars.
Such languages are called inherently ambiguous.
Ambiguity is a property of a grammar, and it is usually,
but not always possible to find an equivalent
unambiguous grammar.
An “inherently ambiguous language” is a language for
which no unambiguous grammar exists.
Inherently Ambiguous
Assignment……
For S → ABC
S →AB [when C → ε ]
S → AC [when B → ε ]
S → BC [when A → ε ]
S→A [when B → ε and C → ε ]
S→B [when A→ ε and C → ε ]
S→C [when A→ ε and B → ε ]
After the removal of null production the resulting rules for S
are:
S → ABC | AB | AC | BC | A | B | C
Removal of Null Production_Example(2)
For A → BB
A →B [when B → ε ]
So, after the removal of null production the resulting rules
for A are:
A → BB | B
For B → CC
B →C [when C → ε ]
So, after the removal of null production the resulting rules
for B are:
B → CC | C | a
Removal of Null Production_Example(2)
For C → AA
C →A [when A → ε ]
So, after the removal of null production the resulting rules
for C are:
C → AA | A | b
So the final production rule after removing the null
productions are:
S → ABC | AB | AC | BC | A | B | C
A → BB | B
B → CC | C | a
C → AA | A | b
Removal of Unit Production
A production rule of the form:
Non Terminal → one Non-Terminal
i.e A → B (where A and B, both are non-
terminals) is called unit production.
Procedure to remove unit production:
To remove the production rule of form A→ B, such that
there exists a production B→ α, where α is a terminal
add A→ α to the grammar
Remove A→ B from the grammar.
Repeat until all unit production are removed.
Removal of Unit Production_Example(1)
Consider a grammar
S → 0A | 1B | C
A → 0S | 00
B→1|A
C → 01
Remove the unit production
Solution,
The unit production in the grammar is:
S→C
B→A
Removal of Unit Production_Example(1)
To remove these unit production we have to search for a
production rule for C and A and add these rule to the
grammar.
Here, C → 01
Now replace the production of S → C by C → 01 and
remove S → C from the grammar.
S → 0A | 1B | 01
Again A → 0S | 00
Replace the production rule B → A by all the rule of
what A gives.
Removal of Unit Production_Example(1)
Replace the production rule B → A by all the rule of
what A gives and remove B → A from grammar .
i.e. B → 1 | A
B → 1 | 0S | 00
Thus final CFG after removal of unit production is:
S → 0A | 1B | 01
A → 0S | 00
B → 1 | 0S | 00
C → 01
Removal of Unit Production_Example(2)
Consider a grammar
S → AB
A→a
B→ C | b
C→ D
D→E
E→a
Remove the unit production
Removal of Unit Production_Example(2)
Solution,
The unit production in the grammar is:
B→C
C→D
D→E
To remove these unit production we have to search for a
production rule that contains the terminal symbol.
Here, such production rule is E → a
Now replace the production of D → E by E → a and remove
D → E from the grammar.
D→a
E→a
Removal of Unit Production_Example(2)
Again for C → D
Replace the production of C → D by D → a and remove C → D
from the grammar.
C→a
D→a
E→a
Lastly for unit production: B → C
Replace the production of B → C by C → a and remove B → C
from the grammar.
B→a|b
C→a
D→a
E→a
Removal of Unit Production_Example(2)
Hence all the unit production has been removed so the final
grammar is:
S→ AB
A→a
B→a|b
C→a
D→a
E→a
Removal of Useless Symbol
Useless symbols are those variables or terminals used
in grammar that can never take part in derivation of
any string from the start symbol.
Formally, we say a symbol X is useful for a grammar
G = (V, ∑, R, S) if there is some derivation of the form:
S →* α X β →* w, where w is in ∑*.
Here, X may be either variable or terminal and the
sentential form α X β might be the first or last in the
derivation.
If X is not useful, we say it is useless.
Removal of Useless Symbol
Eliminating a useless symbol includes identifying
whether or not the symbol is “generating” and
“reachable”.
Removal of useless symbol from CFG includes:
Removal of non generating symbol
Removal of not reachable symbol
A symbol X is generating if X →* w for some terminal
string w.
A symbol y is reachable if there is derivation S →* XyZ
for some X and Z.
Thus we eliminate non-generating and then non
reachable symbols.
Removal of Useless Symbol
For eliminating non generating symbol, identify the
non-generating symbols in CFG and remove those
from productions rules.
For eliminating non-reachable symbols from CFG,
first identify non-reachable symbols and eliminate
symbols non-reachable symbols and production rules.
Removal of Useless Symbol_Example(1)
Consider the grammar G
S→ aB | bX
A→ Bad | bSX | a
B→ aSB | bBX
X→ SBd | aBX | ad
Let, S is start symbol.
Remove all useless symbol
Removal of Useless Symbol_Example(1)
Solution,
First identify all the non-generating symbol in the
grammar.
Here S, A and X are generating symbols but B is non-
generating because there is no way from B you can
reach to any terminal symbol.
So, remove all the rules with B. The new grammar is
then:
S→ bX
A→ bSx | a
X→ ad
Removal of Useless Symbol_Example(1)
Now remove non-reachable symbol if any from the
grammar
Here clearly you can see that we cannot reach to A from
start symbol S so, A is non-reachable.
So, remove A from the grammar and the simplified
grammar will be:
S→ bX
X→ ad
Removal of Useless Symbol_Example(2)
Consider the grammar G
S→ AB | CA
A→ a
B→ BC | AB
C→ aB | b
Let, S is start symbol.
Remove all useless symbol
Removal of Useless Symbol_Example(2)
Solution,
First identify all the non-generating symbol in the grammar.
Here A, C are generating symbols as they can generate
terminals. S can also generate terminal since S→ AC but B is
non-generating because there is no way from B you can
reach to any terminal symbol.
So, remove all the rules with B. The new grammar is then:
S→ CA
A→ a
C→ b
Removal of Useless Symbol_Example(2)
Now remove non-reachable symbol if any from the grammar
Here clearly you can see that we can reach to A and C from
start symbol S so, there is no any non-reachable.
So, the required simplified grammar is:
S→ CA
A→ a
C→ b
Chomsky Normal Form (CNF)
A context free grammar G (V, ∑, R, S) is said to be in
Chomsky's Normal Form (CNF) if every production in
the grammar are in one of the two forms:
A→BC &
A→a
where A, B, and C are the non-terminals and a is
terminal
A grammar in CNF is one which should not have:
ε-production
Unit production
Useless symbols
Chomsky Normal Form (CNF)
Procedure to find Equivalent Grammar in CNF
Step 1: If the start symbol S occurs on same right side
then create a new start symbol S' and a new production
S'→ S.
Step 2: Eliminate all the ε-productions, unit productions
and useless symbols from grammar if any.
Step 3: If the right side of any production is in of the
form A→ aB where 'a' is a terminal and A and B are
non-terminal then the production 1s replaced by A→XB
and X → a. Repeat this step for every Production which
1s of the form A→ aB.
Chomsky Normal Form (CNF)
Step 4: Replace each Production A → B₁..... Bn where
n>2, with A→B₁C where C → B2.... Bn. Repeat this step
for all productions having two or more symbols on the
right side.
Chomsky Normal Form(CNF)_Example(1)
Convert the following CFG into CNF G = (V, ∑, R, S)
where
V = { S, A , B}
∑ = {a, b}
R consists of:
S → aAB | AaB | B
A → aA | ε
B → ab | bA
Solution,
To convert CFG into CNF , we have to simplify the given CFG.
Chomsky Normal Form(CNF)_Example(1)
In given grammar there are ε -productions so we remove those
productions
Here, A → ε is the null production so we replace A with ε by
all possible ways and add the resulting production rule in the
grammar
S → aAB | AaB | aB | B
A → aA | a
B → ab | bA | b
Chomsky Normal Form(CNF)_Example(1)
In given grammar there are unit productions also so we
remove those productions from grammar
Here, S → B is the unit production.
To remove these unit production we have to search for a
production rule for B and add these rule to the grammar.
Here, B → ab | bA | b
Now replace the production of S → B by production rules of
B and remove S → B from the grammar.
S → aAB | AaB | aB | ab | bA | b
A → aA | a
B → ab | bA | b
Chomsky Normal Form(CNF)_Example(1)
S → aAB | AaB | aB | ab | bA | b
A → aA | a
B → ab | bA | b
There is no any useless symbol so this is the final simplified
grammar
In this simplified grammar all the production rules are not in the
form of:
A→ BC & A → a
Now, Converting this simplified form to get CNF:
S → CAB | ACB | CB | CD | DA | b
C→a
D→b
A → CA | a
B → CD | DA | b We also convert ab to CD as ab is not of the form
for CNF
Chomsky Normal Form(CNF)_Example(1)
Here , again the production rules S → CAB and S → ACB are
not in required form of CNF
we can perform like:
S → EB | FB | CB | CD | DA | b
E → CA
F → AC
C→a
D→b
A → CA | a
B → CD | DA | b
This the required CNF of the given CFG
Chomsky Normal Form(CNF)_Example(2)
Convert the following CFG into CNF G = (V, ∑, R, S)
where
V = { S, A , C}
∑ = {a, b}
R consists of:
S → AAC
A → aAb | ε
C → aC | a
Solution,
To convert CFG into CNF , we have to simplify the given CFG.
In given grammar there are ε -productions so we remove those
productions
Chomsky Normal Form(CNF)_Example(2)
Here, A → ε is the null production so we replace A with ε by
all possible ways and add the resulting production rule in the
grammar
S → AAC | AC | C
A → aAb | ab
C → aC | a
In given grammar there are unit productions also so we
remove those productions from grammar
Here, S → C is the unit production.
To remove these unit production we have to search for a
production rule for C and add these rule to the grammar.
Here, C → aC | a
Chomsky Normal Form(CNF)_Example(2)
Now replace the production of S → C by production rules of
C and remove S → C from the grammar.
S → AAC | AC | aC | a
A → aAb | ab
C → aC | a
There is no any useless symbol so this is the final simplified
grammar
In this simplified grammar all the production rules are not in
the form of:
A→ BC & A → a
Chomsky Normal Form(CNF)_Example(2)
Now, Converting this simplified form to get CNF:
S → AAC | AC | aC | a
A → aAb | ab
C → aC | a
These can be written as:
S → AAC | AC | DC | a
D→a
A → DAE | DE
E→b
C → DC | a
Chomsky Normal Form(CNF)_Example(2)
Here , again the production rules S → AAC and A → DAE are
not in required form of CNF
We can perform like:
These can be written as:
S → AF | AC | DC | a
F → AC
D→a
A → DG | DE
G → AE
E→b
C → DC | a
Greibach Normal Form (GNF)
A context free grammar G=(V, ∑, R, S) is said to be in
Greibach Normal Form (GNF) if every production in
the grammar is in the form:
α→aβ*
where α and β Є V and a Є ∑ .
i.e. Non-Terminal →exactly one Terminal
OR
Non-Terminal → exactly one Terminal followed by
any number of Non-Terminal
Regular Grammar
A grammar is said to be regular if it has the production rule
of the form:
A → aB
A→a
A→ε
where A,B Є V and a Є ∑ .
i.e. Non-Terminal →exactly one Terminal
OR
Non-Terminal → exactly one Terminal followed by
exactly one Non-Terminal