Atitit. java parses the implementation of the SQL parser interpreter, atitit. javasql

Source: Internet
Author: User
Tags general sql parser lexer

Atitit. java parses the implementation of the SQL parser interpreter, atitit. javasql

Atitit. java parses the implementation of the SQL parser Interpreter



1. parsing the essence of SQL: Implementing a 4gl dsl programming language compiler 1

2. parse the main SQL process, lexical analysis, and then perform syntax analysis and semantic analysis to build the SQL AST 1

3. Lexical analyzer 2

4. syntax analyzer-anlr 2

5. Eclipse plug-in, anlr Studio 3

6. A javacc-based parser JSqlParser0.7 (yr2011), 3

7. Example code ----- parse the name and type of the SQL table column 3

8.} SQL History 4

9. parse select statement 4

10. zql, JSqlParser, General SQL parser. 5

11. anlr-implemented SQL parser-OQL 5

12. Introduction to Javacc/AST 5

13. SQLJEP http://sqljep.sourceforge.net/5

14. SQL generation SqlBuilder, Querydsl, hb 6

15. Russian summary

16. Refer to 6

 

1. parsing the essence of SQL: Implementing a 4gl dsl programming language compiler

SQL follows eleven 4gl dsl, and the... SQL parser basically follows eleven compilers.

 

2. parse the main SQL process, lexical analysis, and then perform syntax analysis and semantic analysis to build the SQL AST

First, perform lexical analysis, and then perform syntax analysis and semantic analysis.

Lexical analysis, and syntax analysis >>>.

Lexical analysis performs word segmentation on the input statement to parse the meaning of each token. The essence of word segmentation is the matching process of regular expressions. The popular word segmentation tool should be lex, which implements word segmentation through simple rule making. Lex is generally used in combination with yacc. For more information about lex and yacc, see Yacc and Lex Quick Start-IBM. For more information, see LEX and YACC.

However, Mysql does not use lex for lexical analysis, but syntax analysis uses yacc, while yacc requires the lexical analysis function yylex,

However, anlr is more simplified...

 

Author: old wow's paw Attilax iron, EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://blog.csdn.net/attilax

 

3. Lexical analyzer

The MySQL lexical analyzer is created manually.

The syntax analyzer entry function is MYSQLparse, And the lexical analyzer entry function is MYSQLlex.

2. the lexical analysis checks whether the token is a keyword.

The most direct method is to get a large keyword array and perform a half-fold search.

1.1 lexical analyzer (Lexer)

The Lexical analyzer is also called the parser, Lexical analyser, and Tokenizer. A programming language generally consists of keywords and strictly defined Syntax structures. The final purpose of compilation is to translate High-level instructions of the programming language into instructions that can be executed by physical machines or virtual machines. This analyzer analyzes and quantifies the meaningless hidden streams and translates them into discrete character groups (that is, tokens), including keywords, identifiers, and symbols (symbols) and operators are used by the syntax analyzer.

, Lexer does not care about the syntax meaning of the generated single Token and its relationship with the context

 

Anlr combines the two. It allows us to define the lexical rules for recognizing the Token stream and the lexical analysis rules for interpreting the Token stream. Then, anlr automatically generates the corresponding lexical/syntax analyzer based on the syntax file provided by the user.

4. syntax analyzer-anlr

Because I don't want to deal with a large number of integer tables generated by YACC/LEX based on the following push mechanism, I chose another open-source LL (K) syntax/Lexical analyzer-anlr.

In the past, YACC/LEX was too academic, while the anlr Based on LL (k) was still inefficient.

 

Lexer does not care about the syntax meaning of a single Token generated and its relationship with the context. This is the work of Parser. The syntax analyzer organizes the received Tokens and converts them to the sequence allowed by the target language syntax definition.

Both Lexer and Parser are identifiers. Lexer is a character sequence identifier and Parser is a Token sequence identifier. They are similar in nature, but different in terms of division of labor.

Anlr combines the two. It allows us to define the lexical rules for recognizing the Token stream and the lexical analysis rules for interpreting the Token stream. Then, anlr automatically generates the corresponding lexical/syntax analyzer based on the syntax file provided by the user. Users can use them to compile the input text and convert it into other forms (such as AST-Abstract Syntax Tree, Abstract Syntax Tree ). Build SQL AST

 

5. Eclipse plug-in, anlr Studio

 

For better use of anlr, You can also download the Eclipse plug-in to help you complete your work. Anlr Studio

6. A javacc-based parser JSqlParser0.7 (yr2011 ),

It can convert SQL statements into Java objects. Because JsqlParser uses JavaCC for syntax analysis, JavaCC supports JJTree... if so, a small tool SQLParser is written to present the generated object in the form of a tree ^

 

 

Problems and Solutions of JSqlParser
JSqlParser is a parser for SQL statements, including common SQL statements, insert, update, select, and delete, but its compatible syntax is limited, such as parentheses, or some complex structures. Handling of escape characters

 

 

7. Example code ----- parse the name and type of the SQL table column

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 (10, 20) c nm g, mycol2 mypar1 mypar2 (23,323, 3) asdf ('23', '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 (); // obtain the total number of fields.

For (Object object: columnDefinitions ){

ColumnDefinition col = (ColumnDefinition) object;

 

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

Col. getColDataType (). getDataType (), tabName };

C.exe cute (oa );

 

}

8.} SQL History 9. parsing select statements

 

Statement stat = new CCJSqlParserManager (). parse (new StringReader (
"Select * from a where name = 'cui ever '"));
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 ("failed ====> ");
}

System. out. println (StringUtils. join (data [I], ",");
}

 

10. zql, JSqlParser, and General SQL parser. 11. Introduction to the SQL parser implemented by anlr-OQL12. Javacc/AST

JavaCC is a code generator that outputs a lexical analyzer and parser according to the input language definitions. The code output by JavaCC is legal and can be compiled in Java. the Parser and lexical analyzer are a lengthy and complex component. to manually compile such a program, you must carefully consider the interaction of the conditions. In general, you can use javacc to analyze some strings, it is still relatively convenient, and AST is now widely used.

 

13. SQLJEP Co., http://sqljep.sourceforge.net/

 

SQLJEP is a Java class library used to parse and simulate SQL statements. Supports almost all Oracle and MaxDB functions. SQLJEP uses JavaCC for lexical analysis.

 

14. SQL generation SqlBuilder, 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 in a Java program. You only need to use the SqlBuilder method to generate the corresponding SQL database query statement, for example, the following SQL statement:

 

15. Russia's summary: JSqlParser0.7 is still under attack. 16. Refer

Java-based SQL statement parsing-cuizhu forest-bokyue.htm

SQL syntax interpreter jsqlparser-serv-iteyetechnical website .htm

Hibernate source code analysis-qinghuo's notes-note-taking-Private School Online-only excellent video course service .htm

Open-source syntax analyzer-anlr-xue Di's column-blog channel-CSDN.NET.htm

 


High score help: Design and Implementation of Java Custom Script Language Interpreter

If you have the basis of the compilation principle, you should look for a book on the Compilation Principle to see how to do it. This parsing process is not too difficult.

If you haven't learned how to compile, you can't read it. You can't grasp it in a short time. You can refer to this idea:
Create a replacement table:
{"If": "if", "and": "&", ":" {"," if ":"}"}, there may be many other things.
Then replace the program
After replacement:
C = A + B
If (C> 0 & A> 5 ){
D = 100
If (C> 5 & B> 10 ){
D = 200
}
}
Then, perform some syntactic completion operations and convert them to scripting languages such as javascript. In this way, you can execute

For the variable judgment, you can traverse the variable and find the variable on the left of all values assignment operations, that is, the variable to be assigned, and the others are variable.

What command is a java interpreter?

The Java interpreter can directly explain and execute Java byte code. The specific command line format is as follows:
C: \> java options className
ArgumentsclassName must include information about all software packages. Not only the class name itself, but also the class name expected by the Java interpreter (not the file name of the Java Byte Code ), all classes running in the interpreter environment must include the main member functions required by the interpreter for the first call to pass the variables included in the command. Public static void main (string args []) {......}
All the options of the following Java interpreter. -Cs-checksource: This option allows the interpreter to recompile the updated class of the Java source file-recompile the changed class. -Classpath path this option overrides the CLASSPATH environment variable to tell Java where to find the class library. If they are separated by colons, they may contain multiple directories. -Mx x: This option sets the maximum value of the memory allocation pool. The specified pool must be greater than 1,000 bytes.
In addition, "K" and "M" can be attached to a number to specify whether it is a kilobytes or megabytes. The default value is 16 MB. -Ms x this option sets the minimum value of the memory allocation pool. The specified silence pool must be greater than 1,000 bytes.
In addition, "K" and "M" can be used to specify the number in kilobytes or megabytes. The default value is 1 MB. -Noasyncgc: This option disables asynchronous useless Unit collection. Useless Unit collection is activated only when it is called in a program or memory overflow.
-Ss x this option sets the maximum value of the C thread stack to x, and x must be greater than 1 kb. The setting method is the same as-ms. -Oss x this option sets the maximum Java stack value to x.
-V,-verbose this option tells Java to output information to the standard device whenever the class is called. -Verify this option tells Java to use verification on all code. -Verifyremote indicates that Java only verifies the classes loaded by the class loader. -Noverify this option informs Java that verification is not performed. -Verbosegc this option tells Java to display a message when useless unit collectors release the memory. -T this option is available in the Java-g interpreter and the execution is printed one by one. -Debug: This option allows the Java debugger to be connected to the Java interpreter session. When it runs, Java will display a password for starting this debugging session
-D propName = newVal This option allows the user to change the attribute value at runtime

Related Article

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.