SlideShare ist ein Scribd-Unternehmen logo
1 von 23
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Low level java programming
Low level java programmingLow level java programming
Low level java programming
 
Combining logs, metrics, and traces for unified observability
Combining logs, metrics, and traces for unified observabilityCombining logs, metrics, and traces for unified observability
Combining logs, metrics, and traces for unified observability
 
Apache Flink and what it is used for
Apache Flink and what it is used forApache Flink and what it is used for
Apache Flink and what it is used for
 
Kafka 101
Kafka 101Kafka 101
Kafka 101
 
Deploy Prometheus - Grafana and EFK stack on Kubic k8s Clusters
Deploy Prometheus - Grafana and EFK stack on Kubic k8s ClustersDeploy Prometheus - Grafana and EFK stack on Kubic k8s Clusters
Deploy Prometheus - Grafana and EFK stack on Kubic k8s Clusters
 
Parquet - Data I/O - Philadelphia 2013
Parquet - Data I/O - Philadelphia 2013Parquet - Data I/O - Philadelphia 2013
Parquet - Data I/O - Philadelphia 2013
 
Understanding Memory Management In Spark For Fun And Profit
Understanding Memory Management In Spark For Fun And ProfitUnderstanding Memory Management In Spark For Fun And Profit
Understanding Memory Management In Spark For Fun And Profit
 
Spark Summit East 2015 Advanced Devops Student Slides
Spark Summit East 2015 Advanced Devops Student SlidesSpark Summit East 2015 Advanced Devops Student Slides
Spark Summit East 2015 Advanced Devops Student Slides
 
Performance Tuning RocksDB for Kafka Streams’ State Stores
Performance Tuning RocksDB for Kafka Streams’ State StoresPerformance Tuning RocksDB for Kafka Streams’ State Stores
Performance Tuning RocksDB for Kafka Streams’ State Stores
 
Kafka presentation
Kafka presentationKafka presentation
Kafka presentation
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
 
Apache Spark Core
Apache Spark CoreApache Spark Core
Apache Spark Core
 
kafka
kafkakafka
kafka
 
Cassandra Introduction & Features
Cassandra Introduction & FeaturesCassandra Introduction & Features
Cassandra Introduction & Features
 
初探 Elastic Observability 的實踐方法
初探 Elastic Observability 的實踐方法初探 Elastic Observability 的實踐方法
初探 Elastic Observability 的實踐方法
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Presto query optimizer: pursuit of performance
Presto query optimizer: pursuit of performancePresto query optimizer: pursuit of performance
Presto query optimizer: pursuit of performance
 
Introduction to Apache Flink - Fast and reliable big data processing
Introduction to Apache Flink - Fast and reliable big data processingIntroduction to Apache Flink - Fast and reliable big data processing
Introduction to Apache Flink - Fast and reliable big data processing
 
Spark SQL
Spark SQLSpark SQL
Spark SQL
 

Andere mochten auch

Antlr Conference Drools & Hibernate
Antlr Conference   Drools & HibernateAntlr Conference   Drools & Hibernate
Antlr Conference Drools & Hibernate
Alexandre Porcelli
 
Automated antlr tree walker
Automated antlr tree walkerAutomated antlr tree walker
Automated antlr tree walker
geeksec80
 

Andere mochten auch (8)

Antlr Conference Drools & Hibernate
Antlr Conference   Drools & HibernateAntlr Conference   Drools & Hibernate
Antlr Conference Drools & Hibernate
 
Architectures n-tiers
Architectures n-tiersArchitectures n-tiers
Architectures n-tiers
 
Automated antlr tree walker
Automated antlr tree walkerAutomated antlr tree walker
Automated antlr tree walker
 
Impulsione sua carreira contribuindo para projetos open source
Impulsione sua carreira contribuindo para projetos open sourceImpulsione sua carreira contribuindo para projetos open source
Impulsione sua carreira contribuindo para projetos open source
 
20 examples on Domain-Specific Modeling Languages
20 examples on Domain-Specific Modeling Languages20 examples on Domain-Specific Modeling Languages
20 examples on Domain-Specific Modeling Languages
 
Introduction à l'approche ADM de l'OMG
Introduction à l'approche ADM de l'OMGIntroduction à l'approche ADM de l'OMG
Introduction à l'approche ADM de l'OMG
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
20100629 dsl-poitou-charentes-jug
20100629 dsl-poitou-charentes-jug20100629 dsl-poitou-charentes-jug
20100629 dsl-poitou-charentes-jug
 

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

Ähnlich wie Using ANTLR on real example - convert "string combined" queries into parameterized queries (20)

Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
 
Alexey Golub - Writing parsers in c# | 3Shape Meetup
Alexey Golub - Writing parsers in c# | 3Shape MeetupAlexey Golub - Writing parsers in c# | 3Shape Meetup
Alexey Golub - Writing parsers in c# | 3Shape Meetup
 
ANTLR - Writing Parsers the Easy Way
ANTLR - Writing Parsers the Easy WayANTLR - Writing Parsers the Easy Way
ANTLR - Writing Parsers the Easy Way
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
 
presentation_intro_to_python
presentation_intro_to_pythonpresentation_intro_to_python
presentation_intro_to_python
 
presentation_intro_to_python_1462930390_181219.ppt
presentation_intro_to_python_1462930390_181219.pptpresentation_intro_to_python_1462930390_181219.ppt
presentation_intro_to_python_1462930390_181219.ppt
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Java platform
Java platformJava platform
Java platform
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
 
Yacc
YaccYacc
Yacc
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
CD U1-5.pptx
CD U1-5.pptxCD U1-5.pptx
CD U1-5.pptx
 
Modern C++
Modern C++Modern C++
Modern C++
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 

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)