SlideShare a Scribd company logo
© Zühlke 2015
Antlr4
Get the right tool for the job
Antlr4 | Alexander Pacha 24. July 2015 Slide 1
© Zühlke 2015
What is Antlr?
Another Tool for Language Recognition
Antlr4 | Alexander Pacha 24. July 2015 Slide 2
© Zühlke 2015
Basic Concepts
• Every language has syntax and semantic
• A parser is a syntax analyzer
• Two steps:
• Lexical Analysis: Grouping words into tokens
• Actual parsing: Recognize sentence structure and build parse tree
Languages and Parsers
Antlr4 | Alexander Pacha
int a = 42 + 3
24. July 2015 Slide 3
© Zühlke 2015
Antlr Features
• Parser generator from specified grammar (in Antlr Meta-language)
• Generated Parser in selected target language (e.g. Java, C#, Python)
• High performance
• High flexibility (e.g. grammar islands, rewriting input stream)
• Cool features (e.g. error-handling, visitors, listeners)
Antlr4 | Alexander Pacha
a = (42 + 3
24. July 2015 Slide 4
© Zühlke 2015
Building an application with Antlr
Grammar in Extended Backus-Naur-Format (EBNF)
grammar MyGrammar;
rule1 : «stuff»;
rule2 : «more stuff»;
Convenience operators: Optional (?), Zero-or-more (*), One-or-more (+)
Lexer-Rules APPLE: ‘apple‘;
INT: [0-9]+;
Parser-Rules
• Sequence decimal: INT ‘.‘ INT;
• Token dependence vector: ‘[‘ INT+ ‘]‘;
• Choice fruit: APPLE | ORANGE;
• Nested phrase breakfast: fruit JOGHURT;
Antlr4 | Alexander Pacha 24. July 2015 Slide 5
© Zühlke 2015
Grammar Sample
Antlr4 | Alexander Pacha
grammar LabeledExpr;
prog: stat+ ;
stat: expr NEWLINE # printExpr
| ID '=' expr NEWLINE # assign
| CLEAR NEWLINE # clearCmd
| NEWLINE # blank
;
expr: expr op=('*'|'/') expr # MulDiv
| expr op=('+'|'-') expr # AddSub
| INT # int
| ID # id
| '(' expr ')' # parens
;
MUL : '*' ;
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
PRINT: 'print';
CLEAR: 'clear' ;
ID : [a-zA-Z]+ ;
INT : [0-9]+ ;
NEWLINE:'r'? 'n' ;
WS : [ t]+ -> skip ;
24. July 2015 Slide 6
© Zühlke 2015
Generated Tree
a = 42 + 3
b = (a - 5) * 2
5 + 4
clear
b
Antlr4 | Alexander Pacha
Sample program
24. July 2015 Slide 7
© Zühlke 2015
Listener Sample
Output:
Antlr4 | Alexander Pacha
package Sample1;
public class SimpleListener extends LabeledExprBaseListener {
@Override
public void enterInt(LabeledExprParser.IntContext ctx) {
System.out.println(ctx.getText());
}
@Override
public void enterId(LabeledExprParser.IdContext ctx) {
System.out.println("ID: " + ctx.getText());
}
}
42
3
ID: a
5
2
5
4
ID: b
24. July 2015 Slide 8
© Zühlke 2015
Visitor Sample
Antlr4 | Alexander Pacha
package Sample1;
public class SimpleVisitor extends LabeledExprBaseVisitor {
@Override
public Object visitAssign(LabeledExprParser.AssignContext ctx) {
System.out.println(ctx.getText());
return null;
//return super.visitAssign(ctx);
}
@Override
public Object visitAddSub(LabeledExprParser.AddSubContext ctx) {
System.out.println(ctx.getText());
return null;
}
}
Output:
a=42+3
b=(a-5)*2
5+4
24. July 2015 Slide 9
© Zühlke 2015
Quiz
Sample data:
Goal:
Bonus 1: Allow , or ; to be used as separator
Bonus 2: Allow integer and decimal values (e.g. 33.15)
Create grammar to parse CSV-files
Antlr4 | Alexander Pacha
2,34,13
13,33,14
9,66,94
24. July 2015 Slide 10
© Zühlke 2015
Example Solution
Antlr4 | Alexander Pacha
grammar CommaSeparatedValues;
file: row+;
row: field (',' field)* NEWLINE;
field: INT;
INT: [0-9]+;
NEWLINE: 'r'? 'n';
//Bonus 1:
row: field ((','|';') field)* NEWLINE;
//Bonus 2:
field: INT | DECIMAL;
DECIMAL: INT '.' INT;
24. July 2015 Slide 11
© Zühlke 2015
PostScript Parser Demo
Antlr4 | Alexander Pacha
Lisual 2.025
Lisual 2.025
24. July 2015 Slide 12

More Related Content

Similar to Antlr4 get the right tool for the job

Python lec1
Python lec1Python lec1
Python lec1
Swarup Ghosh
 
JShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java PlatformJShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java Platform
JavaDayUA
 
Scilab: Computing Tool For Engineers
Scilab: Computing Tool For EngineersScilab: Computing Tool For Engineers
Scilab: Computing Tool For Engineers
Naren P.R.
 
F# in your pipe
F# in your pipeF# in your pipe
F# in your pipe
Phillip Trelford
 
Efficient Immutable Data Structures (Okasaki for Dummies)
Efficient Immutable Data Structures (Okasaki for Dummies)Efficient Immutable Data Structures (Okasaki for Dummies)
Efficient Immutable Data Structures (Okasaki for Dummies)
Tom Faulhaber
 
Writing Cadence Ocean scripts
Writing Cadence Ocean scriptsWriting Cadence Ocean scripts
Writing Cadence Ocean scripts
Michael Lee
 
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
InfluxData
 
OpenTelemetry 101 FTW
OpenTelemetry 101 FTWOpenTelemetry 101 FTW
OpenTelemetry 101 FTW
NGINX, Inc.
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
FSharp in the enterprise
FSharp in the enterpriseFSharp in the enterprise
FSharp in the enterprise
Phillip Trelford
 
Cape2013 scilab-workshop-19Oct13
Cape2013 scilab-workshop-19Oct13Cape2013 scilab-workshop-19Oct13
Cape2013 scilab-workshop-19Oct13
Naren P.R.
 
The Present and Future of the Web Platform
The Present and Future of the Web PlatformThe Present and Future of the Web Platform
The Present and Future of the Web Platform
C4Media
 
WISS 2015 - Machine Learning lecture by Ludovic Samper
WISS 2015 - Machine Learning lecture by Ludovic Samper WISS 2015 - Machine Learning lecture by Ludovic Samper
WISS 2015 - Machine Learning lecture by Ludovic Samper
Antidot
 
python-2021.pdf
python-2021.pdfpython-2021.pdf
python-2021.pdf
IsaacKingDiran1
 
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Find it. Fix it. Real-World SQL Tuning Cases with Karen MortonFind it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Embarcadero Technologies
 
How OpenStack Makes Python Better (and vice-versa)
How OpenStack Makes Python Better (and vice-versa)How OpenStack Makes Python Better (and vice-versa)
How OpenStack Makes Python Better (and vice-versa)
doughellmann
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer
 
Unit Tests? It is Very Simple and Easy!
Unit Tests? It is Very Simple and Easy!Unit Tests? It is Very Simple and Easy!
Unit Tests? It is Very Simple and Easy!
Return on Intelligence
 
EUGM 2014 - Serge P. Parel (Exquiron): Farewell, PipelinePilot : Migrating th...
EUGM 2014 - Serge P. Parel (Exquiron): Farewell, PipelinePilot : Migrating th...EUGM 2014 - Serge P. Parel (Exquiron): Farewell, PipelinePilot : Migrating th...
EUGM 2014 - Serge P. Parel (Exquiron): Farewell, PipelinePilot : Migrating th...
ChemAxon
 

Similar to Antlr4 get the right tool for the job (20)

Python lec1
Python lec1Python lec1
Python lec1
 
JShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java PlatformJShell: An Interactive Shell for the Java Platform
JShell: An Interactive Shell for the Java Platform
 
Scilab: Computing Tool For Engineers
Scilab: Computing Tool For EngineersScilab: Computing Tool For Engineers
Scilab: Computing Tool For Engineers
 
F# in your pipe
F# in your pipeF# in your pipe
F# in your pipe
 
Efficient Immutable Data Structures (Okasaki for Dummies)
Efficient Immutable Data Structures (Okasaki for Dummies)Efficient Immutable Data Structures (Okasaki for Dummies)
Efficient Immutable Data Structures (Okasaki for Dummies)
 
Writing Cadence Ocean scripts
Writing Cadence Ocean scriptsWriting Cadence Ocean scripts
Writing Cadence Ocean scripts
 
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
 
OpenTelemetry 101 FTW
OpenTelemetry 101 FTWOpenTelemetry 101 FTW
OpenTelemetry 101 FTW
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
FSharp in the enterprise
FSharp in the enterpriseFSharp in the enterprise
FSharp in the enterprise
 
Cape2013 scilab-workshop-19Oct13
Cape2013 scilab-workshop-19Oct13Cape2013 scilab-workshop-19Oct13
Cape2013 scilab-workshop-19Oct13
 
The Present and Future of the Web Platform
The Present and Future of the Web PlatformThe Present and Future of the Web Platform
The Present and Future of the Web Platform
 
WISS 2015 - Machine Learning lecture by Ludovic Samper
WISS 2015 - Machine Learning lecture by Ludovic Samper WISS 2015 - Machine Learning lecture by Ludovic Samper
WISS 2015 - Machine Learning lecture by Ludovic Samper
 
python-2021.pdf
python-2021.pdfpython-2021.pdf
python-2021.pdf
 
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Find it. Fix it. Real-World SQL Tuning Cases with Karen MortonFind it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
 
How OpenStack Makes Python Better (and vice-versa)
How OpenStack Makes Python Better (and vice-versa)How OpenStack Makes Python Better (and vice-versa)
How OpenStack Makes Python Better (and vice-versa)
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
 
Unit Tests? It is Very Simple and Easy!
Unit Tests? It is Very Simple and Easy!Unit Tests? It is Very Simple and Easy!
Unit Tests? It is Very Simple and Easy!
 
EUGM 2014 - Serge P. Parel (Exquiron): Farewell, PipelinePilot : Migrating th...
EUGM 2014 - Serge P. Parel (Exquiron): Farewell, PipelinePilot : Migrating th...EUGM 2014 - Serge P. Parel (Exquiron): Farewell, PipelinePilot : Migrating th...
EUGM 2014 - Serge P. Parel (Exquiron): Farewell, PipelinePilot : Migrating th...
 

More from Alexander Pacha

1, 2, 3 build - continuous integration für mobile apps
1, 2, 3   build - continuous integration für mobile apps1, 2, 3   build - continuous integration für mobile apps
1, 2, 3 build - continuous integration für mobile apps
Alexander Pacha
 
1, 2, 3 build - continuous integration for mobile apps
1, 2, 3   build - continuous integration for mobile apps1, 2, 3   build - continuous integration for mobile apps
1, 2, 3 build - continuous integration for mobile apps
Alexander Pacha
 
I like to move it, move it - What your Android device really feels
I like to move it, move it - What your Android device really feelsI like to move it, move it - What your Android device really feels
I like to move it, move it - What your Android device really feels
Alexander Pacha
 
Android Testing
Android Testing Android Testing
Android Testing
Alexander Pacha
 
Lightningtalk - Android UI-Testautomatisierung leicht gemacht mit Robotium
Lightningtalk - Android UI-Testautomatisierung leicht gemacht mit RobotiumLightningtalk - Android UI-Testautomatisierung leicht gemacht mit Robotium
Lightningtalk - Android UI-Testautomatisierung leicht gemacht mit Robotium
Alexander Pacha
 
C# - A Programmer's Dream Come True
C# - A Programmer's Dream Come TrueC# - A Programmer's Dream Come True
C# - A Programmer's Dream Come True
Alexander Pacha
 
Recherche präsentation
Recherche präsentationRecherche präsentation
Recherche präsentation
Alexander Pacha
 

More from Alexander Pacha (7)

1, 2, 3 build - continuous integration für mobile apps
1, 2, 3   build - continuous integration für mobile apps1, 2, 3   build - continuous integration für mobile apps
1, 2, 3 build - continuous integration für mobile apps
 
1, 2, 3 build - continuous integration for mobile apps
1, 2, 3   build - continuous integration for mobile apps1, 2, 3   build - continuous integration for mobile apps
1, 2, 3 build - continuous integration for mobile apps
 
I like to move it, move it - What your Android device really feels
I like to move it, move it - What your Android device really feelsI like to move it, move it - What your Android device really feels
I like to move it, move it - What your Android device really feels
 
Android Testing
Android Testing Android Testing
Android Testing
 
Lightningtalk - Android UI-Testautomatisierung leicht gemacht mit Robotium
Lightningtalk - Android UI-Testautomatisierung leicht gemacht mit RobotiumLightningtalk - Android UI-Testautomatisierung leicht gemacht mit Robotium
Lightningtalk - Android UI-Testautomatisierung leicht gemacht mit Robotium
 
C# - A Programmer's Dream Come True
C# - A Programmer's Dream Come TrueC# - A Programmer's Dream Come True
C# - A Programmer's Dream Come True
 
Recherche präsentation
Recherche präsentationRecherche präsentation
Recherche präsentation
 

Recently uploaded

Lab session on Robot Control using teach pendant.pptx
Lab session on Robot Control using teach pendant.pptxLab session on Robot Control using teach pendant.pptx
Lab session on Robot Control using teach pendant.pptx
KPavanKumarReddy4
 
If we're running two pumps, why aren't we getting twice as much flow? v.17
If we're running two pumps, why aren't we getting twice as much flow? v.17If we're running two pumps, why aren't we getting twice as much flow? v.17
If we're running two pumps, why aren't we getting twice as much flow? v.17
Brian Gongol
 
AFCAT STATIC Genral knowledge important CAPSULE.pdf
AFCAT STATIC Genral knowledge important CAPSULE.pdfAFCAT STATIC Genral knowledge important CAPSULE.pdf
AFCAT STATIC Genral knowledge important CAPSULE.pdf
vibhapatil140
 
Girls call Service Ludhiana 000XX00000 Provide Best And Top Girl Service And ...
Girls call Service Ludhiana 000XX00000 Provide Best And Top Girl Service And ...Girls call Service Ludhiana 000XX00000 Provide Best And Top Girl Service And ...
Girls call Service Ludhiana 000XX00000 Provide Best And Top Girl Service And ...
singharadhana4778
 
High Profile Girls Call Delhi 9711199171 Provide Best And Top Girl Service An...
High Profile Girls Call Delhi 9711199171 Provide Best And Top Girl Service An...High Profile Girls Call Delhi 9711199171 Provide Best And Top Girl Service An...
High Profile Girls Call Delhi 9711199171 Provide Best And Top Girl Service An...
janvikumar4133
 
Fix Production Bugs Quickly - The Power of Structured Logging in Ruby on Rail...
Fix Production Bugs Quickly - The Power of Structured Logging in Ruby on Rail...Fix Production Bugs Quickly - The Power of Structured Logging in Ruby on Rail...
Fix Production Bugs Quickly - The Power of Structured Logging in Ruby on Rail...
John Gallagher
 
LOCAL-BUDGET-CIRCULAR-NO-158-DATED-JULY-11-2024.pdf
LOCAL-BUDGET-CIRCULAR-NO-158-DATED-JULY-11-2024.pdfLOCAL-BUDGET-CIRCULAR-NO-158-DATED-JULY-11-2024.pdf
LOCAL-BUDGET-CIRCULAR-NO-158-DATED-JULY-11-2024.pdf
jellyjm
 
The Control of Relative Humidity & Moisture Content in The Air
The Control of Relative Humidity & Moisture Content in The AirThe Control of Relative Humidity & Moisture Content in The Air
The Control of Relative Humidity & Moisture Content in The Air
Ashraf Ismail
 
AI chapter1 introduction to artificial intelligence
AI chapter1 introduction to artificial intelligenceAI chapter1 introduction to artificial intelligence
AI chapter1 introduction to artificial intelligence
GeethaAL
 
ECONOMIC FEASIBILITY AND ENVIRONMENTAL IMPLICATIONS OF PERMEABLE PAVEMENT IN ...
ECONOMIC FEASIBILITY AND ENVIRONMENTAL IMPLICATIONS OF PERMEABLE PAVEMENT IN ...ECONOMIC FEASIBILITY AND ENVIRONMENTAL IMPLICATIONS OF PERMEABLE PAVEMENT IN ...
ECONOMIC FEASIBILITY AND ENVIRONMENTAL IMPLICATIONS OF PERMEABLE PAVEMENT IN ...
Fady M. A Hassouna
 
Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 in City
sunnuchadda
 
Sustainable construction is the use of renewable and recyclable materials in ...
Sustainable construction is the use of renewable and recyclable materials in ...Sustainable construction is the use of renewable and recyclable materials in ...
Sustainable construction is the use of renewable and recyclable materials in ...
RohitGhulanavar2
 
System Analysis and Design in a changing world 5th edition
System Analysis and Design in a changing world 5th editionSystem Analysis and Design in a changing world 5th edition
System Analysis and Design in a changing world 5th edition
mnassar75g
 
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptxPresentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Er. Kushal Ghimire
 
DPWH - DEPARTMENT OF PUBLIC WORKS AND HIGHWAYS
DPWH - DEPARTMENT OF PUBLIC WORKS AND HIGHWAYSDPWH - DEPARTMENT OF PUBLIC WORKS AND HIGHWAYS
DPWH - DEPARTMENT OF PUBLIC WORKS AND HIGHWAYS
RyanMacayan
 
Updated Limitations of Simplified Methods for Evaluating the Potential for Li...
Updated Limitations of Simplified Methods for Evaluating the Potential for Li...Updated Limitations of Simplified Methods for Evaluating the Potential for Li...
Updated Limitations of Simplified Methods for Evaluating the Potential for Li...
Robert Pyke
 
UNIT 1 - INTRODUCTION ON DISASTER MANAGEMENT.ppt
UNIT 1 - INTRODUCTION ON DISASTER MANAGEMENT.pptUNIT 1 - INTRODUCTION ON DISASTER MANAGEMENT.ppt
UNIT 1 - INTRODUCTION ON DISASTER MANAGEMENT.ppt
shanmugamram247
 
How to Formulate A Good Research Question
How to Formulate A  Good Research QuestionHow to Formulate A  Good Research Question
How to Formulate A Good Research Question
rkpv2002
 
"Operational and Technical Overview of Electric Locomotives at the Kanpur Ele...
"Operational and Technical Overview of Electric Locomotives at the Kanpur Ele..."Operational and Technical Overview of Electric Locomotives at the Kanpur Ele...
"Operational and Technical Overview of Electric Locomotives at the Kanpur Ele...
nanduchaihan9
 
Chapter 1 Introduction to Software Engineering and Process Models.pdf
Chapter 1 Introduction to Software Engineering and Process Models.pdfChapter 1 Introduction to Software Engineering and Process Models.pdf
Chapter 1 Introduction to Software Engineering and Process Models.pdf
MeghaGupta952452
 

Recently uploaded (20)

Lab session on Robot Control using teach pendant.pptx
Lab session on Robot Control using teach pendant.pptxLab session on Robot Control using teach pendant.pptx
Lab session on Robot Control using teach pendant.pptx
 
If we're running two pumps, why aren't we getting twice as much flow? v.17
If we're running two pumps, why aren't we getting twice as much flow? v.17If we're running two pumps, why aren't we getting twice as much flow? v.17
If we're running two pumps, why aren't we getting twice as much flow? v.17
 
AFCAT STATIC Genral knowledge important CAPSULE.pdf
AFCAT STATIC Genral knowledge important CAPSULE.pdfAFCAT STATIC Genral knowledge important CAPSULE.pdf
AFCAT STATIC Genral knowledge important CAPSULE.pdf
 
Girls call Service Ludhiana 000XX00000 Provide Best And Top Girl Service And ...
Girls call Service Ludhiana 000XX00000 Provide Best And Top Girl Service And ...Girls call Service Ludhiana 000XX00000 Provide Best And Top Girl Service And ...
Girls call Service Ludhiana 000XX00000 Provide Best And Top Girl Service And ...
 
High Profile Girls Call Delhi 9711199171 Provide Best And Top Girl Service An...
High Profile Girls Call Delhi 9711199171 Provide Best And Top Girl Service An...High Profile Girls Call Delhi 9711199171 Provide Best And Top Girl Service An...
High Profile Girls Call Delhi 9711199171 Provide Best And Top Girl Service An...
 
Fix Production Bugs Quickly - The Power of Structured Logging in Ruby on Rail...
Fix Production Bugs Quickly - The Power of Structured Logging in Ruby on Rail...Fix Production Bugs Quickly - The Power of Structured Logging in Ruby on Rail...
Fix Production Bugs Quickly - The Power of Structured Logging in Ruby on Rail...
 
LOCAL-BUDGET-CIRCULAR-NO-158-DATED-JULY-11-2024.pdf
LOCAL-BUDGET-CIRCULAR-NO-158-DATED-JULY-11-2024.pdfLOCAL-BUDGET-CIRCULAR-NO-158-DATED-JULY-11-2024.pdf
LOCAL-BUDGET-CIRCULAR-NO-158-DATED-JULY-11-2024.pdf
 
The Control of Relative Humidity & Moisture Content in The Air
The Control of Relative Humidity & Moisture Content in The AirThe Control of Relative Humidity & Moisture Content in The Air
The Control of Relative Humidity & Moisture Content in The Air
 
AI chapter1 introduction to artificial intelligence
AI chapter1 introduction to artificial intelligenceAI chapter1 introduction to artificial intelligence
AI chapter1 introduction to artificial intelligence
 
ECONOMIC FEASIBILITY AND ENVIRONMENTAL IMPLICATIONS OF PERMEABLE PAVEMENT IN ...
ECONOMIC FEASIBILITY AND ENVIRONMENTAL IMPLICATIONS OF PERMEABLE PAVEMENT IN ...ECONOMIC FEASIBILITY AND ENVIRONMENTAL IMPLICATIONS OF PERMEABLE PAVEMENT IN ...
ECONOMIC FEASIBILITY AND ENVIRONMENTAL IMPLICATIONS OF PERMEABLE PAVEMENT IN ...
 
Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Chennai 000XX00000 Provide Best And Top Girl Service And No1 in City
 
Sustainable construction is the use of renewable and recyclable materials in ...
Sustainable construction is the use of renewable and recyclable materials in ...Sustainable construction is the use of renewable and recyclable materials in ...
Sustainable construction is the use of renewable and recyclable materials in ...
 
System Analysis and Design in a changing world 5th edition
System Analysis and Design in a changing world 5th editionSystem Analysis and Design in a changing world 5th edition
System Analysis and Design in a changing world 5th edition
 
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptxPresentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
 
DPWH - DEPARTMENT OF PUBLIC WORKS AND HIGHWAYS
DPWH - DEPARTMENT OF PUBLIC WORKS AND HIGHWAYSDPWH - DEPARTMENT OF PUBLIC WORKS AND HIGHWAYS
DPWH - DEPARTMENT OF PUBLIC WORKS AND HIGHWAYS
 
Updated Limitations of Simplified Methods for Evaluating the Potential for Li...
Updated Limitations of Simplified Methods for Evaluating the Potential for Li...Updated Limitations of Simplified Methods for Evaluating the Potential for Li...
Updated Limitations of Simplified Methods for Evaluating the Potential for Li...
 
UNIT 1 - INTRODUCTION ON DISASTER MANAGEMENT.ppt
UNIT 1 - INTRODUCTION ON DISASTER MANAGEMENT.pptUNIT 1 - INTRODUCTION ON DISASTER MANAGEMENT.ppt
UNIT 1 - INTRODUCTION ON DISASTER MANAGEMENT.ppt
 
How to Formulate A Good Research Question
How to Formulate A  Good Research QuestionHow to Formulate A  Good Research Question
How to Formulate A Good Research Question
 
"Operational and Technical Overview of Electric Locomotives at the Kanpur Ele...
"Operational and Technical Overview of Electric Locomotives at the Kanpur Ele..."Operational and Technical Overview of Electric Locomotives at the Kanpur Ele...
"Operational and Technical Overview of Electric Locomotives at the Kanpur Ele...
 
Chapter 1 Introduction to Software Engineering and Process Models.pdf
Chapter 1 Introduction to Software Engineering and Process Models.pdfChapter 1 Introduction to Software Engineering and Process Models.pdf
Chapter 1 Introduction to Software Engineering and Process Models.pdf
 

Antlr4 get the right tool for the job

  • 1. © Zühlke 2015 Antlr4 Get the right tool for the job Antlr4 | Alexander Pacha 24. July 2015 Slide 1
  • 2. © Zühlke 2015 What is Antlr? Another Tool for Language Recognition Antlr4 | Alexander Pacha 24. July 2015 Slide 2
  • 3. © Zühlke 2015 Basic Concepts • Every language has syntax and semantic • A parser is a syntax analyzer • Two steps: • Lexical Analysis: Grouping words into tokens • Actual parsing: Recognize sentence structure and build parse tree Languages and Parsers Antlr4 | Alexander Pacha int a = 42 + 3 24. July 2015 Slide 3
  • 4. © Zühlke 2015 Antlr Features • Parser generator from specified grammar (in Antlr Meta-language) • Generated Parser in selected target language (e.g. Java, C#, Python) • High performance • High flexibility (e.g. grammar islands, rewriting input stream) • Cool features (e.g. error-handling, visitors, listeners) Antlr4 | Alexander Pacha a = (42 + 3 24. July 2015 Slide 4
  • 5. © Zühlke 2015 Building an application with Antlr Grammar in Extended Backus-Naur-Format (EBNF) grammar MyGrammar; rule1 : «stuff»; rule2 : «more stuff»; Convenience operators: Optional (?), Zero-or-more (*), One-or-more (+) Lexer-Rules APPLE: ‘apple‘; INT: [0-9]+; Parser-Rules • Sequence decimal: INT ‘.‘ INT; • Token dependence vector: ‘[‘ INT+ ‘]‘; • Choice fruit: APPLE | ORANGE; • Nested phrase breakfast: fruit JOGHURT; Antlr4 | Alexander Pacha 24. July 2015 Slide 5
  • 6. © Zühlke 2015 Grammar Sample Antlr4 | Alexander Pacha grammar LabeledExpr; prog: stat+ ; stat: expr NEWLINE # printExpr | ID '=' expr NEWLINE # assign | CLEAR NEWLINE # clearCmd | NEWLINE # blank ; expr: expr op=('*'|'/') expr # MulDiv | expr op=('+'|'-') expr # AddSub | INT # int | ID # id | '(' expr ')' # parens ; MUL : '*' ; DIV : '/' ; ADD : '+' ; SUB : '-' ; PRINT: 'print'; CLEAR: 'clear' ; ID : [a-zA-Z]+ ; INT : [0-9]+ ; NEWLINE:'r'? 'n' ; WS : [ t]+ -> skip ; 24. July 2015 Slide 6
  • 7. © Zühlke 2015 Generated Tree a = 42 + 3 b = (a - 5) * 2 5 + 4 clear b Antlr4 | Alexander Pacha Sample program 24. July 2015 Slide 7
  • 8. © Zühlke 2015 Listener Sample Output: Antlr4 | Alexander Pacha package Sample1; public class SimpleListener extends LabeledExprBaseListener { @Override public void enterInt(LabeledExprParser.IntContext ctx) { System.out.println(ctx.getText()); } @Override public void enterId(LabeledExprParser.IdContext ctx) { System.out.println("ID: " + ctx.getText()); } } 42 3 ID: a 5 2 5 4 ID: b 24. July 2015 Slide 8
  • 9. © Zühlke 2015 Visitor Sample Antlr4 | Alexander Pacha package Sample1; public class SimpleVisitor extends LabeledExprBaseVisitor { @Override public Object visitAssign(LabeledExprParser.AssignContext ctx) { System.out.println(ctx.getText()); return null; //return super.visitAssign(ctx); } @Override public Object visitAddSub(LabeledExprParser.AddSubContext ctx) { System.out.println(ctx.getText()); return null; } } Output: a=42+3 b=(a-5)*2 5+4 24. July 2015 Slide 9
  • 10. © Zühlke 2015 Quiz Sample data: Goal: Bonus 1: Allow , or ; to be used as separator Bonus 2: Allow integer and decimal values (e.g. 33.15) Create grammar to parse CSV-files Antlr4 | Alexander Pacha 2,34,13 13,33,14 9,66,94 24. July 2015 Slide 10
  • 11. © Zühlke 2015 Example Solution Antlr4 | Alexander Pacha grammar CommaSeparatedValues; file: row+; row: field (',' field)* NEWLINE; field: INT; INT: [0-9]+; NEWLINE: 'r'? 'n'; //Bonus 1: row: field ((','|';') field)* NEWLINE; //Bonus 2: field: INT | DECIMAL; DECIMAL: INT '.' INT; 24. July 2015 Slide 11
  • 12. © Zühlke 2015 PostScript Parser Demo Antlr4 | Alexander Pacha Lisual 2.025 Lisual 2.025 24. July 2015 Slide 12

Editor's Notes

  1. BNF: Besteht aus Alternativen, Token-Referenzen und Regel-Referenzen