1. Gyanastra
  2. Theory of Computation
  3. Context-Free Grammars
  4. Leftmost and Rightmost Derivations
Free lesson, no account needed 6 min read 1 practice questions after it
Asked in past papers9 times
  • GATE CSE2017
  • ISRO20202014
  • UGC NET202220182016201320122009

Theory of Computation · Context-Free Grammars

Leftmost and Rightmost Derivations

Overview

A derivation generates a terminal string from the start variable of a context-free grammar by replacing one variable at a time with the right-hand side of one of its productions.
The order in which variables are replaced defines leftmost and rightmost derivations, and the intermediate strings produced are called sentential forms.

Key concepts

  • Derivation: the process of generating a string of terminals from the start variable of a context-free grammar by repeatedly replacing one variable at a time with the right-hand side of one of its productions.
  • One variable per step: at every step, exactly one variable is replaced by its right-hand side — for example, either SS is replaced by its right-hand side, or BB is replaced by its right-hand side (the right-hand side of BB is small bb).
  • Leftmost derivation: at every step, the leftmost variable in the current string is replaced.
  • Rightmost derivation: at every step, the rightmost variable in the current string is replaced.
  • Sentential forms (also called sequential forms): the intermediate strings produced along the way. The final string contains only terminals and is the derived string; the whole procedure is called a derivation.

How it works

Grammar used (productions applied in this section):

  • S→ASBS \to A S B
  • S→ABS \to A B
  • B→bB \to b

Leftmost derivation — always replace the leftmost variable:

  1. Start from the start variable SS.
  2. Replace SS (the leftmost, and only, variable) using S→ASBS \to A S B, giving ASBA S B.
  3. The current string is ASBA S B. The leftmost variable is SS. Replace SS using S→ABS \to A B (not S→ASBS \to A S B), giving AABBA A B B.
  4. The current string is aaBBa a B B. The leftmost variable is the first BB. Replace it using B→bB \to b (right-hand side is small bb).
  5. Replace the remaining BB using B→bB \to b, giving AAbb=AABBA A b b = AABB.

Therefore, from SS the string aabbaabb is obtained. The intermediate strings SS, ASBA S B, AABBA A B B are the sentential forms.

Formulas

  • S→aSBS \to a S B — production of the grammar; SS is the start variable, BB is a variable and aa is a terminal.
  • S→ABS \to A B — production of the grammar.
  • B→bB \to b — production of the grammar; bb is a terminal.
  • S⇒aSB⇒aaBB⇒aabB⇒aabbS \Rightarrow a S B \Rightarrow a a B B \Rightarrow a a b B \Rightarrow a a b b — the leftmost derivation of aabbaabb; each ⇒\Rightarrow is one replacement step.

Worked examples

Example: Leftmost derivation of AABBAABB

Grammar:

  • S→ASBS \to A S B
  • S→ABS \to A B
  • B→bB \to b
StepCurrent string (sentential form)Variable replacedProduction used
1SS—(start)
2ASBA S BSS (leftmost)S→ASBS \to A S B
3AABBA A B BSS (leftmost)S→ABS \to A B
4AAbBA A b Bfirst BB (leftmost)B→bB \to b
5AAbb=AABBA A b b = AABBremaining BBB→bB \to b

The derivation is written as: S⇒ASB⇒AABB⇒AAbB⇒AAbbS \Rightarrow A S B \Rightarrow A A B B \Rightarrow A A b B \Rightarrow A A b b.

Sentential forms in this derivation: SS, ASBA S B, AABBA A B B, AAbBA A b B. The final string AABBAABB contains only terminals.

Comparison

AspectLeftmost derivationRightmost derivation
Variable replaced at each stepThe leftmost variable in the current sentential formThe rightmost variable in the current sentential form
Rule followedAlways expand the leftmost non-terminal firstAlways expand the rightmost non-terminal first
Intermediate stringsSentential formsSentential forms

Revision summary

  • A derivation replaces exactly one variable by its right-hand side at each step.
  • Leftmost derivation: always expand the leftmost variable; rightmost derivation: always expand the rightmost variable.
  • In the worked example, SS is first replaced by ASBA S B, then the leftmost SS by ABA B (giving AABBA A B B), then each BB by bb, giving AABBAABB.
  • Intermediate strings obtained during a derivation are called sentential forms (sequential forms).
  • The derivation ends when the string contains only terminals.
  • Trap: at step 3 the leftmost SS must be replaced using S→ABS \to A B, not S→ASBS \to A S B.

Previous-year exam insights

  • A string has two distinct parse trees iff it has two distinct leftmost derivations iff it has two distinct rightmost derivations; a grammar is ambiguous iff some string has two distinct parse trees (equivalently two distinct leftmost or two distinct rightmost derivations).
    Asked inUGC NET 2022UGC NET 2018UGC NET 2016
  • An unambiguous grammar has a unique parse tree for every string, hence a unique leftmost derivation and a unique rightmost derivation for each string; however the leftmost and rightmost derivations of a string are generally different sequences (they coincide only in trivial cases), so 'same leftmost and rightmost derivation' is not a property of unambiguous grammars.
    Asked inUGC NET 2016
  • Every leftmost (or rightmost) derivation corresponds one-to-one with a parse tree, and the number of derivation steps equals the number of internal nodes (production applications) in the parse tree; for a string of length n with a grammar whose productions each add one terminal, the step count can be computed by counting nodes.
    Asked inISRO 2014
  • In a rightmost derivation the handle is the leftmost complete right-hand side (the portion reduced next in the corresponding bottom-up parse); identifying the handle of a given right sentential form requires locating the substring just produced by the last rightmost expansion.
    Asked inUGC NET 2013
  • An ambiguous grammar is never LR(k) for any k, but the converse fails: an unambiguous grammar need not be LR(k) for any k (e.g. S → aSa | a | ε needs to guess the middle of an odd palindrome... or S → aSa | bSb | a | b | ε).
    LL(1) grammars are always unambiguous; ambiguous grammars can still be parsed by general methods (Earley, CYK, GLR).
    Asked inUGC NET 2016UGC NET 2009
  • Operator-precedence/associativity is determined by the recursive structure of the grammar: a production of the form E → E op T (left recursion) gives left associativity, while E → T op E (right recursion) gives right associativity; precedence is fixed by the level at which each operator appears in the grammar hierarchy.
    Asked inISRO 2020
Pages 1–2 of 2