Derivation Tree

Also known as 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:

  • S→S+S∣S∗S∣aS \to S + S \mid S * S \mid a

Deriving a+a∗aa + a * a leftmost:

S⇒S+S⇒a+S⇒a+S∗S⇒a+a∗S⇒a+a∗aS \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+a∗aa + a * a under the grammar above, a 2nd tree exists where the root SS splits as S∗SS * S instead. Both trees are valid, so the grammar is ambiguous.

Written by October 17, 2026 1 min read
Was this helpful?