GATE / CS ARCHIVE

SUBJECT COLLECTION

Theory of Computation · Context-Free Grammars & Languages

53 questions, newest first

GATE CSE has asked 53 questions on Context-Free Grammars & Languages (Theory of Computation) from 1992 to 2026, in 33 of the 46 papers; the most recent is in GATE CSE 2026 Set 2. In the last five years it carried 2.1 marks per 100-mark paper on average, against 1.9 since 2003. Since 2014 the questions have been 16 MCQ, 7 MSQ and 1 NAT. 6 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
Show
1 mark · MSQ
Report issue

Ambiguity and derivation uniqueness

Context-Free Grammars & Languages

Question 25, GATE 2026: For the grammar S→aSbS | bS | ε, decide: A the grammar is ambiguous; B abb has two distinct derivations; C abab has only one rightmost derivation; D the generated language is undecidable.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, B, C

Givens and goal

For the grammar S→aSbS | bS | ε, decide: A the grammar is ambiguous; B abb has two distinct derivations; C abab has only one rightmost derivation; D the generated language is undecidable.

Approach

Exhibit different parse structures for the ambiguity claim, prove uniqueness for the specific other word, and use the decidability of context-free membership.

Step-by-step solution

Work with the grammar S → aSbS | bS | ε. Here ε denotes the empty string.

1. The string abb admits these two different leftmost derivations:

   - S ⇒ aSbS ⇒ abS ⇒ abbS ⇒ abb.
   - S ⇒ aSbS ⇒ abSbS ⇒ abbS ⇒ abb.
   In the first derivation, the extra b comes from the final S of the root production. In the second, it comes from the first S. Thus they represent different parse trees, not just different orders of expanding one tree. Both ambiguity (A) and the existence of two distinct derivations (B) follow.

2. For abab, the root must use aSbS because the word starts with a. Its explicit b must be the b in position 2 or position 4. Choosing position 4 would require the first S to generate ba. That is impossible: every nonempty word generated by S ends in b. To prove this invariant, bS ends in b if its child is empty or ends in b if its child does; aSbS has the same property through its explicit b and final child.
3. Consequently the explicit root b is in position 2, its first child is ε, and its final child generates ab. The latter has only the construction aSbS with both children empty. Therefore abab has exactly one parse tree and one rightmost derivation, establishing C. One rightmost derivation is S ⇒ aSbS ⇒ aSbaSbS ⇒ aSbaSb ⇒ aSbab ⇒ abab.
4. This is a context-free grammar. Membership in a context-free language is decidable, for example by converting to a suitable normal form and applying a finite parsing algorithm. Therefore the undecidability claim D is false.

Common error: Two unrestricted derivations can differ solely in expansion order. Ambiguity requires different parse trees, equivalently different leftmost derivations or different rightmost derivations; the witness above meets that stronger test.

Parse-tree distinction for abb can be made explicit by writing the root’s first and last S expansions as a pair. One tree has root a S b S with child words (ε,b); the other has child words (b,ε). Both concatenate to abb, but they attach the additional b to different children. This makes the ambiguity evidence structural, not just a consequence of expanding independent children in different orders.

Original paper, page 20 · Answer key

2 marks · MSQ
Report issue

Counting terminal-symbol invariants

Context-Free Grammars & Languages

Question 52, GATE 2026: The grammar is S→abaABA bba (spaces only separate symbols), A→aaBBA b | bB abaa, and B→aBb | ab. For a generated string s let n₁ count a’s and n₂ count b’s. Assess A: some n₁<n₂; B: always n₁≥n₂; C: some n₁>2n₂; D: always n₁≤2n₂.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, D

Givens and goal

The grammar is S→abaABA bba (spaces only separate symbols), A→aaBBA b | bB abaa, and B→aBb | ab. For a generated string s let n₁ count a’s and n₂ count b’s. Assess A: some n₁<n₂; B: always n₁≥n₂; C: some n₁>2n₂; D: always n₁≤2n₂.

Approach

Track the pair of terminal counts through productions and prove invariant inequalities by induction rather than enumerate all words.

Step-by-step solution

Write a(w) and b(w) for the numbers of the two terminals in a generated word w.

1. Every expansion of B produces ak bk for some k≥1. Hence its two counts are equal.
2. For A's terminating alternative, a B expansion with k of each terminal gives counts a=k+3 and b=k+2. Thus b≤a≤2b.
3. Suppose a word derived from the recursive A child has counts x,y satisfying y≤x≤2y. The surrounding recursive production contributes two a terminals and one b terminal, plus two B words. If those B words together contribute K of each terminal, the new counts are x+K+2 and y+K+1. Their difference is (x−y)+1≥0. Also twice the new b count minus the new a count is (2y−x)+K≥0. Thus both inequalities are invariant under recursion.
4. The start production combines two A words, one B word and an outer string with three a terminals and three b terminals. Adding the counts preserves b≤a≤2b. Consequently every complete word satisfies the two bounds in B and D. The existential violations in A and C are impossible.

Common error: Counting only the explicitly written terminals of a production omits arbitrarily long balanced B expansions. The invariant accounts for them without enumerating strings.

Terminal-count bookkeeping

    Production component             Added a's   Added b's
    A→bB abaa, excluding B                 3           2
    A→aaBBA b, excluding B,B,A             2           1
    S→aba A B A bba, excluding A,B,A       3           3

For example, choosing B→ab and the terminating A alternative gives each A counts (4,3). The shortest choices in S therefore yield totals (3,3)+(4,3)+(1,1)+(4,3)=(12,10), consistent with both bounds. This example illustrates the invariant but the induction, not the example alone, proves it for arbitrarily deep recursion.

Original paper, page 40 · Answer key

1 mark · MSQ
Report issue

Proving grammar ambiguity

Context-Free Grammars & Languages

Question 29, GATE 2026: Which grammars are ambiguous? A S→aSb|ε; B E→E+E|E*E|id; C S→aS|Sa|ε; D S→aS|ε.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C

Givens and goal

Which grammars are ambiguous? A S→aSb|ε; B E→E+E|E*E|id; C S→aS|Sa|ε; D S→aS|ε.

Approach

Exhibit two parse trees for ambiguous choices and explain uniqueness for the others.

Step-by-step solution

1. B generates id+id*id with root production E→E+E, whose right child expands by multiplication. It also generates the same string with root E→E*E, whose left child expands by addition. These are different tree structures, corresponding to id+(id*id) and (id+id)*id.
2. C already has two trees for the one-letter string a: use S→aS→aε, or S→Sa→εa. The root productions differ, so this is genuine ambiguity, not merely a different order of expanding independent nodes.
3. For A, every nonempty string is aⁿbⁿ. The first rule must be aSb and must be applied exactly n times, followed by ε. No alternative tree is possible for that n.
4. For D, a string aⁿ requires exactly n applications of aS followed by ε. Again the chain is unique.
5. Therefore B,C are ambiguous. The existence of an unambiguous grammar for the same language would not remove ambiguity from these particular grammars.

Original paper, page 22 · Answer key

2 marks · MSQ
Report issue

Context-free constraints on four blocks

Context-Free Grammars & Languages

Question 48, GATE 2026: Consider strings a^i b^j c^k d^ℓ with i,j,k,ℓ≥0. Which restrictions yield context-free languages? A i+k=j+ℓ; B i=k and j=ℓ; C i=ℓ and j=k; D i+j=k+ℓ.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, C, D

Givens and goal

Consider strings ai bj ck d^ℓ with i,j,k,ℓ≥0. Which restrictions yield context-free languages? A i+k=j+ℓ; B i=k and j=ℓ; C i=ℓ and j=k; D i+j=k+ℓ.

Approach

Give stack constructions for the positive choices and a pumping-lemma contradiction for the crossing equalities.

Step-by-step solution

1. For A, scan the string while tracking block order a*b*c*d* in finite control. Maintain the signed difference (#a+#c)−(#b+#d). Store its sign in the finite state and its absolute value as repeated stack symbols. On a or c, add 1; on b or d, subtract 1, popping if this cancels the current sign and pushing otherwise. Accept exactly when the final difference is zero. This is a pushdown automaton, so A is context-free.
2. For D, check block order, push one marker for every a or b, and pop one for every c or d. Reject an attempt to pop an empty counter and accept exactly with zero balance at the end. This enforces i+j=k+ℓ.
3. For C the equalities are nested. The grammar S→aSd|T, T→bTc|ε generates precisely ai bj cj di: outer recursion matches a with d, inner recursion matches b with c. Hence C is context-free.
4. For B suppose a context-free pumping length p exists, and take w=ap bp cp dp. Every decomposition w=uvxyz with |vxy|≤p and |vy|>0 has its pumping window in one block or at most two adjacent blocks. It cannot reach both members of either matching pair a/c or b/d.
5. Pump v and y away, using exponent 0. At least one block count decreases, and its matching nonadjacent block is untouched. The equality i=k or j=ℓ therefore fails. This contradicts the pumping lemma for every allowed decomposition, so B is not context-free.
6. The correct selections are A,C,D. Two independent crossing counters cannot in general be handled by the single last-in-first-out store used for nested matching.

Original paper, page 34 · Answer key

1 mark · MCQ
Report issue

Deriving a grammar’s two alternatives

Context-Free Grammars & Languages

Question 19, GATE 2025: A grammar has start symbol S and productions S→aaB | Abb, A→a | aA, B→b | bB. Choose its language: A {a²b^n:n≥1}∪{a^nb²:n≥1}; B {a^nb^(2n):n≥1}∪{a^(2n)b^n:n≥1}; C {a^nb^n:n≥1}; D {a^(2n)b^(2n):n≥1}.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: First identify what each helper nonterminal can generate, then substitute into each start production.
A→a is the base case. Each application of A→aA adds one a before eventually taking the base case, so A generates exactly an for n≥1. Similarly B generates exactly bn for n≥1. Neither can generate the empty string.
Taking S→aaB fixes the number of a’s at two and allows any positive number of b’s. Taking S→Abb allows any positive number of a’s and fixes the number of b’s at two. The language is the union of these alternatives, which is A.
For example aabbb is generated by the first branch, and ab b (the word abb) by the second. The grammar does not synchronize the two counts, so the equality and ratio conditions in B–D are unjustified. The common word aabb belongs to both branches but is counted just once in the language.

Original paper, page 19 · Answer key

2 marks · MCQ
Report issue

Comparing two three-block context-free languages

Context-Free Grammars & Languages

Question 45, GATE 2025: For m,n≥1, L1={a^m b^m c^(m+n)} and L2={a^m b^n c^(m+n)}. Which is true? A both context-free; B only L1 context-free; C only L2 context-free; D neither context-free.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Approach: Give a constructive grammar for L2 and a pumping-lemma contradiction for L1.
L2 is generated by S→aSc | aTc and T→bTc | bc. Each use of S adds one a and one trailing c, with at least one a forced before moving to T. Each T production similarly pairs a b with a c and forces at least one b. The derived words are exactly am bn cm+n, so L2 is context-free.
Suppose L1 were context-free with pumping length p. Choose w=ap bp cp+1, which is in L1 with m=p,n=1. In a pumping decomposition w=uvxyz, |vxy|≤p and |vy|>0. The window cannot touch both the a and c blocks, because it would have to span all p b’s between them.
Let Δa,Δb,Δc count those symbols in the pumped parts v and y. If pumping disrupts block order, it already leaves L1. Otherwise, if Δa≠Δb, pumping down or up breaks equality of a and b counts. If Δa=Δb>0, then Δc=0; pumping up makes the common count at least p+1 while c remains p+1, violating the strict requirement c>m. Finally if Δa=Δb=0, then Δc>0; pumping down leaves at most p c’s while a=b=p, again violating c>m.
All possibilities fail. Thus L1 is not context-free, giving C. The unrestricted extra c’s in L1 do not eliminate the need to enforce both equality and a strict count comparison.

Original paper, page 45 · Answer key

2 marks · NAT
Report issue

Count productions in a CNF derivation

Context-Free Grammars & Languages

Question 59, GATE 2024: A context-free grammar in Chomsky Normal Form has terminals {a,b,c} and 10 variables including start symbol S. It derives w=a³⁰b³⁰c³⁰. How many rule applications occur in a derivation S⇒*w?
Tap to enlarge
Answer & solution

Official answer: 179

Approach: In Chomsky Normal Form, a nonempty derivation uses binary-variable rules A→BC and terminal rules A→a. Count each kind using the final word length.
1. The word contains 30+30+30=90 terminal symbols. Each requires exactly one terminal-producing rule application, so there are 90 such applications.
2. Initially there is one variable occurrence. Each binary rule replaces one variable with two and therefore increases the total number of current symbol occurrences by one. Terminal rules replace one symbol with one and do not change that total.
3. To grow from one symbol to a final string of 90 symbols requires exactly 89 binary-rule applications. Equivalently, the variable branching tree is full binary with 90 leaves and 89 internal branching nodes.
4. Total rule applications =90+89=179.
Answer: 179. The ten available variable names can be reused and do not bound the number of variable occurrences. Derivation order does not change these counts; the CNF epsilon exception is irrelevant to this nonempty word.

Original paper, page 34 · Answer key

2 marks · MSQ
Report issue

Count terminals by production applications

Context-Free Grammars & Languages

Question 52, GATE 2024: Grammar S→aS | aSbS | c generates w. Let na,nb,nc count the respective terminals in w. Which always hold? A na>nb; B na>nc−2; C nc=nb+1; D nc=2nb.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C

Approach: Count how each production changes both the terminal totals and the outstanding nonterminals.

1. Derivation. Let u count applications of S→aS and v count applications of S→aSbS. Then na=u+v and nb=v. A derivation starts with one S; unary rules leave the number of outstanding S unchanged, binary rules increase it by one, and S→c decreases it by one. Thus nc=v+1, proving C. Also na≥v=nc−1>nc−2, proving B. A fails for w=c, where both a and b counts are 0; D also fails for c, whose c count is 1 and b count 0.

2. Explanation and checks. Each binary rule contributes one b and creates one additional S requiring eventual termination by a c-rule. Starting with one S therefore requires exactly one more terminating c-rule than binary rules. Unary aS rules can add any extra a’s without changing b or c counts. Consequently na−nb equals their nonnegative number; it may be zero, so A’s strict inequality is too strong. In B the weaker strict bound still holds because na≥nc−1 is an integer stronger than na>nc−2.

Original paper, page 36 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 39, GATE 2023: For grammar S→aSb|X and X→aX|Xb|a|b with start S, identify the language: A (a+b)*; B a*(a+b)b*; C a*b*(a+b); D a nonregular language.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

X generates every nonempty string of the form aⁱbʲ: its alternatives prepend a or append b and terminate at a or b. These operations never introduce a b before an a. Wrapping with S→aSb preserves that form and adds no strings beyond those already generated by X. Thus L(G)=a*b* minus epsilon, equivalently a*(a+b)b*, option B. The grammar may be context-free while its generated language is regular.

Teaching detail. For completeness, any aⁱbʲ with i+j≥1 can be derived from X. If i≥1, terminate with a and use i−1 prepending steps plus j appending steps. If i=0 and j≥1, terminate with b and append j−1 further b's. Conversely every X-production preserves the a-before-b ordering and cannot create ε. The S wrapping rule also preserves this ordering and nonemptiness. Hence it neither removes any X-generated word nor introduces a word outside the claimed regular language.

Original paper, page 40 · Answer key

2 marks · MSQ
Report issue

Context-Free Grammars & Languages

Question 48, GATE 2022: L1={ww:w∈{a,b}*}, L2={a^n b^n c^m:m,n≥0}, L3={a^m b^n c^n:m,n≥0}. Select FALSE claims: A L1 not context-free, L2/L3 deterministic context-free; B neither L1 nor L2 context-free; C L2,L3 and their intersection all context-free; D neither L1 nor its complement context-free.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B,C,D

The copy language L1={ww} is not context-free: intersecting with a+b+a+b+ gives {aⁱbʲaⁱbʲ: i, j>0}. For pumping length p, aᵖbᵖaᵖbᵖ has any pumping window of length at most p within at most two adjacent blocks. Pumping changes at least one block without changing its matching nonadjacent block, or creates extra alternations, contradicting this language’s form. L2 and L3 are deterministic context-free by pushing one block and matching the adjacent equal-count block; their intersection is {aⁿbⁿcⁿ}, not context-free. Thus A is true and B, C are false. D is also false because the complement of L1 is context-free: include all odd-length words, and for even length choose two different symbols in corresponding half-positions. Let Ea={uav:|u|=|v|} and Eb={ubv:|u|=|v|}; each is context-free via Sa→cS_a d | a for c, d∈Σ, and similarly Sb. E_aE_b ∪ E_bE_a describes exactly the even noncopies, since the two centers differ and are separated by half the total length. Closure under concatenation and union proves the complement claim. Select false statements B, C, D.

For the complement construction, Ea contains odd strings whose center is a and Eb those whose center is b. Concatenating lengths 2r+1 and 2s+1 puts their centers r+s+1 positions apart, exactly half the total length. Thus choosing unequal centers witnesses unequal half-positions. Conversely any mismatching pair in two equal halves determines such a split. Odd-length strings are automatically noncopies and form a regular language.

Original paper, page 32 · Answer key

2 marks · MSQ
Report issue

Context-Free Grammars & Languages

Question 41, GATE 2021: For w,x∈{0,1}*, w^R is reversal. Which languages are context-free? A {wxw^Rx}; B {ww^Rxx^R}; C {wxw^R}; D {wxx^Rw^R}. Empty w and x are allowed.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B; C; D

B concatenates two even-palindrome languages and is context-free. C is all binary strings, since w may be empty. D is exactly the even palindromes: wx xR wR=(wx)(wx)R, and x may be empty. A is not context-free. Here is a pumping argument that accounts for empty w and x rather than merely calling the dependencies crossed. Its intersection with 0+1+0+1+ consists of 0a 1b0c1d satisfying either a=c and b-d is a nonnegative even integer, or b=d and c-a is a nonnegative even integer. This follows by examining where w and x split the first half: either w=0a 1k and x=1d, or w=0a and x=1b0k. The word 0p1p0p1p belongs. A pumping window of length at most p meets at most two adjacent blocks. Pumping a piece crossing a boundary creates extra runs when repeated. Otherwise pumping changes only those adjacent block lengths. If both change, use upward pumping for blocks 1/2 or 3/4 and downward pumping for blocks 2/3 to violate both conditions. If only one changes, increase block 1 or 4, or decrease block 2 or 3. Every possible pumping decomposition thus has a failing exponent. Closure under regular intersection proves A is not context-free. Answer B, C, D.

Constructive positive proofs: let E→0E0|1E1|ε generate even palindromes. For B use start H→EE, concatenating two such words. For C use H→0H|1H|ε, since taking w=ε yields arbitrary x. For D use E again, because the concatenated first half wx is arbitrary. These grammars establish membership instead of relying only on a visual impression of symmetry; A requires the non-CFL argument above.

Original paper, page 78 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 32, GATE 2020: L1={wxyx:w,x,y∈{0,1}*}; L2={xy:x,y∈{a,b}*, |x|=|y|, x≠y}. Which statement is true? A L1 regular and L2 context-free; B L1 context-free but not regular, L2 context-free; C neither context-free; D L1 context-free, L2 not context-free.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Worked explanation

Empty strings are permitted for the starred variables in L1. Setting x=y=ε produces every binary w, so repeated appearances of x impose no restriction at all. For L2, the grammar below is more informative than incorrectly invoking closure under complement of context-free languages. Write V→b|aVa|aVb|bVa|bVb in addition to the displayed U productions. U and V wrap arbitrary equal numbers of symbols around different central letters. Concatenating them makes the central positions differ by half the total length. Conversely, if unequal halves of length m differ at position r, choose first component length 2r−1 and second length 2(m−r)+1. Their centers land at r and m+r. This proves both grammar inclusions, hence context-freeness without claiming that arbitrary complements of CFLs are CFLs.

Derivation and answer

For L1 choose x=ε, y=ε and let w be any binary string; hence L1=Σ*, regular. For L2 a CFG can guess a differing position in the two equal-length halves. Explicitly let U generate all odd-length strings with central a via U→a|aUa|aUb|bUa|bUb, and V generate those with central b by the analogous rules with base b. S→UV|VU then generates exactly even-length strings whose two halves differ somewhere. If component lengths are 2i+1 and 2j+1, their differing central symbols are i+j+1 positions apart, exactly half the total length. Conversely any mismatch supplies such a split. Thus L2 is context-free and A holds.

Original paper, page 11 · Answer key

2 marks · MCQ
Report issue

Nested versus crossing language constraints

Context-Free Grammars & Languages

Question 31, GATE 2019: Over {a,b}, choose the language that is not context-free: A {ww^R}; B {wa^n b^n w^R}; C {wa^n w^R b^n}; D {a^n b^i : i is n, 3n, or 5n}. Here w ranges over all finite binary-alphabet strings, n≥0, and w^R is reversal.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. To distinguish these languages, separate nested matching, which a stack supports, from overlapping matching constraints. A is generated by S→aSa|bSb|ε, so it is context-free.
2. B is also nested: use S→aSa|bSb|T and T→aTb|ε. The S rules build w and its reversal around the central equal-length a/b runs. D is a union of three context-free languages; for each r in {1,3,5}, Sr→aS_r br|ε generates its fixed-ratio component. CFLs are closed under finite union.
3. For C, intersect with the regular language a+ b a+ b a+ b+. CFLs are closed under intersection with regular languages. In this pattern the two isolated b symbols force w=ai b at, and n=k, giving precisely K={ai b aj b ai bk : i,k≥1, j≥k, j−k even}, with j=2t+k.
4. Suppose K were context-free with pumping length p. Choose s=ap b ap b ap bp. In any CFL pumping decomposition s=uvxyz, |vxy|≤p and |vy|>0. The window cannot touch both the first and third a-runs, nor both the middle a-run and final b-run.
5. If v or y contains a separator b, pumping down removes a required separator and destroys the regular pattern. Otherwise, pumping any first/third-run a changes one side of their required equality without changing the other. This also leaves K.
6. The remaining possibility pumps only middle-run a's or only final-run b's. Pumping down middle a's makes j<k; pumping up final b's makes k>j. Thus every decomposition fails for some pump count, contradicting the pumping lemma. K, and hence C, is not context-free. Answer C.

Original paper, page 10 · Answer key

2 marks · MCQ
Report issue

Which counting languages are context-free

Context-Free Grammars & Languages

Question 35, GATE 2018: Over words a^m b^n c^p d^q, with all exponents nonnegative, define I m+p=n+q; II m=n and p=q; III m=n=p and p≠q; IV mn=p+q. Which are context-free? A I,IV; B I,II; C II,III; D II,IV.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Method

One stack can track a single signed difference, so the crossed-looking equality in I is still manageable; a finite-state component checks block order. II has grammar S→AB, A→aAb|ε, B→cBd|ε. For III, restricting to positive a,b,c blocks and no d leaves the standard non-CFL equal-three-count language. For IV, the multiplication constraint cannot be reduced to one additive counter. The pumping argument below makes this precise, including decompositions that cross a block boundary rather than assuming where the pumped substrings lie.

Solution

I is context-free: a PDA tracks the signed running difference(#a+#c)−(#b+#d) with one stack symbol for positive excess and another for negative excess, canceling opposite contributions; finite control enforces a*b*c*d* order. II is the concatenation{an bn}{cp dp}, a CFL. For III, intersect with a+b+c+ (no d); the result is{an bn cn: n≥1}, not context-free. For IV, suppose pumping length p and take ap bp cp². Pumping up either disrupts block order or adds α a symbols, β b symbols and γ c symbols, with α+β+γ≤p and at least one added symbol. If α+β=0, the product stays p² but γ>0. Otherwise the required increase in c is p(α+β)+αβ≥p, while actual γ≤p−α−β<p. In every case the equality mn=p+q fails (here the fourth-block length remains 0). Thus only I and II are CFL, B.

Original paper, page 14 · Answer key

1 mark · MCQ
Report issue

Context-Free Grammars & Languages

Question 10, GATE 2017: Grammar: S→abScT | abcT; T→bT | b. Which language? A {(ab)^n(cb)^n:n≥1}; B {(ab)^n c b^m1 c b^m2 ... c b^mn:n,m1,...,mn≥1}; C {(ab)^n(cb^m)^n:m,n≥1}; D {(ab)^n(cb^n)^m:m,n≥1}.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. T→bT|b generates bm for any m≥1. Different occurrences of T are expanded independently.

2. After k applications of S→abScT and then S→abcT, there are k+1 leading ab pairs and k+1 trailing cT groups. Put n=k+1≥1.

3. Replace each T by its own positive b count. The result is (ab)n cbm1 ... cbmn, B. Options A and C unnecessarily force all b blocks to have equal size.

Original paper, page 6 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 34, GATE 2017: Grammar S→SaS | aSb | bSa | SS | ε. Which string is NOT generated? A abab; B aaab; C abbaa; D babba.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step through it (4 derivations)

1. Use the invariant #a−#b≥0. ε starts at zero; SaS increases it by one; aSb and bSa preserve it; SS adds two nonnegative differences. All generated strings satisfy the invariant.

2. babba has two a’s and three b’s, violating it, so D cannot be generated. To ensure it is the only excluded option, exhibit constructions for the others.

3. For abab, use S⇒SS⇒aSb aSb⇒abab. For aaab, use S⇒aSb⇒aSaSb⇒aaSb⇒aaSaSb⇒aaaSb⇒aaab. For abbaa, concatenate ab,ba,a using SS, using S⇒aSb⇒ab, S⇒bSa⇒ba, and S⇒SaS⇒a (ε reductions are implicit). Thus A,B,C are generated.

Original paper, page 15 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 37, GATE 2017: G1: S→aSb | T, T→cT | ε. G2: S→bSa | T, T→cT | ε. Classify L(G1)∩L(G2): A finite; B infinite regular; C context-free but not regular; D recursive but not context-free.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. Applying G1’s outer production n times wraps n a’s before and n b’s after T. T supplies any cm, so L(G1)={an cm bn:n,m≥0}.

2. Similarly L(G2)={bn cm an:n,m≥0}. Any G1 word with n>0 starts with a, while any G2 word with positive outer count starts with b. Such words cannot agree.

3. Common words must have zero outer count in both grammars, leaving exactly c*. This contains ε,c,cc,..., so it is infinite and regular. Answer B.

Original paper, page 17 · Answer key

1 mark · MCQ
Report issue

Context-Free Grammars & Languages

Question 16, GATE 2017: Start variable S has grammar S→XY, X→aX|a, Y→aYb|ε. Which language is generated? A:{a^m b^n | m≥n,n>0}. B:{a^m b^n | m≥n,n≥0}. C:{a^m b^n | m>n,n≥0}. D:{a^m b^n | m>n,n>0}.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. X contributes at least one a: using aX k-1 times and then a gives ak for every k≥1. It cannot derive ε.

2. Y contributes matched numbers of a and b. Applying aYb n times and then ε gives an bn for every n≥0. Therefore XY yields ak+nbn, so m=k+n>n and n may be zero.

3. Conversely, if m>n≥0, choose k=m-n≥1 in X and n matching pairs in Y to derive the desired string. The characterization is exact, C. For example a is generated, ruling out options requiring n>0; equal counts are impossible because X supplies extra a symbols.

Original paper, page 7 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 40, GATE 2017: L1={a^p | p prime}; L2={a^n b^m c^(2m) | n,m≥0}; L3={a^n b^n c^(2n) | n≥0}; L4={a^n b^n | n≥1}. Which claims hold? I:L1 is CFL but not regular. II:L2 is not CFL. III:L3 is not CFL but recursive. IV:L4 is deterministic CFL. A:I,II,IV. B:II,III. C:I,IV. D:III,IV.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

1. I is false. If L1 were context-free, choose a prime q above its pumping length. Any pumping decomposition increases the unary length by a positive d when pumped once extra. Pumping q extra copies gives length q+qd=q(1+d), a composite number, contradicting membership.

2. II is false: a CFG for L2 is S→aS|B and B→bBcc|ε. It independently generates any number of a symbols, then matches each b with two c symbols.

3. III is true. L3 is decidable by checking symbol order and counting the three runs. If it were CFL, inverse homomorphism under h(a)=a,h(b)=b,h(c)=cc would make {an bn cn} context-free, a contradiction to that standard non-CFL result. IV is true: a deterministic PDA pushes for a symbols, switches on the first b and pops for each b, accepting exactly matching positive counts. Therefore III and IV, D.

Original paper, page 15 · Answer key

1 mark · MCQ
Report issue

Context-Free Grammars & Languages

Question 16, GATE 2016: Which language is generated by S→aS | bS | ε? A: {a^n b^m:n,m≥0}. B: All words over {a,b} with equal numbers of a and b. C: {a^n:n≥0} union {b^n:n≥0} union {a^n b^n:n≥0}. D: {a,b}*.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

At every step S can emit either a or b and recurse, then terminate with epsilon. Every finite binary-alphabet word, including epsilon, is generated, giving {a, b}*, D.

For any desired word w, choose the production beginning with its next character repeatedly, then use S→ε after its final character. For example baba follows S⇒bS⇒baS⇒babS⇒babaS⇒baba. Conversely, every production emits only a or b, so no other alphabet symbols can appear. This two-direction argument proves equality with {a,b}*, rather than merely showing a few example strings are generated. The ε production explicitly includes the empty word.

Original paper, page 8 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 42, GATE 2016: Consider grammars G1:S→aS|B, B→b|bB; and G2:S→aA|bB, A→aA|B|ε, B→bB|ε. All options describe languages {a^m b^n} with nonnegative integer exponents. Which pair of constraints corresponds to G1,G2 respectively? A:(m>0 or n>0),(m>0 and n>0). B:(m>0 and n>0),(m>0 or n≥0). C:(m≥0 or n>0),(m>0 and n>0). D:(m≥0 and n>0),(m>0 or n>0).
Tap to enlarge
Your answer
Answer & solution

Official answer: D

G1 emits any number of initial a symbols, then at least one b, so its language is am bn with m≥0, n>0. G2 chooses an initial a or b, then permits all remaining a symbols before its b symbols; it generates every nonempty am bn and not epsilon. Thus m>0 or n>0, giving D.

In G1 the only way to eliminate S is to enter B, and B has no ε rule, so a b is compulsory. In G2 the first production emits a or b, ruling out ε; after that, ε rules allow stopping. Once a derivation enters B, it emits only b symbols, so neither grammar can place an a after a b. Witnesses distinguish boundaries: b is in both languages, a is in G2 only, and ε is in neither. These tests select the inequalities in D.

Original paper, page 16 · Answer key

1 mark · MCQ
Report issue

Context-Free Grammars & Languages

Question 17, GATE 2016: L1 has grammar S1→aS1b|ε, and L2 has S2→abS2|ε. P: L1 is regular; Q: L2 is regular. A both; B P only; C Q only; D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

L1={an bn: n≥0}, nonregular by pumping/distinguishable prefixes. L2=(ab)* is regular. Therefore P false, Q true, C.

Each use of S1→aS1b adds one a and one b at opposite ends; ε stops the derivation. Pumping an a-prefix of ap bp changes only the a count, proving nonregularity. In contrast, S2 repeatedly appends the fixed block ab; a three-state complete DFA can check alternating a,b blocks and reject violations.

Original paper, page 29 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 43, GATE 2016: L1={a^n b^m c^(n+m):m,n≥1}; L2={a^n b^n c^(2n):n≥1}. A both context-free; B only L1; C only L2; D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

L1 is generated by S→aSc|aBc and B→bBc|bc: each a or b adds one trailing c, with positive counts of both. Thus L1 is context-free. If L2 were context-free, its inverse image under h(a)=a, h(b)=b, h(c)=cc would be {an bn cn: n≥1}, which is not context-free by the pumping lemma. CFL closure under inverse homomorphism therefore proves L2 non-context-free. Answer B.

A pushdown intuition for L1 pushes one marker per a or b and pops one per c, with finite control enforcing the a-then-b-then-c phases. L2 must enforce three linked quantities. Under h(c)=cc, an input an bn cn maps exactly to an bn c2n, transferring the known non-CFL obstruction through inverse homomorphism.

Original paper, page 37 · Answer key

2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 44, GATE 2015: Which languages are context-free? L1={a^m b^n a^n b^m | m,n≥1}; L2={a^m b^n a^m b^n | m,n≥1}; L3={a^m b^n | m=2n+1}. Exponents denote nonnegative integer counts unless constrained otherwise. Options: (A) L1 and L2 only; (B) L1 and L3 only; (C) L2 and L3 only; (D) L3 only.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. L1=am bn an bm has nested dependencies: S→aSb|aTb and T→bTa|ba generates m, n≥1, so it is CFL.

2. L3 is generated by U→aaUb|a for n≥0. L2=am bn am bn is not CFL: for pumping length p, choose ap bp ap bp. The pumping window of length≤p touches at most two adjacent blocks, so pumping changes at least one block but cannot change its corresponding nonadjacent equal block; if a pumped piece crosses a boundary, pumping can also destroy the four-block form. Thus pumping fails. L1, L3 only, B.

Original paper, page 68 · Answer key

2 marks · MCQ
Report issue

Regular and unequal-block languages

Context-Free Grammars & Languages

Question 32, GATE 2013: Let L1={0^p1^q0^r:p, q, r≥0} and L2={0^p1^q0^r:p, q, r≥0, p≠r}. Which is false? A L2 is context-free. B L1∩L2 is context-free. C complement(L2) is recursive. D complement(L1) is context-free but not regular.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

L1=0*1*0* is regular. L2, whose outer zero-block counts differ, is context-free: split into more leading zeros or more trailing zeros and use a pushdown comparison. Since L2⊆L1, their intersection is L2 and context-free. L2 is decidable by finite examination of the block counts (including the possible zero middle block), so its complement is recursive. The complement of regular L1 is regular and consequently context-free. Thus option D, which says this last complement is context-free but not regular, has a false not-regular clause. It is regular and therefore context-free; D is the requested false statement.

For the decisive option D, L1 is represented directly by 0*1*0*, hence is regular. Complement a complete DFA by swapping accepting and nonaccepting states; this proves its complement is regular as well. Being regular implies context-free, but it contradicts the extra words “not regular.” For L2, a pushdown machine can compare the first and final zero blocks when a nonempty middle 1block separates them; the all-zero case can be treated separately. A finite input can also be tested by enumerating its finitely many possible block splits, giving decidability without needing a closed form.

Original paper, page 7 · Answer key

2 marks · MCQ
Report issue

Equal three-block language is not context-free

Context-Free Grammars & Languages

Question 26, GATE 2011: Let L1={0^p1^q : p,q in N}, L2={0^p1^q : p=q in N}, and L3={0^p1^q0^r : p=q=r in N}. Which statement is NOT TRUE?

(A) A PDA can recognize L1 and L2
(B) L1 is regular
(C) All three languages are context-free
(D) Turing machines can recognize all three languages
Tap to enlarge
Your answer
Answer & solution

Official answer: C

L1=0*1* is regular and hence recognizable by a PDA; L2 matches two block lengths and is context-free. L3={0n1n0n} is not context-free: for pumping length p, choose 0p1p0p. A pumping window of length≤p cannot reach both outer zero blocks. Pumping a nonempty part therefore changes at most two blocks while leaving another unchanged, or destroys the block order, so equality of all three counts fails. All three are decidable by a Turing machine. Thus the claim that all three are context-free is false, C.

A PDA for L2 pushes one marker per initial 0 and pops one per following 1, rejecting if lengths differ or block order is wrong. L1 does not even need the stack. L3 requires coordinating three unbounded block lengths, and the pumping argument establishes the failure of context-freeness rather than merely asserting that a particular PDA design fails. A Turing machine can repeatedly mark one symbol in each block, so the failure is specific to context-free recognition, not computability.

Original paper, page 7 · Answer key

2 marks · MCQ
Report issue

Context-free linear counting constraints

Context-Free Grammars & Languages

Question 40, GATE 2010: For nonnegative integers i,j, let L₁={0^i1^j:i≠j}, L₂={0^i1^j:i=j}, L₃={0^i1^j:i=2j+1}, L₄={0^i1^j:i≠2j}. Which statement is true?
Options: (A) Only L₂ is context-free; (B) Only L₂ and L₃ are context-free; (C) Only L₁ and L₂ are context-free; (D) All are context-free.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

All four languages are context free, D. L₂ has grammar S→0S1|ε. L₃ has S→00S1|0. For L₁, split into i>j and i<j: matched pairs 0j1j can have a nonempty zero prefix or nonempty one suffix, respectively, and CFLs are closed under concatenation with regular languages and finite union. For L₄, let E={02j1j}, generated by S→00S1|ε. The part i>2j is 0⁺E. For i<2j, even i gives E1⁺, while odd i gives 0E1⁺. These are all CFLs by the same closure properties, proving the claim without incorrectly invoking closure of CFLs under complement.

For the strict-inequality cases, the closure argument is constructive. L₁=0⁺{0j1j} ∪ {0i1i}1⁺. For L₄, write E={02j1j}; the cases i>2j, i<2j with i even, and i<2j with i odd are respectively 0⁺E, E1⁺ and 0E1⁺. This separates the arithmetic conditions without relying on the generally false statement that context-free languages are closed under complement.

Original paper, page 10

1 mark · MCQ
Report issue

Odd-palindrome grammar

Context-Free Grammars & Languages

Question 12, GATE 2009: For grammar S→aSa|bSb|a|b over {a, b}, identify the language: A all palindromes; B odd-length palindromes; C all strings beginning and ending with the same symbol; D even-length palindromes.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

The base productions produce a single a or b. Every recursive production adds the same symbol at both ends, preserving the palindrome property and odd length. Conversely strip matching outer pairs from any odd palindrome until one central symbol remains, yielding a derivation. Thus exactly all odd-length palindromes, B.

Every derivation starts from a one-symbol base a or b and adds matching symbols around the outside. Length therefore remains odd and the string remains a palindrome. Conversely, removing the equal end symbols from any odd palindrome repeatedly reaches its one-symbol centre, giving a reverse derivation. Even palindromes are excluded because no ε base exists; a string such as aabba begins/ends alike but is not generated.

Original paper, page 3

2 marks · MCQ
Report issue

Intersection leaves an equal-block prefix

Context-Free Grammars & Languages

Question 40, GATE 2009: L1={a^m b^m c a^n b^n:m, n≥0}; L2={a^i b^j c^k:i, j, k≥0}; L=L1∩L2. Classify: A not recursive; B regular; C context-free but nonregular; D RE but not context-free.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

L1 contains exactly one c between am bm and an bn. For membership in L2=a*b*c*, no a or b may follow that c, so n=0. Thus the intersection is{am bm c:m≥0}. A grammar for it is S→Tc, T→aTb|ε. The language is context-free and nonregular: removing its fixed final c by regular right quotient would otherwise make{am bm} regular. Therefore C.

L2 permits only a block of a’s, then b’s, then c’s. A word of L1 has one central c, so its trailing an bn must be empty to fit L2. Thus L={am bm c:m≥0}. The grammar S→Tc and T→aTb|ε proves context-freeness. For nonregularity, removing the final fixed c from a hypothetical finite automaton would recognize the classic nonregular language am bm. Hence C, not the much stronger undecidability alternatives.

Original paper, page 9

1 mark · MCQ
Report issue

Unary prime-length language

Context-Free Grammars & Languages

Question 9, GATE 2008: Which statement holds for {a^p | p is prime}?

(A) Not accepted by a Turing machine
(B) Regular but not context-free
(C) Context-free but not regular
(D) Neither regular nor context-free, but accepted by a Turing machine
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Primality of the input length is decidable, so a Turing machine accepts the language. It is not regular: if a pumping length is p, choose prime q>p; a pumpable nonempty segment has length d≤p, and pumping it q+1 times changes length to q+qd=q(1+d), composite.
2. Every unary context-free language is regular (its length set is ultimately periodic/semilinear), so this language is not context-free either. Therefore D.

Original paper, page 3

2 marks · MCQ
Report issue

Grammar transformations and normal-form trees

Context-Free Grammars & Languages

Question 50, GATE 2008: Which statements are true? I: every left-recursive grammar can be converted to a right-recursive grammar and vice versa. II: all ε-productions can be removed from any CFG by suitable transformations. III: a CFG with only X→w or X→wY, where w is a terminal string and Y a nonterminal, always generates a regular language. IV: derivation trees for a CFG in Chomsky normal form are always binary trees.

(A) I,II,III,IV
(B) II,III,IV only
(C) I,III,IV only
(D) I,II,IV only
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Under the conventional grammar-transformation interpretation, I, III and IV are true, giving C. I refers to eliminating left recursion using right-recursive auxiliaries and the symmetric transformation for right recursion; it does not mean every context-free grammar becomes a right-linear regular grammar.
2. II is false as stated: if ε belongs to the language, removing every ε-production cannot preserve that language, since no nonempty sentential form can derive the empty string without one. III is a right-linear grammar, possibly with multi-terminal labels; expanding labels into paths yields a finite automaton, so its language is regular.
3. IV is true in the at-most-two-children sense of binary tree: CNF uses A→BC or A→a (and possibly the special start ε-rule), so no derivation-tree node has more than two children.

Original paper, page 9

2 marks · MCQ
Report issue

Matching language patterns to compiler requirements

Context-Free Grammars & Languages

Question 51, GATE 2008: Match E: checking identifiers are declared before use; F: agreement of numbers of formal and actual function parameters; G: arithmetic expressions with matched parentheses; H: palindromes. Models: P={a^n b^m c^n d^m | n,m≥1}; Q: X→XbX|XcX|dXf|g; R={wcw | w∈{a,b}*}; S: X→bXb|cXc|ε.

(A) E–P,F–R,G–Q,H–S
(B) E–R,F–P,G–S,H–Q
(C) E–R,F–P,G–Q,H–S
(D) E–P,F–R,G–S,H–Q
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. The matching is E–R, F–P, G–Q, H–S, option C. E requires the same identifier string in declaration and use, modeled by the copy language wcw.
2. F matches counts of formal and actual parameters; an bm cn dm models the two cross-referenced counts. G is modeled by X→XbX | XcX | dXf | g: b and c act as binary operators, d/f as matched opening/closing parentheses, and g as an operand.
3. H is modeled by X→bXb | cXc | ε, which generates even-length palindromes. These are structural analogies, not claims that the small grammars capture every feature of a programming language or all odd-length palindromes.

Original paper, page 9

2 marks · MCQ
Report issue

String generated by equal-count grammar

Context-Free Grammars & Languages

Question 78, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. Which string is generated?
Options:
(A) aaaabb
(B) aabbbb
(C) aabbab
(D) abbbba
Question 78, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. Which string is generated?
Options:
(A) aaaabb
(B) aabbbb
(C) aabbab
(D) abbbba
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step through it (4 derivations)

1. Approach. Track the invariant #a−#b for strings derived from each nonterminal: it is 0 from S, +1 from A and −1 from B. Reject options violating the invariant, then construct a derivation for the survivor.

2. Derivation and interpretation. Every S-derived string has equal numbers of a and b; A-derived strings have one more a, and B-derived strings one more b. These invariants follow directly from all eight productions. Only aabbab among the four options has equal counts. It is generated by S => aB => aaBB => aabB => aabbS => aabbaB => aabbab. Thus C.

Original paper, page 20

2 marks · MCQ
Report issue

Number of parse trees for aabbab

Context-Free Grammars & Languages

Question 79, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. For aabbab, the answer string to linked Q78, how many distinct derivation trees exist?
Options:
(A) 1
(B) 2
(C) 3
(D) 4
Question 79, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. For aabbab, the answer string to linked Q78, how many distinct derivation trees exist?
Options:
(A) 1
(B) 2
(C) 3
(D) 4
Question 79, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. For aabbab, the answer string to linked Q78, how many distinct derivation trees exist?
Options:
(A) 1
(B) 2
(C) 3
(D) 4
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step through it (2 derivations)

1. Approach. The first two a symbols force S→aB→aaBB. Enumerate how the remaining bbab splits between the two B subtrees. Count trees, not different orders of expanding the same tree.

2. Derivation and interpretation. The first two terminals aa force S -> aB and B -> aBB. The remaining bbab must split between the two B children. Each child has one more b than a. Exactly two splits work: b | bab and bba | b. For the first, the right child uniquely derives bab via B -> bS -> baB -> bab. For the second, the left child uniquely derives bba via B -> bS -> bbA -> bba. The single b children use B -> b. Thus there are 2 parse trees, option B; different orders of expanding a fixed tree do not count as additional trees.

Original paper, page 20

1 mark · MCQ
Report issue

Context-free block-count languages

Context-Free Grammars & Languages

Question 19, GATE 2006: For n,m≥0 define L₁={0^(n+m)1^n0^m}, L₂={0^(n+m)1^(n+m)0^m}, and L₃={0^(n+m)1^(n+m)0^(n+m)}. Which languages are not context-free?
Options:
(A) L₁ only
(B) L₃ only
(C) L₁ and L₂
(D) L₂ and L₃
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. L1={0n+m1n0m} is context-free: S→0S0|T and T→0T1|ε generates it. Write L2={0k1k0m:0≤m≤k}.

2. It is not context-free: apply the CFL pumping lemma to 0p1p0p. A pump confined to one block breaks either equality of the first two block lengths or the last≤first inequality (choose pumping direction accordingly).

3. A pump crossing one boundary cannot touch all three blocks; preserving the first two lengths while pumping down makes the third too long, while other changes break equality or block order. L3 is{0k1k0k:k≥0}, also non-context-free by the usual three-block pumping argument.

4. Hence L2 and L3, D.

5. Teaching check: For L₂ the two equal leading block lengths and the upper bound on the final block are linked. A pumping window of length p cannot span all three p-length blocks in 0p1p0p. Even if it changes the first two blocks equally, pumping down leaves the untouched third block too long.

Original paper, page 3

2 marks · MCQ
Report issue

Ambiguous grammar for a regular language

Context-Free Grammars & Languages

Question 32, GATE 2006: Grammar G has productions S→SS | ab | ba | ε. Consider I: G is ambiguous; II: G generates every string with equal counts of a and b; III: the language of G can be accepted by a deterministic PDA. Which statements are true?
Options:
(A) I only
(B) I and III only
(C) II and III only
(D) I, II and III
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. The grammar generates exactly(ab+ba)*: SS concatenates its two-letter blocks and ε contributes nothing. It is ambiguous, for example ab has both S→ab and S→SS→abS→ab, with different parse trees.

2. It does not generate aabb, although that string has equal a/b counts. The generated language is regular and therefore accepted by a deterministic PDA (even a DFA).

3. Statements1 and 3 hold, B; the PDA accepts the language, not the grammar object.

4. Teaching check: Equal counts are necessary for this grammar but not sufficient: concatenating ab and ba blocks cannot produce aabb. The ε production together with S→SS also allows redundant decompositions, so even a very short generated word has multiple parse trees.

Original paper, page 4

2 marks · MCQ
Report issue

Grammar for unequal a and b counts

Context-Free Grammars & Languages

Question 84, GATE 2006: Which grammar generates precisely {a^i b^j:i,j≥0 and i≠j}? ε denotes the empty string.
Options:
(A) S→AC|CB; C→aCb|a|b; A→aA|ε; B→Bb|ε
(B) S→aS|Sb|a|b
(C) S→AC|CB; C→aCb|ε; A→aA|ε; B→Bb|ε
(D) S→AC|CB; C→aCb|ε; A→aA|a; B→Bb|b
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. In D, C→aCb|ε generates balanced ak bk. A→aA|a adds a positive number of a symbols, so S→AC generates all i>j.

2. B→Bb|b adds a positive number of b symbols, so S→CB generates all j>i. Together these are precisely ai bj with i≠j.

3. A permits cancellation of C’s ±1 imbalance by the other nonterminal: for example S⇒AC⇒aC⇒ab. B also generates ab.

4. C allows A/B→ε and therefore generates equal counts, including ε. Only D excludes every equal-count string while covering every unequal-count string.

5. Teaching check: C supplies equal counts, while A supplies at least one extra a or B at least one extra b. Because A and B cannot derive ε in D, every generated string has unequal counts. Conversely any unequal pair of nonnegative counts decomposes into a common balanced part and one positive excess.

Original paper, page 13

2 marks · MCQ
Report issue

Derivation length for unequal counts

Context-Free Grammars & Languages

Question 85, GATE 2006: Using grammar S→AC|CB, C→aCb|ε, A→aA|a, B→Bb|b, count the production applications in a derivation from S of a^l b^m, with l,m≥0 and l≠m. GFG prints the same expression in A and D; GateForum D instead has +3.
Options:
(A) max(l,m)+2
(B) l+m+2
(C) l+m+3
(D) max(l,m)+2 [GFG]; max(l,m)+3 [GateForum]
Question 85, GATE 2006: Using grammar S→AC|CB, C→aCb|ε, A→aA|a, B→Bb|b, count the production applications in a derivation from S of a^l b^m, with l,m≥0 and l≠m. GFG prints the same expression in A and D; GateForum D instead has +3.
Options:
(A) max(l,m)+2
(B) l+m+2
(C) l+m+3
(D) max(l,m)+2 [GFG]; max(l,m)+3 [GateForum]
Tap to enlarge
Your answer
Answer & solution

Answer: max(l,m)+2 (options A and D are printed identically) (no official key was published)

1. For l>m use S→AC (1 step), expand C by C→aCb exactly m times and then C→ε (m+1 steps), and derive al−m from A using l−m productions (l−m−1 recursive steps and one terminal step). Total=1+(m+1)+(l−m)=l+2.

2. For m>l the CB branch gives m+2. Thus the length is max(l,m)+2, regardless of the order in which independent nonterminals are expanded.

3. Both A and D visibly print this same expression, so no unique option letter is supported.

4. Teaching check: Count production applications, including C→ε and the initial S choice. For l=3,m=1, use S→AC, A→aA→aa, C→aCb→ab: five applications, equal to max(3,1)+2. Interleaving A and C expansions does not alter the number of productions used.

Original paper, page 13

2 marks · MCQ
Report issue

Classify a language with additive block counts

Context-Free Grammars & Languages

Question 87, GATE 2004: Classify L={a^m b^n c^(m+n):m,n≥1}.
Options:
(A) Regular
(B) Context-free but not regular
(C) Context-sensitive but not context-free
(D) Type-0 but not context-sensitive
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. For L={am bn cm+n:m,n≥1}, a context-free grammar is S→aSc | aTc and T→bTc | bc. The outer recursion supplies matching a/c pairs and the inner recursion matching b/c pairs, so it generates exactly L.

2. It is not regular: for a putative pumping length p, use ap b cp+1. Any pumpable segment within the first p symbols consists only of a characters; pumping changes the a count without changing the c count, violating the defining equality.

3. Therefore context-free but not regular, B.

4. Teaching check: A pushdown automaton can push one marker for each a and b, then pop one for each c, while tracking the required block order and nonempty a,b blocks. A finite automaton cannot maintain the unbounded count equality; the pumping argument makes this limitation rigorous.

Original paper, page 12

2 marks · MCQ
Report issue

Deterministic recognition of balanced brackets

Context-Free Grammars & Languages

Question 51, GATE 2003: Let G=({S},{a,b},R,S), where R is S→aSb | SS | ε. Which statement is true?
A. G is not ambiguous.
B. There exist x,y in L(G) with xy not in L(G).
C. A deterministic pushdown automaton accepts L(G).
D. A deterministic finite-state automaton accepts L(G).
Question 51, GATE 2003: Let G=({S},{a,b},R,S), where R is S→aSb | SS | ε. Which statement is true?
A. G is not ambiguous.
B. There exist x,y in L(G) with xy not in L(G).
C. A deterministic pushdown automaton accepts L(G).
D. A deterministic finite-state automaton accepts L(G).
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Interpret a as an opening bracket and b as a closing bracket. aSb encloses a balanced string, SS concatenates two balanced strings, and ε is balanced. Conversely, split any nonempty balanced string at its first matching close to obtain these productions.
2. A deterministic stack machine pushes on a, pops on b, rejects a pop from an empty stack, and accepts exactly with empty stack at end. This establishes C.
3. A is false: ε has both the direct S→ε tree and S→SS→εε. B is false because S→SS supplies closure under concatenation. D is false because intersecting with regular a*b* leaves the nonregular language {an bn:n≥0}.

Solution: The grammar generates balanced strings with a as opening bracket and b as closing bracket, including concatenations and the empty word. A deterministic pushdown automaton pushes on a, pops on b, rejects an unmatched b, and accepts at the end exactly when its stack is empty. Thus C is true. The grammar itself is ambiguous, for example ε can be produced directly or through S→SS with two empty children. Its language is closed under concatenation by S→SS and is not regular, since intersection with a*b* gives {aⁿbⁿ:n≥0}.

Original paper, page 7

1 mark · MCQ
Report issue

Language generated by symmetric zero insertion

Context-Free Grammars & Languages

Question 1.5, GATE 2000: For grammar S→0S0|00, decide whether L is 0+, regular but not 0+, context-free but not regular, or not context-free.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step 1: The base production yields two zeros. Each use of 0S0 adds exactly two more, so every generated word has positive even length. Conversely, applying the recursive rule k times generates 02k+2.

Step 2: Therefore L={02m:m≥1}=00(00)*, a regular language. It excludes the one-symbol string 0, which belongs to 0+.

Thus B. A recursive-looking grammar does not imply a nonregular language.

Original paper, page 1

Descriptive
Report issue

Exponents generated by a recursive grammar

Context-Free Grammars & Languages

Question 7(b), GATE 2000: Grammar S→aSAb | ε and A→bA | ε, with start S, generates strings aⁱbʲ, i,j≥0.

Task: Determine the exact condition on i,j.
Question 7(b), GATE 2000: Grammar S→aSAb | ε and A→bA | ε, with start S, generates strings aⁱbʲ, i,j≥0.

Task: Determine the exact condition on i,j.
Tap to enlarge
Shared context

Grammar S→aSAb | ε and A→bA | ε, with start S, generates strings aⁱbʲ, i,j≥0.

Answer & solution

Answer: (i,j)=(0,0), or i≥1 and j≥i. (no official key was published)

1. Every application of S→aSAb adds one a at the front and one mandatory b at the end, with an A that can generate any additional number of b symbols.

2. After i recursive applications and S→ε, the form is aⁱ(Ab)ⁱ. Thus j=i+k₁+⋯+kᵢ for arbitrary nonnegative integers k₁,…,kᵢ.

3. If i≥1, this gives j≥i. Every such j is attainable by assigning j−i extra b symbols to one A and zero to all the others.

4. If i=0, S goes directly to ε and there are no A symbols, so j must also be zero. The looser answer j≥i incorrectly includes nonempty strings consisting only of b.

Original paper, page 7

5 marks · Descriptive
Report issue

The marked copy language is not context-free

Context-Free Grammars & Languages

Question 7, GATE 1999: Prove L={xcx:x∈{0,1}*} is not context-free, where c is a terminal different from 0 and 1.
Tap to enlarge
Shared context

Prove L={xcx:x∈{0,1}*} is not context-free, where c is a terminal different from 0 and 1.

Answer & solution

Answer: A context-free pumping-lemma argument on 0ᵖ1ᵖc0ᵖ1ᵖ yields a contradiction. (no official key was published)

Suppose L were context-free and let p be its pumping length. Choose w=0ᵖ1ᵖc0ᵖ1ᵖ∈L. For any pumping decomposition w=uvxyz, the lemma requires |vxy|≤p, |vy|>0, and uvⁱxyⁱz∈L for every i≥0. We show pumping down (i=0) always fails.

If v or y contains c, removing v and y removes the unique separator. The result cannot be in L.

Otherwise c is not pumped. If vxy lies entirely on one side of c, pumping down shortens only that copy. The two sides have unequal lengths, so cannot be equal.

The remaining case is that vxy spans c, with c inside the unpumped substring x. Because |vxy|≤p, everything it can reach to the left of c is in the first block of ones, and everything to the right is in the second block of zeros: it cannot reach the first zeros or the final ones, each separated by a whole length-p block. Thus v=1ᵃ and y=0ᵇ, allowing a or b to be zero, but a+b>0. Pumping down leaves 0ᵖ1p−ac0p−b1ᵖ. Equality of the two copies would force both a=0 (compare ones) and b=0 (compare zeros), contrary to |vy|>0.

Every allowable decomposition fails the lemma, so L is not context-free. The argument relies on a genuine copy xc x, not the context-free reversed-copy language xc xᴿ.

Original paper, page 7

Descriptive
Report issue

Language of an unequal-count grammar

Context-Free Grammars & Languages

Question 14(a), GATE 1998: Find L(G1) for S1→aS1b|aAb|aBb, A→aA|a, B→Bb|b.
Question 14(a), GATE 1998: Find L(G1) for S1→aS1b|aAb|aBb, A→aA|a, B→Bb|b.
Tap to enlarge
Answer & solution

Answer: {a^i b^j : i,j≥1 and i≠j}. (no official key was published)

A generates exactly ar with r≥1: each recursive step adds one a and the final A→a supplies the last. Similarly B generates exactly bs with s≥1. The left recursion in B changes derivation direction, not its generated language or uniqueness for a fixed length.

After t≥0 uses of S1→aS1b, choosing S1→aAb and A⇒ar produces at+r+1bt+1. Both exponents are positive and the number of a's exceeds the number of b's by r≥1. Choosing S1→aBb instead gives at+1bt+s+1, whose b-count exceeds its a-count by s≥1. These are the only terminating choices, so equality of the counts is impossible.

Conversely, for any i>j≥1, choose t=j−1 and r=i−j, yielding ai bj through the A branch. For j>i≥1, choose t=i−1 and s=j−i through the B branch. Therefore every positive unequal pair occurs, proving L(G1)={ai bj:i,j≥1,i≠j}.

This grammar is also unambiguous. The comparison between i and j forces the A or B branch; the smaller exponent forces t; the difference forces r or s. The unary A or B derivation of that length is unique. This fact will be useful when answering the inherent-ambiguity question, but does not need to be assumed to establish the language.

Original paper, page 7

Descriptive
Report issue

Extend the grammar in four rules

Context-Free Grammars & Languages

Question 14(b), GATE 1998: Using G1 from part (a), construct a CFG for L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)} by adding no more than five productions.
Question 14(b), GATE 1998: Using G1 from part (a), construct a CFG for L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)} by adding no more than five productions.
Question 14(b), GATE 1998: Using G1 from part (a), construct a CFG for L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)} by adding no more than five productions.
Tap to enlarge
Answer & solution

Answer: Add S2→TAB|S1T and T→aTb|ab, taking S2 as the new start symbol: four added alternatives. (no official key was published)

Keep every production of G1. Introduce new nonterminals S2 and T, take S2 as the new start symbol, and add exactly four individual productions:

S2 → TAB
S2 → S1T
T → aTb
T → ab

T generates precisely {an bn:n≥1}. Existing A and B generate a+ and b+, so AB generates all ak bl with k,l≥1. Consequently the first new start production TAB generates exactly the words with i=j and arbitrary positive k,l. The second production S1T generates exactly those with i≠j and k=l, because part (a) established the unequal-count language of S1.

These two cases together are equivalent to i=j or k=l. If the first equality holds, the first branch handles the word whether or not the second equality also holds. If the first equality fails, the language condition forces k=l, so the second branch handles it. Conversely every generated word satisfies one of those required equalities. Positive lengths of all four runs are guaranteed by T,A,B and S1.

The extension uses four alternatives, within the stated limit of five, even when each bar-separated alternative is counted separately. Reusing the unequal-count grammar is deliberate: the simpler-looking union “balanced first or balanced second” would have overlapping branches when both pairs are balanced. Here the two branches are disjoint, which also supplies a clean unambiguity proof for part (c).

Original paper, page 7

Descriptive
Report issue

Is the four-run language inherently ambiguous?

Context-Free Grammars & Languages

Question 14(c), GATE 1998: For L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)}, decide whether it is inherently ambiguous. Use the grammar extension from part (b) if helpful.
Question 14(c), GATE 1998: For L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)}, decide whether it is inherently ambiguous. Use the grammar extension from part (b) if helpful.
Question 14(c), GATE 1998: For L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)}, decide whether it is inherently ambiguous. Use the grammar extension from part (b) if helpful.
Question 14(c), GATE 1998: For L2={a^i b^j a^k b^l : i,j,k,l≥1 and (i=j or k=l)}, decide whether it is inherently ambiguous. Use the grammar extension from part (b) if helpful.
Tap to enlarge
Answer & solution

Answer: No. The four-rule extension gives an unambiguous CFG. (no official key was published)

A context-free language is inherently ambiguous only if every CFG generating it is ambiguous. It is therefore enough to exhibit and prove one unambiguous grammar. Use the extension from part (b): S2→TAB|S1T, T→aTb|ab, retaining the unambiguous unequal-count grammar G1.

Every word of L2 has four nonempty runs ai,bj,ak,bl. The boundaries between the runs are determined by changes of symbol, particularly the middle b-to-a change, so a derivation cannot shift the split between its two a*b* components.

If i=j, only S2→TAB can apply, since S1 generates unequal first counts. The exponent i fixes the unique derivation in T. The remaining exponents k and l fix the unique unary derivations in A and B. If i≠j, only S2→S1T can apply. Then membership requires k=l. Part (a)'s proof gives a unique derivation of ai bj in S1, and the common value k=l gives a unique derivation in T. Hence each word has exactly one parse tree.

Words satisfying both equalities still use only the first branch, so they do not create an intersection ambiguity. A different grammar with productions for “first pair equal” and “second pair equal” may generate those words twice, but ambiguity of one grammar does not establish inherent ambiguity of its language. This four-run language also should not be confused with classical three-run examples whose equality conditions share the middle count. The explicit disjoint grammar here proves that L2 is not inherently ambiguous.

Original paper, page 7

3 marks · Descriptive
Report issue

Characterize a block grammar

Context-Free Grammars & Languages

Question 11(a), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
What condition on i,j,k,m characterizes the generated language?
Question 11(a), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
What condition on i,j,k,m characterizes the generated language?
Question 11(a), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
What condition on i,j,k,m characterizes the generated language?
Tap to enlarge
Answer & solution

Answer: i, j, k, m≥0 and i+k=j+m (no official key was published)

Let t be the number of uses of S→bSe before S→PQR. Let p,q,r be the numbers of recursive productions used by P,Q,R. Their contributions are P⇒bp cp, Q⇒cq dq and R⇒dr er. Combining them with the outer b/e pairs gives

i=t+p, j=p+q, k=q+r, m=r+t,
where t,p,q,r are nonnegative integers. Adding opposite equations yields the necessary relation i+k=j+m.

To prove sufficiency, assume all four exponents are nonnegative and i+k=j+m. Choose any integer t satisfying max(0,i−j)≤t≤min(i,m), and put p=i−t, q=j−i+t, r=m−t. These parameters are nonnegative and reproduce every exponent, including k=j−i+m. Such a t always exists: i−j≤m follows from k≥0, while i−j≤i follows from j≥0. Thus the equality, together with nonnegativity, is also sufficient.

Its full generated form is bt+pcp+qdq+rer ct, with a trailing block of c symbols after the d/e blocks. For example S⇒bSc⇒bPQRc⇒bcdc is possible, contradicting that the paper's claim that every generated word is in b*c*d*e*. Restricting to words of the claimed block form forces either t=0, or q=r=0 so no d/e lies before the final c. The t=0 case gives i=p,j=p+q,k=q+r,m=r, hence i+k=j+m and j≥i. The other case gives only bi ci, already included in that subset.

Taken literally as string exponents, only zero exponents are meaningful, leaving ε; the language intended by the grammar is not that singleton. This solution uses nonnegative counts, as explicitly printed, and discloses both source differences.

Original paper, page 7

2 marks · Descriptive
Report issue

Find the shortest ambiguous word

Context-Free Grammars & Languages

Question 11(b), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
Find a shortest word having two distinct parse trees.
Question 11(b), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
Find a shortest word having two distinct parse trees.
Question 11(b), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
Find a shortest word having two distinct parse trees.
Tap to enlarge
Answer & solution

Answer: bcde (length 4) (no official key was published)

Bcde has two different parses. The difference is whether the initial b and final e are supplied by the outer S rule or by P and R:

Parse 1: S⇒PQR, with P⇒bPc⇒bc, Q⇒ε, R⇒dRe⇒de. Concatenation gives bcde.
Parse 2: S⇒bSe⇒bPQRe, with P⇒ε, Q⇒cQd⇒cd, R⇒ε. This also gives bcde.

These are distinct parse trees because the first root uses S→PQR and the second root uses S→bSe. Merely changing the order of independent rewrites would not demonstrate ambiguity; here the production at the root really differs.

To prove shortestness, fix a word's exponents. Every parse is uniquely determined by t, the number of outer S→bSe applications, since p=i−t, q=j−i+t and r=m−t determine the unique chains in P,Q,R. Two parses require two possible t values. If consecutive valid choices are t and t+1, the lower choice has p≥1 and r≥1. Those two recursive chains alone contribute at least two b/c symbols and two d/e symbols, so the total length is at least four. The displayed word attains that bound.

Original paper, page 7

5 marks · Descriptive
Report issue

Eliminate nullable and unit rules

Context-Free Grammars & Languages

Question 11, GATE 1996: Transform S→ABAC, A→aA|ε, B→bB|ε, C→d into an equivalent CFG with no ε-productions and no unit productions.
Tap to enlarge
Answer & solution

Answer: S→ABAC|BAC|AAC|ABC|AC|BC|d; A→aA|a; B→bB|b; C→d. (no official key was published)

The nullable nonterminals are A and B. C is not nullable because it must produce d; S is consequently not nullable. In S→ABAC, independently retain or omit each of the two occurrences of A and the one occurrence of B. The distinct right sides are ABAC, BAC, AAC, ABC, AC, BC and C. The side AC arises twice because either A can be the sole retained A, but duplicate productions need only be written once.

Removing A→ε and B→ε requires adding the terminating alternatives A→a and B→b: their old recursive rules could previously terminate by using the nullable final nonterminal. Keep A→aA|a and B→bB|b, so these variables now generate positive rather than arbitrary nonnegative repetitions. Keep C→d.

The only unit production introduced is S→C. Substitute C's nonunit right side to obtain S→d, then delete S→C. The resulting grammar is S→ABAC|BAC|AAC|ABC|AC|BC|d, A→aA|a, B→bB|b, C→d.

Every rule now has a nonempty right side; none has a sole nonterminal. The original language is {ai bj ak d:i,j,k≥0}. The seven start alternatives cover exactly which of the three repetition blocks are empty, with the all-empty case producing d. Thus the transformation preserves the language, including the shortest word d.

Original paper, page 8

1 mark · MCQ
Report issue

A damaged grammar classification

Context-Free Grammars & Languages

Question 1.10, GATE 1995: Classify the reproduced grammar as context-free, regular, context-sensitive or LR(k). Its final GFG line reads bα→bdb|b; other nonterminal labels and the alternate differ.
Question 1.10, GATE 1995: Classify the reproduced grammar as context-free, regular, context-sensitive or LR(k). Its final GFG line reads bα→bdb|b; other nonterminal labels and the alternate differ.
Tap to enlarge
Your answer
Answer & solution

Answer: No listed classification can be justified: the printed grammar has a contracting rule (no official key was published)

A context-free production must have one nonterminal on its left side; the visible bα left side has multiple symbols. A context-sensitive grammar in the usual noncontracting form cannot contain bα→b, which shortens a nonempty string. Regular and LR(k) grammars are context-free and therefore fail the same left-side condition. Moreover, the reproductions disagree on labels and contain undeclared-looking symbols, so even a valid unrestricted grammar cannot be reconstructed confidently. If the contraction or labels are typographic errors, classification might change. No option is asserted without a trustworthy correction of the source.

Original paper, page 1

Descriptive
Report issue

Explain cycles and LL(1) parsing

Context-Free Grammars & Languages

Question 9(b), GATE 1995: A grammar has a cycle if A⇒⁺A for some nonterminal A. Show that a grammar with cycles cannot be LL(1), stating any necessary qualification.
Tap to enlarge
Answer & solution

Answer: A useful/productive cycle is incompatible with LL(1); the unrestricted claim needs a reduced-grammar assumption. (no official key was published)

Assume the cyclic nonterminal A is reachable in a successful derivation and can derive a terminal string w (possibly ε). Starting at A, one may derive w directly, or first traverse the nonempty cycle A⇒⁺A and then derive the same w. Inserting the cycle into a successful start-symbol derivation gives two distinct parse trees for the same terminal string. Every LL(1) grammar is unambiguous, so this useful cyclic grammar cannot be LL(1).

Operationally, a predictive parser cannot decide from the same next input symbol whether to begin another traversal of a nullable cycle or leave it to produce the terminal string. The proof concerns a cycle deriving A itself, not ordinary right recursion such as A→aA, which consumes input and can be LL(1).

The statement as printed omits the usual reduced-grammar qualification. As a literal counterexample, S→a with an unreachable rule A→A generates only a and has a conflict-free predictive table for all successful parses from S; the unreachable cycle contributes no competing table entry. A nonproductive cycle likewise cannot be inserted in a successful parse. Thus the intended theorem is valid for reduced grammars, or for cycles on useful nonterminals, rather than arbitrary added useless rules.

Original paper, page 6

5 marks · Descriptive
Report issue

Length of a strict CNF derivation

Context-Free Grammars & Languages

Question 20, GATE 1994: A CFG has only productions A→BC and A→a. For a generated terminal word of length ℓ, how many production applications occur in its derivation? GateForum omits the symbol after “length”; GFG supplies ℓ.
Tap to enlarge
Answer & solution

Answer: 2ℓ−1 production applications for ℓ≥1. (no official key was published)

Each terminal of the final word is introduced by exactly one A→a production: no other production introduces terminals, and no production subsequently removes them. Hence there are exactly ℓ terminal-producing steps. Start with one nonterminal. Each A→BC application increases the number of nonterminals by one, whereas each A→a decreases it by one. If b is the number of binary applications, the final nonterminal count is 1+b−ℓ=0, so b=ℓ−1. The total number of applications is b+ℓ=2ℓ−1.

The same result follows by viewing the nonterminal portion of the parse tree as a full binary tree: ℓ terminal-producing leaves require ℓ−1 binary internal nodes. The count is independent of which nonterminal is expanded first, of leftmost versus rightmost derivation, and of ambiguity of the grammar. For ℓ=1 there is just the single terminal production, agreeing with the formula.

This is a count of steps, not a count of sentential forms: including both the initial symbol and final word gives 2ℓ sentential forms. Under the strict production forms stated here ε cannot be generated. If a different CNF convention permits S→ε, its ε derivation is a separate one-step exception, not an application of the formula at ℓ=0.

Original paper, page 6

Report issue

Length of a CNF derivation

Context-Free Grammars & Languages

Question 2(xviii), GATE 1992: For a nonempty word w of length ℓ in a Chomsky-normal-form grammar, how many production applications derive it?
Question 2(xviii), GATE 1992: For a nonempty word w of length ℓ in a Chomsky-normal-form grammar, how many production applications derive it?
Tap to enlarge
Select all that apply
Answer & solution

Answer: C: 2ℓ−1 (no official key was published)

A CNF parse tree uses binary nonterminal productions A→BC and terminal productions A→a. There are ℓ terminal-producing leaves. A full binary tree with ℓ leaves has ℓ−1 binary internal nodes, so the total applications are ℓ+(ℓ−1)=2ℓ−1. The order of expanding variables does not change that count. The special ε-production sometimes allowed for the start symbol is outside this nonempty-word case; substituting ℓ=0 into the formula would not count its derivation correctly.

Original paper, page 4