Derivations and Parse Trees
Context: FIT2014_MOC Β· how a CFG produces a string β the step-by-step rewrite and its tree Β· the bridge to the PDA construction
Quick Revision
- π― Objective: a derivation applies production rules one at a time, ; a parse tree records the structure of that derivation, hiding the order.
- β‘ Key Constraint: many derivations share one parse tree β the tree captures structure (and hence meaning); the order of rewrites is what distinguishes leftmost vs rightmost.
π Derivations
- A derivation β a sequence where each replaces one nonterminal using a production, ending at a terminal string .
- Example (grammar , deriving ):
π³ Parse trees
- Structure, not order β the root is ; each internal node is a nonterminal whose children are the symbols on the right side of the rule applied to it; the leaves read left-to-right spell .
- Why they matter β the tree encodes grouping/precedence β e.g. for arithmetic it shows that binds tighter than .
βοΈ Leftmost vs rightmost
- Leftmost derivation β always rewrite the leftmost nonterminal first.
- Rightmost derivation β always rewrite the rightmost nonterminal first.
- Same tree, different sequence β for
4 + 2*3with a precedence grammar, the leftmost and rightmost derivations differ step-by-step but yield the same parse tree (and the same grouping ):
| Leftmost | Rightmost | |
|---|---|---|
| rewrite order | leftmost nonterminal each step | rightmost nonterminal each step |
| step 1 | ||
| middle | expands before | expands before |
| result |
- Theorem β whenever a string has a derivation, it has a leftmost derivation of the same length. (Proof: reorder rule applications; the tree is unchanged.)
π The prefix property (of leftmost derivations)
- Observation β at any stage of any derivation, the string to the left of the first nonterminal is a prefix of the final derived string.
- Leftmost sharpening β when a leftmost derivation applies a rule , those leading terminals are appended to the confirmed prefix, growing it.
- Why it is the key idea β this βgrow a correct prefix, defer the restβ behaviour is exactly what the PDA simulates β confirmed terminals are read off the input, the deferred suffix (including nonterminals) lives on the stack.
β οΈ Common Mistakes
- π‘ Derivation β parse tree β a string can have many derivations (leftmost, rightmost, and mixtures) but they may all share one parse tree; the tree is the canonical object.
- π‘ is one step; is many β a single rewrites exactly one nonterminal.
- π‘ Leftmost/rightmost is about order, not result β both derive the same string via the same tree; they differ only in which nonterminal is expanded next.
- π‘ Ambiguity is a separate issue β a grammar is ambiguous if some string has two different parse trees β distinct from having two derivations of the same tree.
π§ Active Recall
A string has both a leftmost and a rightmost derivation. What do they share and how do they differ?
Answer
- Short answer: they produce the same string via the same parse tree (same grouping), and β by the theorem β have the same length. They differ only in the order nonterminals are expanded: leftmost always takes the left-most nonterminal, rightmost the right-most.
- Why: The tree fixes the rules, the order is free β a parse tree determines exactly which productions are used and how they nest; traversing it left-to-right gives the leftmost derivation, right-to-left the rightmost, so both are just linearisations of one structure.
Why is the "prefix property" of leftmost derivations the crucial idea for simulating a CFG with a machine?
Answer
- Short answer: in a leftmost derivation the material left of the first nonterminal is always a confirmed prefix of the target string, so a machine can read that prefix off the input and keep only the unresolved suffix (terminals not yet matched plus nonterminals) on a stack.
- Why: Grow-prefix / stack-the-rest β this is precisely the PDA simulation of a grammar: expand the top nonterminal on the stack by a production, and pop-match terminals against the input as they surface β the leftmost strategy guarantees the input is consumed in order.