Implementation of Atitit.java Parsing SQL language parser interpreter

Source: Internet
Author: User
Tags closure lexer manual writing object object

Implementation of Atitit.java parsing SQL Language parser interpreter



1. Parsing The Nature of SQL : Implementing a compiler for a 4GL DSL programming language 1

2. Parsing the basic process of SQL , lexical analysis, and then parsing, semantic analysis, constructing the AST of SQL 1

3. Lexical Analyzer 2

4. Parser --ANTLR 2

5. Eclipse Plug- in, ANTLR Studio 3

6. A parser based on the JAVACC implementation Jsqlparser0.7 (yr2011), 3

7 . Sample code ----- Parse The name of the SQL table column and type 3

8. History of }sql 4

9. Parse Select statement 4

ZQL,jsqlparser,GeneralSQL parser. 5

ANTLR Implementation of SQL parser -OQL 5

Javacc/ast Simple Introduction 5

Sqljep HTTP://SQLJEP.SOURCEFORGE.NET/5

SQL Generation Sqlbuilder, QUERYDSL, HB 6

15. Russia's summary : also gather fire JSqlParser0.7 Walk the Orchid . 6

16. References 6

1. ParsingSQLthe Essence:Implement a4GL DSLcompiler for programming languages

SQL Walk 11 x 4GL DSL,.. SQL parser basically walk 11 compiler implementations

2. ParsingSQLthe basic process of,Lexical analysis, followed by grammatical analysis, semantic analysis, constructionSQLof theAST

First of all, lexical analysis, and then the syntax analysis, semantic analysis

Lexical analysis , and grammar analysis >>>

Lexical analysis will be entered into the statement of Word segmentation (token), to parse out the meaning of each token. The essence of participle is the matching process of the normal form, the more popular word breaker should be lex, through a simple rule-making, to achieve word segmentation. Lex is generally used in conjunction with YACC. For basic knowledge of Lex and YACC, please refer to YACC and Lex high speed primer-IBM. If you want to study in depth, you can look atLex and Yacc.

However, MySQL does not use Lex to achieve lexical analysis, but the syntax analysis is used YACC, and YACC need lexical analysis function Yylex,

Just ANTLR a lot of other simplifications ...

Author:: Old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

3. Lexical analyzer

The lexical parser for MySQL is hand-crafted.

The parser's entry function is Mysqlparse, and the lexical parser's entry function is Mysqllex.

2. The lexical analysis will check if token is keyword.

The most straightforward approach is to get a large keyword array for binary lookups.

1.1 Lexical analyzer (Lexer)

Lexical analyzers are also known as Scanner,lexical Analyser and Tokenizer. Programming languages are usually made up of keyword and strictly defined grammatical structures. The goal of compiling is to translate the high-level instruction of the programming language into the instruction that the material machine or virtual machine can run. The work of this parser is to analyze and quantify the meaningless stream of characters, translating them into discrete groups (that is, tokens of one), including keyword, identifiers, symbols (symbols), and operators for use by the parser.

, Lexer does not care about the syntactic meaning of the generated single token and its relationship to the context

ANTLR the above two together, it agrees that we define lexical rules for identifying character streams and lexical analysis rules for interpreting token flows. The ANTLR will then automatically generate the corresponding lexical/parser based on the user-supplied syntax file.

4. Parser--ANTLR

And because I don't want to deal with a whole bunch of yacc/lex generated by the principle of pushing my own initiative, I've chosen an open source LL (K) syntax/lexical analyzer-ANTLR.

Before Yacc/lex appeared to be too academic school, and LL (k)-based ANTLR although in the efficiency of a slightly inadequate

Lexer does not care about the syntactic meaning of the generated single token and its relationship to the context, which is the work of parser. The parser organizes the received tokens and transforms it into a sequence that is agreed upon by the target language syntax definition.

Whether lexer or parser is a recognizer, Lexer is a character sequence recognizer and parser is the token sequence recognizer. They are essentially similar things, but only in the division of labor is different.

ANTLR the above two together, it agrees that we define lexical rules for identifying character streams and lexical analysis rules for interpreting token flows. The ANTLR will then automatically generate the corresponding lexical/parser based on the user-supplied syntax file. The user is able to compile the input text and convert it into other forms (such as ast-abstract Syntax tree, abstract syntax trees). Build an AST of SQL

5. EclipsePlugins,,ANTLR Studio

To better use ANTLR, you can also download the ANTLR Eclipse plugin to help you finish your work. ANTLR Studio

6. One based onJAVACCthe implemented parserJsqlparser0.7 (yr2011),

It can convert SQL statements to Java objects because Jsqlparser is parsed using JavaCC , and JavaCC natively supports jjtree ... If you write a gadget Sqlparser, the resulting object is presented in a tree form ^

problems existing in Jsqlparser and their solutions
Jsqlparser is a parser for SQL statements, contains some SQL statements that are used frequently, insert,update,select,delete, and so on, but with limited syntax, such as parentheses, or some complex structure. Handling of escape characters

7. Sample Code-----parsingSQLName of the table column andtype

Final String sql = Filex. Read ("C:\\pojo.sql", "GBK");

New SqlParseO7 (SQL)

This . sqlParseO7 . Parse (new Closure ()

Public void Parse (Closure c) throws jsqlparserexception {

Ccjsqlparsermanager Parsermanager = New Ccjsqlparsermanager ();

String statement =

//"CREATE TABLE Mytab (mycol A (Ten) C nm g, Mycol2 mypar1 mypar2 (23,323,3) asdf c12> (' at ', ' 123 ') dasd, "

//+ "PRIMARY KEY (mycol2, mycol)) type = myisam";

CreateTable createtable = (createtable) parsermanager

. Parse (new StringReader (this. SQL ));

List columndefinitions = Createtable.getcolumndefinitions ();

String tabname = createtable.gettable (). GetName ();

System.out.println (Columndefinitions.size ());//Get the total number of fields.

for (Object object:columndefinitions) {

ColumnDefinition col = (columndefinition) object;

Object[] oa = {col.getcolumnname (),

Col.getcoldatatype (). Getdatatype (), tabname};

C.execute (OA);

}

8. }Sql's History9. ParsingSelectStatement

Statement stat = new Ccjsqlparsermanager (). Parse (New StringReader (
"SELECT * from a WHERE name = ' Cui Forever '");
Select select = (select) stat;
Expression where = ((Plainselect) select.getselectbody ()). Getwhere ();

Whereexpressionvisitor visitor = new Whereexpressionvisitor (Rowmeta, where);

for (int i = 0; i < data.length; i++) {
Object result = Visitor.eval (Data[i]);

if (Result instanceof Boolean && (Boolean) result). Booleanvalue ()) {
System.out.print ("Through =====>");
} else {
System.out.print ("Not through =====>");
}

System.out.println (Stringutils.join (Data[i], ","));
}

Ten. Zql,Jsqlparser,General SQL parser.ANTLR.implementation ofSQLParser-OQLJavacc/ast.a simple introduction

JavaCC is a code generator that can output a lexical parser and parser based on the input language definition, JavaCC The output code is a legitimate, compiled Java code. Parsers and lexical analyzers themselves are a lengthy and complex component, and manual writing of such a program requires careful consideration of the interaction of the various conditions, in general, by JAVACC the analysis of some strings, or relatively convenient, is now widely used AST.

Sqljep http://sqljep.sourceforge.net/

Sqljep is a Java class library for parsing and simulating running SQL statements. Supports nearly all functions of Oracle and MaxDB. Sqljep uses JavaCC to do lexical analysis.

. SqlBuildSqlbuilder, QUERYDSL, HB

3.SqlBuilder http://openhms.sourceforge.net/sqlbuilder/

Sqlbuilder is a Java class library that tries to help you avoid the pain of writing SQL queries directly within a Java program. You just need to use the Sqlbuilder method, which will help you generate the appropriate SQL database query statements, such as the following SQL statement:

15. Russia's summary:and the fire .JSqlParser0.7Walk the Blue ..16. References

Java implementation of SQL statement parsing-Bamboo forest-Blog Park. htm

SQL syntax Interpreter Jsqlparser-serv-iteye technical site. htm

Hibernate source Analysis-Green Fire notes-notes-private online-just do the boutique video course service. htm

Open Source Parser--ANTLR-Shide's Column-Blog channel-CSDN.NET.htm

Implementation of Atitit.java Parsing SQL language parser interpreter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.