Giter VIP home page Giter VIP logo

iso-sql-2003-antlr3's Introduction

Attempt to fix grammar for a StackOverflow question

Overview

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.

What is fixed and what is not

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 by query_specification rule to parse SELECT 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.

Repository overview

  • sql2003Lexer.g and sql2003Parser.g: fixed grammars
  • java/sql2003Lexer.java: lexer Java code generated by antlr-3.3
  • java/sql2003Lexer.tokens and sql2003Parser.tokens: files generated by antlr-3.3
  • java/ANTLRDemo.java: demo console app, which prints a parse tree for given SQL file
  • java/Part1.java ... java/Part6.java: 6 interfaces with extracted static members
  • java/Test.sql: sample SQL
  • java/sql2003Parser.java: modified Java code of parser.

How to run it

$ javac sql2003Lexer.java Part1.java Part2.java Part3.java Part4.java Part5.java Part6.java sql2003Parser.java  ANTLRDemo.java
$ java ANTLRDemo Test.sql

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.