SlideShare a Scribd company logo
Using ANTLR on real
      example

 convert “string combined” queries into
        parameterized queries
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.
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 JVM

    Jython, language for JVM
Where we need ANTLR?

    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...
Why just not use 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.
ANTLR parsing workflow
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)
Tools under ANTLR umbrella
 ANTLR Grammars:
    Java, C, C++, ECMAScript, ANTLR, C#,
    PHP, Verilog, x86 Assembler, ISO SQL
    2003, PL/SQL, Clojure, XPath, Pascal,
    GraphViz Dot, Fortran, Python, CSS,
    Objective C, Lua, Ruby, Eiffel, ECMA
    CIL (.NET), Classic ASP, CORBA IDL
Tools under ANTLR umbrella
    Editors, IDEs, etc:
•
    ANTLRWorks, GUI IDE. http://antlr.org/works/
•
    Eclipse, NetBeans, JetBrains IDEA, Visual Studio
    integration.
•
    VIM syntax highlighter. https://github.com/rollxx/vim-
    antlr
•
    ANTLR-Mode for Emacs. http://antlr-
    mode.sourceforge.net/
ANTLRWorks. Editor window
ANTLRWorks. Interpreter window
Ambigious path visualization
ANTLRWorks. Interactive
     debugger
Eclipse. ANTLR integration
JetBrains IDEA. ANTRL integration
Sample syntax. CSV grammar
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)
Real example. Syntax tree




strsql = "SELECT * FROM TABLE_NAME WHERE
    FIRST_FIELD = " & DOTTED.PARAM_VAR & "
    AND SECOND_FIELD = " &
    DOTTED.FUNC_CALL(DOTTED.FUNC_ARG)
Grammar:1. Options, tokens
Grammar:2. Lexer/parser members
Grammar:3. Top-level elements
Grammar:4. End
Questions are Welcome!




                         31337

More Related Content

PPTX
An Introduction to ANTLR
PPTX
Apache Beam (incubating)
PDF
Universal metrics with Apache Beam
PPTX
Streaming Data Pipelines With Apache Beam
PDF
Reactive stream processing using Akka streams
PDF
Introduction to elasticsearch
PDF
Native Java with GraalVM
PPTX
Introduction to Apache ZooKeeper
An Introduction to ANTLR
Apache Beam (incubating)
Universal metrics with Apache Beam
Streaming Data Pipelines With Apache Beam
Reactive stream processing using Akka streams
Introduction to elasticsearch
Native Java with GraalVM
Introduction to Apache ZooKeeper

What's hot (20)

PPTX
Introduction to GraphQL Presentation.pptx
PDF
Introduction to Prometheus and Cortex (WOUG)
PPTX
02 terraform core concepts
PPTX
Writing and using Hamcrest Matchers
PDF
GraphQL Fundamentals
PDF
Reactive Stream Processing with Akka Streams
PDF
Elasticsearch in Netflix
PPTX
AngularJS Architecture
PPTX
Akka Actor presentation
PPTX
Elastic search overview
PPTX
Bypassing anti virus using powershell
PDF
Introduction à ElasticSearch
PDF
Quarkus Denmark 2019
PPTX
Apache Flink and what it is used for
PDF
PPTX
Apache Beam: A unified model for batch and stream processing data
PPT
Django, What is it, Why is it cool?
PPTX
PDF
Airflow for Beginners
PDF
Loki - like prometheus, but for logs
Introduction to GraphQL Presentation.pptx
Introduction to Prometheus and Cortex (WOUG)
02 terraform core concepts
Writing and using Hamcrest Matchers
GraphQL Fundamentals
Reactive Stream Processing with Akka Streams
Elasticsearch in Netflix
AngularJS Architecture
Akka Actor presentation
Elastic search overview
Bypassing anti virus using powershell
Introduction à ElasticSearch
Quarkus Denmark 2019
Apache Flink and what it is used for
Apache Beam: A unified model for batch and stream processing data
Django, What is it, Why is it cool?
Airflow for Beginners
Loki - like prometheus, but for logs
Ad

Viewers also liked (10)

KEY
Antlr Conference Drools & Hibernate
ODP
PPTX
Architectures n-tiers
PDF
Automated antlr tree walker
PDF
Impulsione sua carreira contribuindo para projetos open source
PDF
20 examples on Domain-Specific Modeling Languages
ODP
ANTLR4 and its testing
PDF
Introduction à l'approche ADM de l'OMG
PPT
Antlr V3
PDF
20100629 dsl-poitou-charentes-jug
Antlr Conference Drools & Hibernate
Architectures n-tiers
Automated antlr tree walker
Impulsione sua carreira contribuindo para projetos open source
20 examples on Domain-Specific Modeling Languages
ANTLR4 and its testing
Introduction à l'approche ADM de l'OMG
Antlr V3
20100629 dsl-poitou-charentes-jug
Ad

Similar to Using ANTLR on real example - convert "string combined" queries into parameterized queries (20)

PPTX
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
PDF
Alexey Golub - Writing parsers in c# | 3Shape Meetup
PPTX
ANTLR - Writing Parsers the Easy Way
PPTX
Compiler Construction
PPTX
1._Introduction_.pptx
PPTX
COMPILER CONSTRUCTION KU 1.pptx
PPT
presentation_intro_to_python
PPT
presentation_intro_to_python_1462930390_181219.ppt
PPT
Cpcs302 1
PPT
Lexical analyzer
DOC
Pcd question bank
PPT
Compier Design_Unit I.ppt
PPT
Compier Design_Unit I.ppt
PPTX
Java platform
PPTX
Lecture 1 introduction to language processors
PPTX
PPTX
CD U1-5.pptx
PPTX
Modern C++
PPTX
A Lecture of Compiler Design Subject.pptx
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
Alexey Golub - Writing parsers in c# | 3Shape Meetup
ANTLR - Writing Parsers the Easy Way
Compiler Construction
1._Introduction_.pptx
COMPILER CONSTRUCTION KU 1.pptx
presentation_intro_to_python
presentation_intro_to_python_1462930390_181219.ppt
Cpcs302 1
Lexical analyzer
Pcd question bank
Compier Design_Unit I.ppt
Compier Design_Unit I.ppt
Java platform
Lecture 1 introduction to language processors
CD U1-5.pptx
Modern C++
A Lecture of Compiler Design Subject.pptx

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
KodekX | Application Modernization Development
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
KodekX | Application Modernization Development
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Agricultural_Statistics_at_a_Glance_2022_0.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
MYSQL Presentation for SQL database connectivity
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Monthly Chronicles - July 2025
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation_ Review paper, used for researhc scholars
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

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

  • 1. Using ANTLR on real 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 least in 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 need ANTLR?  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 not use 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.
  • 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 SQL 2003, PL/SQL, Clojure, XPath, Pascal, GraphViz Dot, Fortran, Python, CSS, Objective C, Lua, Ruby, Eiffel, ECMA CIL (.NET), Classic ASP, CORBA IDL
  • 9. Tools under ANTLR umbrella Editors, IDEs, etc: • ANTLRWorks, GUI IDE. http://antlr.org/works/ • Eclipse, NetBeans, JetBrains IDEA, Visual Studio integration. • VIM syntax highlighter. https://github.com/rollxx/vim- antlr • ANTLR-Mode for Emacs. http://antlr- mode.sourceforge.net/
  • 15. JetBrains IDEA. ANTRL integration
  • 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 tree strsql = "SELECT * FROM TABLE_NAME WHERE FIRST_FIELD = " & DOTTED.PARAM_VAR & " AND SECOND_FIELD = " & DOTTED.FUNC_CALL(DOTTED.FUNC_ARG)