I took your example on as an exercise, doing it in the style I've learned from Wolfgang Kinkeldei. Note in particular how the lexing is done. I put my sample code up as a gist. You may find Wolfgang's original example instructive also, even though it is for CSS.
I've limited the syntax to little more than the minimum needed to get the example working, but I think it's clear how it would be extended. Symbols names and are from the SQL-2003 standard, and the BNF, while just a very small subset, follows that standard pretty closely.