Skip to content

An implementation of an ANTLR4 TSQL grammar at the 2008R2 spec

License

Notifications You must be signed in to change notification settings

jimidle/tsqlparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TSQL as of 2008R2

Introduction

This grammar was originally an ANTLR v3 grammar, with at most LL(3) token lookahead, and is now converted to ANTLR v4.

The original grammar used the ANTLR3 AST building techniques and as such, some of the grammar rules are a little more verbose and expanded out than they strictly need to be. However, as any serious use of this grammar will likely involve some form of AST construction, it makes sense to leave it as is.

The expression rule, which is the basis of much of the otherwise overly verbose SQL language, has been adapted to use the ANTLR4 left-recursive expression rule pattern, which is easier to deal with than the expanded precedence orient rules that v3 required. The predicate pattern has also been adapted to v4.

Why?

There a number of SQL grammars published in the ANTLR contributed grammars repository, but they all suffer from being cut-down copies of the normative specs, without regard to the actual language and parsing efficiency.

This was a commercial implementation of TSQL and as such, I consider it high quality. It does not suffer from the problems of massive specification ambiguity and in many cases can parse TSQL in SLL mode. The SQL mistake of making ';' generally optional, makes otherwise trivially parsed statements such as GO more ambiguous than they need to be, but that is a relatively minor issue.

Compatibility

This grammar is entirely complete with TSQL 2008R2, but since that time, TSQL has been expanded with many constructs that would need to be added to catch up with the latest spec. Contributions are welcome, however, they will need to be written in the same style as the existing grammar, avoiding ambiguity and not just copying the normative spec/BMF.

LICENSE

The MIT license pretty much allows you to do anything with this grammar other than claim it as your own. If you use it, please let me know, I would be interested to know how it is being used. The only requirement is attribution.