ACT CH 3 Context Free Languages
ACT CH 3 Context Free Languages
ACT CH 3 Context Free Languages
Regular Program
• CFG is more powerful than finite automata or RE’s, but still cannot
define all possible languages.
• Context free languages are recognized by push down automata
• Many programming languages have recursive structure that can be
defined by CFGs
ACT CH-3: Context Free Languages 4
ACT CH-3: Context Free Languages 5
ACT CH-3: Context Free Languages 6
Con’t...
or
simply reading the leaf nodes, we can obtain the desired string.
S AB A aaA | B Bb |
26
S AB A aaA | B Bb |
S AB
S
A B
yield AB
27
S AB A aaA | B Bb |
S AB aaAB
S
A B
yield aaAB
a a A
28
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
yield aaABb
29
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb
S
A B
a a A B b
yield
aaBb aaBb
30
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb aab
Derivation Tree S
(parse tree)
A B
a a A B b
yield
aab aab
31
Exercise
1. Construct a derivation tree for the string aabbabba for the CFG
given by,
S → aB | bA
A → a | aS | bAA
B → b | bS | aBB
2. Show the derivation tree for string "aabbbb" with the following
grammar.
S → AB | ε
A → aB
B → Sb
• Only one parse tree is possible for id(id)id+, so the given grammar
is unambiguous.
•.
• Theorem (useless productions), Let G be a CFG. Then G' that does not
contain any useless variables or productions such that L(G)=L(G').
• A symbol can be useless if:
It does not appear on the right-hand side of the production rule
It does not take part in the derivation of any string.
• Similarly, a variable can be useless if it does not take part in the derivation
of any string.
• For Example:
• T → aaB | abA | aaT
• A → aA
• B → ab | b
• C → ad
ACT CH-3: Context Free Languages 39
Con’t...
• The variable 'C' will never occur in the derivation of any string and
never reach from the starting variable 'T’, so the production C → ad is
useless. Hence, eliminate it.
• Production A → aA is also useless because there is no way to
terminate and never produce a string.
• To remove this useless production A → aA, we will first find all the variables
which will never lead to a terminal string such as variable 'A'.
• Then we will remove all the productions in which the variable ‘A‘ occurs
43
Example 2:Remove the null productions from the following grammar
S -> ABAC
A -> aA / ϵ
B -> bB / ϵ
C -> c
• Solution: To eliminate A -> ϵ we have to change the productions containing A in the
right side. Those productions are S -> ABAC and A -> aA.
• Replacing each occurrence of A by ϵ, we get four new productions.
• S -> ABC / BAC / BC
• A -> a
• Add these productions to the grammar and eliminate A -> ϵ.
S -> ABAC / ABC / BAC / BC
A -> aA / a
B -> bB / ϵ
C -> c
49
Chomsky Normal Form(CNF)
• A CFG(context free grammar) is in CNF(Chomsky normal form) if all
production rules satisfy one of the following conditions:
1. Start symbol generating ε. For example, A → ε.
2. A non-terminal generating two non-terminals.
For example, S → AB.
3. A non-terminal generating a terminal. For example, S → a.
• To be in CNF, all the productions must derive either two non-terminals
or a single terminal.
• CNF restricts the number of symbols on the right side of a production
to be two.
• The two symbols must be non-terminals or a single terminal.
ACT CH-3: Context Free Languages 50
Example1
S → AB
A→a
B→b
This context free grammar is in Chomsky normal form.
Rule-01: Reduce the grammar completely by-
Eliminating ∈ productions
Eliminating unit productions
Eliminating useless productions
51
Continued….
Rule-02:
• Replace each production of the form A → B1B2B3….Bn , where n > 2
with A → B1C where C → B2B3….Bn.
• Repeat this step for all the productions having more than two variables
on RHS.
Rule-03:
• Replace each production of the form A → aB with A → XB and X →
a.
• Repeat this step for all the productions having the form A → aB.
52
Example-1: Convert the given grammar to CNF-
S → aAD
A → aB / bAB
B→b
D→d
Solution-
Step-01:
The given grammar is already completely reduced.
Step-02:
The productions already in chomsky normal form are-
B→b ………..(1)
D→d ………..(2)
These productions will remain as they are.
53
The productions not in chomsky normal form are-
S → aAD ………..(3)
A → aB / bAB ………..(4)
We will convert these productions in chomsky normal form.
Step-03:
Replace the terminal symbols a and b by new variables Ca and Cb.
This is done by introducing the following two new productions in the grammar-
Ca → a ………..(5)
Cb → b ………..(6)
Now, the productions (3) and (4) modifies to-
S → CaAD ………..(7)
A → CaB / CbAB ………..(8)
Step-04:
Replace AD and AB by new variables CAD and CAB respectively.
This is done by introducing the following two new productions in the grammar-
CAD → AD ………..(9)
CAB → AB ………..(10)
ACT CH-3: Context Free Languages 54
Con’t....
• Now, the productions (7) and (8) modifies to-
S → CaCAD ………..(11)
A → CaB / CbCAB ………..(12)
Step-05:
• From (1), (2), (5), (6), (9), (10), (11) and (12), the resultant grammar is-
S → CaCAD
A → CaB / CbCAB
B→b
D→d
Ca → a
Cb → b
CAD → AD
CAB → AB
This grammar is in chomsky normal form
Problem-02:
Convert the given grammar to CNF-
S → 1A / 0B
A → 1AA / 0S / 0 Step-03:
B → 0BB / 1S / 1
Solution- Replace the terminal symbols 0 and 1 by
Step-01: new variables C and D.
The given grammar is already completely reduced.
Step-02: This is done by introducing the following two
The productions already in chomsky normal form new productions in the grammar-
are- C→0 ………..(6)
A→0 ………..(1) D→1 ………..(7)
B→1 ………..(2)
These productions will remain as they are. Now, the productions (3), (4) and (5)
The productions not in chomsky normal form are- modifies to-
S → 1A / 0B ………..(3) S → DA / CB ………..(8)
A → 1AA / 0S ………..(4) A → DAA / CS ………..(9)
B → 0BB / 1S ………..(5) B → CBB / DS ………..(10)
We will convert these productions in chomsky normal
form.
56
Step-04:
57
Grienbach Normal Form(GNF)
65
h r e e
t e r- T
C h a p
do f
En
66