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 1–5 of 33
Consider the following grammar where is the start symbol, and and are terminal symbols.
Which of the following statements is/are true?
Which of the following statements is/are true?
One or more options are correct.
Answer: (A), (B), (C)
The grammar is .
(B) and (A): the string starts with , so it must use and . There are two ways:
- , (where );
- , .
These are two different parse trees, so has two distinct derivations and the grammar is ambiguous.
(C): starts with , so and . Try the choices for :
- : then , so , which works ( with both empty).
- : the next symbol must be , but it is .
- : cannot be derived, because every must be followed later by a .
- : nothing is left for the .
So the parse tree is unique, and so is the rightmost derivation.
(D): the language is context-free, and membership in a context-free language is decidable. So the language is not undecidable.
Answer: (A), (B), (C)
(B) and (A): the string starts with , so it must use and . There are two ways:
- , (where );
- , .
These are two different parse trees, so has two distinct derivations and the grammar is ambiguous.
(C): starts with , so and . Try the choices for :
- : then , so , which works ( with both empty).
- : the next symbol must be , but it is .
- : cannot be derived, because every must be followed later by a .
- : nothing is left for the .
So the parse tree is unique, and so is the rightmost derivation.
(D): the language is context-free, and membership in a context-free language is decidable. So the language is not undecidable.
Answer: (A), (B), (C)
Consider the following context-free grammar .
In the above grammar, is the start symbol, and are terminal symbols, and and are non-terminal symbols.
Let be the language generated by the grammar . For a string , let be the number of 's in and be the number of 's in .
Which of the following statements is/are true?
In the above grammar, is the start symbol, and are terminal symbols, and and are non-terminal symbols.
Let be the language generated by the grammar . For a string , let be the number of 's in and be the number of 's in .
Which of the following statements is/are true?
One or more options are correct.
Answer: (B), (D)
Count the 's and 's in each part. Let the excess be .
B: gives with . So excess 0, and at least one .
A: each use of an -rule adds terminals plus -parts (excess 0):
- : terminals give 2 a's and 1 b, so excess . It has at least 1 b of its own and at least 2 more from the two 's, so at least 3 b's.
- : terminals give 3 a's and 2 b's, so excess , and at least b's.
An -string uses rules (the last one must be ). So and .
S: has 3 a's and 3 b's in its terminals (excess 0), one (excess 0, at least 1 b) and two -strings with . So , and .
So always. This makes (B) true and (A) false. Also . This makes (D) true and (C) false.
Answer: (B), (D)
B: gives with . So excess 0, and at least one .
A: each use of an -rule adds terminals plus -parts (excess 0):
- : terminals give 2 a's and 1 b, so excess . It has at least 1 b of its own and at least 2 more from the two 's, so at least 3 b's.
- : terminals give 3 a's and 2 b's, so excess , and at least b's.
An -string uses rules (the last one must be ). So and .
S: has 3 a's and 3 b's in its terminals (excess 0), one (excess 0, at least 1 b) and two -strings with . So , and .
So always. This makes (B) true and (A) false. Also . This makes (D) true and (C) false.
Answer: (B), (D)
Let and let .
Which of the following constraints ensure(s) that the language is context-free?
Which of the following constraints ensure(s) that the language is context-free?
One or more options are correct.
Answer: (A), (C), (D)
Each language is with a constraint. Test each one with a stack.
(A) : this means . A PDA keeps a running count: add 1 for each and , subtract 1 for each and . The count can be positive or negative, but it can always be kept on one stack: push while the count is positive, and if the stack is empty when a subtract is needed, push to record a negative count (and then cancel against the next adds). Accept when the stack is empty at the end. So it is context-free.
(B) and : the language is . The 's match the 's and the 's match the 's, and these two matchings cross. One stack cannot do both. (The pumping lemma for CFLs on shows it.) Not context-free.
(C) and : the language is . The matchings are nested: push the 's, push the 's, pop them for the 's, then pop the 's for the 's. Context-free.
(D) : push one symbol for each and , then pop one for each and . Accept when the stack is empty at the end. Context-free.
Answer: (A), (C), (D)
(A) : this means . A PDA keeps a running count: add 1 for each and , subtract 1 for each and . The count can be positive or negative, but it can always be kept on one stack: push while the count is positive, and if the stack is empty when a subtract is needed, push to record a negative count (and then cancel against the next adds). Accept when the stack is empty at the end. So it is context-free.
(B) and : the language is . The 's match the 's and the 's match the 's, and these two matchings cross. One stack cannot do both. (The pumping lemma for CFLs on shows it.) Not context-free.
(C) and : the language is . The matchings are nested: push the 's, push the 's, pop them for the 's, then pop the 's for the 's. Context-free.
(D) : push one symbol for each and , then pop one for each and . Accept when the stack is empty at the end. Context-free.
Answer: (A), (C), (D)
Given a Context-Free Grammar as follows:
Which ONE of the following statements is TRUE?
Which ONE of the following statements is TRUE?
Answer: (C)
Here , since gives and gives .
Item sets after reading :
- From the start state: .
- After then : .
SLR(1): in the first state, SLR reduces on every symbol of , including . But the item says to shift . That is a shift-reduce conflict. (The second state has the same problem on .) So the grammar is not SLR(1).
LALR(1): the lookahead of is taken from the context of that state. In the first state, comes from , so the lookahead is only and there is no conflict with shifting . In the second state, comes from , so the lookahead is only and there is no conflict with shifting . The two states have different cores, so LALR does not merge them. So the grammar is LALR(1) (and also CLR(1)).
Answer: (C)
Item sets after reading :
- From the start state: .
- After then : .
SLR(1): in the first state, SLR reduces on every symbol of , including . But the item says to shift . That is a shift-reduce conflict. (The second state has the same problem on .) So the grammar is not SLR(1).
LALR(1): the lookahead of is taken from the context of that state. In the first state, comes from , so the lookahead is only and there is no conflict with shifting . In the second state, comes from , so the lookahead is only and there is no conflict with shifting . The two states have different cores, so LALR does not merge them. So the grammar is LALR(1) (and also CLR(1)).
Answer: (C)
Consider two grammars and with the production rules given below:
where are the terminals.
Which of the following option(s) is/are CORRECT?
where are the terminals.
Which of the following option(s) is/are CORRECT?
One or more options are correct.
Answer: (C), (D)
LL(1): in , both and begin with . One token of lookahead cannot choose, so there is a FIRST/FIRST conflict and is not LL(1). In , and both can begin with , because . Again a FIRST/FIRST conflict, so is not LL(1).
Ambiguity of : has two parse trees: the else goes with the inner if or with the outer if.
Ambiguity of : take .
- Tree 1: , where and derives . The outer if has no else.
- Tree 2: , where and . Here the last else belongs to the outer if.
The rule allows an unmatched if after an else, which makes this possible. So is ambiguous too.
So (A) and (B) are false.
Answer: (C), (D)
Ambiguity of : has two parse trees: the else goes with the inner if or with the outer if.
Ambiguity of : take .
- Tree 1: , where and derives . The outer if has no else.
- Tree 2: , where and . Here the last else belongs to the outer if.
The rule allows an unmatched if after an else, which makes this possible. So is ambiguous too.
So (A) and (B) are false.
Answer: (C), (D)
Keep practising