How does a leftmost derivation of a string differ from a rightmost derivation of the same string?
answer
- same rules, different order
- which nonterminal do you rewrite next
- sentential forms are the intermediate lines
- terminals pile up on one side first
- step count is the same either way
basics
~20 sBoth rewrite the same nonterminal occurrences by the same rules, but in a different order: a leftmost derivation always rewrites the leftmost nonterminal of the current line, a rightmost derivation always the rightmost one. The intermediate lines differ; the rule applications do not.
solid answer
~50 sA derivation is a sequence of lines, each called a **sentential form**, starting at the start symbol and ending at the target string. At every step you must choose which nonterminal in the current line to rewrite. A **leftmost** derivation always picks the leftmost one; a **rightmost** derivation always picks the rightmost one. For a rule set where a filter is `TERM and FILTER`, the leftmost order expands the first comparison down to words before touching the tail, while the rightmost order expands the tail first and fills in the first comparison last. Both use the same set of rule applications at the same positions, and both take the same number of steps, so when the grammar offers only one way to build the string they describe the same structure — they are two conventions for linearising it, nothing more.
code
pseudocode · 16 linesgrammar, start symbol FILTER (upper case = nonterminal):
FILTER -> TERM and FILTER | TERM
TERM -> FIELD is VALUE
FIELD -> status | owner
VALUE -> open | me
leftmost derivation of: status is open and owner is me
FILTER
=> TERM and FILTER
=> FIELD is VALUE and FILTER
=> status is VALUE and FILTER
=> status is open and FILTER
=> status is open and TERM
=> status is open and FIELD is VALUE
=> status is open and owner is VALUE
=> status is open and owner is mego deeper
Recall that a derivation is a sequence of lines from the start symbol down to the text, and that leftmost and rightmost name which placeholder gets replaced at each line.
Write both orders out for a short sentence and point at what changes: the intermediate lines change shape, the rule applications and the step count do not.
Use the derivation as an argument rather than a ritual: show that different lengths mean different rules were used, and say what that implies about the rule set you are reviewing.
Frame why a team standardises on one canonical order in its written specification, and what reviewers gain from derivations that can be compared line by line.
## Sentential forms: the lines of a derivation A derivation is written as a sequence of lines joined by a rewriting arrow. Each line is a **sentential form**: a string of terminals and nonterminals mixed together, reachable from the start symbol. The first sentential form is the start symbol alone; the last contains no nonterminal and is the generated string, which is why an all-terminal sentential form is called a **sentence** of the grammar. At each step there may be more than one nonterminal in the line, so the derivation has a choice of *where* to apply a rule, quite separate from the choice of *which* rule to apply. Fixing that first choice by a rule of thumb gives the two named orders. ## The same sentence, twice Take the four-rule saved-filter grammar with start symbol `FILTER`: ``` FILTER -> TERM and FILTER | TERM TERM -> FIELD is VALUE FIELD -> status | owner VALUE -> open | me ``` The rightmost derivation of `status is open and owner is me` rewrites the rightmost nonterminal every time: ``` FILTER => TERM and FILTER => TERM and TERM => TERM and FIELD is VALUE => TERM and FIELD is me => TERM and owner is me => FIELD is VALUE and owner is me => FIELD is open and owner is me => status is open and owner is me ``` The leftmost derivation of the same sentence is in the code example beside this answer. Both are eight steps long, and both apply exactly the same eight productions to exactly the same symbol occurrences. What differs is the shape of the intermediate lines: the leftmost order finishes the first comparison into words before it ever touches the tail, while the rightmost order leaves `TERM` standing on the left until the final three steps. | | Leftmost derivation | Rightmost derivation | |---|---|---| | Which nonterminal is rewritten | the leftmost one in the line | the rightmost one in the line | | Early sentential forms | terminals accumulate on the left | terminals accumulate on the right | | Number of steps for a given structure | identical | identical | | Multiset of productions applied | identical | identical | | What it fixes | the order of rewriting only | the order of rewriting only | ## Why the derivation length is not a free variable One step rewrites exactly one nonterminal occurrence, so the number of steps equals the number of nonterminal occurrences that ever appear — which is one per rule application. Reordering the steps cannot change that count. If two derivations of the same sentence have different lengths, they are not reorderings of each other: they used different rules, and the grammar therefore offers more than one way to build that sentence. Spotting that is the doorway to the ambiguity question, which belongs to its own subject and is not what this one asks. ## What the choice of order does not change It is tempting to think the two orders describe different structures, because the lines on the page look so different. They do not. The rewriting relation only ever replaces a single nonterminal by the right-hand side of one of its own productions, and that replacement is independent of what surrounds it: rewriting the tail first cannot affect which rules are available for the head. So when the same rule applications are available in both orders, both orders describe the same nesting — the same parent-child relationships between rule applications. The practical consequences are worth stating plainly: - **A derivation is evidence, not a plan.** Exhibiting any derivation of a string shows it is in the language; you are free to write down whichever order is easiest to read. - **The two orders have names because tools are built around them.** Prediction-driven and reduction-driven parser families each correspond naturally to one of the two, which is why interview questions about parser families assume this vocabulary. - **A mixed order is still a derivation.** Nothing forbids rewriting the middle nonterminal one step and the leftmost the next; such a derivation is perfectly valid, just not canonical. Leftmost and rightmost are singled out because they are deterministic conventions, not because other orders are illegal. - **The sentential forms of a leftmost derivation have a useful shape**: everything to the left of the rewritten nonterminal is already terminal, which is exactly the prefix a left-to-right reader has consumed. ## What an interviewer is listening for A weak answer treats the two orders as two different meanings. A strong one separates the *choice of rule*, which is about what the language contains, from the *choice of position*, which is only about bookkeeping — and then says what it takes for that separation to be safe: the same rules applied to the same occurrences. That sentence is what the follow-up about parse trees is built on.
- Is a derivation that rewrites neither the leftmost nor the rightmost nonterminal still valid?Yes. The rewriting relation permits any nonterminal occurrence to be replaced at any step, so a mixed order generates exactly the same strings. Leftmost and rightmost are singled out only because each is a deterministic convention that pins the choice of position, which makes derivations comparable and makes the parser families that follow them describable.
- Two derivations of the same sentence have different lengths. What does that tell you?They are not reorderings of one another. Each step applies exactly one production, so any two orderings of the same rule applications have the same length. Different lengths mean different rules were used, so the grammar offers more than one way to build that sentence — the starting point for the ambiguity question.
- What is special about the sentential forms of a leftmost derivation?Everything to the left of the nonterminal being rewritten is already terminal, so each line splits cleanly into a finished prefix and an unfinished remainder. That shape mirrors how a left-to-right reader consumes input, which is why the leftmost order is the natural one to quote when discussing prediction-driven parsing.
saying these in an interview costs you the question
- Thinks the two orders generate different sets of strings.
- Says a rightmost derivation reads the input backwards.
- Claims one order needs more steps than the other.
- Treats a sentential form as always all terminals.
- Believes any order other than leftmost or rightmost is illegal.
- Confuses choosing a position with choosing a production.