Consider the following languages:
I.
II.
III.
IV.
Which of the languages above are context-free?
- (A)
I and IV only
- (B)
I and II only
- (C)
II and III only
- (D)
II and IV only
Consider the following languages:
I.
II.
III.
IV.
Which of the languages above are context-free?
I and IV only
I and II only
II and III only
II and IV only
ANSWER
B. Only I and II are context-free.
Official answer: BSource: Official answer key, GATE 2018 CS (IIT Guwahati)
Language I: is the same as . Split on the sign of this common value .
Case : put . Then and , so the word is . A grammar is , , , . This case is context-free.
Case : put . Then and , so the word is , a concatenation of three languages of the form . This case is context-free.
The union of two context-free languages is context-free, so I is context-free.
Language II: is generated by , , . So II is context-free.
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.
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.
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.
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 .
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.
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