SlideShare a Scribd company logo
1 of 61
5/11/2021 Saeed Parsa 1
Compiler Design
Introduction to Compiler
Saeed Parsa
Room 332,
School of Computer Engineering,
Iran University of Science & Technology
parsa@iust.ac.ir
Winter 2021
Why compilers?
5/11/2021 Saeed Parsa 2
Compiler
Software
Quality
Measurement
Reverse
Engineering
Software
Security
Automatic
Distribution &
Parallelization
Software
Testing
A compiler structure
• Page 7 of the Aho’s Book
5/11/2021 Saeed Parsa 3
Compilation steps with an example
• Page 7 of the Aho’s Book
5/11/2021 Saeed Parsa 4
Example of compilation process
Consider the example statement:
position = initial + rate * 60
1. Lexical Analysis
There are 30 characters in the statement.
The characters are transformed by lexical analysis into a sequence of 7 tokens.
Token1.name = “position”;
Token2.name = “=“;
Token3.name = “initial“;
Token4.name = “+“;
Token5.name = “rate“;
Token6.name = “*“;
Token7.name = “60“;
5/11/2021 Saeed Parsa 5
Example of compilation process (Continued)
Consider the example statement:
position = initial + rate * 60
2. Syntax Analysis
Those tokens are then used by syntax analyzer to build a tree of height 4, representing the
correctness of the statement according to the grammar.
G1:
assignmentSt ::= identifier = expression
expression ::= expression + term | expression – term | term
term ::= term * factor | term / factor | factor
factor ::= identifier | number | (expression)
5/11/2021 Saeed Parsa 6
Example of compilation process (Continued)
Consider the example statement:
position = initial + rate * 60
2. Syntax Analysis
Those tokens are then used syntax
analyzer to build a tree of height 4,
representing the correctness of the
statement according to the grammar.
5/11/2021 Saeed Parsa 7
Example of compilation process (Continued)
Consider the example statement:
position = initial + rate * 60
3. Semantics Analysis
Semantic analysis may transform the tree into one of height 5, that includes a type conversion
necessary for real addition on an integer operand.
4. Intermediate code generation
Intermediate code generation uses a simple traversal algorithm to linearize the tree back into a
sequence of machine-independent three-address-code instructions.
t1 = inttoreal(60)
t2 = id3 * t1
t3 = id2 + t2
id1 = t3
5/11/2021 Saeed Parsa 8
position = initial + rate * 60
Example of compilation process (Continued)
5/11/2021 Saeed Parsa 9
t1 = inttoreal(60)
t2 = id3 * t1
t3 = id2 + t2
id1 = t3
position = initial + rate * 60
t1 = id3 * 60.0
id1 = id2 + t1
http://www.personal.kent.edu/~rmuhamma/Compilers/compnotes.html
Consider the example statement:
position = initial + rate * 60
5. Optimization
Optimization of the intermediate code allows the four instructions to be reduced to two
machine-independent instructions.
Example of compilation process (Continued)
5/11/2021 Saeed Parsa 10
Consider the example statement:
position = initial + rate * 60
7. Final code generation
Final code generation might implement these two instructions using 5 machine instructions,
in which the actual registers and addressing modes of the CPU are utilized.
t1 = inttoreal(60)
t2 = id3 * t1
t3 = id2 + t2
id1 = t3
position = initial + rate * 60
t1 = id3 * 60.0
id1 = id2 + t1
MOVF id3, R2
MULF #60.0,
R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
http://www.personal.kent.edu/~rmuhamma/Compilers/compnotes.html
Compiler construction tools
5/11/2021 Saeed Parsa 11
On-line documentation is available for the following compiler tools:
• Scanner generators for C/C++: Flex (pdf), Lex (pdf).
• Parser generators for C/C++: Bison (in HTML), Bison (pdf), Yacc (pdf).
• Available scanner generators for Java:
• JLex, a scanner generator for Java, very similar to Lex.
• JFLex, flex for Java.
http://www.personal.kent.edu/~rmuhamma/Compilers/compnotes.html
Compiler construction tools
5/11/2021 Saeed Parsa 12
• Available parser generators for Java:
• CUP, a parser generator for Java, very similar to YACC.
• BYACC/J, a different version of Berkeley YACC for Java. It is an extension of the standard
YACC (a -j flag has been added to generate Java code).
• Other compiler tools:
• JavaCC, a parser generator for Java, including scanner generator and parser generator. Input
specifications are different than those suitable for Lex/YACC. Also, unlike YACC, JavaCC
generates a top-down parser.
• ANTLR, a set of language translation tools (formerly PCCTS). Includes scanner/parser
generators for C, C++, and Java.
Compiler construction tools
5/11/2021 Saeed Parsa 13
1. Parser Generators
2. Scanner Generators
3. Syntax-directed Translation Engines
4. Automatic Code Generators
5. Data-flow Analysis Engines
6. Compiler Construction Toolkits
1. Parser Generators
5/11/2021 Saeed Parsa 14
 It produces syntax analyzers (parsers) from the input that is based on a grammatical description
of programming language or on a context-free grammar.
 It is useful as the syntax analysis phase is highly complex and consumes more manual and
compilation time.
 Example: PIC, EQM, ANTLR, YACC
2. Scanner Generators
5/11/2021 Saeed Parsa 15
 It generates lexical analyzers from the input that consists of regular expression description based
on tokens of a language.
 It generates a finite automaton to recognize the regular expression.
 Example: Lex, Flex, ANTLR
Compiler construction tools
5/11/2021 Saeed Parsa 16
3. Syntax-directed translation engines that produce collections of routines for walking a parse tree
and generating intermediate code.
4. Code-generator generators that produce a code generator from a collection of rules for
translating each operation of the intermediate language into the machine language for a target
machine.
5. Data-flow analysis engines that facilitate the gathering of information about how values are
transmitted from one part of a program to each other part. Data-flow analysis is a key part of
code optimization.
6. Compiler-construction toolkits that provide an integrated set of routines for constructing various
phases of a compiler.
Page 8 of the Aho’s Book
Compiler construction tools
5/11/2021 Saeed Parsa 17
https://link.springer.com/content/pdf/10.1007%2F3-540-53669-8_77.pdf
What is ANTLR?
5/11/2021 Saeed Parsa 18
What is ANTLR?
5/11/2021 Saeed Parsa 19
 ANTLR, Another Tool for Language Recognition, (formerly PCCTS) is a language tool that
provides a framework for constructing recognizers, compilers, and translators from
grammatical descriptions.
 ANTLR is a parser generator.
 ANTLR is open source, written in JAVA.
 ANTLR provides a “tree walker” to traverse parse tress.
 There are two tree walking mechanism provided by the ANTLR library - Listener & Visitor.
What is ANTLR?
5/11/2021 Saeed Parsa 20
 There are 3 primary differences between the Listener and Visitor libraries:
1. 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
2. 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.
3. Listener uses an explicit stack allocated on the heap, whereas visitor uses call stack to
manage tree traversals. This might lead to Stack Overflow exceptions while using visitor
on deeply nested ASTs.
What is ANTLR?
5/11/2021 Saeed Parsa 21
Antlr is a public-domain, software tool developed by Terence Parr
to assist with the development of translators and compilers.
ANTLR automatically generates the lexical analyzer and parser for
you by analyzing the grammar you provide.
Grammar
ANRLR Parser
Generator
Lexer
Parser
What is ANTLR?
5/11/2021 Saeed Parsa 22
A parse tree walker allows walking through the parse tree and perform any action
at each node.
Why to use ANTLR?
5/11/2021 Saeed Parsa 23
ANTLR is extremely popular with 5,000 downloads a month and is included on
all Linux and OS X distributions. It is widely used because it:
 Generates human-readable code that is easy to fold into other applications
 Generates powerful recursive-descent recognizers using LL(*), an extension to
LL(k) that uses arbitrary look ahead to make decisions
 Tightly integrates StringTemplate,5 a template engine specifically designed to
generate structured text such as source code
 Has a graphical grammar development environment called ANTLRWorks6 that
can debug parsers generated in any ANTLR target language
Why to use ANTLR?
5/11/2021 Saeed Parsa 24
1. Is actively supported with a good project website and a high-traffic mailing list7
• Comes with complete source under the BSD license
2. Is extremely flexible and automates or formalizes many common tasks
3. Supports multiple target languages such as Java, C#, Python, Ruby, Objective-C, C,
and C++.
See:
• http://www.stringtemplate.org
• http://www.antlr.org/works
• http://www.antlr.org:8080/pipermail/antlr-interest/
https://doc.lagout.org/programmation/Pragmatic%20Programmers/The%20Definitive%20ANTLR%20Reference.pdf
Install ANTLR
5/11/2021 Saeed Parsa 25
ANTLR is written in Java, so you must have Java installed on your machine even if you are
going to use ANTLR with, say, Python. ANTLR requires a Java version of 1.6 or higher.
Step 1: Install Java
1.1 Download and install Java JDK 13.0.2 (64-bit). Java Development Kit (64-bit).
Date released 2018. Free Download. (159.8 MB).
https://www.filehorse.com/download-java-development-kit-64/old-versions/page-2/
Step 2: Download the tool
2.1 Download antlr-4.8-complete.jar (or whatever version) from https:
https://www.antlr.org/download/
2.2 Save antlr-4.8-complete.jar to your directory for 3rd party Java libraries, say:
C:Javalib
Install ANTLR
5/11/2021 Saeed Parsa 26
Step 3: Set paths
3.1 Create three text files antlr.txt, grun.txt and class.txt and save then to c:java.lib
3.2 Rename the above files to antlr.bat, grun.bat and class.bat
3.3 Copy the following commands into the batch files:
class.bat: SET CLASSPATH=.; %CLASSPATH%
grun.bat: java org.antlr.v4.gui.TestRig %*
antlr.bat: java org.antlr.v4.Tool %*
3.4 Add antlr-4.8-complete.jar to CLASSPATH, either:
Permanently: Using Control Panel > System > Advanced system settings > Environment variables
3.5 Use the windows search, to look for “environment”.
Install ANTLR
5/11/2021 Saeed Parsa 27
3.6 Click on “Edit Environment Variables” > “Environment Variables”
3.7 The following widow will pop up on the screen.
Install ANTLR
5/11/2021 Saeed Parsa 28
3.8 Click on “New”. The following window will be displayed
Install ANTLR
5/11/2021 Saeed Parsa 29
3.9 Add Java path to Path by clicking on the “Path” option
Install ANTLR
5/11/2021 Saeed Parsa 30
3.10 The following window will be opened. Click on the push-button, labeled “New”.
Install ANTLR
5/11/2021 Saeed Parsa 31
3.11 Enter java compiler path and the Class Path entered before.
Install ANTLR
5/11/2021 Saeed Parsa 32
3.12.1 Now by doing such settings it will be possible to access the downloaded jar file.
The above image is from the javalib folder, which contains the antlr-4.8-complete.jar software
we downloaded from www.antlr.org.
Running ANTLR
5/11/2021 Saeed Parsa 33
Step 2: Add or create a grammar file (*.g4) in your project
2.1 Download the desired Grammar from the following URL and save it in the
C:javalib directory
https://github.com/antlr/grammars-v4/blob/master/cpp/CPP14.g4
2.2 Run Antlr to create lexer and parser
Create two batch files as follows:
(1) antlr4.bat: java org.antlr.v4.Tool%*
(2) grun.bat: antlr4 -listener -visitor -Dlanguage=CSharp CPP14.g4
Or run the following commands:
java org.antlr.v4.Tool -Werror -o {outputdirectory} -Dlanguage=CSharp input.g4 -visitor
java -jar antlr-4.8-complete.jar -Dlanguage=CSharp input.g4 -visitor
Running ANTLR
5/11/2021 Saeed Parsa 34
• In antlr4.bat the command java org.antlr.v4.Tool% is included. This command
actually invokes the antlr.v4 software.
• The second batch file, grun.bat, includes the command:
antlr4 -listener -visitor -Dlanguage=CSharp CPP14.g4.
• In this command:
• Using the -listener -visitor switches we have actually requested that this
command generate both these files in addition to the parser and lexer.
• Dlanguage = CSharp tells antlr to generate the files generated from CPP14.G4
should be in C#.
Running ANTLR
5/11/2021 Saeed Parsa 35
After executing this batch file, all the required files will be generated and saved
in the JAVALIB folder.
Running ANTLR
5/11/2021 Saeed Parsa 36
Alternatively:
2.2 Run Antlr to create listener and visitor
java org.antlr.v4.Tool -Werror -o {outputdirectory} -Dlanguage=CSharp input.g4 -visitor
java -jar antlr-4.5.1-complete.jar -Dlanguage=CSharp input.g4 –visitor -listener
2.3 As a result the Parser, Lexer, Visitor and Listener classes will be created
Running ANTLR
5/11/2021 Saeed Parsa 37
Install ANTLR in C#
5/11/2021 Saeed Parsa 38
Adding ANTLR plugin:
 A JRE needs to be on the executable search path (i.e. the absolute path is in
the %PATH% environment variable)
 Installation of the ANTLR4 packages is via the NuGet Package Manager
 Grammar file(s)’ compilation options need(s) to be customized manually by editing
the project’s configuration file (*.csproj)
 The latest stable version of ANTLR4 (version 4.3.0) is only supported on .NET
Framework 4.5 and below, therefore there is a need to change the target framework
from the default (if the default version is higher)
 References to the ANTLR namespaces are required
Install ANTLR in C#
5/11/2021 Saeed Parsa 39
1. First, create a Windows Form App project in Visual Studio.
2. Right click on the project name in the solution explorer and add a folder,
Antlor4.
Install ANTLR in C#
5/11/2021 Saeed Parsa 40
3. Copy the content of javalib into the Antlor4 folder.
Install ANTLR in C#
5/11/2021 Saeed Parsa 41
• Now after adding the folder to the visual
environment we need to add a package to use
Antlr in the .net environment.
• To do so, right-click on References and then in the
popup window click on the Manage NuGet
package option and then add a package called
antlr4.rantime.standard to the project.
Install ANTLR in C#
5/11/2021 Saeed Parsa 42
NuGet:
• NuGet is a package and dependency manager.
• Helps to find, install, update and remove packages.
• Focuses primarily on package and dependency management.
• Lists all available packages for download.
• Adding a NuGet package to a Visual Studio project is similar to adding a reference.
• Go to Solution Explorer,
• Right-click on the References folder,
• Click Manage NuGet Packages.
Install ANTLR in C#
5/11/2021 Saeed Parsa 43
For Visual Studio 2017:
1. Right click the top-level solution node in the Solution Explorer window and
select Manage NuGet Packages for Solution.
2. In the upper left, choose Browse and then choose nuget.org as the Package
source
3. Next to the Search box, check Include prerelease
4. In the Search box, type Antlr4 to search for the package
5. In the search results, locate and select the package called Antlr4. Verify that the
name is listed as Antlr4.
6. In the right pane, select the C# projects you want to use ANTLR4 by clicking their
checkboxes
7. Click Install under the list of projects
8. Approve changes and accept license agreements, if prompted.
Install ANTLR in C#
5/11/2021 Saeed Parsa 44
Install ANTLR in C#
5/11/2021 Saeed Parsa 45
After adding the packages to our project, we use their namespaces and use the capabilities of
these packages in our project.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows.Forms;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using static CPP14Parser;
Assignment 1
5/11/2021 Saeed Parsa 46
Subject : Install & use ANTLR
Deadline: Two weeks
Mark: 5 out of 100.
Assignment 1
5/11/2021 Saeed Parsa 47
Write a program to accept a C++ program as input and generate parse tree for the
program
1. Run ANTLR to generate a lexical analyzer (lexer) and a parser for C++ .
2. Give a C++ program to your C++ compiler to generate parse and depict the
parse tree for the program.
3. Provide a report describing the installation step and your program.
You may generate lexer and parser for other languages such as C# , Java, and
Python.
Your code could be either in Python, or C# language.
Assignment 1
5/11/2021 Saeed Parsa 48
Steps to install ANTLR:
1. Download Java JDK 13.0.2 (64-bit). Java Development Kit (64-bit).
Date released 2018. Free Download. (159.8 MB).
https://www.filehorse.com/download-java-development-kit-64/old-versions/page-2/
2. Download Antlr 4.8-complete.jar (or whatever version) from https:
https://www.antlr.org/download/
3. Create a directory for 3rd party Java libraries :
C:Javalib
4. Save antlr-4.8-complete.jar to C:Javalib.
Assignment 1
5/11/2021 Saeed Parsa 49
Steps to install ANTLR:
5. Add the followings to ’environment variables’ in windows 10.
 C:Program FilesJavajdk-13.0.2bin
 C:Javalib
 %CLASSPATH%
6. Add C:Javalibantlr-4.8-complete.jar as the variable value of the environment variable
named CLASSPATH.
7. Download the CPP grammar from the following URL address into “C:javalib” folder:
https://github.com/antlr/grammars-v4/tree/master/cpp
Similarly the Python grammar could be downloaded from:
https://github.com/antlr/grammars-v4/tree/master/python
Assignment 1
5/11/2021 Saeed Parsa 50
Steps to install ANTLR:
8. Generate parser and lexer
java -jar antlr4-4.8.jar -Dlanguage=CSharp CPP14.g4
or
java -jar antlr4-4.8.jar -Dlanguage=Phyton3 CPP14.g4
or
java -jar antlr4-4.8.jar -Dlanguage=Cpp grammar.g4
For instance suppose you choose to generate Python3 code:
java -jar ./antlr-4.8-complete.jar -Dlanguage=Python3 CPP14.g4
Assignment 1
5/11/2021 Saeed Parsa 51
Steps to install ANTLR:
 This will generate the following files, which you can then integrate in your project
CPP14Lexer.py: including the source of a class, CPP14Lexer.
CPP14Parser.py: including the source of a class, CPP14Parser.
CPP14Listener.py: including the source of a class, CPP14Listener.
9. In addition to the above three files, four other files are generated that are used by ANTLR.
All these files should be copied into the folder where you save your Python program.
Assignment 1
5/11/2021 Saeed Parsa 52
Steps to install ANTLR:
10. To access the ANLR-4 runtime library within a Python project, MyProject, in the PyChram
environment select:
File > Setting > MyProject > Project interpreter > + (add)
Search for “ANTR4” in the popped up window, select “ANTLR4 runtime Python3” option,
and click on the “install package” button.
http://ati.ttu.ee/~kjans/antlr/pycharm_antlr4_guide.pdf
Assignment 1
5/11/2021 Saeed Parsa 53
Steps to install ANTLR:
11. Write a program to generate and display Parse tree for a given C++ program.
https://www.thetopsites.net/article/50064110.shtml
https://github.com/antlr/antlr4/blob/master/runtime/Python3/src/antlr4/Parser.py
Assignment 1
5/11/2021 Saeed Parsa 54
Note: CLASSPATH is a parameter in the Java Virtual Machine or the Java compiler
that specifies the location of user-defined classes and packages. The parameter may
be set either on the command-line, or through an environment variable.
 To add ANTLR to class path,
• Click on ‘Windows search’ key and enter “environment”
• go to the following address (you may click on the following address to
get access to the related document):
• Control Panel > System > Advanced system settings > Environment variables
 Under Environment Variables, you can see two sections.
 The top section shows User variables,
 The bottom section shows System Variables.
Assignment 1
5/11/2021 Saeed Parsa 55
 In either of them, find for Variable name PATH or path.
 If it is available click on path in the system variable window and then press edit.
 Pressing the “Edit” button a new window labeled ‘Edit system variable’ will pop up.
 Press the “new” button, and add the following three items to the list”
1. The path for accessing Java: C:Program FilesJavajdk-13.0.2bin
2. The 3rd party Java libraries address: C:javalib
3. Class path: %CLASSPATH%
Assignment 1
5/11/2021 Saeed Parsa 56
Assignment 1
5/11/2021 Saeed Parsa 57
Assignment 1
5/11/2021 Saeed Parsa 58
Assignment 1
5/11/2021 Saeed Parsa 59
The place of IUST in the world
5/11/2021 Saeed Parsa 60
https://www.researchgate.net/publication/328099969_Software_Fault_Localisation_A_Systematic_Mapping_Study
5/11/2021 Saeed Parsa 61

More Related Content

What's hot

Generative Adversarial Network (GAN)
Generative Adversarial Network (GAN)Generative Adversarial Network (GAN)
Generative Adversarial Network (GAN)Prakhar Rastogi
 
Packet analysis using wireshark
Packet analysis using wiresharkPacket analysis using wireshark
Packet analysis using wiresharkBasaveswar Kureti
 
An Overview of Performance Evaluation & Simulation
An Overview of Performance Evaluation & SimulationAn Overview of Performance Evaluation & Simulation
An Overview of Performance Evaluation & Simulationdasdfadfdsfsdfasdf
 
Model Monitoring at Scale with Apache Spark and Verta
Model Monitoring at Scale with Apache Spark and VertaModel Monitoring at Scale with Apache Spark and Verta
Model Monitoring at Scale with Apache Spark and VertaDatabricks
 
Fedarated learning
Fedarated learningFedarated learning
Fedarated learningVaishakhKP1
 
Types of Machine Learning
Types of Machine LearningTypes of Machine Learning
Types of Machine LearningSamra Shahzadi
 
Introduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowIntroduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowSri Ambati
 
Machine Learning
Machine LearningMachine Learning
Machine LearningRahul Kumar
 
Object Detection with Transformers
Object Detection with TransformersObject Detection with Transformers
Object Detection with TransformersDatabricks
 
Machine Learning
Machine LearningMachine Learning
Machine LearningKumar P
 
Introduction to Deep Learning
Introduction to Deep LearningIntroduction to Deep Learning
Introduction to Deep LearningOswald Campesato
 
Introduction to Keras
Introduction to KerasIntroduction to Keras
Introduction to KerasJohn Ramey
 

What's hot (20)

3. mining frequent patterns
3. mining frequent patterns3. mining frequent patterns
3. mining frequent patterns
 
Generative Adversarial Network (GAN)
Generative Adversarial Network (GAN)Generative Adversarial Network (GAN)
Generative Adversarial Network (GAN)
 
Deep Learning
Deep Learning Deep Learning
Deep Learning
 
Textmining Introduction
Textmining IntroductionTextmining Introduction
Textmining Introduction
 
MLOps for production-level machine learning
MLOps for production-level machine learningMLOps for production-level machine learning
MLOps for production-level machine learning
 
Tor Presentation
Tor PresentationTor Presentation
Tor Presentation
 
Wireshark
WiresharkWireshark
Wireshark
 
Packet analysis using wireshark
Packet analysis using wiresharkPacket analysis using wireshark
Packet analysis using wireshark
 
An Overview of Performance Evaluation & Simulation
An Overview of Performance Evaluation & SimulationAn Overview of Performance Evaluation & Simulation
An Overview of Performance Evaluation & Simulation
 
Malware analysis
Malware analysisMalware analysis
Malware analysis
 
Model Monitoring at Scale with Apache Spark and Verta
Model Monitoring at Scale with Apache Spark and VertaModel Monitoring at Scale with Apache Spark and Verta
Model Monitoring at Scale with Apache Spark and Verta
 
Fedarated learning
Fedarated learningFedarated learning
Fedarated learning
 
Types of Machine Learning
Types of Machine LearningTypes of Machine Learning
Types of Machine Learning
 
Introduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowIntroduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlow
 
Wireshark tutorial
Wireshark tutorialWireshark tutorial
Wireshark tutorial
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Object Detection with Transformers
Object Detection with TransformersObject Detection with Transformers
Object Detection with Transformers
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Introduction to Deep Learning
Introduction to Deep LearningIntroduction to Deep Learning
Introduction to Deep Learning
 
Introduction to Keras
Introduction to KerasIntroduction to Keras
Introduction to Keras
 

Similar to 2. introduction to compiler

Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Eelco Visser
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introductionRana Ehtisham Ul Haq
 
Petapath HP Cast 12 - Programming for High Performance Accelerated Systems
Petapath HP Cast 12 - Programming for High Performance Accelerated SystemsPetapath HP Cast 12 - Programming for High Performance Accelerated Systems
Petapath HP Cast 12 - Programming for High Performance Accelerated Systemsdairsie
 
Chapter One
Chapter OneChapter One
Chapter Onebolovv
 
Code Analysis and Refactoring with CDT
Code Analysis and Refactoring with CDTCode Analysis and Refactoring with CDT
Code Analysis and Refactoring with CDTdschaefer
 
API design
API designAPI design
API designsakpece
 
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...Jorge Hidalgo
 
Scale Up Performance with Intel® Development
Scale Up Performance with Intel® DevelopmentScale Up Performance with Intel® Development
Scale Up Performance with Intel® DevelopmentIntel IT Center
 
Cockatrice: A Hardware Design Environment with Elixir
Cockatrice: A Hardware Design Environment with ElixirCockatrice: A Hardware Design Environment with Elixir
Cockatrice: A Hardware Design Environment with ElixirHideki Takase
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processorsRebaz Najeeb
 
Ghoshal_resume_LinkedIn_20160705
Ghoshal_resume_LinkedIn_20160705Ghoshal_resume_LinkedIn_20160705
Ghoshal_resume_LinkedIn_20160705Siddhartha Ghoshal
 
Software Development Automation With Scripting Languages
Software Development Automation With Scripting LanguagesSoftware Development Automation With Scripting Languages
Software Development Automation With Scripting LanguagesIonela
 
ContainerDayVietnam2016: Become a Cloud-native Developer
ContainerDayVietnam2016: Become a Cloud-native DeveloperContainerDayVietnam2016: Become a Cloud-native Developer
ContainerDayVietnam2016: Become a Cloud-native DeveloperDocker-Hanoi
 
Track A-Compilation guiding and adjusting - IBM
Track A-Compilation guiding and adjusting - IBMTrack A-Compilation guiding and adjusting - IBM
Track A-Compilation guiding and adjusting - IBMchiportal
 

Similar to 2. introduction to compiler (20)

2. introduction
2. introduction2. introduction
2. introduction
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Petapath HP Cast 12 - Programming for High Performance Accelerated Systems
Petapath HP Cast 12 - Programming for High Performance Accelerated SystemsPetapath HP Cast 12 - Programming for High Performance Accelerated Systems
Petapath HP Cast 12 - Programming for High Performance Accelerated Systems
 
Chapter One
Chapter OneChapter One
Chapter One
 
Code Analysis and Refactoring with CDT
Code Analysis and Refactoring with CDTCode Analysis and Refactoring with CDT
Code Analysis and Refactoring with CDT
 
API design
API designAPI design
API design
 
ApiDesign
ApiDesignApiDesign
ApiDesign
 
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Scale Up Performance with Intel® Development
Scale Up Performance with Intel® DevelopmentScale Up Performance with Intel® Development
Scale Up Performance with Intel® Development
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
Cockatrice: A Hardware Design Environment with Elixir
Cockatrice: A Hardware Design Environment with ElixirCockatrice: A Hardware Design Environment with Elixir
Cockatrice: A Hardware Design Environment with Elixir
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
 
Ghoshal_resume_LinkedIn_20160705
Ghoshal_resume_LinkedIn_20160705Ghoshal_resume_LinkedIn_20160705
Ghoshal_resume_LinkedIn_20160705
 
Software Development Automation With Scripting Languages
Software Development Automation With Scripting LanguagesSoftware Development Automation With Scripting Languages
Software Development Automation With Scripting Languages
 
ContainerDayVietnam2016: Become a Cloud-native Developer
ContainerDayVietnam2016: Become a Cloud-native DeveloperContainerDayVietnam2016: Become a Cloud-native Developer
ContainerDayVietnam2016: Become a Cloud-native Developer
 
Ankur_Srivastava
Ankur_SrivastavaAnkur_Srivastava
Ankur_Srivastava
 
Track A-Compilation guiding and adjusting - IBM
Track A-Compilation guiding and adjusting - IBMTrack A-Compilation guiding and adjusting - IBM
Track A-Compilation guiding and adjusting - IBM
 
Compiler
Compiler Compiler
Compiler
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
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
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
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
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 

2. introduction to compiler

  • 1. 5/11/2021 Saeed Parsa 1 Compiler Design Introduction to Compiler Saeed Parsa Room 332, School of Computer Engineering, Iran University of Science & Technology parsa@iust.ac.ir Winter 2021
  • 2. Why compilers? 5/11/2021 Saeed Parsa 2 Compiler Software Quality Measurement Reverse Engineering Software Security Automatic Distribution & Parallelization Software Testing
  • 3. A compiler structure • Page 7 of the Aho’s Book 5/11/2021 Saeed Parsa 3
  • 4. Compilation steps with an example • Page 7 of the Aho’s Book 5/11/2021 Saeed Parsa 4
  • 5. Example of compilation process Consider the example statement: position = initial + rate * 60 1. Lexical Analysis There are 30 characters in the statement. The characters are transformed by lexical analysis into a sequence of 7 tokens. Token1.name = “position”; Token2.name = “=“; Token3.name = “initial“; Token4.name = “+“; Token5.name = “rate“; Token6.name = “*“; Token7.name = “60“; 5/11/2021 Saeed Parsa 5
  • 6. Example of compilation process (Continued) Consider the example statement: position = initial + rate * 60 2. Syntax Analysis Those tokens are then used by syntax analyzer to build a tree of height 4, representing the correctness of the statement according to the grammar. G1: assignmentSt ::= identifier = expression expression ::= expression + term | expression – term | term term ::= term * factor | term / factor | factor factor ::= identifier | number | (expression) 5/11/2021 Saeed Parsa 6
  • 7. Example of compilation process (Continued) Consider the example statement: position = initial + rate * 60 2. Syntax Analysis Those tokens are then used syntax analyzer to build a tree of height 4, representing the correctness of the statement according to the grammar. 5/11/2021 Saeed Parsa 7
  • 8. Example of compilation process (Continued) Consider the example statement: position = initial + rate * 60 3. Semantics Analysis Semantic analysis may transform the tree into one of height 5, that includes a type conversion necessary for real addition on an integer operand. 4. Intermediate code generation Intermediate code generation uses a simple traversal algorithm to linearize the tree back into a sequence of machine-independent three-address-code instructions. t1 = inttoreal(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 5/11/2021 Saeed Parsa 8 position = initial + rate * 60
  • 9. Example of compilation process (Continued) 5/11/2021 Saeed Parsa 9 t1 = inttoreal(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 position = initial + rate * 60 t1 = id3 * 60.0 id1 = id2 + t1 http://www.personal.kent.edu/~rmuhamma/Compilers/compnotes.html Consider the example statement: position = initial + rate * 60 5. Optimization Optimization of the intermediate code allows the four instructions to be reduced to two machine-independent instructions.
  • 10. Example of compilation process (Continued) 5/11/2021 Saeed Parsa 10 Consider the example statement: position = initial + rate * 60 7. Final code generation Final code generation might implement these two instructions using 5 machine instructions, in which the actual registers and addressing modes of the CPU are utilized. t1 = inttoreal(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 position = initial + rate * 60 t1 = id3 * 60.0 id1 = id2 + t1 MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 http://www.personal.kent.edu/~rmuhamma/Compilers/compnotes.html
  • 11. Compiler construction tools 5/11/2021 Saeed Parsa 11 On-line documentation is available for the following compiler tools: • Scanner generators for C/C++: Flex (pdf), Lex (pdf). • Parser generators for C/C++: Bison (in HTML), Bison (pdf), Yacc (pdf). • Available scanner generators for Java: • JLex, a scanner generator for Java, very similar to Lex. • JFLex, flex for Java. http://www.personal.kent.edu/~rmuhamma/Compilers/compnotes.html
  • 12. Compiler construction tools 5/11/2021 Saeed Parsa 12 • Available parser generators for Java: • CUP, a parser generator for Java, very similar to YACC. • BYACC/J, a different version of Berkeley YACC for Java. It is an extension of the standard YACC (a -j flag has been added to generate Java code). • Other compiler tools: • JavaCC, a parser generator for Java, including scanner generator and parser generator. Input specifications are different than those suitable for Lex/YACC. Also, unlike YACC, JavaCC generates a top-down parser. • ANTLR, a set of language translation tools (formerly PCCTS). Includes scanner/parser generators for C, C++, and Java.
  • 13. Compiler construction tools 5/11/2021 Saeed Parsa 13 1. Parser Generators 2. Scanner Generators 3. Syntax-directed Translation Engines 4. Automatic Code Generators 5. Data-flow Analysis Engines 6. Compiler Construction Toolkits
  • 14. 1. Parser Generators 5/11/2021 Saeed Parsa 14  It produces syntax analyzers (parsers) from the input that is based on a grammatical description of programming language or on a context-free grammar.  It is useful as the syntax analysis phase is highly complex and consumes more manual and compilation time.  Example: PIC, EQM, ANTLR, YACC
  • 15. 2. Scanner Generators 5/11/2021 Saeed Parsa 15  It generates lexical analyzers from the input that consists of regular expression description based on tokens of a language.  It generates a finite automaton to recognize the regular expression.  Example: Lex, Flex, ANTLR
  • 16. Compiler construction tools 5/11/2021 Saeed Parsa 16 3. Syntax-directed translation engines that produce collections of routines for walking a parse tree and generating intermediate code. 4. Code-generator generators that produce a code generator from a collection of rules for translating each operation of the intermediate language into the machine language for a target machine. 5. Data-flow analysis engines that facilitate the gathering of information about how values are transmitted from one part of a program to each other part. Data-flow analysis is a key part of code optimization. 6. Compiler-construction toolkits that provide an integrated set of routines for constructing various phases of a compiler. Page 8 of the Aho’s Book
  • 17. Compiler construction tools 5/11/2021 Saeed Parsa 17 https://link.springer.com/content/pdf/10.1007%2F3-540-53669-8_77.pdf
  • 18. What is ANTLR? 5/11/2021 Saeed Parsa 18
  • 19. What is ANTLR? 5/11/2021 Saeed Parsa 19  ANTLR, Another Tool for Language Recognition, (formerly PCCTS) is a language tool that provides a framework for constructing recognizers, compilers, and translators from grammatical descriptions.  ANTLR is a parser generator.  ANTLR is open source, written in JAVA.  ANTLR provides a “tree walker” to traverse parse tress.  There are two tree walking mechanism provided by the ANTLR library - Listener & Visitor.
  • 20. What is ANTLR? 5/11/2021 Saeed Parsa 20  There are 3 primary differences between the Listener and Visitor libraries: 1. 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 2. 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. 3. Listener uses an explicit stack allocated on the heap, whereas visitor uses call stack to manage tree traversals. This might lead to Stack Overflow exceptions while using visitor on deeply nested ASTs.
  • 21. What is ANTLR? 5/11/2021 Saeed Parsa 21 Antlr is a public-domain, software tool developed by Terence Parr to assist with the development of translators and compilers. ANTLR automatically generates the lexical analyzer and parser for you by analyzing the grammar you provide. Grammar ANRLR Parser Generator Lexer Parser
  • 22. What is ANTLR? 5/11/2021 Saeed Parsa 22 A parse tree walker allows walking through the parse tree and perform any action at each node.
  • 23. Why to use ANTLR? 5/11/2021 Saeed Parsa 23 ANTLR is extremely popular with 5,000 downloads a month and is included on all Linux and OS X distributions. It is widely used because it:  Generates human-readable code that is easy to fold into other applications  Generates powerful recursive-descent recognizers using LL(*), an extension to LL(k) that uses arbitrary look ahead to make decisions  Tightly integrates StringTemplate,5 a template engine specifically designed to generate structured text such as source code  Has a graphical grammar development environment called ANTLRWorks6 that can debug parsers generated in any ANTLR target language
  • 24. Why to use ANTLR? 5/11/2021 Saeed Parsa 24 1. Is actively supported with a good project website and a high-traffic mailing list7 • Comes with complete source under the BSD license 2. Is extremely flexible and automates or formalizes many common tasks 3. Supports multiple target languages such as Java, C#, Python, Ruby, Objective-C, C, and C++. See: • http://www.stringtemplate.org • http://www.antlr.org/works • http://www.antlr.org:8080/pipermail/antlr-interest/ https://doc.lagout.org/programmation/Pragmatic%20Programmers/The%20Definitive%20ANTLR%20Reference.pdf
  • 25. Install ANTLR 5/11/2021 Saeed Parsa 25 ANTLR is written in Java, so you must have Java installed on your machine even if you are going to use ANTLR with, say, Python. ANTLR requires a Java version of 1.6 or higher. Step 1: Install Java 1.1 Download and install Java JDK 13.0.2 (64-bit). Java Development Kit (64-bit). Date released 2018. Free Download. (159.8 MB). https://www.filehorse.com/download-java-development-kit-64/old-versions/page-2/ Step 2: Download the tool 2.1 Download antlr-4.8-complete.jar (or whatever version) from https: https://www.antlr.org/download/ 2.2 Save antlr-4.8-complete.jar to your directory for 3rd party Java libraries, say: C:Javalib
  • 26. Install ANTLR 5/11/2021 Saeed Parsa 26 Step 3: Set paths 3.1 Create three text files antlr.txt, grun.txt and class.txt and save then to c:java.lib 3.2 Rename the above files to antlr.bat, grun.bat and class.bat 3.3 Copy the following commands into the batch files: class.bat: SET CLASSPATH=.; %CLASSPATH% grun.bat: java org.antlr.v4.gui.TestRig %* antlr.bat: java org.antlr.v4.Tool %* 3.4 Add antlr-4.8-complete.jar to CLASSPATH, either: Permanently: Using Control Panel > System > Advanced system settings > Environment variables 3.5 Use the windows search, to look for “environment”.
  • 27. Install ANTLR 5/11/2021 Saeed Parsa 27 3.6 Click on “Edit Environment Variables” > “Environment Variables” 3.7 The following widow will pop up on the screen.
  • 28. Install ANTLR 5/11/2021 Saeed Parsa 28 3.8 Click on “New”. The following window will be displayed
  • 29. Install ANTLR 5/11/2021 Saeed Parsa 29 3.9 Add Java path to Path by clicking on the “Path” option
  • 30. Install ANTLR 5/11/2021 Saeed Parsa 30 3.10 The following window will be opened. Click on the push-button, labeled “New”.
  • 31. Install ANTLR 5/11/2021 Saeed Parsa 31 3.11 Enter java compiler path and the Class Path entered before.
  • 32. Install ANTLR 5/11/2021 Saeed Parsa 32 3.12.1 Now by doing such settings it will be possible to access the downloaded jar file. The above image is from the javalib folder, which contains the antlr-4.8-complete.jar software we downloaded from www.antlr.org.
  • 33. Running ANTLR 5/11/2021 Saeed Parsa 33 Step 2: Add or create a grammar file (*.g4) in your project 2.1 Download the desired Grammar from the following URL and save it in the C:javalib directory https://github.com/antlr/grammars-v4/blob/master/cpp/CPP14.g4 2.2 Run Antlr to create lexer and parser Create two batch files as follows: (1) antlr4.bat: java org.antlr.v4.Tool%* (2) grun.bat: antlr4 -listener -visitor -Dlanguage=CSharp CPP14.g4 Or run the following commands: java org.antlr.v4.Tool -Werror -o {outputdirectory} -Dlanguage=CSharp input.g4 -visitor java -jar antlr-4.8-complete.jar -Dlanguage=CSharp input.g4 -visitor
  • 34. Running ANTLR 5/11/2021 Saeed Parsa 34 • In antlr4.bat the command java org.antlr.v4.Tool% is included. This command actually invokes the antlr.v4 software. • The second batch file, grun.bat, includes the command: antlr4 -listener -visitor -Dlanguage=CSharp CPP14.g4. • In this command: • Using the -listener -visitor switches we have actually requested that this command generate both these files in addition to the parser and lexer. • Dlanguage = CSharp tells antlr to generate the files generated from CPP14.G4 should be in C#.
  • 35. Running ANTLR 5/11/2021 Saeed Parsa 35 After executing this batch file, all the required files will be generated and saved in the JAVALIB folder.
  • 36. Running ANTLR 5/11/2021 Saeed Parsa 36 Alternatively: 2.2 Run Antlr to create listener and visitor java org.antlr.v4.Tool -Werror -o {outputdirectory} -Dlanguage=CSharp input.g4 -visitor java -jar antlr-4.5.1-complete.jar -Dlanguage=CSharp input.g4 –visitor -listener 2.3 As a result the Parser, Lexer, Visitor and Listener classes will be created
  • 38. Install ANTLR in C# 5/11/2021 Saeed Parsa 38 Adding ANTLR plugin:  A JRE needs to be on the executable search path (i.e. the absolute path is in the %PATH% environment variable)  Installation of the ANTLR4 packages is via the NuGet Package Manager  Grammar file(s)’ compilation options need(s) to be customized manually by editing the project’s configuration file (*.csproj)  The latest stable version of ANTLR4 (version 4.3.0) is only supported on .NET Framework 4.5 and below, therefore there is a need to change the target framework from the default (if the default version is higher)  References to the ANTLR namespaces are required
  • 39. Install ANTLR in C# 5/11/2021 Saeed Parsa 39 1. First, create a Windows Form App project in Visual Studio. 2. Right click on the project name in the solution explorer and add a folder, Antlor4.
  • 40. Install ANTLR in C# 5/11/2021 Saeed Parsa 40 3. Copy the content of javalib into the Antlor4 folder.
  • 41. Install ANTLR in C# 5/11/2021 Saeed Parsa 41 • Now after adding the folder to the visual environment we need to add a package to use Antlr in the .net environment. • To do so, right-click on References and then in the popup window click on the Manage NuGet package option and then add a package called antlr4.rantime.standard to the project.
  • 42. Install ANTLR in C# 5/11/2021 Saeed Parsa 42 NuGet: • NuGet is a package and dependency manager. • Helps to find, install, update and remove packages. • Focuses primarily on package and dependency management. • Lists all available packages for download. • Adding a NuGet package to a Visual Studio project is similar to adding a reference. • Go to Solution Explorer, • Right-click on the References folder, • Click Manage NuGet Packages.
  • 43. Install ANTLR in C# 5/11/2021 Saeed Parsa 43 For Visual Studio 2017: 1. Right click the top-level solution node in the Solution Explorer window and select Manage NuGet Packages for Solution. 2. In the upper left, choose Browse and then choose nuget.org as the Package source 3. Next to the Search box, check Include prerelease 4. In the Search box, type Antlr4 to search for the package 5. In the search results, locate and select the package called Antlr4. Verify that the name is listed as Antlr4. 6. In the right pane, select the C# projects you want to use ANTLR4 by clicking their checkboxes 7. Click Install under the list of projects 8. Approve changes and accept license agreements, if prompted.
  • 44. Install ANTLR in C# 5/11/2021 Saeed Parsa 44
  • 45. Install ANTLR in C# 5/11/2021 Saeed Parsa 45 After adding the packages to our project, we use their namespaces and use the capabilities of these packages in our project. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Reflection; using System.Resources; using System.Text; using System.Windows.Forms; using Antlr4.Runtime; using Antlr4.Runtime.Misc; using Antlr4.Runtime.Tree; using static CPP14Parser;
  • 46. Assignment 1 5/11/2021 Saeed Parsa 46 Subject : Install & use ANTLR Deadline: Two weeks Mark: 5 out of 100.
  • 47. Assignment 1 5/11/2021 Saeed Parsa 47 Write a program to accept a C++ program as input and generate parse tree for the program 1. Run ANTLR to generate a lexical analyzer (lexer) and a parser for C++ . 2. Give a C++ program to your C++ compiler to generate parse and depict the parse tree for the program. 3. Provide a report describing the installation step and your program. You may generate lexer and parser for other languages such as C# , Java, and Python. Your code could be either in Python, or C# language.
  • 48. Assignment 1 5/11/2021 Saeed Parsa 48 Steps to install ANTLR: 1. Download Java JDK 13.0.2 (64-bit). Java Development Kit (64-bit). Date released 2018. Free Download. (159.8 MB). https://www.filehorse.com/download-java-development-kit-64/old-versions/page-2/ 2. Download Antlr 4.8-complete.jar (or whatever version) from https: https://www.antlr.org/download/ 3. Create a directory for 3rd party Java libraries : C:Javalib 4. Save antlr-4.8-complete.jar to C:Javalib.
  • 49. Assignment 1 5/11/2021 Saeed Parsa 49 Steps to install ANTLR: 5. Add the followings to ’environment variables’ in windows 10.  C:Program FilesJavajdk-13.0.2bin  C:Javalib  %CLASSPATH% 6. Add C:Javalibantlr-4.8-complete.jar as the variable value of the environment variable named CLASSPATH. 7. Download the CPP grammar from the following URL address into “C:javalib” folder: https://github.com/antlr/grammars-v4/tree/master/cpp Similarly the Python grammar could be downloaded from: https://github.com/antlr/grammars-v4/tree/master/python
  • 50. Assignment 1 5/11/2021 Saeed Parsa 50 Steps to install ANTLR: 8. Generate parser and lexer java -jar antlr4-4.8.jar -Dlanguage=CSharp CPP14.g4 or java -jar antlr4-4.8.jar -Dlanguage=Phyton3 CPP14.g4 or java -jar antlr4-4.8.jar -Dlanguage=Cpp grammar.g4 For instance suppose you choose to generate Python3 code: java -jar ./antlr-4.8-complete.jar -Dlanguage=Python3 CPP14.g4
  • 51. Assignment 1 5/11/2021 Saeed Parsa 51 Steps to install ANTLR:  This will generate the following files, which you can then integrate in your project CPP14Lexer.py: including the source of a class, CPP14Lexer. CPP14Parser.py: including the source of a class, CPP14Parser. CPP14Listener.py: including the source of a class, CPP14Listener. 9. In addition to the above three files, four other files are generated that are used by ANTLR. All these files should be copied into the folder where you save your Python program.
  • 52. Assignment 1 5/11/2021 Saeed Parsa 52 Steps to install ANTLR: 10. To access the ANLR-4 runtime library within a Python project, MyProject, in the PyChram environment select: File > Setting > MyProject > Project interpreter > + (add) Search for “ANTR4” in the popped up window, select “ANTLR4 runtime Python3” option, and click on the “install package” button. http://ati.ttu.ee/~kjans/antlr/pycharm_antlr4_guide.pdf
  • 53. Assignment 1 5/11/2021 Saeed Parsa 53 Steps to install ANTLR: 11. Write a program to generate and display Parse tree for a given C++ program. https://www.thetopsites.net/article/50064110.shtml https://github.com/antlr/antlr4/blob/master/runtime/Python3/src/antlr4/Parser.py
  • 54. Assignment 1 5/11/2021 Saeed Parsa 54 Note: CLASSPATH is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.  To add ANTLR to class path, • Click on ‘Windows search’ key and enter “environment” • go to the following address (you may click on the following address to get access to the related document): • Control Panel > System > Advanced system settings > Environment variables  Under Environment Variables, you can see two sections.  The top section shows User variables,  The bottom section shows System Variables.
  • 55. Assignment 1 5/11/2021 Saeed Parsa 55  In either of them, find for Variable name PATH or path.  If it is available click on path in the system variable window and then press edit.  Pressing the “Edit” button a new window labeled ‘Edit system variable’ will pop up.  Press the “new” button, and add the following three items to the list” 1. The path for accessing Java: C:Program FilesJavajdk-13.0.2bin 2. The 3rd party Java libraries address: C:javalib 3. Class path: %CLASSPATH%
  • 60. The place of IUST in the world 5/11/2021 Saeed Parsa 60 https://www.researchgate.net/publication/328099969_Software_Fault_Localisation_A_Systematic_Mapping_Study