Definition (parse tree)

Let $G = (Σ, V, P, S)$ be a context-free grammar, and $A\in V$ be a variable.

A pair $\mathbf{T} = (T,l)$ is a $G$-parse tree starting from $A$ if $T$ is a finite finitely branching tree and $l: T \to Ω$ satisfying

Write $σ_\mathbf{T} = l(t_0) \ldots l(t_m)$ for the string parsed by $\mathbf{T}$, where $l_0 < t_1 < \ldots < t_m$ are the leaves of $\mathbf{T}$ totally-ordered by the left-to-right order.

For $t\in T$ not a leaf, define the subtree at $t$ by $\mathbf{T}_t = (T_t, l_t)$ where $T_t = \sb{s\in \N^\ast}{ts\in T}$ and $l_t(s) = l(ts)$.

If $\mathbf{T}$ and $\mathbf{T}'$ are $G$-parse trees, $t\in T$ with $l(t) = A$ and $\mathbf{T}'$ starts from $A$, then we can graft $\mathbf{T}'$ into $\mathbf{T}$: define $\mathrm{graft}(\mathbf{T}, t, \mathbf{T}') = (T^\star, l^\star)$ where $T^\star = \sb{s\in T}{t\not\subseteq s} \cup \sb{ts}{s\in T'}$ and $$ \begin{align*} l^\star(s) = \begin{cases} l(s), &\text{if } t\not\subseteq s, \\ l'(u), &\text{if } s = tu \text{ for some $u\in T'$}. \end{cases} \end{align*} $$