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*3 with a precedence grammar, the leftmost and rightmost derivations differ step-by-step but yield the same parse tree (and the same grouping ):
LeftmostRightmost
rewrite orderleftmost nonterminal each steprightmost nonterminal each step
step 1
middleexpands 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