0% found this document useful (0 votes)
48 views7 pages

ToC Assignment 66

This document contains 7 questions about context-free grammars and languages. The questions cover topics such as generating languages from grammars, constructing grammars for specific languages, determining ambiguity, and finding equivalent unambiguous grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views7 pages

ToC Assignment 66

This document contains 7 questions about context-free grammars and languages. The questions cover topics such as generating languages from grammars, constructing grammars for specific languages, determining ambiguity, and finding equivalent unambiguous grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CS602-Advance Theory of Computation

Dr. Abdus Salam


Assignment # 6

1. Which language generates the grammar G given by the following productions?


S → aSa | aBa
B → bB | b

2. Find a CFG that generates the language:


L(G) = { an bm cm d2n | n ≥ 0, m > 0}.
3. Find a CFG that generates the language
L(G) = { an bm | 0 ≤ n ≤ m ≤ 2n}.

4. Consider the grammar


S → abScB | λ
B → bB | b
What language does it generate?
5. Construct context free grammars to accept the following languages.
a. {w | w starts and ends with the same symbol}
Answer : S  aTa | bTb
T= aT | bT | λ
b. {w | |w| is odd}
S  aT | bT
T= aT | bT | λ

c. {w | |w| is odd and its middle symbol is 0}


Answer is : S 0S0 | 0S1 | 1S1 | 1S0 | 0
d. {w#x | wR is a substring of x, where w, x ∈ {a, b}*}
S → Sa | Sb
S→R
R → aRa | bRb
R→U
U → Ua | Ub
e. {0n1n | n>0} U {0n12n | n>0}

f. Binary strings with twice as many 1s as 0s.

S 0 S 1 S 1 S | 1 S 0 S 1 S | 1 S 1 S 0 S | 

6. Explain why the grammar below is ambiguous.


S → 0A | 1B
A → 0AA | 1S | 1
B → 1BB | 0S | 0
Answer
S  0A00AA001S10011B1001101
S  0A00AA0011S00110A001101

7. Given the following ambiguous context free grammar


S → Ab | aaB
A → a | Aa
B→b

a) Find the string w generated by the grammar that has two leftmost derivations. Show
the derivations.
b) Show the two derivation trees for the strings.

c) Find an equivalent unambiguous context-free grammar.


Answer:
A desirable grammar is G = ({S, A}, {a, b}, S, P) with the productions
S → Ab
A → Aa | a

d) Give the unique leftmost derivation and derivation tree for the string s generated
from the unambiguous grammar above

You might also like