Using ANTLR on real example - convert "string combined" queries into parameterized queries

Embed Size (px)

DESCRIPTION

1. Hello ANTLR: ANother Tool for Language Recognition 2. Where we can use ANTLR? 3. Why just not use regular expression language? 4. Tools under ANTLR umbrella 5. ANTLR basic syntax 6. ANTLR on real example

Citation preview

  • 1. Using ANTLR on realexample convert string combined queries intoparameterized queries

2. Simon Wiki says:ANTLR (pronounced Antler), or ANother Tool for LanguageRecognition, is a parser generator that uses LL(*) parsing.ANTLR takes as input a grammar that specifies a language andgenerates as output source code for a recognizer for thatlanguage. A language is specified using a context-free grammarwhich is expressed using Extended BackusNaur Form (EBNF).ANTLR allows generating lexers, parsers, tree parsers, andcombined lexer-parsers. Parsers can automatically generateabstract syntax trees which can be further processed with treeparsers. ANTLR provides a single consistent notation for specifyinglexers, parsers, and tree parsers. This is in contrast with otherparser/lexer generators and adds greatly to the tools ease of use. 3. Used at least in following products:Drools, JBoss rule engine (DRL DSL)Hibernate, Java ORM (HQL DSL)NHibernate, .NET ORM (HQL DSL)Groovy, language for JVMJython, language for JVM 4. Where we need ANTLR?Parsing a text stream of formal dataParsing a text stream of incomplete formal dataComplex parsingParsing with good error handlingWriting Domain-Specific LanguageYou have enough time and some data to parse... 5. Why just not use regularexpression language?In most cases you should go with RegExSO: RegEx is a text search tool. If all you need to do is pullstrings out of strings then its often the hammer of choice.SO: ANTLR is a parser generator. If you need error messagesand parse actions or any of the complicated things thatcome with a interpreter/compiler then its a good option.SO: ANTLR has perfect support for "error-messages": theyshow line/column numbers and what was wrong. RegExdoesnt have this support.ANTLR is a something (a-lot-of-things) on top of regularexpression language. 6. ANTLR parsing workflow 7. Tools under ANTLR umbrella ANTLR3 Code Generation Targets: Java, JavaScript (in sync with development) C, C++, C#, Objective C, Ruby (almost in sync) Python, ActionScript (current with 3.1 instead of 3.4) 8. Tools under ANTLR umbrella ANTLR Grammars:Java, C, C++, ECMAScript, ANTLR, C#,PHP, Verilog, x86 Assembler, ISO SQL2003, PL/SQL, Clojure, XPath, Pascal,GraphViz Dot, Fortran, Python, CSS,Objective C, Lua, Ruby, Eiffel, ECMACIL (.NET), Classic ASP, CORBA IDL 9. Tools under ANTLR umbrellaEditors, IDEs, etc:ANTLRWorks, GUI IDE. http://antlr.org/works/Eclipse, NetBeans, JetBrains IDEA, Visual Studiointegration.VIM syntax highlighter. https://github.com/rollxx/vim-antlrANTLR-Mode for Emacs. http://antlr-mode.sourceforge.net/ 10. ANTLRWorks. Editor window 11. ANTLRWorks. Interpreter window 12. Ambigious path visualization 13. ANTLRWorks. Interactive debugger 14. Eclipse. ANTLR integration 15. JetBrains IDEA. ANTRL integration 16. Sample syntax. CSV grammar 17. Real example. Test cases 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. Syntax treestrsql = "SELECT * FROM TABLE_NAME WHEREFIRST_FIELD = " & DOTTED.PARAM_VAR & "AND SECOND_FIELD = " &DOTTED.FUNC_CALL(DOTTED.FUNC_ARG) 19. Grammar:1. Options, tokens 20. Grammar:2. Lexer/parser members 21. Grammar:3. Top-level elements 22. Grammar:4. End 23. Questions are Welcome! 31337