MODULE 3 (24-10-2024)
MODULE 3 (24-10-2024)
3. CFG to generate strings of a’s and b’s of any length L={e, aba, baba,………}
S –> ε |a|b|Sa|Sb
aba
S->Sa ; S->Sa
->Sb a ; S->Sb
-> aba ; S ->a
For some strings there exist more than one parse tree
Or more than one leftmost derivation or more than one rightmost derivation
P:
E→E+E|E*E|E–E| E/E
E→ id
G=(V T P S) V={E} T={id,+,_,*,/} S =E
Example: id+id*id
Solution: The sentence id +id *id can be obtained from left most derivation in two ways as
shown below.
E E+E E E*E
id+E E+E*E
id+E*E id+E*E
id+ id *E id+id*E
id+ id *id id+ id *id
Der1 --ibtibtibtaea
S->iCtSeS
-> ibtSeS ; C->b
-> ibt iCtS eS ; C->b
->ibtibt SeS ; S->iCtS
-> ibtibt iCtS eS ; C->b
->ibtibtibt SeS ; S->a
->ibtibtibt aeS ; S->a
->ibtibtibtaea
4) consider grammer
G = {V,T,R,S}
V = {S,A}
T = {A,B}
R={ SAA
AAAA
Aa
ABa
AAb}
4) SA Sas/A/C
AaA/ℇ Aa
BbA Baa
cacb
C- non generating (elimination)
Sas/A
Aa
Baa
B-non reachable
Sas/A
Aa
5) AaA/a/Bb/cC
AaB
Ba/Aa
CcCD
Dddd
C- non generating
Aan/a/Bb
AaB
Ba/Aa
Dddd
D- non reachable
San/a/Bb
AaB
Ba/Aa
Eliminating ℇ productions
* ℇ production is grammer are not essential
* If L ha a CFG, then L-{ ℇ } has CFG without ℇ -production
* If ℇ is not in L, then L
Steps
Discover the nullable variable
A variable is nullable if
A ℇ
1) Determine all nullable symbols of G
*Any production of CFG of the form
A ℇ is called
ℇ productions
*Any variable A for which the derivation
A ℇ is possible is called nullable
Example 1
Consider the grammer
SABC
ABC/a
BbAC/ ℇ
CSAB/ ℇ
B,C are directly nullable
A is also nullable : ABC
S is also nullable : SABC
SABC/AB/BC/AC/A/B/C
ABC/a
BC/B/C/a
BbAC/bA/bC/b
CCAB/CA/CB/C
2) Sas1b
S1as1b/ ℇ
ℇ- production are S1ℇ can be removed after adding new production obtained by
substituting ℇ for S1 where it occurs to the sigma
Sa s1b/ab
S1a s1b/ab
3) S ABac
ABC
Bb/ ℇ
CD/ ℇ
Dd
A, B, C, S are nullable
SABaC/Bac/AaC/ABa/ac/Aa/Ba/a
AB/C/BC
Bb
CD
Dd
1) SAB
Aa
Be/b
CD
DE/bC
Ed/Ab
Unit Non unit
BC SAB
CD Aa
DE Bb
DbC
BCDE Ed/Ab
Example
1. Remove unit productions Non unit productions
SAa/B SAa
BA/bb Bbb
Aa/bc/B Aa/bc
Dependency graph
A
S B
SAa
Aa/bc/bb
Ba/bc/bb
SAa/a/bc/bb
2. Ia/b/Ia/Ib/I0/I1
FI/(E)
Tf/T*F
ET/E+T
I F T
3) SAa/B/Ca
BaB/b
CDb/D
DE/d
Eab
SB
CDE
Rewrite Eab
Dd/ab
CDb/d/ab
BaB/b
SAa/Ca/aB/b
1) B, D are nullable
BbBb/bb/a
CCA/AC
AaA/aC/Bac/aaa
SABC/BaB/AC/Ba/aB/a
2) No unit productions
3) C is the only varaiable which is non generating eliminate C
SBaB/Ba/aB/a
AaA/aaa
BbBb/bb/a
4) CNF Introduce two variables
Ea
Fb
SBEB/BE/EB/a
AEA/EEE
BFBF/FF/a
SBG/BE/EB/a
GEB
AEA/EH
HEE
BFI/FF/a
IBF