I don't know if there's an easier way, but I wrote a lexer and parser for SQL expressions into a custom Abstract Syntax Tree (as I didn't know about .net's Expression Trees at the time) and I didn't really enjoy parsing SQL.
The syntax just is not very parse friendly, as order is different depending on context (e.g. the not in NOT IN
vs. IS NOT
), tokens are overloaded (the parenthesis for overriding the default operator precedence vs. parentheses for creating a list as in WHERE x IN (1, 2)
) and so forth.
Obviously using a parser generator rather than doing your own lexing and parsing would make things easier, but I don't know if there's anything more specific to SQL.
So, writing your own is definitely possible, albeit tedious.