I am new to ANTLR
& grammar writing. My requirement is to parse insert/update/delete SQL
queries to get details like which table is being update/inserted/deleting a row, list of columns & their values etc. Yes there is a good documentation of ANTLR
, but if anyone can help me with specific grammar for query parsing then it would be great help.
2 Answers
There are various SQL grammars on the Wiki: http://www.antlr.org/grammar/list
Beware though: they are contributed by ANTLR-users, chances are that they've not been properly tested and/or contain bugs.
But why generate an SQL parser yourself? It would probably be easier to use some exiting SQL parser. Just do a search on "SQL parser Java" (assuming you're working with Java), and you're bound to get dozens of hits.

- 166,582
- 36
- 299
- 288
-
Yes...I am working with Java. And yeah I trid with several parser like General SQL parser (GSP) is good one. But I need jdk 14 compatible parser, which I am not able to find. If you are ware about any jdk14 compatible parser then please suggest. – hemu Aug 10 '12 at 09:00
-
Why must it be JDK 14 (1.4?) compatible? If you are analyzing SQL code on a modern PC, I'd expect you to be able to use any weapons you like. This "requirement" seems like it just makes the problem hard to solve, without adding any value. – Ira Baxter Aug 10 '12 at 09:09
-
@ Ira Baxter - Yes you are right. But in our application we have some legacy. So I must have it `jdk 1.4` compatible. – hemu Aug 10 '12 at 09:21
-
Why does it matter what you application code is? Your SQL analyzer is looking at the application code, fine, but the analyzer doesn't have to made of the same stuff as your application. Usually when one adds egregious constraints to a problem, it doesn't get solved. – Ira Baxter Aug 10 '12 at 14:52
Implementing a "decent" SQL parser is actually fairly hard. In SQL, one can write all kinds of complex statements (nested joins, ...), and people really do this, so you have to implement the "full" language. (I've seen SQL queries that cover tens of pages. Stupid, yes, but then you have to work with what you encounter).
I suggest checking out SQL2011 (the standard) as being rather comprehensive. However, that grammar may not be ANTLR friendly, so be prepared for a fair bit of work.
You also have to worry about database/vendor specific extensions to standard SQL. PL/SQL (just the SQL sublanguage part) contains lots of Oracle-specific extensions. If you are facing PL/SQL stored procedures, and you want to do that table/column tracing, you may have to do the procedural part of PL/SQL too, and that's also pretty big.

- 93,541
- 22
- 172
- 341