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 is replaced by its right-hand side, or is replaced by its right-hand side (the right-hand side of is small ).
- 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):
Leftmost derivation — always replace the leftmost variable:
- Start from the start variable .
- Replace (the leftmost, and only, variable) using , giving .
- The current string is . The leftmost variable is . Replace using (not ), giving .
- The current string is . The leftmost variable is the first . Replace it using (right-hand side is small ).
- Replace the remaining using , giving .
Therefore, from the string is obtained. The intermediate strings , , are the sentential forms.
Formulas
- — production of the grammar; is the start variable, is a variable and is a terminal.
- — production of the grammar.
- — production of the grammar; is a terminal.
- — the leftmost derivation of ; each is one replacement step.
Worked examples
Example: Leftmost derivation of
Grammar:
| Step | Current string (sentential form) | Variable replaced | Production used |
|---|---|---|---|
| 1 | — | (start) | |
| 2 | (leftmost) | ||
| 3 | (leftmost) | ||
| 4 | first (leftmost) | ||
| 5 | remaining |
The derivation is written as: .
Sentential forms in this derivation: , , , . The final string contains only terminals.
Comparison
| Aspect | Leftmost derivation | Rightmost derivation |
|---|---|---|
| Variable replaced at each step | The leftmost variable in the current sentential form | The rightmost variable in the current sentential form |
| Rule followed | Always expand the leftmost non-terminal first | Always expand the rightmost non-terminal first |
| Intermediate strings | Sentential forms | Sentential 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, is first replaced by , then the leftmost by (giving ), then each by , giving .
- 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 must be replaced using , not .
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