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
ODP
PPTX
Autoscaling Flink with Reactive Mode
PDF
Kafka Streams: What it is, and how to use it?
PDF
Universal metrics with Apache Beam
PDF
Embracing Observability in CI/CD with OpenTelemetry
ODP
ANTLR4 and its testing
PPTX
Apache Kafka Best Practices
An Introduction to ANTLR
Autoscaling Flink with Reactive Mode
Kafka Streams: What it is, and how to use it?
Universal metrics with Apache Beam
Embracing Observability in CI/CD with OpenTelemetry
ANTLR4 and its testing
Apache Kafka Best Practices

What's hot (20)

PPTX
RocksDB detail
PPTX
In memory databases presentation
PPTX
ELK Stack
PDF
Exploring the power of OpenTelemetry on Kubernetes
PDF
How We Optimize Spark SQL Jobs With parallel and sync IO
PPTX
Elastic - ELK, Logstash & Kibana
PPTX
OSS NA 2019 - Demo Booth deck overview of Egeria
PPTX
An Introduction to Elastic Search.
PDF
HBaseCon 2015: Taming GC Pauses for Large Java Heap in HBase
PDF
Log aggregation: using Elasticsearch, Fluentd/Fluentbit and Kibana (EFK)
PPTX
OpenTelemetry For Developers
PDF
Grafana Loki: like Prometheus, but for Logs
PPTX
Integrating Splunk into your Spring Applications
PPTX
Practical learnings from running thousands of Flink jobs
PPTX
Elastic Stack Introduction
PDF
Security and Multi-Tenancy with Apache Pulsar in Yahoo! (Verizon Media) - Pul...
PPTX
Apache Kafka at LinkedIn
PDF
Loki - like prometheus, but for logs
ODP
Deep Dive Into Elasticsearch
PPTX
Akka Actor presentation
RocksDB detail
In memory databases presentation
ELK Stack
Exploring the power of OpenTelemetry on Kubernetes
How We Optimize Spark SQL Jobs With parallel and sync IO
Elastic - ELK, Logstash & Kibana
OSS NA 2019 - Demo Booth deck overview of Egeria
An Introduction to Elastic Search.
HBaseCon 2015: Taming GC Pauses for Large Java Heap in HBase
Log aggregation: using Elasticsearch, Fluentd/Fluentbit and Kibana (EFK)
OpenTelemetry For Developers
Grafana Loki: like Prometheus, but for Logs
Integrating Splunk into your Spring Applications
Practical learnings from running thousands of Flink jobs
Elastic Stack Introduction
Security and Multi-Tenancy with Apache Pulsar in Yahoo! (Verizon Media) - Pul...
Apache Kafka at LinkedIn
Loki - like prometheus, but for logs
Deep Dive Into Elasticsearch
Akka Actor presentation
Ad

Viewers also liked (8)

KEY
Antlr Conference Drools & Hibernate
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
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
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
CloudStack 4.21: First Look Webinar slides
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
STKI Israel Market Study 2025 version august
PPTX
Configure Apache Mutual Authentication
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Architecture types and enterprise applications.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PPTX
Modernising the Digital Integration Hub
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Comparative analysis of machine learning models for fake news detection in so...
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
Statistics on Ai - sourced from AIPRM.pdf
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
CloudStack 4.21: First Look Webinar slides
Benefits of Physical activity for teenagers.pptx
STKI Israel Market Study 2025 version august
Configure Apache Mutual Authentication
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
NewMind AI Weekly Chronicles – August ’25 Week III
Architecture types and enterprise applications.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Enhancing plagiarism detection using data pre-processing and machine learning...
Modernising the Digital Integration Hub
Developing a website for English-speaking practice to English as a foreign la...
Comparative analysis of machine learning models for fake news detection in so...
sbt 2.0: go big (Scala Days 2025 edition)
Consumable AI The What, Why & How for Small Teams.pdf
Statistics on Ai - sourced from AIPRM.pdf
OpenACC and Open Hackathons Monthly Highlights July 2025
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
Training Program for knowledge in solar cell and solar industry
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...

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)