IAE Academy

GATE 2018 CS Q.35: solution and answer

GATE 2018 · CSQ.35 · 2 marks · MCQ

Consider the following languages:
I.
II.
III.
IV.
Which of the languages above are context-free?

  1. (A)

    I and IV only

  2. (B)

    I and II only

  3. (C)

    II and III only

  4. (D)

    II and IV only

See the question in the paper

Answer and solution

ANSWER

B. Only I and II are context-free.

Official answer: BSource: Official answer key, GATE 2018 CS (IIT Guwahati)

  1. Language I: is the same as . Split on the sign of this common value .

  2. Case : put . Then and , so the word is . A grammar is , , , . This case is context-free.

  3. Case : put . Then and , so the word is , a concatenation of three languages of the form . This case is context-free.

  4. The union of two context-free languages is context-free, so I is context-free.

  5. Language II: is generated by , , . So II is context-free.

  6. Language III is not context-free. Use Ogden's lemma with constant . Take , which is in III because . Mark the letters . Write with containing a marked letter and containing at most marked letters.

  7. If or contains two different letters, then is not of the form , so it is outside III. Otherwise and each repeat one letter, and since is the first block, contains at least one . Then and together repeat at most one letter other than , so at least one of the counts of and is unchanged and equals . In the number of 's is greater than , so fails and the word is not in III. This holds for every split, so III is not context-free.

  8. Language IV is not context-free. Use Ogden's lemma. Take , which is in IV because . Mark every and every . Then has at most marked letters and has at least one.

  9. If contained an and a , it would contain all letters plus a , which is marked letters, impossible. So lies inside or inside . Again a letter-mixing or gives a word outside the form .

  10. Now pump with and let , repeat and letters. (a) , with : the word has , , , and . (b) , both inside the 's: grows, and stay, so . (c) both inside the 's: grows only, so . (d) , with : here and , so equality needs . If then holds marked letters, which is not allowed. If then , but must contain a marked letter. So equality fails. In every case IV, so IV is not context-free.

  11. Hence exactly I and II are context-free, which is option (B).

KEY IDEA

Languages with a single comparison of counts, or with nested pairs that can be matched on one stack, are context-free. Languages that need three counts equal, or a product of two counts, are not.

WATCH OUT FOR

Thinking language I needs two independent comparisons. The condition is equivalent to , and each sign of that difference gives a nested structure a PDA can match. Language III looks harder to reject because of the extra condition , but its three equal counts already rule it out.

From the GATE 2018 CS paper. Every question of this paper · Download the full paper · All 8 Context-free languages questions, with notes

More Context-free languages questions