Context Free Grammars and Languages
This CFL accepts equal number of a’s followed by equal number of b’s
This CFL accepts all strtings consists of equal number of a’s and b’s
Derivation: The derivation of a string from the Start symbol is only a valid string. Derivation is
method of deducing a string is called Derivation,The some of possible strings are :
1. SaB Sab ( Here B is replaced by Bb production)
2. SbA Sba ( Here A is replaced by Aa production)
3. SaB SabS (by BbS) SabaB (by SaB) Sabab (by Bb)
Leftmost and Rightmost Derivation
DERIVATION TREE OR PARSE TREE:
:
1. Eliminate useless symbols from the grammar:
SaS/A/C Aa Baa CaCb
Here C is eliminated because it is not causes for any string SaS/A Aa Baa
B is also a useless symbol. After removing B, the productions are SaS/A, Aa
2. Eliminate useless symbols from the grammar:
SaAa AbBB Bab CaB
All variables are producing strings. In next step, we have to eliminate the useless symbols.
SaAa AbBB Bab CaB
CaB has been removed, because it is not in the derivation.
SaAa AbBB Bab is the solution.
Elimination of € Productions:
1. Reduce the following grammar such that there are no € Productions
SaS/bA, AaA/€
SaS SbA Sb and AaA Aa
2. SAaB/aaB A€ BbbA/€ From this grammar eliminate €-productions and
then eliminate useless symbols
Elimination of € productions.
SAaB/aaB/Aa/aB/aa/a (substitute € for each occurrence of A and B separately)
BbbA/bb
Here A is useless symbol so production of a is eliminated from S and B
The resultant grammar is:
SaaB/Aa/aB/aa/
Bbb
Elimination of Unit Productions:
1. SA/bb AB/b BS/a Here the unit productions are SA, AB, BS
SA gives Sb
SAB gives SB gives Sa
AB gives Aa
SBS gives Abb
BS gives Bbb
BSA gives Bb
The new productions are
Sbb/b/a
Ab/a/bb
Ba/bb/b
Here A and B are useless symbols , the resultant grammar is Sbb/b/a
Convert the following CFG into CNF form
SaAD, AaB/bAB , Bb, Dd
For CFG to be in CNF the productions are of the form: ABC or Aa
In the above given CFG Bb and Dd are in CNF from
SaAD is converted to CNF as follows
SCaAD where Ca a
SCaV1 where V1AD
AaB /bAB is converted to CNF as follows:
ACaB
A bAB is converted as ACbAB where Cbb
ACbV2 where V2 AB
The resultant CNF is
SCaV1
ACaB/CbV2
Caa , Cbb and V1AD, V2AB
Bb
Dd
2
PARSING: TOP-DOWN Vs BOTTOM-UP