$\forall x \,\exists k$ s.t. $f^{(k)}(x)=0$, then $f$ is a polynomial My friend sent me the following problem:

Suppose that $f$ is real analytic on $(a,b)$, and that for all $x$ in
  $(a,b)$ there exists a non-negative integer $k$ such that
  $f^{(k)}(x)=0$.  Show that $f$ is a polynomial.

I believe I solved it (you can read my answer below the fold if you are interested). Then my friend posed the question of what happens if $f$ is only $C^{\infty}$. 
I believe my argument below has shown that $\{x: \exists \,\,\text{a nbd of } x\,\,\text{on which }f \,\,\text{is a polynomial}\}$ is dense in $(a,b)$. But I can't  seem to show that that implies $f$ is a polynomial. Can someone think of a counter-example, or finish the proof?

Proposition: Suppose that $f$ is real analytic on $(a,b)$, and that for all $x$ in $(a,b)$ there exists a non-negative integer $k$ such that $f^{(k)}(x)=0$.  Then $f$ is a polynomial. 
Proof: If $f$ agrees with a polynomial $p$ on an open interval, then the Taylor expansion at any point in that interval is finite, and so has an infinite radius of convergence. Properties of analytic functions then imply that $f=p$ on $(a,b)$. So suppose for contradiction that $f$ does not agree with a polynomial on an open interval.
For all $x\in (a,b)$, define $n_x$ to be the smallest integer $k$ for which $f^{(k)}(x)=0$. Let $x\in (a,b)$ be arbitrary. Then by continuity of all derivatives, there exists a neighborhood $(a_1,b_1)\subseteq (a,b)$ such that $n_y\geq n_x$ for all $y\in (a_1,b_1)$. Now if $n_y=n_x$ for all $y\in (a_1,b_1)$, then $f$ agrees with a polynomial on that interval, a contradiction. So there exists $x_1\in (a_1,b_1)$ such that $n_{x_1}>n_x$. Take $[\alpha_1, \beta_1]\subset (a_1,b_1)$ such that $x_1\in [\alpha_1,\beta_1]$. Now repeat the process, starting with $x_1$, to generate $x_2 \in [\alpha_2,\beta_2]$ such that $n_{x_2}>n_{x_1}$.
The nested interval theorem now implies that there exists a point $x^* \in \cap_{i}[\alpha_i,\beta_i]$, and since $n_{x_i}\xrightarrow{i\to\infty} \infty$, $f^{k}(x^*) \neq 0\,\,\forall k$, a contradiction. $\blacksquare$ 
 A: The following proof should do the job (if it's correct):
Let $\epsilon>0$ and denote by $X_0=[a+\epsilon,b-\epsilon]$. Notice that $X_0$ is a complete metric space (we needed to restrict it on the boundaries to obtain completeness). Now let:
$$A_k=\{x\in X_0|f^{(k)}(x)\neq0\}=(f^{(k)})^{-1}(\mathbb{R}-\{0\})\subseteq X_0$$
Those sets are open, assume that all of them are dense, then the Baire category theorem asserts that their intersection is also dense. However, by assumption we have $\bigcap_{k\ge0}A_k=\emptyset$, which would be a contradiction. Thus there is some $k_0$ such that $X_0-A_{k_0}$ contains some open set $U_0$, i.e. $f|_{U_0}$ must be a polynomial.
Let $X_1=X_0-U_0$ and repeat all of the above to obtain some open set $U_1\subseteq X_1$ on which $f$ is again a polynomial, and so on ad infinitum (or until $U_n=X_n$, which would give us the statement).
Define $U=\bigcup_{n\ge0}U_n$. If $U\subsetneq X_0$, then the same process as above on $X_0-U$ would give us a new open not contained in $U$, which would be a contradiction, so $U=X_0$. By compactness of $X_0$, there is some $N$ such that $X_0=\bigcup_{n\ge0}^NU_n$, and thus $f$ must be a polynomial on $X_0$.
(Equivalently, the answer on MO provided by Bryan does the job, and is probably much more elegantly exposed than mine.)
Now assume that $f$ is not a polynomial everywhere. This means that there is some small open set near the boundary where $f$ is not a polynomial, and in particular some $x_0$ such that $f$ is not a polynomial on any open neighborhood of $x_0$. But by making $\epsilon$ smaller if necessary, we can take $x_0$ in the interior of $X_0$, obtaining a contradiction. Thus $f$ must be a polynomial on the whole of $(a,b)$.
I would be grateful if somebody could check if it works and if I haven't made any stupid mistakes.
