Question

Consider the language L={an∣n≥0}∪{anbn∣n≥0}L = \{a^n \mid n \ge 0\} \cup \{a^n b^n \mid n \ge 0\} and the following statements.

I. LL is deterministic context-free.
II. LL is context-free but not deterministic context-free.
III. LL is not LL(k)\text{LL}(k) for any kk.

Which of the above statements is/are TRUE?

Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2020 answer key

Solution

Determine the determinism of the language by constructing a DPDA with an endmarker, and test LL(k) grammar feasibility.

Given language: L={an∣n≥0}∪{anbn∣n≥0}L = \{a^n \mid n \ge 0\} \cup \{a^n b^n \mid n \ge 0\} over Σ={a,b}\Sigma = \{a, b\}.

Evaluation of Statements I and II (DCFL property):

  • To test if LL is a deterministic context-free language (DCFL), check if a DPDA with an endmarker $\$ can accept LL by final state:
    1. Read all aa's and push them onto the stack.
    2. If the endmarker $\$ is read immediately (no bb's follow), enter an accepting state (accepts ana^n).
    3. If bb's follow, transition to a state that pops one aa for each bb read. When the endmarker $\$ is reached with an empty stack, enter an accepting state (accepts anbna^n b^n).
  • Since all transitions are deterministic with the lookahead endmarker $\$, LL is deterministic context-free (DCFL).
  • Thus, Statement I is TRUE and Statement II is FALSE.

Evaluation of Statement III (LL(k) property):

  • An LL(k)\text{LL}(k) parser must decide whether to expand S→anS \to a^n or S→anbnS \to a^n b^n after reading at most kk tokens of lookahead.
  • If an input string starts with ak+1a^{k+1}, lookahead of length kk only sees a sequence of aa's (aka^k). The parser cannot determine whether the string will terminate as ak+1a^{k+1} or continue with bk+1b^{k+1}.
  • Since kk is finite and the length of aa's can be arbitrarily large, no LL(k)\text{LL}(k) grammar can parse LL.
  • Thus, Statement III is TRUE.

Statements I and III only are TRUE.

Final answer

C

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2020 Computer Science and Information Technology (CS)
Question
20 of 65, printed as CS Q.10
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.