SlideShare a Scribd company logo
1 of 64
Antlr Compiler Generator
12/28/2020 Saeed Parsa 1
12/28/2020 Saeed Parsa 2
Compiler
Software
Quality
Measurement
Reverse
Engineering
‫ا‬Software
Security
Automatic
Distribution &
Paralleization
Software
Testing
12/28/2020 Saeed Parsa 3
Page 7 of the Aho’s Book
12/28/2020 Saeed Parsa 4
Page 7 of the Aho’s Book
12/28/2020 Saeed Parsa 5
https://github.com/antlr/antlr4/blob/master/doc/lexer-rules.md
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“;
12/28/2020 Saeed Parsa 6
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)
12/28/2020 Saeed Parsa 7
Consider the example statement:
position = initial + rate * 60
2. Syntax Analysis
Those tokens are then used
byyntax analyzer to build a tree of
height 4, representing the
correctness of the statement
according to the grammar.
12/28/2020 Saeed Parsa 8
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
position = initial + rate * 60
12/28/2020 Saeed Parsa 9
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.
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
12/28/2020 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
12/28/2020 Saeed Parsa 11
http://www.personal.kent.edu/~rmuhamma/Compilers/compnotes.html
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.
•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.
12/28/2020 Saeed Parsa 12
https://www.geeksforgeeks.org/compiler-construction-tools/
1. Parser Generators
2. Scanner Generators
3. Syntax-directed Translation Engines
4. Automatic Code Generators
5. Data-flow Analysis Engines
6. Compiler Construction Toolkits
12/28/2020 Saeed Parsa 13
https://www.geeksforgeeks.org/compiler-construction-tools/
 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
12/28/2020 Saeed Parsa 14
https://www.geeksforgeeks.org/compiler-construction-tools/
 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
12/28/2020 Saeed Parsa 15
https://www.geeksforgeeks.org/compiler-construction-tools/
 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
12/28/2020 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- ow
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
12/28/2020 Saeed Parsa 17https://link.springer.com/content/pdf/10.1007%2F3-540-53669-8_77.pdf
12/28/2020 Saeed Parsa 18
12/28/2020 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
12/28/2020 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 StackOverFlow exceptions while using visitor
on deeply nested ASTs
What
12/28/2020 21
What
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
12/28/2020 22
What
A pars tree walker allows walking through the parse tree and perform any action at each node,
12/28/2020 Saeed Parsa 23
Why
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 lookahead 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
12/28/2020 Saeed Parsa 24
Why
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++ Perhaps most importantly, ANTLR is m
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
12/28/2020 Saeed Parsa 25
Installing ANTLR
12/28/2020 Saeed Parsa 26
12/28/2020 Saeed Parsa 27
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
12/28/2020 Saeed Parsa 28
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”.
12/28/2020 Saeed Parsa 29
3.6 Click on “Edit Environment Variables” > “Environment Variables”
3.7 The following widow will pop up on the screen.
12/28/2020 Saeed Parsa 30
3.8 Click on “New”. The following window will be displayed
12/28/2020 Saeed Parsa 31
3.9 Add Java path to Path by clicking on the “Path” option
12/28/2020 Saeed Parsa 32
3.10 The following window will be opened. Click on the push-button, labeled “New”.
3.11 Enter java compiler path and the Class Path entered before
12/28/2020 33
12/28/2020 Saeed Parsa 34
3.12.1 Now by doing such settings it will be possible to access the downlowded 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.
12/28/2020 Saeed Parsa 35
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=Python3 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=Python3 input.g4 –visitor -listener
12/28/2020 Saeed Parsa 36
- In antlr4.bat the command java org.antlr.v4.Tool% is included. This command actually
invokes the antlr.v4 software.
- The second batch fle, 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 #.
12/28/2020 Saeed Parsa 37
After executing this batch file, all the required files will be generated and saved in the JAVALIB
folder.
12/28/2020 Saeed Parsa 38
2.2 Run Antlr to create lexer and parser
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
2.3 As a result the Parser, Lexer, Visitor and Listener classes will be created
12/28/2020 Saeed Parsa 39
12/28/2020 Saeed Parsa 40
12/28/2020 Saeed Parsa 41
https://www.thestuffweuse.com/2016/05/07/antlr4-with-c-in-visual-studio-2015/
https://github.com/tunnelvisionlabs/antlr4cs
https://github.com/antlr/grammars-v4/blob/master/cpp/CPP14.g4
12/28/2020 Saeed Parsa 42
 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
12/28/2020 Saeed Parsa 43
1. First, create a WindowsFormsApp project in visualstudio.
2. Rightclick on the project name in the solution explorer and add a folder, Antlor4.
12/28/2020 Saeed Parsa 44
1. Copy the content of javalib into the Antlor4 folder.
12/28/2020 Saeed Parsa 45
• 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.
12/28/2020 Saeed Parsa 46
• 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.
12/28/2020 Saeed Parsa 47
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.
12/28/2020 Saeed Parsa 48
12/28/2020 Saeed Parsa 49
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;
The first homework
12/28/2020 Saeed Parsa 50
12/28/2020 Saeed Parsa 51
:
Subject : Antlr
Deadline: Two weeks
Mark: 5 out of 100.
12/28/2020 Saeed Parsa 52
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 Phyton.
Your code could be either in Phyton, or C# language,
Exercise 1: Install and use ANTLR
12/28/2020 Saeed Parsa 53
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.
5. Add the followings to ’environment variables’ in windows 10.
- C:Program FilesJavajdk-13.0.2bin
- C:Javalib
- %CLASSPATH%
Exercise 1: ANTLR installation steps
12/28/2020 Saeed Parsa 54
7. Add C:Javalibantlr-4.8-complete.jar as the variable value of the environment variable
named CLASSPATH.
8. 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
9. Geneeate 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
Exercise 1: ANTLR installation steps (continued)
12/28/2020 Saeed Parsa 55
 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
10. 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.
11. 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
Exercise 1: ANTLR installation steps (continued)
12/28/2020 Saeed Parsa 56
12. Write a program to generate and display Parse tree for a given C++ program.
Exercise 1: ANTLR installation steps (continued)
https://www.thetopsites.net/article/50064110.shtml
https://github.com/antlr/antlr4/blob/master/runtime/Python3/src/antlr4/Parser.py
12/28/2020 Saeed Parsa 57
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
Exercise 1: ANTLR installation steps - 1
 Under Environment Variables, you can see two sections.
 The top section shows User variables,
 The bottom section shows System Variables.
12/28/2020 Saeed Parsa 58
 In either of them, find for Variable name PATH or path.
Exercise 1: ANTLR installation steps - 2
 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%
12/28/2020 Saeed Parsa 59
Exercise 1: Define the location of Java compiler
12/28/2020 Saeed Parsa 60
Exercise 1: Define the location of Java compiler
12/28/2020 Saeed Parsa 61
Exercise 1: Define the location of Java compiler
12/28/2020 Saeed Parsa 62
 Add ANTLR by clicking on ‘New’ on “Environment Variables” window and insert into
variable name and variable value fields the following items:
 Variable name: CLASSPATH
 Variable value: C:Javalibantlr-4.8-complete.jar/
Exercise 1: Define the address of ANTLR-4.8
12/28/2020 Saeed Parsa 63
Exercise 1: Define the address of ANTLR-4.8 - 1
12/28/2020 Saeed Parsa 64

More Related Content

What's hot

.NET Overview
.NET Overview.NET Overview
.NET OverviewGreg Sohl
 
CMPT470-usask-guest-lecture
CMPT470-usask-guest-lectureCMPT470-usask-guest-lecture
CMPT470-usask-guest-lectureMasud Rahman
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesEelco Visser
 
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android ApplicationsSteelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android ApplicationsTom Keetch
 
Improving DroidBox
Improving DroidBoxImproving DroidBox
Improving DroidBoxKelwin Yang
 
Reverse engineering android apps
Reverse engineering android appsReverse engineering android apps
Reverse engineering android appsPranay Airan
 
Towards using Capability Machines for Secure Compilation - CHERI
Towards using Capability Machines for Secure Compilation - CHERITowards using Capability Machines for Secure Compilation - CHERI
Towards using Capability Machines for Secure Compilation - CHERIAkram El-Korashy
 
Os Worthington
Os WorthingtonOs Worthington
Os Worthingtonoscon2007
 
IRJET- Encrypted Negative Password using RSA Algorithm
IRJET- Encrypted Negative Password using RSA AlgorithmIRJET- Encrypted Negative Password using RSA Algorithm
IRJET- Encrypted Negative Password using RSA AlgorithmIRJET Journal
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programmingSrinivas Narasegouda
 
Isolating the Ghost in the Machine: Unveiling Post Exploitation Threatsrsac
Isolating the Ghost in the Machine:  Unveiling Post Exploitation ThreatsrsacIsolating the Ghost in the Machine:  Unveiling Post Exploitation Threatsrsac
Isolating the Ghost in the Machine: Unveiling Post Exploitation ThreatsrsacPriyanka Aash
 

What's hot (14)

The Rust Borrow Checker
The Rust Borrow CheckerThe Rust Borrow Checker
The Rust Borrow Checker
 
Kalyani_Resume_latest
Kalyani_Resume_latestKalyani_Resume_latest
Kalyani_Resume_latest
 
.NET Overview
.NET Overview.NET Overview
.NET Overview
 
CMPT470-usask-guest-lecture
CMPT470-usask-guest-lectureCMPT470-usask-guest-lecture
CMPT470-usask-guest-lecture
 
Verilogspk1
Verilogspk1Verilogspk1
Verilogspk1
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
 
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android ApplicationsSteelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
 
Improving DroidBox
Improving DroidBoxImproving DroidBox
Improving DroidBox
 
Reverse engineering android apps
Reverse engineering android appsReverse engineering android apps
Reverse engineering android apps
 
Towards using Capability Machines for Secure Compilation - CHERI
Towards using Capability Machines for Secure Compilation - CHERITowards using Capability Machines for Secure Compilation - CHERI
Towards using Capability Machines for Secure Compilation - CHERI
 
Os Worthington
Os WorthingtonOs Worthington
Os Worthington
 
IRJET- Encrypted Negative Password using RSA Algorithm
IRJET- Encrypted Negative Password using RSA AlgorithmIRJET- Encrypted Negative Password using RSA Algorithm
IRJET- Encrypted Negative Password using RSA Algorithm
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Isolating the Ghost in the Machine: Unveiling Post Exploitation Threatsrsac
Isolating the Ghost in the Machine:  Unveiling Post Exploitation ThreatsrsacIsolating the Ghost in the Machine:  Unveiling Post Exploitation Threatsrsac
Isolating the Ghost in the Machine: Unveiling Post Exploitation Threatsrsac
 

Similar to 2. introduction

Introduction to Apache Spark
Introduction to Apache Spark Introduction to Apache Spark
Introduction to Apache Spark Hubert Fan Chiang
 
Speeding up Programs with OpenACC in GCC
Speeding up Programs with OpenACC in GCCSpeeding up Programs with OpenACC in GCC
Speeding up Programs with OpenACC in GCCinside-BigData.com
 
String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?Jeremy Schneider
 
PLNOG20 - Paweł Małachowski - Stress your DUT–wykorzystanie narzędzi open sou...
PLNOG20 - Paweł Małachowski - Stress your DUT–wykorzystanie narzędzi open sou...PLNOG20 - Paweł Małachowski - Stress your DUT–wykorzystanie narzędzi open sou...
PLNOG20 - Paweł Małachowski - Stress your DUT–wykorzystanie narzędzi open sou...PROIDEA
 
DEF CON 27 - workshop - ISAAC EVANS - discover exploit and eradicate entire v...
DEF CON 27 - workshop - ISAAC EVANS - discover exploit and eradicate entire v...DEF CON 27 - workshop - ISAAC EVANS - discover exploit and eradicate entire v...
DEF CON 27 - workshop - ISAAC EVANS - discover exploit and eradicate entire v...Felipe Prado
 
Performance Verification for ESL Design Methodology from AADL Models
Performance Verification for ESL Design Methodology from AADL ModelsPerformance Verification for ESL Design Methodology from AADL Models
Performance Verification for ESL Design Methodology from AADL ModelsSpace Codesign
 
breed_python_tx_redacted
breed_python_tx_redactedbreed_python_tx_redacted
breed_python_tx_redactedRyan Breed
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsESUG
 
Querying 1.8 billion reddit comments with python
Querying 1.8 billion reddit comments with pythonQuerying 1.8 billion reddit comments with python
Querying 1.8 billion reddit comments with pythonDaniel Rodriguez
 
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...Databricks
 
Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F...
 Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F... Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F...
Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F...Databricks
 
Toub parallelism tour_oct2009
Toub parallelism tour_oct2009Toub parallelism tour_oct2009
Toub parallelism tour_oct2009nkaluva
 
Eta lang Beauty And The Beast
Eta lang Beauty And The Beast Eta lang Beauty And The Beast
Eta lang Beauty And The Beast Jarek Ratajski
 
ROCm and Distributed Deep Learning on Spark and TensorFlow
ROCm and Distributed Deep Learning on Spark and TensorFlowROCm and Distributed Deep Learning on Spark and TensorFlow
ROCm and Distributed Deep Learning on Spark and TensorFlowDatabricks
 
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...Alessandro Confetti
 
From FPGA-based Reconfigurable Systems to Autonomic Heterogeneous Computing S...
From FPGA-based Reconfigurable Systems to Autonomic Heterogeneous Computing S...From FPGA-based Reconfigurable Systems to Autonomic Heterogeneous Computing S...
From FPGA-based Reconfigurable Systems to Autonomic Heterogeneous Computing S...NECST Lab @ Politecnico di Milano
 
Automated ML Workflow for Distributed Big Data Using Analytics Zoo (CVPR2020 ...
Automated ML Workflow for Distributed Big Data Using Analytics Zoo (CVPR2020 ...Automated ML Workflow for Distributed Big Data Using Analytics Zoo (CVPR2020 ...
Automated ML Workflow for Distributed Big Data Using Analytics Zoo (CVPR2020 ...Jason Dai
 
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)Igalia
 

Similar to 2. introduction (20)

Azure HDInsight
Azure HDInsightAzure HDInsight
Azure HDInsight
 
Introduction to Apache Spark
Introduction to Apache Spark Introduction to Apache Spark
Introduction to Apache Spark
 
Speeding up Programs with OpenACC in GCC
Speeding up Programs with OpenACC in GCCSpeeding up Programs with OpenACC in GCC
Speeding up Programs with OpenACC in GCC
 
String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?
 
Stress your DUT
Stress your DUTStress your DUT
Stress your DUT
 
PLNOG20 - Paweł Małachowski - Stress your DUT–wykorzystanie narzędzi open sou...
PLNOG20 - Paweł Małachowski - Stress your DUT–wykorzystanie narzędzi open sou...PLNOG20 - Paweł Małachowski - Stress your DUT–wykorzystanie narzędzi open sou...
PLNOG20 - Paweł Małachowski - Stress your DUT–wykorzystanie narzędzi open sou...
 
DEF CON 27 - workshop - ISAAC EVANS - discover exploit and eradicate entire v...
DEF CON 27 - workshop - ISAAC EVANS - discover exploit and eradicate entire v...DEF CON 27 - workshop - ISAAC EVANS - discover exploit and eradicate entire v...
DEF CON 27 - workshop - ISAAC EVANS - discover exploit and eradicate entire v...
 
Performance Verification for ESL Design Methodology from AADL Models
Performance Verification for ESL Design Methodology from AADL ModelsPerformance Verification for ESL Design Methodology from AADL Models
Performance Verification for ESL Design Methodology from AADL Models
 
breed_python_tx_redacted
breed_python_tx_redactedbreed_python_tx_redacted
breed_python_tx_redacted
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
 
Querying 1.8 billion reddit comments with python
Querying 1.8 billion reddit comments with pythonQuerying 1.8 billion reddit comments with python
Querying 1.8 billion reddit comments with python
 
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
Spark SQL Catalyst Code Optimization using Function Outlining with Kavana Bha...
 
Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F...
 Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F... Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F...
Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F...
 
Toub parallelism tour_oct2009
Toub parallelism tour_oct2009Toub parallelism tour_oct2009
Toub parallelism tour_oct2009
 
Eta lang Beauty And The Beast
Eta lang Beauty And The Beast Eta lang Beauty And The Beast
Eta lang Beauty And The Beast
 
ROCm and Distributed Deep Learning on Spark and TensorFlow
ROCm and Distributed Deep Learning on Spark and TensorFlowROCm and Distributed Deep Learning on Spark and TensorFlow
ROCm and Distributed Deep Learning on Spark and TensorFlow
 
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...
 
From FPGA-based Reconfigurable Systems to Autonomic Heterogeneous Computing S...
From FPGA-based Reconfigurable Systems to Autonomic Heterogeneous Computing S...From FPGA-based Reconfigurable Systems to Autonomic Heterogeneous Computing S...
From FPGA-based Reconfigurable Systems to Autonomic Heterogeneous Computing S...
 
Automated ML Workflow for Distributed Big Data Using Analytics Zoo (CVPR2020 ...
Automated ML Workflow for Distributed Big Data Using Analytics Zoo (CVPR2020 ...Automated ML Workflow for Distributed Big Data Using Analytics Zoo (CVPR2020 ...
Automated ML Workflow for Distributed Big Data Using Analytics Zoo (CVPR2020 ...
 
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
 

Recently uploaded

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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
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
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 

Recently uploaded (20)

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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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
 
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
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
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 ...
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 

2. introduction

  • 2. 12/28/2020 Saeed Parsa 2 Compiler Software Quality Measurement Reverse Engineering ‫ا‬Software Security Automatic Distribution & Paralleization Software Testing
  • 3. 12/28/2020 Saeed Parsa 3 Page 7 of the Aho’s Book
  • 4. 12/28/2020 Saeed Parsa 4 Page 7 of the Aho’s Book
  • 5. 12/28/2020 Saeed Parsa 5 https://github.com/antlr/antlr4/blob/master/doc/lexer-rules.md 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“;
  • 6. 12/28/2020 Saeed Parsa 6 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)
  • 7. 12/28/2020 Saeed Parsa 7 Consider the example statement: position = initial + rate * 60 2. Syntax Analysis Those tokens are then used byyntax analyzer to build a tree of height 4, representing the correctness of the statement according to the grammar.
  • 8. 12/28/2020 Saeed Parsa 8 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 position = initial + rate * 60
  • 9. 12/28/2020 Saeed Parsa 9 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. 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
  • 10. 12/28/2020 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. 12/28/2020 Saeed Parsa 11 http://www.personal.kent.edu/~rmuhamma/Compilers/compnotes.html 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. •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.
  • 12. 12/28/2020 Saeed Parsa 12 https://www.geeksforgeeks.org/compiler-construction-tools/ 1. Parser Generators 2. Scanner Generators 3. Syntax-directed Translation Engines 4. Automatic Code Generators 5. Data-flow Analysis Engines 6. Compiler Construction Toolkits
  • 13. 12/28/2020 Saeed Parsa 13 https://www.geeksforgeeks.org/compiler-construction-tools/  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
  • 14. 12/28/2020 Saeed Parsa 14 https://www.geeksforgeeks.org/compiler-construction-tools/  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
  • 15. 12/28/2020 Saeed Parsa 15 https://www.geeksforgeeks.org/compiler-construction-tools/  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. 12/28/2020 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- ow 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. 12/28/2020 Saeed Parsa 17https://link.springer.com/content/pdf/10.1007%2F3-540-53669-8_77.pdf
  • 19. 12/28/2020 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
  • 20. 12/28/2020 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 StackOverFlow exceptions while using visitor on deeply nested ASTs What
  • 21. 12/28/2020 21 What 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. 12/28/2020 22 What A pars tree walker allows walking through the parse tree and perform any action at each node,
  • 23. 12/28/2020 Saeed Parsa 23 Why 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 lookahead 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. 12/28/2020 Saeed Parsa 24 Why 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++ Perhaps most importantly, ANTLR is m 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. 12/28/2020 Saeed Parsa 25 Installing ANTLR
  • 27. 12/28/2020 Saeed Parsa 27 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
  • 28. 12/28/2020 Saeed Parsa 28 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”.
  • 29. 12/28/2020 Saeed Parsa 29 3.6 Click on “Edit Environment Variables” > “Environment Variables” 3.7 The following widow will pop up on the screen.
  • 30. 12/28/2020 Saeed Parsa 30 3.8 Click on “New”. The following window will be displayed
  • 31. 12/28/2020 Saeed Parsa 31 3.9 Add Java path to Path by clicking on the “Path” option
  • 32. 12/28/2020 Saeed Parsa 32 3.10 The following window will be opened. Click on the push-button, labeled “New”. 3.11 Enter java compiler path and the Class Path entered before
  • 34. 12/28/2020 Saeed Parsa 34 3.12.1 Now by doing such settings it will be possible to access the downlowded 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.
  • 35. 12/28/2020 Saeed Parsa 35 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=Python3 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=Python3 input.g4 –visitor -listener
  • 36. 12/28/2020 Saeed Parsa 36 - In antlr4.bat the command java org.antlr.v4.Tool% is included. This command actually invokes the antlr.v4 software. - The second batch fle, 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 #.
  • 37. 12/28/2020 Saeed Parsa 37 After executing this batch file, all the required files will be generated and saved in the JAVALIB folder.
  • 38. 12/28/2020 Saeed Parsa 38 2.2 Run Antlr to create lexer and parser 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 2.3 As a result the Parser, Lexer, Visitor and Listener classes will be created
  • 41. 12/28/2020 Saeed Parsa 41 https://www.thestuffweuse.com/2016/05/07/antlr4-with-c-in-visual-studio-2015/ https://github.com/tunnelvisionlabs/antlr4cs https://github.com/antlr/grammars-v4/blob/master/cpp/CPP14.g4
  • 42. 12/28/2020 Saeed Parsa 42  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
  • 43. 12/28/2020 Saeed Parsa 43 1. First, create a WindowsFormsApp project in visualstudio. 2. Rightclick on the project name in the solution explorer and add a folder, Antlor4.
  • 44. 12/28/2020 Saeed Parsa 44 1. Copy the content of javalib into the Antlor4 folder.
  • 45. 12/28/2020 Saeed Parsa 45 • 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.
  • 46. 12/28/2020 Saeed Parsa 46 • 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.
  • 47. 12/28/2020 Saeed Parsa 47 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.
  • 49. 12/28/2020 Saeed Parsa 49 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;
  • 51. 12/28/2020 Saeed Parsa 51 : Subject : Antlr Deadline: Two weeks Mark: 5 out of 100.
  • 52. 12/28/2020 Saeed Parsa 52 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 Phyton. Your code could be either in Phyton, or C# language, Exercise 1: Install and use ANTLR
  • 53. 12/28/2020 Saeed Parsa 53 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. 5. Add the followings to ’environment variables’ in windows 10. - C:Program FilesJavajdk-13.0.2bin - C:Javalib - %CLASSPATH% Exercise 1: ANTLR installation steps
  • 54. 12/28/2020 Saeed Parsa 54 7. Add C:Javalibantlr-4.8-complete.jar as the variable value of the environment variable named CLASSPATH. 8. 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 9. Geneeate 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 Exercise 1: ANTLR installation steps (continued)
  • 55. 12/28/2020 Saeed Parsa 55  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 10. 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. 11. 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 Exercise 1: ANTLR installation steps (continued)
  • 56. 12/28/2020 Saeed Parsa 56 12. Write a program to generate and display Parse tree for a given C++ program. Exercise 1: ANTLR installation steps (continued) https://www.thetopsites.net/article/50064110.shtml https://github.com/antlr/antlr4/blob/master/runtime/Python3/src/antlr4/Parser.py
  • 57. 12/28/2020 Saeed Parsa 57 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 Exercise 1: ANTLR installation steps - 1  Under Environment Variables, you can see two sections.  The top section shows User variables,  The bottom section shows System Variables.
  • 58. 12/28/2020 Saeed Parsa 58  In either of them, find for Variable name PATH or path. Exercise 1: ANTLR installation steps - 2  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%
  • 59. 12/28/2020 Saeed Parsa 59 Exercise 1: Define the location of Java compiler
  • 60. 12/28/2020 Saeed Parsa 60 Exercise 1: Define the location of Java compiler
  • 61. 12/28/2020 Saeed Parsa 61 Exercise 1: Define the location of Java compiler
  • 62. 12/28/2020 Saeed Parsa 62  Add ANTLR by clicking on ‘New’ on “Environment Variables” window and insert into variable name and variable value fields the following items:  Variable name: CLASSPATH  Variable value: C:Javalibantlr-4.8-complete.jar/ Exercise 1: Define the address of ANTLR-4.8
  • 63. 12/28/2020 Saeed Parsa 63 Exercise 1: Define the address of ANTLR-4.8 - 1