IAE Academy

GATE 2023 CS Q.39: solution and answer

GATE 2023 · CSQ.39 · 2 marks · MCQ

Consider the context-free grammar below

where and are non-terminals, and and are terminal symbols. The starting non-terminal is .

Which one of the following statements is CORRECT?

  1. (A)

    The language generated by is

  2. (B)

    The language generated by is

  3. (C)

    The language generated by is

  4. (D)

    The language generated by is not a regular language

See the question in the paper

Answer and solution

ANSWER

The language generated by is , option (B).

Official answer: BSource: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. Solve the inner non-terminal first. In , each use of puts an on the left and each use of puts a on the right. The derivation stops with exactly one terminal from or . So , that is, strings with and .

  2. wraps matching pairs around , and ends the wrapping. So .

  3. Write . Then the string is , which has the form with . So .

  4. Conversely, any string in is generated by taking , , , since already gives all of . So , which is regular, and (B) is correct.

  5. Check the others. (A): , but , because in every symbol after the single special symbol is a , and a string starting with must have as that first , leaving only 's after it. So (A) is false. (C): by taking , but . The only symbols before are 's, so would be the first with only 's after it, or the last with only 's before it, and neither fits . So (C) is false. (D): is regular, so (D) is false.

KEY IDEA

Find first. The wrapping in cannot produce anything outside , because the extra 's and 's fit into the existing starred parts. The language stays regular.

WATCH OUT FOR

Seeing and assuming the language needs matched counts of and , hence non-regular (D). Here the middle part already generates any number of 's before and any number of 's after the special symbol, so the matching pairs add nothing new.

From the GATE 2023 CS paper. Every question of this paper · Download the full paper · All 9 CFG & PDA questions, with notes

More CFG & PDA questions