3 Context Free Grammars CFG and Languages
3 Context Free Grammars CFG and Languages
Summary of types:
1. Construct CFG for R.E [R.E->CFG]
6. Removal of ambiguity
8. Removal of £ productions
12. Theory
University:
1. Construct CFG for the regular Expression (0+1)*
Non University:
1. Construct CFG which consists of all the strings having at least one
occurrence of 000
University:
1. Construct the production rules for defining a Lang L={} (Dec 2006,8 M)
L= { wcwT| w*}
a) L= {}
b) L= {}
a) L= {}
b) L= {}
a) (011+1) *(01)*
i) L {}
ii) {}
b) the set of even strings {a,b}* with the two middle symbols equal
c) The set of odd length strings in {a,b}* whose first, middle and last symbols
are all same
(Dec-09,4M)
I. L1={aibjck| i=j+k}
Non-University:
1. Construct a grammar for the language containing strings of at least two a’s
2. Construct CFG for language L which has all the strings which are all
palindrome over ∑={a,b}
3. Construct CFG for the lang in which there are no consecutive b’s, the strings
may or may not have consecutive a’s
4. Construct CFG for the lang containing at least one occurrence of double a
5. Construct CFG for the lang containing all the strings of diff first and last
symbols over ∑={0,1}
Non-University:
S -> aB | bA
A -> a|aS|bAA
B -> b|bS|aBB
G= (V,T,P,S) where
V={S,T}
T={0,1}
P={S-> T00T
T-> 0T|1T|}
Type 4: Draw derivation tree
Non-University:
1. Draw a derivation tree for the string abaaba for the CFG given by
S -> bSb
S-> a/b/}
2. Derive the string “aaabbabba” Draw derivation tree using CFG given by,
(May 2011,Dec-11,6M)
S -> aB | bA
A -> a|aS|bAA
B -> b|bS|aBB
Type 5: Check/show that grammar is
ambiguous
University:
1. Is the following CFG ambiguous? (Dec
05,4M)
G= ({S,A},{a,b},P,S)
P= { S->aAS|a
A->SbA|SS|ba
S->aS|
S->aSbS
G={V={E,F},T={a,b,-},E,P}
E->F-E
F->a
E->E-F
F->b
E->F
I. S->SS|a|b
II. S->ABA,A->aAb|,B->bB|
III. S->aSb|aaSb|
Non-University:
5. Check whether the given grammar is ambiguous or not
S -> iCtS
S -> iCtSeS
S -> a
C -> b
S->aB|ab
A->aAB|a
B->ABb|b
S->aS|
S->aSbS
Remove ambiguity
G={V={E,F},T={a,b,-},E,P}
E->F-E
F->a
E->E-F
F->b
E->F
Remove ambiguity
3. Remove ambiguity
(may 10,12M)
IV. S->SS|a|b
V. S->ABA,A->aAb|,B->bB|
VI. S->aSb|aaSb|
Non-University:
4. Remove ambiguity
S->iCtS|iCtSeS|a
C->c1|c2
5. Remove ambiguity
E->E+E|E*E|(E)|I
I->a/b
6. Remove ambiguity
S->SS|a|b
7. Remove ambiguity
S->aSb|aaSb|
Type 7: Removal of Useless symbols
Non-University:
1. Consider the CFG G=(V,T,P,S) where V={S,A,B} T={0,1}
S -> 1B|11
A -> 0
S -> AB|CA
B -> BC|AB
A ->a
C ->aB|b
G=(V,∑,R,S)
Where V={S,X,Y}
∑={0,1}
R={S-> XY|0
X->1}
S->aA|bB
A->aA|a
B->bB
D->ab|Ea
E->aC|d
S->aS|A|C
A->a
B->aa
C->aCb
S->aA|a|Bb|cC
A->aB
B->a|Aa
C->cCD
D->ddd
Non-University:
S->XYX
X->0X|
Y->1Y|
2. Remove production from CFG .
S->aSa
S->bSb
S->
S->a | Ab | aBa
A->b |
B->b | A
S -> XY
X->Zb
Y->BW
Z->AB
W->Z
A->aA|bA|
B->Ba|Bb|
5. Eliminate productions
S->P0Q|QQ|0R|RQP
P->R0|1R|RR|RQP
Q->Q0|PQ|
R->0P|QQQ
Type 9: Removal of productions
Non-University:
S->0A|1B|C
A->0S|00
B->1|A
C->01
S-> A|0C1
A->0S|00
B->1|0S|00
C->01
S->AB
A->a
B->C|b
C->D
D->E|bC
E->d|Ab
Type 10: Convert CFG -> CNF
University:
1. Check whether the given CFG is in CNF (Dec-05,12
M)(May2011,6M)
Consider G= (V,T,P,S)
Where V={S,A,B}
T= {a,b}
P consists of
S-> aB
S->bA
A->a
A->aS
A->bAA
B->b
B->bS
B->aBB
S->Aba, S->aab,B->Ac
S->AACD
A->aAb|
C->aC|a
A->ada|bDb|
1) S->AS|a 2) S->AS|AAS
A->SA|b A->SA|aa
5.
Non-University:
6. Convert CFG into CNF
S->aaaaS
S->aaaa
8. Consider a=({S,A},{a,b},P,S)
Where P consists of
S->aAS|a
A->SbA|SS|ba
S->ABA
A->aA|
B->bB|
S->a|b|aSS
11. Convert the foll grammar to CNF
S->~S|[S
Type 11: Convert CFG -> GNF
University:
1. Convert the given grammar S->AB,A->BS|b , B->SA|a into GNF (Dec
06,07,May-07,8M)
2. Convert the given grammar S->Bs,S->Aa ,A->bc, B->Ac into GNF (May
06,8 M)
A1->A2 A3
A2-> A3 A1 |b
A3-> A1 A2|a
S->AA|0
A->SS|1
S->SS
S->0S1|01
Non-University:
6. Convert given CFG to GNF
S->ABA
A->aA|
B->bB|
S->AA|0
A->SS|1
S-> CA
A->a
C->aB|b
1) S->XY1|0
X->00X
Y->1X1
2)S->01|0S|00S
10. Convert CFG to GNF:
S->aSa|bSb|c
Type 12: Theory
University:
1. Define Type 0 and Type 1 grammar (May-06,May-074M)
i) Parser design
4. What is normal form ? Explain CNF and GNF with suitable example(May-
08,Dec 08 4M)
6. Applications of CFG.
(6M,may2011)
7. Prove that CFL’s are closed under union, concatenation and kleene
closure (Dec.2011,8M)
Non-University:
1. construct regular grammar for given DFA
a a,b
b
Type 14:Construct Regular Grammar for
given FA
Non-University:
1. Construct regular grammar for given FA
b b a,b
a b
a a
1 0
1 0
0
Type 15: Regular Grammar -> FA
University:
1. Draw NFA accepting the language generated by grammar with productions
(Dec-07,8M)
S-> abA|bB|aba
A-> b|aB|bA
B->aB|aA
Non-University:
1. Construct FA recognizing L(G) where G is the grammar
S->aS|bA|b
A->aA|bS|a
Type 16: Convert Right linear grammar->left
linear grammar
University:
1. Convert given Right linear grammar into equivalent left linear grammar
(Dec08,May09,4M)
S->10A|01
A-> 00A|1
Non-University:
1. Convert given Right linear grammar into equivalent left linear grammar
S->bB
B->bC
B->aB
C->a
B->b
2. Convert given Right linear grammar into equivalent left linear grammar
S->0A
A->1A
A->
3. Convert given Right linear grammar into equivalent left linear grammar
A0->aA1
A1->bA1
A1->a
A1->bA0
Type 17: Convert left linear grammar->right linear
grammar
Non-University:
1. Convert the following left linear grammar to right linear.
S->Aa|Bb
A->Bb
B->Ba|b
Type 18: Regular Expression->Regular grammar
University:
1. Construct right linear grammar corresponding to the regular expression
R=(1+(01)*)1*(0+1)
(May 06,8M)
(May
06,08,8M)
Non-University:
3. Construct a regular grammar for the regular expression a*b(a+b)*
University:
1. Describe the lang generated by the following grammars
a) Regular grammar
S-> aA|bC|b
A-> aS|bB
B->aC|bA|a
C->aB|bS
b) S->bS|aA|
A->aA|bB|b
B->bS
2. Draw NFA accepting the lang generated by grammar with production and
explain which lang it generates.
(May-08,8M)
S->aA|bC|b
A->aS|bB
B->aC|bA|a
C->aB|bS
S->bS|aA|
A->aA|bB|b
B->bS
4. Describe the lang generated by each of these grammar and justify your
ans with the example string derive from the grammar of the productions
given below (May-10,12M)
1. S->aSa|bSb|aAb|bAa
A->aAa|bAb|a|b|
2. S->bT|aT|
T->as|bs