Which of the following derivations does a top-down parser use while parsing an…
2013
Which of the following derivations does a top-down parser use while parsing an input string ? The input is scanned from left to right.
- A.
Leftmost derivation
- B.
Leftmost derivation traced out in reverse
- C.
Rightmost derivation traced out in reverse
- D.
Rightmost derivation
Attempted by 199 students.
Show answer & explanation
Correct answer: A
Answer: Leftmost derivation.
Why:
Top-down parsers begin with the start symbol and expand nonterminals. At each step they expand the leftmost nonterminal, so the parser constructs a leftmost derivation of the input.
The input is scanned from left to right and matched against the terminals produced by these leftmost expansions; LL and recursive-descent parsers follow this approach.
By contrast, bottom-up parsers (such as LR/shift-reduce) effectively construct a rightmost derivation in reverse, so any option that references a rightmost derivation in reverse refers to bottom-up parsing, not top-down.
Short example:
Grammar: S -> a S b | ε
Leftmost derivation for input 'a a b b': S => a S b
=> a a S b b
=> a a b b (by expanding S -> ε)
A video solution is available for this question — log in and enroll to watch it.