Context Free Grammar · Theory of Computation
33 questions · asked in 12 of 27 years (2000–2026)
MCQ 26 · MSQ 5 · NAT 21-mark 15 · 2-mark 18
2000Questions per year2026
Questions 31–33 of 33
Let G = ({S}, {a, b} R, S) be a context free grammar where the rule set R is
Which of the following statements is true?
Which of the following statements is true?
Answer: (C)
The problem becomes easier if we write ( in place of terminal and ) in place of terminal .
the set of balanced parentheses [every left parenthesis has a matching right parenthesis, and they are well nested].
Examples: .
(A) The string has three different derivation trees:
, where the left S becomes and the right S becomes
, where the right S becomes and the left S becomes
So G is ambiguous, and (A) is false.
(B) Joining two balanced strings gives a balanced string, for example and give . So (B) is false.
(C) We can build a deterministic PDA for : push each left parenthesis on the stack and pop one for each right parenthesis. Accept when the input ends with an empty stack. So (C) is true.
(D) We cannot build a DFA for , because a stack is needed to count the unmatched left parentheses without limit. So (D) is false.
Answer: (C)
the set of balanced parentheses [every left parenthesis has a matching right parenthesis, and they are well nested].
Examples: .
(A) The string has three different derivation trees:
, where the left S becomes and the right S becomes
, where the right S becomes and the left S becomes
So G is ambiguous, and (A) is false.
(B) Joining two balanced strings gives a balanced string, for example and give . So (B) is false.
(C) We can build a deterministic PDA for : push each left parenthesis on the stack and pop one for each right parenthesis. Accept when the input ends with an empty stack. So (C) is true.
(D) We cannot build a DFA for , because a stack is needed to count the unmatched left parentheses without limit. So (D) is false.
Answer: (C)
Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?
Answer: (D)
An LL(1) parser works top down, and it can only use a grammar that is unambiguous, not left recursive and left factored. Removing left recursion and left factoring fix only the last two. Neither one removes ambiguity, so an ambiguous grammar stays ambiguous after both steps, and an ambiguous grammar is never LL(1). Some context-free languages are inherently ambiguous, so no unambiguous grammar exists for them at all.
Even an unambiguous grammar for a deterministic context-free language may not be convertible: some deterministic CFLs have no LL(k) grammar for any . (Every deterministic CFL does have an LR(1) grammar.)
So (A), (B) and (C) are not enough for an arbitrary CFG.
Answer: (D)
Even an unambiguous grammar for a deterministic context-free language may not be convertible: some deterministic CFLs have no LL(k) grammar for any . (Every deterministic CFL does have an LR(1) grammar.)
So (A), (B) and (C) are not enough for an arbitrary CFG.
Answer: (D)
Consider the following decision problems:
(P1): Does a given finite state machine accept a given string?
(P2): Does a given context free grammar generate an infinite number of strings?
Which of the following statements is true?
(P1): Does a given finite state machine accept a given string?
(P2): Does a given context free grammar generate an infinite number of strings?
Which of the following statements is true?
Answer: (A)
P1: Run the machine on the string. A finite state machine reads one symbol per step and always halts after as many steps as the string is long, so P1 is decidable.
P2: Remove useless symbols and put the grammar in Chomsky normal form with variables. Then the pumping lemma constant is , which we can compute from the grammar. The language is infinite if and only if it has a string of length from to .
- If such a string exists, pump it to get infinitely many strings.
- If the language is infinite, it has a string of length at least . Take the shortest one. If it were of length or more, pumping it down would remove at most symbols and give a shorter string still of length at least , a contradiction. So its length is below .
There are only finitely many strings in that length range, and the CYK algorithm tests each one. So P2 is decidable.
So both P1 and P2 are decidable.
Answer: (A)
P2: Remove useless symbols and put the grammar in Chomsky normal form with variables. Then the pumping lemma constant is , which we can compute from the grammar. The language is infinite if and only if it has a string of length from to .
- If such a string exists, pump it to get infinitely many strings.
- If the language is infinite, it has a string of length at least . Take the shortest one. If it were of length or more, pumping it down would remove at most symbols and give a shorter string still of length at least , a contradiction. So its length is below .
There are only finitely many strings in that length range, and the CYK algorithm tests each one. So P2 is decidable.
So both P1 and P2 are decidable.
Answer: (A)
Keep practising