Parsing · Compiler Design
58 questions · asked in 25 of 27 years (2000–2026)
MCQ 46 · MSQ 5 · NAT 71-mark 39 · 2-mark 19
2000Questions per year2026
Questions 36–40 of 58
Consider the grammar given below:
Consider the following strings.
i. xxyyx
ii. xxyyxy
iii. xyxy
iv. yxxy
v. yxx
vi. xyx
Which of the above strings are generated by the grammar ?
Consider the following strings.
i. xxyyx
ii. xxyyxy
iii. xyxy
iv. yxxy
v. yxx
vi. xyx
Which of the above strings are generated by the grammar ?
Answer: (C)
Let B generate strings with one more y than x, and A generate strings with one more x than y.
A: has one extra x. is x plus an S with equal counts, so one extra x. is one y, plus two A's with two extra x, so one extra x.
B: , and give one extra y in the same way.
So and give strings with equal numbers of x and y. A generated string must have equal x and y.
Count in each string:
i. xxyyx has 3 x and 2 y, so no.
ii. xxyyxy has 3 x and 3 y. , so yes.
iii. xyxy: , so yes.
iv. yxxy: , so yes.
v. yxx has 2 x and 1 y, so no.
vi. xyx has 2 x and 1 y, so no.
So ii, iii and iv are generated.
Answer: (C)
A: has one extra x. is x plus an S with equal counts, so one extra x. is one y, plus two A's with two extra x, so one extra x.
B: , and give one extra y in the same way.
So and give strings with equal numbers of x and y. A generated string must have equal x and y.
Count in each string:
i. xxyyx has 3 x and 2 y, so no.
ii. xxyyxy has 3 x and 3 y. , so yes.
iii. xyxy: , so yes.
iv. yxxy: , so yes.
v. yxx has 2 x and 1 y, so no.
vi. xyx has 2 x and 1 y, so no.
So ii, iii and iv are generated.
Answer: (C)
Consider an ambiguous grammar G and its disambiguated version D. Let the language recognized by the two grammars be denoted by L(G) and L(D) respectively. Which one of the following is true?
Answer: (C)
Correct Option: C
The two grammars must give the same language. When we change an ambiguous grammar into an unambiguous one, the new grammar must generate the same language as the old one.
Example: Ambiguous parse trees for the string ''
Unambiguous
Both grammars give the language of the regular expression:
Consider the CFG with {S, A,B} as the non-terminal alphabet, {a,b} as the terminal alphabet, S as the start symbol and the following set of production rules:
S aB SbA
B b A a
B bS A aS
B aBB A bAA
For the string "aabbab", how many derivation trees are there?
S aB SbA
B b A a
B bS A aS
B aBB A bAA
For the string "aabbab", how many derivation trees are there?
Answer: (B)
The string is . It starts with a, and the only rule for S that starts with a is . So B must give .
B starts with a, so is the only choice (the other B rules start with b). So the two B's together give . The split can be 1 + 3, 2 + 2 or 3 + 1 symbols.
2 + 2 is impossible: no B gives (that needs with S giving the single symbol b, but every S gives at least 2 symbols), and no B gives (that needs , which gives at least 3 symbols).
Split : the first B is . For only fits, and S must give , which is only . So this split has exactly one tree:
Split : the first B is (it starts with b and has 3 symbols), and S must give , which is only . The second B is . So this split has exactly one tree:
Every other step was forced, so there are exactly derivation trees.
Answer: (B)
B starts with a, so is the only choice (the other B rules start with b). So the two B's together give . The split can be 1 + 3, 2 + 2 or 3 + 1 symbols.
2 + 2 is impossible: no B gives (that needs with S giving the single symbol b, but every S gives at least 2 symbols), and no B gives (that needs , which gives at least 3 symbols).
Split : the first B is . For only fits, and S must give , which is only . So this split has exactly one tree:
Split : the first B is (it starts with b and has 3 symbols), and S must give , which is only . The second B is . So this split has exactly one tree:
Every other step was forced, so there are exactly derivation trees.
Answer: (B)
Consider the CFG with {S, A,B} as the non-terminal alphabet, {a,b} as the terminal alphabet, S as the start symbol and the following set of production rules:
S aB SbA
B b A a
B bS A aS
B aBB A bAA
Which of the following strings is generated by the grammar?
S aB SbA
B b A a
B bS A aS
B aBB A bAA
Which of the following strings is generated by the grammar?
Answer: (C)
Derivation of the string:
Start with S:
Next step:
Next step:
Next step:
Next step:
Final string:
Correct Answer:
Start with S:
Next step:
Next step:
Next step:
Next step:
Final string:
Correct Answer:
Consider the grammar with non-terminals N={}, terminals
T={a,b,i,t,e}, with S as the start symbol, and the following set of rules:
The grammar is NOT LL(1) because:
The grammar is NOT LL(1) because:
Answer: (C)
An ambiguous grammar can never be LL(1), so first find a string with two parse trees. This is the "dangling else" grammar.
Take (with and the inner S given by ). The outer contains an inner , so there are two 's side by side, and the last part can come from either one:
1) inner (with ), outer : the else goes with the inner if.
2) inner , outer (with ): the else goes with the outer if.
The figure shows the nested S and its two 's (shaded). Both choices give the same string with different trees, so the grammar is ambiguous.
The LL(1) test fails too. For , the rule with needs , but .
- In , S is followed by , so .
- In the same rule is at the end, so , and so .
So on input e the table entry for holds both and .
Other options: (A) is wrong because no non-terminal can derive a string that starts with itself (S starts with i or a, with e, C with b). (B) is wrong because right recursion does not stop a grammar from being LL(1). (D) is wrong because every rule has a single non-terminal on its left side, so the grammar is context-free.
Answer: (C)
Take (with and the inner S given by ). The outer contains an inner , so there are two 's side by side, and the last part can come from either one:
1) inner (with ), outer : the else goes with the inner if.
2) inner , outer (with ): the else goes with the outer if.
The figure shows the nested S and its two 's (shaded). Both choices give the same string with different trees, so the grammar is ambiguous.
The LL(1) test fails too. For , the rule with needs , but .
- In , S is followed by , so .
- In the same rule is at the end, so , and so .
So on input e the table entry for holds both and .
Other options: (A) is wrong because no non-terminal can derive a string that starts with itself (S starts with i or a, with e, C with b). (B) is wrong because right recursion does not stop a grammar from being LL(1). (D) is wrong because every rule has a single non-terminal on its left side, so the grammar is context-free.
Answer: (C)
Keep practising