SlideShare une entreprise Scribd logo
1  sur  12
© 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

Contenu connexe

Similaire à Antlr4 get the right tool for the job

Writing Cadence Ocean scripts
Writing Cadence Ocean scriptsWriting Cadence Ocean scripts
Writing Cadence Ocean scripts
Michael Lee
 

Similaire à 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...
 

Plus de Alexander Pacha

Plus de 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
 

Dernier

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Dernier (20)

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 

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

Notes de l'éditeur

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