Atitit. implementation of the interpreter of the java parser SQL language 1. parsing the essence of SQL: Implementing a 4gldsl programming language compiler 12. parse the main SQL process, lexical analysis, and then perform syntax analysis and semantic analysis to build the SQL AST13. lexical analyzer 24. syntax analyzer-antl25.eclipse plug-in, antlstudio36.
Atitit. implementation of the interpreter of the java parser SQL language 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.
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. Introduction to the SQL parser-OQL12. Javacc/AST implemented by zql, JSqlParser, and General SQL parser.11. anlr
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