SlideShare a Scribd company logo
1 of 36
By:
Morteza Zakeri
PhD Student
Iran University of Science and Technology
Winter 2020
Part 3: Getting Started in C#
2
Agenda
• ANTLR in Visual Studio 2019 and C#
• Back to the Grammar
• Implement Your First Visitor
• Listeners vs Visitors
11 February 2020 Introduction to ANTLR – Morteza Zakeri
3
ANTLR in Visual Studio 2019 and C#
• Install AntlrVSIX
11 February 2020 Introduction to ANTLR – Morteza Zakeri
4
ANTLR in Visual Studio 2019 and C#
• Create a new project
• Use .NET 4.6.x 
11 February 2020 Introduction to ANTLR – Morteza Zakeri
5
ANTLR in Visual Studio 2019 and C#
• Download and Install JDK
• Download ANTLR jar file
• antlr.org/download/antlr-4.8-
complete.jar
• Set Environment variables
• JAVA_HOME
• Antrl4ToolPath
11 February 2020 Introduction to ANTLR – Morteza Zakeri
6
Antlr4BuildTasks
• The purpose of the package is to integrate the Antlr tool into
the building of NET programs that reference Antlr using the
Java-based Antlr tool.
• The advantage of this package is that it decouples the Java-
based Antlr tool from the package itself.
• Make sure you do not have a version skew between the Java
Antlr tool and the runtime versions.
11 February 2020 Introduction to ANTLR – Morteza Zakeri
7
Back to the Grammar
• Expr.g4
11 February 2020 Introduction to ANTLR – Morteza Zakeri
8
Build and Run Your Project
11 February 2020 Introduction to ANTLR – Morteza Zakeri
9
Implement Your First Visitor
11 February 2020 Introduction to ANTLR – Morteza Zakeri
var visitor = new MyExprVisitor<string>();
var result = visitor.Visit(tree);
10
Listener vs Visitor
• Listener methods are called automatically by the ANTLR
provided walker object, whereas visitor methods must walk
their children with explicit visit calls. Forgetting to invoke
visit() on a node’s children means those subtrees don’t get
visited.
11 February 2020 Introduction to ANTLR – Morteza Zakeri
11
Listener vs Visitor
• Listener methods can’t return a value, whereas visitor
methods can return any custom type. With listener, you will
have to use mutable variables to store values, whereas with
visitor there is no such need.
11 February 2020 Introduction to ANTLR – Morteza Zakeri
12
Listener vs Visitor
• Listener uses an explicit stack allocated on the heap,
whereas visitor uses call stack to manage tree traversals. This
might lead to StackOverflow exceptions while using visitor
on deeply nested ASTs.
• Read more:
• https://saumitra.me/blog/antlr4-visitor-vs-listener-pattern/
• https://jakubdziworski.github.io/java/2016/04/01/antlr_visitor_vs
_listener.html
11 February 2020 Introduction to ANTLR – Morteza Zakeri
13
Assignments and Projects
11 February 2020 Introduction to ANTLR – Morteza Zakeri
14
Assignments and Projects
• Assignment 0: C++ (Python) lexical and syntax analyzing
• Assignment 1: Instrumenting the C++ (Python) source codes
based on independent execution paths.
• Assignment 2: Refactoring C++ (Python) source codes based
on clean code principles (Robert C. Martin book).
• Assignment 3: Extracting class diagram (annotated directed
graph) from C++ (Python) source code.
• Assignment 5 (Optional): Identifying the design patterns in
the code.
11 February 2020 Introduction to ANTLR – Morteza Zakeri
15
Assignments and Projects
• Final Project: Put all together 
• A comprehensive tool for program analysis!
11 February 2020 Introduction to ANTLR – Morteza Zakeri
16
Assignment 0: Build Compiler Front-end
• Put your compiler knowledge in practice!
• A software engineer with compiler science.
• Download the C++ (Python) grammar from ANTLR GitHub
pages.
• https://github.com/antlr/grammars-v4
11 February 2020 Introduction to ANTLR – Morteza Zakeri
17
Assignment 1: Instrumentation
• Part 1: Simple instrumenting
• Input: C++ (Python) source code
• Output: C++ (Python) instrumented source code
11 February 2020 Introduction to ANTLR – Morteza Zakeri
18
Assignment 1: Instrumentation
• Example 1:
• Input and Output
11 February 2020 Introduction to ANTLR – Morteza Zakeri
19
Assignment 1: Instrumentation
• Another example (GCD program):
11 February 2020 Introduction to ANTLR – Morteza Zakeri
20
Assignment 1: Instrumentation
• Problem solving idea
• Implement listener for two Non-terminals
• Rewriting the Input Stream
11 February 2020 Introduction to ANTLR – Morteza Zakeri
public override void EnterStatement([NotNull]CPP14Parser.StatementContext context)
{
// put your code here
}
public override void EnterFunctionbody([NotNull] CPP14Parser.FunctionbodyContext
context){
// put your code here
}
21
Assignment 1: Instrumentation
• Part 2: Deal with some challenges
• Block with Single Statements
11 February 2020 Introduction to ANTLR – Morteza Zakeri
22
Assignment 1: Instrumentation
• Problem solving idea
• Implement listener for two Non-terminals:
11 February 2020 Introduction to ANTLR – Morteza Zakeri
23
Assignment 1: Instrumentation
• Part 3: Deal with some challenges
• Loops make some problems, e.g.:
gcd.exe < 6 4
<p(1)p(2)p(3)p(2)p(3)p(2)p(2)end>
< p(1)p(2)p(3)end >
• Or
 gcd.exe < 24 16
<p(1)p(2)p(3)p(2)p(3)p(2)p(2)p(3)p(2)p(2)p(2)p(2)p(3)p(2)
p(2)p(2)p(2)p(2)p(2)p(2)p(2)end>
11 February 2020 Introduction to ANTLR – Morteza Zakeri
24
Assignment 1: Instrumentation
• Tips for a solution (function level):
• Reduce each sequence to independent
execution path
• Independent execution paths can be
computed using CFG
• Use Antlr to extract CGF and Independent
execution paths:
i. <p(1)p(2)p(3)end>
ii. <p(1)p(2)end>
iii. <p(1)end>
11 February 2020 Introduction to ANTLR – Morteza Zakeri
25
Assignment 1: Instrumentation
• Tips for a solution (program level):
• We cared about functions, i.e., unit testing 
• Testing at function level
• What about program? 
• Testing at program level (integration testing)
• In the level of program we need call flow graph
• Use Antlr to extract call flow graph
11 February 2020 Introduction to ANTLR – Morteza Zakeri
26
Assignment 2: Clean Code
• Part 1: Find the clean code violence in names, functions,
comments, and formatting, based on the Clean Code book
by Robert C. Martin.
• Read chapters 1 – 5
• Using ANTLR
• Report the result to the programmer
11 February 2020 Introduction to ANTLR – Morteza Zakeri
Robert C. Martin
27
Assignment 2: Clean Code
• Part 2: Refactor the founded clean code violence
automatically.
• Again using ANTLR
• Using NLTK for naming
• https://www.nltk.org/
11 February 2020 Introduction to ANTLR – Morteza Zakeri
28
Assignment 3: Class Diagram Extraction
• We will talk about it in the next sessions.
11 February 2020 Introduction to ANTLR – Morteza Zakeri
29
Project
• Final Project: Put all assignment together
• Policies
• Student with odd digit in last of their ID number (e.g. 9513231117):
• Prepare your tools for Python3
• Other students (e.g. 9513231118):
• Prepare your tools for CPP14
• Deadlines:
• Assignment 1 and 2 deadlines: 20 Farvardin 1399.
• Assignment 3 deadline will be determined later!
11 February 2020 Introduction to ANTLR – Morteza Zakeri
30
Commercialize Your Tool
11 February 2020 Introduction to ANTLR – Morteza Zakeri
31
References
1. AntlrVSIX
• https://github.com/kaby76/AntlrVSIX/blob/master/doc/readme.md
2. Getting Started With ANTLR in C#
1. https://dzone.com/articles/getting-started-with-antlr-in-c
3. The Definitive ANTLR 4 Reference
• Terence Parr, The Pragmatic Programmers, LLC; 2012.
4. ANTLR 4 Official Website:
• http://www.antlr.org/
11 February 2020 Introduction to ANTLR – Morteza Zakeri
o Do you have any question?
• m - z a ke r i @ l i v e . c o m
11 February 2020 Introduction to ANTLR – Morteza Zakeri
33
Appendix
• Extension Methods in C#
• $ in C# Strings
34
Extension Methods in C#
• Extension methods enable you to "add" methods to existing
types
• without creating a new derived type, recompiling, or otherwise
modifying the original type.
• special kind of static method, but they are called as if they were
instance methods on the extended type.
• The most common extension methods are the LINQ standard
query operators that add query functionality to the existing
System.Collections.IEnumerable and
System.Collections.Generic.IEnumerable<T> types.
11 February 2020 Introduction to ANTLR – Morteza Zakeri
35
Extension Methods in C#
11 February 2020 Introduction to ANTLR – Morteza Zakeri
36
$ in C# Strings
• $ is short-hand for String.Format() and is used with string
interpolations, which is a new feature of C# 6.
11 February 2020 Introduction to ANTLR – Morteza Zakeri

More Related Content

Similar to Antlr part3 getting_started_in_c_sharp

IPv6 Security Talk mit Joe Klein
IPv6 Security Talk mit Joe KleinIPv6 Security Talk mit Joe Klein
IPv6 Security Talk mit Joe KleinDigicomp Academy AG
 
Machine Learning-based Tools to Support Library Update
Machine Learning-based Tools to Support Library UpdateMachine Learning-based Tools to Support Library Update
Machine Learning-based Tools to Support Library UpdateOleksandr Zaitsev
 
Introduction to Open Telemetry as Observability Library
Introduction to Open  Telemetry as Observability LibraryIntroduction to Open  Telemetry as Observability Library
Introduction to Open Telemetry as Observability LibraryTonny Adhi Sabastian
 
ISI_Report_(Repaired) (4) (1)
ISI_Report_(Repaired) (4) (1)ISI_Report_(Repaired) (4) (1)
ISI_Report_(Repaired) (4) (1)Indranil Roy
 
Keeping a codebase fresh for over a decade
Keeping a codebase fresh for over a decadeKeeping a codebase fresh for over a decade
Keeping a codebase fresh for over a decadeChristian Keuerleber
 
Master-Master Replication and Scaling of an Application Between Each of the I...
Master-Master Replication and Scaling of an Application Between Each of the I...Master-Master Replication and Scaling of an Application Between Each of the I...
Master-Master Replication and Scaling of an Application Between Each of the I...vsoshnikov
 
Wireless Sensor Network for AgriTech Applications
Wireless Sensor Network for AgriTech Applications Wireless Sensor Network for AgriTech Applications
Wireless Sensor Network for AgriTech Applications IoTForum | TiE Bangalore
 
UNIT I INTRODUCTION TO INTERNET OF THINGS
UNIT I INTRODUCTION TO INTERNET OF THINGSUNIT I INTRODUCTION TO INTERNET OF THINGS
UNIT I INTRODUCTION TO INTERNET OF THINGSbinuvijay1
 
OpenChain Webinar #5: Software Heritage
OpenChain Webinar #5: Software HeritageOpenChain Webinar #5: Software Heritage
OpenChain Webinar #5: Software HeritageShane Coughlan
 
ROS-Industrial Kuka LBR-iiwa community meeting
ROS-Industrial Kuka LBR-iiwa community meetingROS-Industrial Kuka LBR-iiwa community meeting
ROS-Industrial Kuka LBR-iiwa community meetingClay Flannigan
 
Effective Android Development. UA Mobile 2016.
Effective Android Development. UA Mobile 2016.Effective Android Development. UA Mobile 2016.
Effective Android Development. UA Mobile 2016.UA Mobile
 
The Onward Journey: Porting Twisted to Python 3
The Onward Journey: Porting Twisted to Python 3The Onward Journey: Porting Twisted to Python 3
The Onward Journey: Porting Twisted to Python 3Craig Rodrigues
 
Black Hat USA 2016 Survey Report (FFRI Monthly Research 2016.8)
Black Hat USA 2016  Survey Report (FFRI Monthly Research 2016.8)Black Hat USA 2016  Survey Report (FFRI Monthly Research 2016.8)
Black Hat USA 2016 Survey Report (FFRI Monthly Research 2016.8)FFRI, Inc.
 
FIWARE Global Summit - Connecting Sensors to FIWARE with IDAS: An Overview
FIWARE Global Summit - Connecting Sensors to FIWARE with IDAS: An OverviewFIWARE Global Summit - Connecting Sensors to FIWARE with IDAS: An Overview
FIWARE Global Summit - Connecting Sensors to FIWARE with IDAS: An OverviewFIWARE
 
Capstone Project Report 2016 final
Capstone Project Report 2016 finalCapstone Project Report 2016 final
Capstone Project Report 2016 finalBenjamin Golson
 
Lightweight APIs in mRuby (Михаил Бортник)
Lightweight APIs in mRuby (Михаил Бортник)Lightweight APIs in mRuby (Михаил Бортник)
Lightweight APIs in mRuby (Михаил Бортник)Fwdays
 
IOT UNIT 1 INTRODUCTION TO INTERNET OF THINGS
IOT UNIT 1 INTRODUCTION TO INTERNET OF THINGSIOT UNIT 1 INTRODUCTION TO INTERNET OF THINGS
IOT UNIT 1 INTRODUCTION TO INTERNET OF THINGSbinuvijay1
 
Socio-technical evolution and migration in the Ruby ecosystem
Socio-technical evolution and migration in the Ruby ecosystemSocio-technical evolution and migration in the Ruby ecosystem
Socio-technical evolution and migration in the Ruby ecosystemTom Mens
 
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream ProjectsITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream ProjectsITCamp
 

Similar to Antlr part3 getting_started_in_c_sharp (20)

IPv6 Security Talk mit Joe Klein
IPv6 Security Talk mit Joe KleinIPv6 Security Talk mit Joe Klein
IPv6 Security Talk mit Joe Klein
 
miniproject
miniprojectminiproject
miniproject
 
Machine Learning-based Tools to Support Library Update
Machine Learning-based Tools to Support Library UpdateMachine Learning-based Tools to Support Library Update
Machine Learning-based Tools to Support Library Update
 
Introduction to Open Telemetry as Observability Library
Introduction to Open  Telemetry as Observability LibraryIntroduction to Open  Telemetry as Observability Library
Introduction to Open Telemetry as Observability Library
 
ISI_Report_(Repaired) (4) (1)
ISI_Report_(Repaired) (4) (1)ISI_Report_(Repaired) (4) (1)
ISI_Report_(Repaired) (4) (1)
 
Keeping a codebase fresh for over a decade
Keeping a codebase fresh for over a decadeKeeping a codebase fresh for over a decade
Keeping a codebase fresh for over a decade
 
Master-Master Replication and Scaling of an Application Between Each of the I...
Master-Master Replication and Scaling of an Application Between Each of the I...Master-Master Replication and Scaling of an Application Between Each of the I...
Master-Master Replication and Scaling of an Application Between Each of the I...
 
Wireless Sensor Network for AgriTech Applications
Wireless Sensor Network for AgriTech Applications Wireless Sensor Network for AgriTech Applications
Wireless Sensor Network for AgriTech Applications
 
UNIT I INTRODUCTION TO INTERNET OF THINGS
UNIT I INTRODUCTION TO INTERNET OF THINGSUNIT I INTRODUCTION TO INTERNET OF THINGS
UNIT I INTRODUCTION TO INTERNET OF THINGS
 
OpenChain Webinar #5: Software Heritage
OpenChain Webinar #5: Software HeritageOpenChain Webinar #5: Software Heritage
OpenChain Webinar #5: Software Heritage
 
ROS-Industrial Kuka LBR-iiwa community meeting
ROS-Industrial Kuka LBR-iiwa community meetingROS-Industrial Kuka LBR-iiwa community meeting
ROS-Industrial Kuka LBR-iiwa community meeting
 
Effective Android Development. UA Mobile 2016.
Effective Android Development. UA Mobile 2016.Effective Android Development. UA Mobile 2016.
Effective Android Development. UA Mobile 2016.
 
The Onward Journey: Porting Twisted to Python 3
The Onward Journey: Porting Twisted to Python 3The Onward Journey: Porting Twisted to Python 3
The Onward Journey: Porting Twisted to Python 3
 
Black Hat USA 2016 Survey Report (FFRI Monthly Research 2016.8)
Black Hat USA 2016  Survey Report (FFRI Monthly Research 2016.8)Black Hat USA 2016  Survey Report (FFRI Monthly Research 2016.8)
Black Hat USA 2016 Survey Report (FFRI Monthly Research 2016.8)
 
FIWARE Global Summit - Connecting Sensors to FIWARE with IDAS: An Overview
FIWARE Global Summit - Connecting Sensors to FIWARE with IDAS: An OverviewFIWARE Global Summit - Connecting Sensors to FIWARE with IDAS: An Overview
FIWARE Global Summit - Connecting Sensors to FIWARE with IDAS: An Overview
 
Capstone Project Report 2016 final
Capstone Project Report 2016 finalCapstone Project Report 2016 final
Capstone Project Report 2016 final
 
Lightweight APIs in mRuby (Михаил Бортник)
Lightweight APIs in mRuby (Михаил Бортник)Lightweight APIs in mRuby (Михаил Бортник)
Lightweight APIs in mRuby (Михаил Бортник)
 
IOT UNIT 1 INTRODUCTION TO INTERNET OF THINGS
IOT UNIT 1 INTRODUCTION TO INTERNET OF THINGSIOT UNIT 1 INTRODUCTION TO INTERNET OF THINGS
IOT UNIT 1 INTRODUCTION TO INTERNET OF THINGS
 
Socio-technical evolution and migration in the Ruby ecosystem
Socio-technical evolution and migration in the Ruby ecosystemSocio-technical evolution and migration in the Ruby ecosystem
Socio-technical evolution and migration in the Ruby ecosystem
 
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream ProjectsITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
 

More from Morteza Zakeri

2-requirements-modelling
2-requirements-modelling2-requirements-modelling
2-requirements-modellingMorteza Zakeri
 
1-requirements-elicitation
1-requirements-elicitation1-requirements-elicitation
1-requirements-elicitationMorteza Zakeri
 
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...Morteza Zakeri
 
Internet of Things: Middle-ware Platforms, Security, and Intrusion Detection
Internet of Things: Middle-ware Platforms, Security, and Intrusion DetectionInternet of Things: Middle-ware Platforms, Security, and Intrusion Detection
Internet of Things: Middle-ware Platforms, Security, and Intrusion DetectionMorteza Zakeri
 
Community Detection with Genetic Algorithm
Community Detection with Genetic AlgorithmCommunity Detection with Genetic Algorithm
Community Detection with Genetic AlgorithmMorteza Zakeri
 
SpotifyX Architectural Review
SpotifyX Architectural ReviewSpotifyX Architectural Review
SpotifyX Architectural ReviewMorteza Zakeri
 
An overview of anomaly detection techniques
An overview of anomaly detection techniquesAn overview of anomaly detection techniques
An overview of anomaly detection techniquesMorteza Zakeri
 
SQLite and object-relational mapping in Java
SQLite and object-relational mapping in JavaSQLite and object-relational mapping in Java
SQLite and object-relational mapping in JavaMorteza Zakeri
 
Apache Mesos: Architecture, Design and Code Review
Apache Mesos: Architecture, Design and Code ReviewApache Mesos: Architecture, Design and Code Review
Apache Mesos: Architecture, Design and Code ReviewMorteza Zakeri
 
یادگیری توالی به توالی با شبکه های عصبی
یادگیری توالی به توالی با شبکه های عصبییادگیری توالی به توالی با شبکه های عصبی
یادگیری توالی به توالی با شبکه های عصبیMorteza Zakeri
 
Sequence to sequence learning with neural networks
Sequence to sequence learning with neural networksSequence to sequence learning with neural networks
Sequence to sequence learning with neural networksMorteza Zakeri
 
Bridge Management System Using NoSQL Solutions
Bridge Management System Using NoSQL SolutionsBridge Management System Using NoSQL Solutions
Bridge Management System Using NoSQL SolutionsMorteza Zakeri
 
Extracting architectural model of software from source code
Extracting architectural model of software from source codeExtracting architectural model of software from source code
Extracting architectural model of software from source codeMorteza Zakeri
 

More from Morteza Zakeri (20)

9-roslyn-guidelines
9-roslyn-guidelines9-roslyn-guidelines
9-roslyn-guidelines
 
7-clean-code
7-clean-code7-clean-code
7-clean-code
 
8-bad-smells
8-bad-smells8-bad-smells
8-bad-smells
 
6-TDD
6-TDD6-TDD
6-TDD
 
3-use-casemodelling
3-use-casemodelling3-use-casemodelling
3-use-casemodelling
 
5-modular-design
5-modular-design5-modular-design
5-modular-design
 
4-architectural-views
4-architectural-views4-architectural-views
4-architectural-views
 
2-requirements-modelling
2-requirements-modelling2-requirements-modelling
2-requirements-modelling
 
1-requirements-elicitation
1-requirements-elicitation1-requirements-elicitation
1-requirements-elicitation
 
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
Analysis of Social Phenomena Using Machine Learning Techniques: A Mixed Resea...
 
Internet of Things: Middle-ware Platforms, Security, and Intrusion Detection
Internet of Things: Middle-ware Platforms, Security, and Intrusion DetectionInternet of Things: Middle-ware Platforms, Security, and Intrusion Detection
Internet of Things: Middle-ware Platforms, Security, and Intrusion Detection
 
Community Detection with Genetic Algorithm
Community Detection with Genetic AlgorithmCommunity Detection with Genetic Algorithm
Community Detection with Genetic Algorithm
 
SpotifyX Architectural Review
SpotifyX Architectural ReviewSpotifyX Architectural Review
SpotifyX Architectural Review
 
An overview of anomaly detection techniques
An overview of anomaly detection techniquesAn overview of anomaly detection techniques
An overview of anomaly detection techniques
 
SQLite and object-relational mapping in Java
SQLite and object-relational mapping in JavaSQLite and object-relational mapping in Java
SQLite and object-relational mapping in Java
 
Apache Mesos: Architecture, Design and Code Review
Apache Mesos: Architecture, Design and Code ReviewApache Mesos: Architecture, Design and Code Review
Apache Mesos: Architecture, Design and Code Review
 
یادگیری توالی به توالی با شبکه های عصبی
یادگیری توالی به توالی با شبکه های عصبییادگیری توالی به توالی با شبکه های عصبی
یادگیری توالی به توالی با شبکه های عصبی
 
Sequence to sequence learning with neural networks
Sequence to sequence learning with neural networksSequence to sequence learning with neural networks
Sequence to sequence learning with neural networks
 
Bridge Management System Using NoSQL Solutions
Bridge Management System Using NoSQL SolutionsBridge Management System Using NoSQL Solutions
Bridge Management System Using NoSQL Solutions
 
Extracting architectural model of software from source code
Extracting architectural model of software from source codeExtracting architectural model of software from source code
Extracting architectural model of software from source code
 

Recently uploaded

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Recently uploaded (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

Antlr part3 getting_started_in_c_sharp

  • 1. By: Morteza Zakeri PhD Student Iran University of Science and Technology Winter 2020 Part 3: Getting Started in C#
  • 2. 2 Agenda • ANTLR in Visual Studio 2019 and C# • Back to the Grammar • Implement Your First Visitor • Listeners vs Visitors 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 3. 3 ANTLR in Visual Studio 2019 and C# • Install AntlrVSIX 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 4. 4 ANTLR in Visual Studio 2019 and C# • Create a new project • Use .NET 4.6.x  11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 5. 5 ANTLR in Visual Studio 2019 and C# • Download and Install JDK • Download ANTLR jar file • antlr.org/download/antlr-4.8- complete.jar • Set Environment variables • JAVA_HOME • Antrl4ToolPath 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 6. 6 Antlr4BuildTasks • The purpose of the package is to integrate the Antlr tool into the building of NET programs that reference Antlr using the Java-based Antlr tool. • The advantage of this package is that it decouples the Java- based Antlr tool from the package itself. • Make sure you do not have a version skew between the Java Antlr tool and the runtime versions. 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 7. 7 Back to the Grammar • Expr.g4 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 8. 8 Build and Run Your Project 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 9. 9 Implement Your First Visitor 11 February 2020 Introduction to ANTLR – Morteza Zakeri var visitor = new MyExprVisitor<string>(); var result = visitor.Visit(tree);
  • 10. 10 Listener vs Visitor • Listener methods are called automatically by the ANTLR provided walker object, whereas visitor methods must walk their children with explicit visit calls. Forgetting to invoke visit() on a node’s children means those subtrees don’t get visited. 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 11. 11 Listener vs Visitor • Listener methods can’t return a value, whereas visitor methods can return any custom type. With listener, you will have to use mutable variables to store values, whereas with visitor there is no such need. 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 12. 12 Listener vs Visitor • Listener uses an explicit stack allocated on the heap, whereas visitor uses call stack to manage tree traversals. This might lead to StackOverflow exceptions while using visitor on deeply nested ASTs. • Read more: • https://saumitra.me/blog/antlr4-visitor-vs-listener-pattern/ • https://jakubdziworski.github.io/java/2016/04/01/antlr_visitor_vs _listener.html 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 13. 13 Assignments and Projects 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 14. 14 Assignments and Projects • Assignment 0: C++ (Python) lexical and syntax analyzing • Assignment 1: Instrumenting the C++ (Python) source codes based on independent execution paths. • Assignment 2: Refactoring C++ (Python) source codes based on clean code principles (Robert C. Martin book). • Assignment 3: Extracting class diagram (annotated directed graph) from C++ (Python) source code. • Assignment 5 (Optional): Identifying the design patterns in the code. 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 15. 15 Assignments and Projects • Final Project: Put all together  • A comprehensive tool for program analysis! 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 16. 16 Assignment 0: Build Compiler Front-end • Put your compiler knowledge in practice! • A software engineer with compiler science. • Download the C++ (Python) grammar from ANTLR GitHub pages. • https://github.com/antlr/grammars-v4 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 17. 17 Assignment 1: Instrumentation • Part 1: Simple instrumenting • Input: C++ (Python) source code • Output: C++ (Python) instrumented source code 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 18. 18 Assignment 1: Instrumentation • Example 1: • Input and Output 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 19. 19 Assignment 1: Instrumentation • Another example (GCD program): 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 20. 20 Assignment 1: Instrumentation • Problem solving idea • Implement listener for two Non-terminals • Rewriting the Input Stream 11 February 2020 Introduction to ANTLR – Morteza Zakeri public override void EnterStatement([NotNull]CPP14Parser.StatementContext context) { // put your code here } public override void EnterFunctionbody([NotNull] CPP14Parser.FunctionbodyContext context){ // put your code here }
  • 21. 21 Assignment 1: Instrumentation • Part 2: Deal with some challenges • Block with Single Statements 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 22. 22 Assignment 1: Instrumentation • Problem solving idea • Implement listener for two Non-terminals: 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 23. 23 Assignment 1: Instrumentation • Part 3: Deal with some challenges • Loops make some problems, e.g.: gcd.exe < 6 4 <p(1)p(2)p(3)p(2)p(3)p(2)p(2)end> < p(1)p(2)p(3)end > • Or  gcd.exe < 24 16 <p(1)p(2)p(3)p(2)p(3)p(2)p(2)p(3)p(2)p(2)p(2)p(2)p(3)p(2) p(2)p(2)p(2)p(2)p(2)p(2)p(2)end> 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 24. 24 Assignment 1: Instrumentation • Tips for a solution (function level): • Reduce each sequence to independent execution path • Independent execution paths can be computed using CFG • Use Antlr to extract CGF and Independent execution paths: i. <p(1)p(2)p(3)end> ii. <p(1)p(2)end> iii. <p(1)end> 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 25. 25 Assignment 1: Instrumentation • Tips for a solution (program level): • We cared about functions, i.e., unit testing  • Testing at function level • What about program?  • Testing at program level (integration testing) • In the level of program we need call flow graph • Use Antlr to extract call flow graph 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 26. 26 Assignment 2: Clean Code • Part 1: Find the clean code violence in names, functions, comments, and formatting, based on the Clean Code book by Robert C. Martin. • Read chapters 1 – 5 • Using ANTLR • Report the result to the programmer 11 February 2020 Introduction to ANTLR – Morteza Zakeri Robert C. Martin
  • 27. 27 Assignment 2: Clean Code • Part 2: Refactor the founded clean code violence automatically. • Again using ANTLR • Using NLTK for naming • https://www.nltk.org/ 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 28. 28 Assignment 3: Class Diagram Extraction • We will talk about it in the next sessions. 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 29. 29 Project • Final Project: Put all assignment together • Policies • Student with odd digit in last of their ID number (e.g. 9513231117): • Prepare your tools for Python3 • Other students (e.g. 9513231118): • Prepare your tools for CPP14 • Deadlines: • Assignment 1 and 2 deadlines: 20 Farvardin 1399. • Assignment 3 deadline will be determined later! 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 30. 30 Commercialize Your Tool 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 31. 31 References 1. AntlrVSIX • https://github.com/kaby76/AntlrVSIX/blob/master/doc/readme.md 2. Getting Started With ANTLR in C# 1. https://dzone.com/articles/getting-started-with-antlr-in-c 3. The Definitive ANTLR 4 Reference • Terence Parr, The Pragmatic Programmers, LLC; 2012. 4. ANTLR 4 Official Website: • http://www.antlr.org/ 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 32. o Do you have any question? • m - z a ke r i @ l i v e . c o m 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 33. 33 Appendix • Extension Methods in C# • $ in C# Strings
  • 34. 34 Extension Methods in C# • Extension methods enable you to "add" methods to existing types • without creating a new derived type, recompiling, or otherwise modifying the original type. • special kind of static method, but they are called as if they were instance methods on the extended type. • The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable<T> types. 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 35. 35 Extension Methods in C# 11 February 2020 Introduction to ANTLR – Morteza Zakeri
  • 36. 36 $ in C# Strings • $ is short-hand for String.Format() and is used with string interpolations, which is a new feature of C# 6. 11 February 2020 Introduction to ANTLR – Morteza Zakeri