Here you can find ISO SQL 2003 grammar created by Douglas Godfrey. If you try to compile java code generated by ANTLRv3.3 on the basis of this grammar, you get a lot of errors described in this stackoverflow question.
In this repository you can find fixed grammars (to the extent described below) and manually modified java code of the parser. The former allows to generate syntactically correct Java code from the grammar. The latter allows to compile it taking into account Java bytecode’s restrictions (code size). It also features demo console app, which prints a parse tree for given SQL file.
First, I started to fix obvious errors of the grammar, which led to the compilation errors in generated java code. These were the same error that the author of the question has faced. Eventually I have fixed all Java syntax errors but faced another one which it impossible to fix directly because it originates from limitation of JVM, thecompilation error: code too large
. Reading ANTLR mailing list there was a hint to extract some static members of the huge classes into separate interfaces and “implement” them to have a sort of multiple inheritance. With trial and error I ended up with 6 interfaces ”imlemented” by parser in sql2003Parser.java
.
But still there are 2 problems:
- Wrong start rule. Douglas Godfrey wrote grammar that starts with "sql2003Parser" rule. Unfortunately if you call parser by this start rule, it won’t parse correctly even simplest
select a from b
. So I call parser byquery_specification
rule to parseSELECT
clause only. - Some other errors in grammar. I didn’t dig too deep in the grammar but
query_specification
fails to parse some random complex SQLs.
sql2003Lexer.g
andsql2003Parser.g
: fixed grammarsjava/sql2003Lexer.java
: lexer Java code generated by antlr-3.3java/sql2003Lexer.tokens
andsql2003Parser.tokens
: files generated by antlr-3.3java/ANTLRDemo.java
: demo console app, which prints a parse tree for given SQL filejava/Part1.java
...java/Part6.java
: 6 interfaces with extracted static membersjava/Test.sql
: sample SQLjava/sql2003Parser.java
: modified Java code of parser.
$ javac sql2003Lexer.java Part1.java Part2.java Part3.java Part4.java Part5.java Part6.java sql2003Parser.java ANTLRDemo.java
$ java ANTLRDemo Test.sql