Derivation Tree

2 min read Last updated Sat Jun 27 2026 08:46:52 GMT+0000 (Coordinated Universal Time)

Aka. parse tree. Represents the hierarchical structure of a derivation from a CFG.

Structure:

  • Root: start symbol SS
  • Internal nodes: non-terminals
  • Leaves: terminals or Λ\Lambda

Reading leaf nodes left-to-right gives the derived string (the yield of the tree).

Types of Derivations

Leftmost Derivation

Always replace the leftmost non-terminal first.

Rightmost Derivation

Always replace the rightmost non-terminal first.

Each derivation tree corresponds to exactly 1 leftmost derivation and exactly 1 rightmost derivation.

Example

Grammar:

  • SS+SSSaS \to S + S \mid S * S \mid a

Deriving a+aaa + a * a leftmost:

SS+Sa+Sa+SSa+aSa+aaS \Rightarrow S + S \Rightarrow a + S \Rightarrow a + S * S \Rightarrow a + a * S \Rightarrow a + a * a

The corresponding derivation tree has SS at the root, with children SS, ++, SS. The right SS has children SS, *, SS.

Ambiguity

A grammar is ambiguous iff some string has 2 or more distinct derivation trees (equivalently, 2 distinct leftmost derivations).

For a+aaa + a * a under the grammar above, a 2nd tree exists where the root SS splits as SSS * S instead. Both trees are valid, so the grammar is ambiguous.

Was this helpful?