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.

  1. A.

    Leftmost derivation

  2. B.

    Leftmost derivation traced out in reverse

  3. C.

    Rightmost derivation traced out in reverse

  4. 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:

  1. Grammar: S -> a S b | ε

  2. Leftmost derivation for input 'a a b b': S => a S b

  3. => a a S b b

  4. => a a b b (by expanding S -> ε)

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor