You need to pay attention to put the super- and subscripts at the right place in your code. Since it is not fully clear to me from your handwritten example in combination with the code you provide how the two latter braces should be positioned: Did you mean the following?
\documentclass{article}
\begin{document}
\[
\lefteqn{\underbrace{\phantom{S \rightarrow A}}_{n_0}} S \rightarrow
\lefteqn{\overbrace{\phantom{A \rightarrow A A}}^{n_1}} A \rightarrow
\lefteqn{\underbrace{\phantom{A A \rightarrow \epsilon A}}_{n_2}} A A \rightarrow \epsilon
\lefteqn{\overbrace{\phantom{A \rightarrow \epsilon}}^{n_3}} A \rightarrow \epsilon
\]
\end{document}
Or maybe this?
\documentclass{article}
\begin{document}
\[
\lefteqn{\underbrace{\phantom{S \rightarrow A}}_{n_0}} S \rightarrow
\lefteqn{\overbrace{\phantom{A \rightarrow A A}}^{n_1}} A \rightarrow
\lefteqn{\underbrace{\phantom{A A \rightarrow \epsilon}}_{n_2}} A A \rightarrow \epsilon
\lefteqn{\overbrace{A \rightarrow \epsilon}^{n_3}}
\]
\end{document}