0% found this document useful (0 votes)
14 views35 pages

3 Context Free Grammars CFG and Languages

Uploaded by

Aditya Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views35 pages

3 Context Free Grammars CFG and Languages

Uploaded by

Aditya Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Unit 3

Context free grammar &


languages

Summary of types:
1. Construct CFG for R.E [R.E->CFG]

2. Construct CFG from language[Lang->CFG]

3. Derive leftmost and Rightmost derivation

4. Draw derivation tree/Syntax Tree/parse Tree

5. Check/show that grammar is ambiguous

6. Removal of ambiguity

7. Removal of Useless symbols

8. Removal of £ productions

9. Removal of unit productions

10. Convert CFG->CNF

11. Convert CFG->GNF

12. Theory

13. Construct Regular Grammar for given DFA(DFA->Regular


Grammar)
14. Regular Grammar for given FA(FA->Regular Grammer)

15. Regular Grammar ->FA

16. Convert Right linear grammar->left linear grammar

17. Convert Left linear grammar->Right linear grammar

18. Regular Expression->Regular grammar

19. Regular Grammar -> lang

Type1: Construct CFG for R.E

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

2. Find CFG for the regular expression (110+11)*(10)*


Type 2: Construct CFG for Language

University:
1. Construct the production rules for defining a Lang L={} (Dec 2006,8 M)

2. Construct the grammar G to represent


(Dec-05,8M)

L= { wcwT| w*}

3. Construct CFG for


(Dec 2008,12 M)

a) L= {}

b) L= {}

3. Construct CFG for


(Dec 2008, 12 M)

a) L= {}

b) L= {}

4. Construct CFG for


(May 2009, 12M)

a) (011+1) *(01)*

b) Where i,k >=0

5. Construct CFG for


(Dec 2006, 8 M)

i) L {}

ii) {}

6. In each case find the CFG generating given lang (Dec


2007, May-08 8 M)
a) the set of odd length strings in {a,b}* with middle symbol a

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)

7. Find CFG for


(Dec 2007, 8 M)

I. L1={aibjck| i=j+k}

II. L2={aibjck| j=i+k}

III. L3={aibjck|i=j or 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}

6. Build a CFG for the language L={| j>i+k}

7. Build a CFG for the language L={| i=j}

8. Build a CFG for the language L={| j<=k }

9. Construct CFG for generating lang L={}


Type 3: Derive leftmost and rightmost
derivation

Non-University:

1. Derive the string “aaabbabba” for leftmost derivation and rightmost


derivation using CFG given by,
(May 2011,dec-11,6M)

S -> aB | bA

A -> a|aS|bAA

B -> b|bS|aBB

2. Derive the string 1000111 for leftmost derivation using CFG

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

G where P={ S -> aSa

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

2. Consider the grammar having production (Dec-


05,4M)

S->aS|

S->aSbS

This grammar is ambiguous?

3. Consider the grammar


(dec-06,May 07,8M)

G={V={E,F},T={a,b,-},E,P}

Where P consists of rules:

E->F-E

F->a

E->E-F

F->b

E->F

Is the CFG ambiguous


4. Is the following CFG ambiguous? (may
10,12M)

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

6. Show that the grammar S->a| abSb | aAb

A->bs| aAAb is ambiguous

7. Is the following CFG ambiguous?

S->aB|ab

A->aAB|a

B->ABb|b

8. S->SS|a|b Is the given CFG ambiguous?


Type 6: Removing Ambiguity
University:
1. Consider the grammar having production
(Dec-05,4M)

S->aS|
S->aSbS

Remove ambiguity

2. Consider the grammar (dec-


06,May 07,May11,8M)

G={V={E,F},T={a,b,-},E,P}

Where P consists of rules:

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}

P={ S -> A11B|11A

S -> 1B|11

A -> 0

B -> BB} for removing useless symbols.

2. Find CFG with no useless symbols equivalent to

S -> AB|CA

B -> BC|AB

A ->a

C ->aB|b

3. Consider the following CFG

G=(V,∑,R,S)

Where V={S,X,Y}

∑={0,1}

R={S-> XY|0

X->1}

Remove useless symbols from it

4. Remove useless symbols from foll CFG

S->aA|bB

A->aA|a

B->bB

D->ab|Ea
E->aC|d

5. Remove useless symbols from foll CFG

S->aS|A|C

A->a

B->aa

C->aCb

6. Remove useless symbols from foll CFG

S->aA|a|Bb|cC

A->aB

B->a|Aa

C->cCD

D->ddd

Type 8: Removal of productions

Non-University:

1. Remove production from CFG by preserving meaning of it.

S->XYX

X->0X|

Y->1Y|
2. Remove production from CFG .

S->aSa

S->bSb

S->

3. Remove production from CFG .

S->a | Ab | aBa

A->b |

B->b | A

4. Remove production from CFG .

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:

1. Remove unit productions

S->0A|1B|C

A->0S|00

B->1|A

C->01

2. Optimise the CFG given below by reducing grammar

S-> A|0C1

A->0S|00

B->1|0S|00

C->01

3. Eliminate unit productions

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

If it is not in CNF .Find equivalent CNF

2. Convert CFG to CNF


(may-06,8 M)

S->Aba, S->aab,B->Ac

3. Convert CFG to CNF


(may-10,6 M)

S->AACD
A->aAb|

C->aC|a

A->ada|bDb|

4. Justify whether the grammars are in CNF or not


(may-11,8 M)

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

7. Convert the given CFG to CNF S->aSa|bSb|a|b

8. Consider a=({S,A},{a,b},P,S)

Where P consists of

S->aAS|a

A->SbA|SS|ba

Convert it to its equivalent CNF

9. Convert the foll CFG to CNF

S->ABA

A->aA|

B->bB|

10. Convert the foll grammar to CNF

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)

3. Convert the given grammar into GNF (Dec-


06,May11,8M)

A1->A2 A3

A2-> A3 A1 |b

A3-> A1 A2|a

4. Convert given CFG to GNF (Dec-


07,8M)

S->AA|0

A->SS|1

5. Convert given CFG to GNF (Dec-


11,8M)

S->SS
S->0S1|01

Non-University:
6. Convert given CFG to GNF

S->ABA

A->aA|

B->bB|

7. Convert given CFG to GNF where V={S,A},T={0,1} and P is

S->AA|0

A->SS|1

8. Convert given CFG to GNF

S-> CA

A->a

C->aB|b

9. Convert CFG to GNF:

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)

2. Application of CFG in XML (May 2006Dec-2005,Dec 06,4M)

3. Explain Application of CFG for

i) Parser design

ii) In markup Language.

4. What is normal form ? Explain CNF and GNF with suitable example(May-
08,Dec 08 4M)

5. Write a short note on closure properties of CFL(May 2009 4M)

6. Applications of CFG.
(6M,may2011)

7. Prove that CFL’s are closed under union, concatenation and kleene
closure (Dec.2011,8M)

8. Short note on Chomsky hierarchy.


(Dec.2011,8M)
Type 13:Construct Regular Grammar for
given DFA

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

2. Construct regular grammar for 1

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)

2. Construct right linear grammar corresponding to the regular expression


R=(0+1)1*(1+(01)*)

(May
06,08,8M)

Non-University:
3. Construct a regular grammar for the regular expression a*b(a+b)*

4. Construct a regular grammar for (ab+a)*(aa+b)


Type 19: Regular Grammar -> Lang

University:
1. Describe the lang generated by the following grammars

Show the derivation of such a string from grammar (dec-


07,May-09,Dec-098M)

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

3. Describe the lang generated by the grammar


(May-08,6M)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy