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 41–45 of 58
Which one of the following is a top-down parser?
Answer: (A)
Recursive descent parser: it is a TOP DOWN parser.
Operator precedence parser: it is a BOTTOM UP parser.
An LR(k) parser: it is a BOTTOM UP parser.
An LALR(k) parser: it is a BOTTOM UP parser.
The grammar
SAC|CB
CaCb|
AaA|a
BBb|b
generates the language , what is the length of the derivation (number of steps starting from S) to generate the string with ?
SAC|CB
CaCb|
AaA|a
BBb|b
generates the language , what is the length of the derivation (number of steps starting from S) to generate the string with ?
Answer: (A)
The string is with , so either or .
Case I, : write the string as , where .
- : 1 step.
- gives in steps (for example is 1 step, is 2 steps).
- gives in steps ( uses of and one ).
Total: .
Case II, : write the string as , where .
- : 1 step.
- gives in steps.
- gives in steps.
Total: .
In both cases the length is .
Answer: (A)
Case I, : write the string as , where .
- : 1 step.
- gives in steps (for example is 1 step, is 2 steps).
- gives in steps ( uses of and one ).
Total: .
Case II, : write the string as , where .
- : 1 step.
- gives in steps.
- gives in steps.
Total: .
In both cases the length is .
Answer: (A)
Which one of the following grammars generates the language
(A) (B) S → aS | Sb | a | b
S → AC | CB
C → aCb | a | b
A → aA | ∈
B → Bb | ∈
(C) (D)
S → AC | CB S → AC | CB
C → aCb | ∈ C → aCb | ∈
A → aA | ∈ A → aA | a
B → Bb | ∈ B → Bb | bAnswer: (D)
Check each grammar for a string of the form with that it generates (such a string is not in L), and check that D gives all strings with .
Option A. ends with an a or a b, so C gives or . Then can be derived: . It has , so A is wrong.
Option B. can be derived: . So B is wrong.
Option C. C gives (), and A and B can be . So , which has . So C is wrong.
Option D. C gives . A gives (at least one a), so gives , which is with . B gives , so gives , which is with . Together these are exactly the strings with .
Answer: (D)
Option A. ends with an a or a b, so C gives or . Then can be derived: . It has , so A is wrong.
Option B. can be derived: . So B is wrong.
Option C. C gives (), and A and B can be . So , which has . So C is wrong.
Option D. C gives . A gives (at least one a), so gives , which is with . B gives , so gives , which is with . Together these are exactly the strings with .
Answer: (D)
Consider the following grammar
S FR
R * S |
F id
In the predictive parser table, M, of the grammar the entries M[S, id] and M[R,$] respectively
S FR
R * S |
F id
In the predictive parser table, M, of the grammar the entries M[S, id] and M[R,$] respectively
Answer: (A)
FIRST(S) = FIRST(F) = {id}, so goes in M[S, id].
because S is the start symbol and S appears nowhere else on a right side except at the end of . R ends the production , so FOLLOW(R) contains FOLLOW(S), and .
Rule: for a production , put it in M[A, t] for every t in FOLLOW(A). So goes in M[R, ]. The production only goes under the input *, so it is not in M[R, ].
and .
Answer: (A)
because S is the start symbol and S appears nowhere else on a right side except at the end of . R ends the production , so FOLLOW(R) contains FOLLOW(S), and .
Rule: for a production , put it in M[A, t] for every t in FOLLOW(A). So goes in M[R, ]. The production only goes under the input *, so it is not in M[R, ].
and .
Answer: (A)
Consider the following grammar.
S S * E
S E
E F + E
E F
F F id
Consider the following LR(0) items corresponding to the grammar above.
(i) S S * .E
(ii) E F. + E
(iii) E F + .E
Given the items above, which two of them will appear in the same set in the canonical sets-of-items for the grammar?
S S * E
S E
E F + E
E F
F F id
Consider the following LR(0) items corresponding to the grammar above.
(i) S S * .E
(ii) E F. + E
(iii) E F + .E
Given the items above, which two of them will appear in the same set in the canonical sets-of-items for the grammar?
Answer: (D)
In an LR(0) state, every kernel item (an item whose dot is not at the start) was reached by the same grammar symbol, and that symbol is just before the dot in each of them. Closure only adds items with the dot at the start, so these three items cannot come from a closure; each one is a kernel item of the state it appears in.
- (i) has * before the dot, so its state is entered on *.
- (ii) has F before the dot, so its state is entered on F.
- (iii) has + before the dot, so its state is entered on +.
The three symbols are different, so no two of the items can be in the same set.
Answer: (D)
- (i) has * before the dot, so its state is entered on *.
- (ii) has F before the dot, so its state is entered on F.
- (iii) has + before the dot, so its state is entered on +.
The three symbols are different, so no two of the items can be in the same set.
Answer: (D)
Keep practising