SQL parser (as understood by SQLite)

SQL parser generated by jlemon

License

License

Categories

Categories

SQLite Data Databases
GroupId

GroupId

com.github.gwenn
ArtifactId

ArtifactId

sqlite-parser
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

SQL parser (as understood by SQLite)
SQL parser generated by jlemon
Project URL

Project URL

https://github.com/gwenn/jlemon
Source Code Management

Source Code Management

https://github.com/gwenn/jlemon

Download sqlite-parser

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.gwenn/sqlite-parser/ -->
<dependency>
    <groupId>com.github.gwenn</groupId>
    <artifactId>sqlite-parser</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.gwenn/sqlite-parser/
implementation 'com.github.gwenn:sqlite-parser:0.1.0'
// https://jarcasting.com/artifacts/com.github.gwenn/sqlite-parser/
implementation ("com.github.gwenn:sqlite-parser:0.1.0")
'com.github.gwenn:sqlite-parser:jar:0.1.0'
<dependency org="com.github.gwenn" name="sqlite-parser" rev="0.1.0">
  <artifact name="sqlite-parser" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.gwenn', module='sqlite-parser', version='0.1.0')
)
libraryDependencies += "com.github.gwenn" % "sqlite-parser" % "0.1.0"
[com.github.gwenn/sqlite-parser "0.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25

test (2)

Group / Artifact Type Version
junit : junit jar 4.11
org.slf4j : slf4j-log4j12 jar 1.7.25

Project Modules

There are no modules declared in this project.

jlemon

A fork of the LEMON parser generator that generates Java code and the associated SQL parser.

Files lemon.c, lempar.c are extracted from SQLite v3.18.0.

Maven Central Javadocs

Usage

> # compile
> cc -g -O2 -o jlemon lemon.c
> # run
> jlemon <filename>.y
> # preprocess generated code
> cpp -P <filename>.j > yyParser.java

For example:

cc -g -O2 -o jlemon lemon.c
./jlemon src/test/java/simple/parser.y
cpp -P src/test/java/simple/parser.j > src/test/java/simple/yyParser.java
mvn test

Or on windows platform:

cl /O2 /Fejlemon.exe lemon.c
jlemon.exe src\test\java\simple\parser.y
cl /EP /C src\test\java\simple\parser.j > src\test\java\simple\yyParser.java

Generated Parser API

public class yyParser {
  // %extra_argument declaration

  public yyParser(
    ParseARG_PDECL               /* Optional %extra_argument parameter */
  ) {
    // constructor
  }
  public void ParseFinalize() {  /* or can be renamed to %nameFinalize */
    // optional (clean the stack)
  }
  public void Parse(             /* or can be renamed to %name */
    int yymajor,                 /* The major token code number */
    ParseTOKENTYPE yyminor       /* The value for the token */
  ){
    // To be called by lexer
  }
}

Hack

As there is no union in Java, yy%d fields of YYMINORTYPE have been replaced by yy%d() getters and yy%d(%type value) setters. But when translating code (see translate_code), the logic used to make the difference between a read access to a yy%d field and a write access is fallible.

Maybe try a code manipulator (janino) ? Or javolution Union ?

SQL Parser

SQLite lexer and SQLite parser have been ported from C to Java. The parser generates an AST.

The parser is/will be used to fix DatabaseMetaData implementation.

  • java.sql.DatabaseMetaData.getColumns
  • java.sql.DatabaseMetaData.getPrimaryKeys
  • java.sql.DatabaseMetaData.getBestRowIdentifier
  • java.sql.DatabaseMetaData.getCrossReference
  • java.sql.DatabaseMetaData.getImportedKeys
  • java.sql.DatabaseMetaData.getExportedKeys
  • java.sql.DatabaseMetaData.getIndexInfo

Lexer/Parser

  • Keep track of position (line, column).
  • Streamable (stop at the end of statement).
  • Resumable (restart after the end of statement).

Test

SQL lexer and parser have been tested with the following scripts:

Versions

Version
0.1.0