Would the following grammar be ambiguous? I have the grammar $S = aSbS|bSaS|\varepsilon$. When I was first looking at the problem I thought it was unambiguous. When I looked at the answer it said that it was ambiguous. The solution it gave was:

$S \rightarrow aSbS \rightarrow a\varepsilon bS \rightarrow a\varepsilon b\varepsilon \rightarrow ab$

$S \rightarrow aSbS \rightarrow aSb\varepsilon \rightarrow a\varepsilon b\varepsilon \rightarrow ab$

This doesn't make sense to me though because aren't these both producing the same parse tree?
Indeed, both of those derivations produce the same parse tree.

A grammar is ambiguous iff it has two different leftmost derivations for a sentence. (Equivalently, if it has two different rightmost derivations.) Only one of the provided derivations is leftmost, so these two derivations do not constitute a demonstration of ambiguity.

The grammar is ambiguous, though:

$$\begin{align}&amp;S\to aSbS \to abS \to abaSbS\to ababS \to abab\\
&amp;S\to aSbS \to abSaSbS \to abaSbS \to ababS \to abab\\
\end{align}$$