Using ANTLR on real example - convert "string combined" queries into parameterized queries
The document discusses the ANTLR tool, a parser generator that transforms grammars into source code for language recognition, highlighting its utility in parsing complex data and generating structured output such as abstract syntax trees. It contrasts ANTLR's capabilities with regular expressions, emphasizing ANTLR's advantages in error handling and detailed error messaging. Additionally, it lists various programming languages supported by ANTLR and provides examples of query syntax to demonstrate its practical applications.
Introduction to ANTLR as a parser generator, its role, types of outputs, and applications in various products. Examples of usage contexts include parsing formal and complex data.
Comparative analysis between Regular Expressions and ANTLR, highlighting when to use each tool, specifically focusing on error handling and functionality.
Overview of ANTLR parsing workflow and the various tools available under the ANTLR umbrella, including code generation targets and supported grammars.
Features of ANTLRWorks, including editor functionalities, interactive debugging, and how it facilitates the development process.
Integration of ANTLR with popular IDEs like Eclipse and JetBrains IDEA to enhance development capabilities.
Demonstrates real-life scenarios where ANTLR is applied, showcasing various SQL query patterns and their corresponding grammar elements.
Invitation for questions and open discussion regarding the presentation on ANTLR.
Using ANTLR on real example - convert "string combined" queries into parameterized queries
1.
Using ANTLR onreal example convert “string combined” queries into parameterized queries
2.
Simon Wiki says: ANTLR (pronounced Antler), or ANother Tool for Language Recognition, is a parser generator that uses LL(*) parsing. ANTLR takes as input a grammar that specifies a language and generates as output source code for a recognizer for that language. A language is specified using a context-free grammar which is expressed using Extended Backus–Naur Form (EBNF). ANTLR allows generating lexers, parsers, tree parsers, and combined lexer-parsers. Parsers can automatically generate abstract syntax trees which can be further processed with tree parsers. ANTLR provides a single consistent notation for specifying lexers, parsers, and tree parsers. This is in contrast with other parser/lexer generators and adds greatly to the tool's ease of use.
3.
Used at leastin following products: Drools, JBoss rule engine (DRL DSL) Hibernate, Java ORM (HQL DSL) NHibernate, .NET ORM (HQL DSL) Groovy, language for JVM Jython, language for JVM
4.
Where we needANTLR? Parsing a text stream of formal data Parsing a text stream of incomplete formal data Complex parsing Parsing with good error handling Writing Domain-Specific Language You have enough time and some data to parse...
5.
Why just notuse regular expression language? In most cases you should go with RegEx SO: “RegEx is a text search tool. If all you need to do is pull strings out of strings then it's often the hammer of choice.” SO: “ANTLR is a parser generator. If you need error messages and parse actions or any of the complicated things that come with a interpreter/compiler then it's a good option.” SO: “ANTLR has perfect support for "error-messages": they show line/column numbers and what was wrong. RegEx doesn't have this support.” ANTLR is a something (a-lot-of-things) on top of regular expression language.
Real example. Testcases • Query without any parameters • Query with concat and variable • Query with dotted and escaped table names and single quote in sql • Query with function call and func args concat • Query with function call with several func args • Query with nested function call with several func args • Query with concat and two variables • Insert query with four params • Query with dotted param and function name and funciton arg • Endline symbol will be dropped from query • Single line comment will be dropped from query • Strip single quote only if it next to parameter • Query with like keyword (FAILED) • Refactor multiline query (FAILED)
18.
Real example. Syntaxtree strsql = "SELECT * FROM TABLE_NAME WHERE FIRST_FIELD = " & DOTTED.PARAM_VAR & " AND SECOND_FIELD = " & DOTTED.FUNC_CALL(DOTTED.FUNC_ARG)