Consider the following languages.
Which one of the following is TRUE?
- (A)
is regular and is context-free.
- (B)
is context-free but not regular and is context-free.
- (C)
Neither nor is context-free.
- (D)
is context-free but is not context-free.
Consider the following languages.
Which one of the following is TRUE?
is regular and is context-free.
is context-free but not regular and is context-free.
Neither nor is context-free.
is context-free but is not context-free.
ANSWER
Option (A): is regular and is context-free.
Official answer: ASource: Official answer key, GATE 2020 CS (IIT Delhi)
is regular. Every string of can be rewritten with a one-symbol . Write a member as with and let . Then the string is . The first and third blocks are nonempty, so it has the form with nonempty. Conversely, a string of that form is in by taking . So .
A string of length is in exactly when its last symbol also occurs at some position with . Position leaves room for a nonempty , and leaves room for a nonempty before the final .
Hence . This is a regular expression, so is regular, and therefore also context-free.
is context-free. A string with is in exactly when for some position , so has even length . Take the grammar , , , . Here generates exactly the odd-length strings over whose middle symbol is , and those whose middle symbol is .
Soundness: let produce a string of length and one of length . Then has length with . The is at position and the is at position . These are exactly apart, so the two halves differ at position . The rule is the same with the roles of and swapped.
Completeness: if has length with and , split as followed by . The first part has odd length and middle symbol , so derives it. The second part has length and its middle symbol is at position , which is , so derives it. If instead and , the same split works with . So the grammar generates exactly , and is context-free.
Therefore is regular and is context-free, which is option (A). Options (B) and (D) are false because is regular (not 'not regular'), and (D) also because is context-free. Option (C) is false because both languages are context-free.
KEY IDEA
The word with long collapses to with a single symbol , so is regular. The set of even-length strings whose halves differ is context-free: guess one mismatch pair, which sits exactly half the length apart, using two odd-length palindrome-style blocks with centres and .
WATCH OUT FOR
Treating as a 'copy language' that needs memory of and so is not regular, or thinking (with ) is not context-free because is not. The complement of is context-free because it only needs to remember one mismatch position.
From the GATE 2020 CS paper. Every question of this paper · Download the full paper · All 8 Context-free languages questions, with notes