SlideShare una empresa de Scribd logo
1 de 63
Descargar para leer sin conexión
Implementing a Mini
Programming Language:
From Zero to Production
Luciano Sabença
Lead Software Engineer - Wavy
August/2020
ABOUT ME
● Lead Software Engineer at Wavy Global/Movile
● RedisConf Speaker (2018) / Redis Day London (2019)
● Interested in: Distributed System, Computer Theory
and Machine Learning.
● BS degree with praise in Computer Science by
University of Campinas
● @lucianosabenca
Luciano Sabença
● About Wavy
● Why did we need another lang?
● Compilers 101
● Defining the Grammar
● Implementing the Grammar
● Possible Follow ups
AGENDA
Disclaimers
● I'm not a compiler engineer
● I'm not an expert in:
○ Formal Languages
○ Lexers/Parsers
● This talk will be a little bit dense:
○ I promise I will do my best
○ Code is available at:
https://github.com/luciano-sabenca/TDC-2020-Implementing-a-mini-language
6
Another Lang?
Why did we need another lang?
● Create good conversations are hard!
○ You really need to take dynamic decisions
based on the conversation messages.
Let's take an example:
Why did we need another lang?
● Create good conversations are hard!
○ You really need to take dynamic decisions
based on the conversation messages.
Let's take an example:
Why did we need another lang?
100852
From 0 to 10, what's
your opinion about our
last contact?
100852
From 0 to 10, what's
your opinion about our
last contact?
8
100852
From 0 to 10, what's
your opinion about our
last contact?
8
We're glad that you
enjoy our contact!
Please free feel to
contact us anytime
Why did we need another lang?
100852
From 0 to 10, what's
your opinion about our
last contact?
100852
From 0 to 10, what's
your opinion about our
last contact?
8
100852
From 0 to 10, what's
your opinion about our
last contact?
8
We're glad that you
enjoy our contact!
Please free feel to
contact us anytime
Why did we need another lang?
100852
From 0 to 10, what's
your opinion about our
last contact?
100852
From 0 to 10, what's
your opinion about our
last contact?
8
100852
From 0 to 10, what's
your opinion about our
last contact?
8
We're glad that you
enjoy our contact!
Please free feel to
contact us anytime
Why did we need another lang?
100852
From 0 to 10, what's
your opinion about our
last contact?
100852
From 0 to 10, what's
your opinion about our
last contact?
5
100852
From 0 to 10, what's
your opinion about our
last contact?
5
We're sorry for you
experience. Please
could you share with
us your thoughts
about:
100852
5
We're sorry for you
experience. Please
could you share with
us your thoughts
about:
...
Why did we need another lang?
100852
From 0 to 10, what's
your opinion about our
last contact?
100852
From 0 to 10, what's
your opinion about our
last contact?
5
100852
From 0 to 10, what's
your opinion about our
last contact?
5
We're sorry for you
experience. Please
could you share with
us your thoughts
about:
100852
5
We're sorry for you
experience. Please
could you share with
us your thoughts
about:
...
Why did we need another lang?
100852
From 0 to 10, what's
your opinion about our
last contact?
100852
From 0 to 10, what's
your opinion about our
last contact?
5
100852
From 0 to 10, what's
your opinion about our
last contact?
5
We're sorry for you
experience. Please
could you share with
us your thoughts
about:
100852
5
We're sorry for you
experience. Please
could you share with
us your thoughts
about:
...
Why did we need another lang?
100852
From 0 to 10, what's
your opinion about our
last contact?
100852
From 0 to 10, what's
your opinion about our
last contact?
5
100852
From 0 to 10, what's
your opinion about our
last contact?
5
We're sorry for you
experience. Please
could you share with
us your thoughts
about:
100852
5
We're sorry for you
experience. Please
could you share with
us your thoughts
about:
...
Why did we need another lang?
● It's impossible to predict ALL the possible
conversations.
● We need to be prepared to take complex decisions
during the conversation! We need flexibility!
Embedding a full language?
○ It's possible, but it's too much power! It may
cause:
■ Security bugs
■ Performance problems (e.g. infinite loop,
expensive operations, recursions…)
■ Annoying type conversions
Why did we need another lang?
Create a mini-language?
○ Pros:
■ We can easily limit what the user can do
■ We can design the language to solve a
specific problem.
○ Cons:
■ It's a little bit challenger
Let's do it!
Why did we need another lang?
Create a mini-language?
○ Pros:
■ We can easily limit what the user can do
■ We can design the language to solve a
specific problem.
○ Cons:
■ It's a little bit challenger
Let's do it!
Why did we need another lang?
20
Compilers 101
Lexical
Analyser
Syntax
Analyser
Compilers 101
Source Code
Semantic
Implement
ation
Lexical
Analyser
Syntax
Analyser
Compilers 101
Source Code
Semantic
Implement
ation
Lexical
Analyser
Syntax
Analyser
Compilers 101
Source Code
Semantic
Implement
ation
Lexical
Analyser
Syntax
Analyser
Compilers 101
Source Code
Semantic
Implement
ation
Compilers 101 - Lexical Analysis
Objective:
○ Transform the input in a token sequence.
○ Categorize the tokens.
Example:
myData[value1] == "ABC"
Type: Identifier
Value: myData
Type: LBRACE
Value: [
Type: Identifier
Value: value1
Type: RBRACE
Value: ]
Type: Operator
Value: ==
Type: String
Value: "ABC"
Compilers 101 - Lexical Analysis
Objective:
○ Transform the input in a token sequence.
○ Categorize the tokens.
Example:
myData == "ABC"
Type: Identifier
Value: myData
Type: LBRACE
Value: [
Type: Identifier
Value: value1
Type: RBRACE
Value: ]
Type: Operator
Value: ==
Type: String
Value: "ABC"
Compilers 101 - Lexical Analysis
Objective:
○ Transform the input in a token sequence.
○ Categorize the tokens.
Example:
myData == "ABC"
Type: Identifier
Value: myData
Type: Operator
Value: ==
Type: String
Value: "ABC"
Compilers 101 - Syntax Analysis (parsing)
Objective:
○ Ensure that the input is a valid grammar
○ Generate a parse tree
Example (simplified):
==
"ABC"
myData
[value1]
Compilers 101 - Syntax Analysis (parsing)
Objective:
○ Ensure that the input is a valid grammar
○ Generate a parse tree
Example (simplified):
==
"ABC"
myData
[value1]
Compilers 101 - Semantic Implementation
Objective:
○ Effectively implements the behaviour of this
grammar.
○ We won't implement a full compiler here but we will do
implement the grammar
31
Defining the Grammar
● ANTLR (ANother Tool For Language Recognition)
is a Lexer + Parser written in Java.
○ Used by a lot of projects:
■ Apache Cassandra
■ Twitter's Search Engine
■ Presto
■ Apex (Salesforce Programming Language)
Workflow:
Defining the Grammar - ANTLR
Define the
Grammar
ANTLR
Implement
the grammar
● ANTLR (ANother Tool For Language Recognition)
is a Lexer + Parser written in Java.
○ Used by a lot of projects:
■ Apache Cassandra
■ Twitter's Search Engine
■ Presto
■ Apex (Salesforce Programming Language)
Workflow:
Defining the Grammar - ANTLR
Define the
Grammar
ANTLR
Implement
the grammar
Defining the Grammar - ANTLR
Lexical
Analyser
Syntax
Analyser
Source Code
Semantic
Implement
ation
ANTLR
Defining the Grammar - Objectives
What do we want to implement?
Complex Logical Expressions:
myData == "ABC" OR (myData2 > 2 AND myData3 < 10)
Switch/Case Expressions:
WHEN { myData == "ABC" -> "VALUE_1" ; myData2 > 2 ->
"VALUE_2"; else -> "VALUE_3" }
Defining the Grammar - Objectives
What do we want to implement?
Complex Logical Expressions:
myData == "ABC" OR (myData2 > 2 AND myData3 < 10)
Switch/Case Expressions:
WHEN { myData == "ABC" -> "VALUE_1" ; myData2 > 2 ->
"VALUE_2"; else -> "VALUE_3" }
Defining the Grammar - Objectives
What do we want to implement?
Complex Logical Expressions:
myData == "ABC" OR (myData2 > 2 AND myData3 < 10)
Switch/Case Expressions:
WHEN {
myData == "ABC" -> "VALUE_1";
myData2 > 2 -> "VALUE_2";
else -> "VALUE_3"
}
Defining the Grammar - Tokens
Defining the Tokens ("keywords"):
○ Logical Comparators: AND,OR
○ Comparators: ==, <, >, >=, <=
○ "Types": INT, String,var, true,
false
○ Keywords: WHEN, ->, else
○ Others: {, }, ;
Defining the Grammar - Tokens
Defining the Grammar - Rules
● Define the Rules is the single most important
step on creating a new language
● You MUST avoid ambiguity!
● You MUST avoid infinite recursion!
● It MUST be general!
Defining the Grammar - Rules
● Define the Rules is the single most important
step on creating a new language
● You MUST avoid ambiguity!
● You MUST avoid infinite recursion!
● It MUST be general!
Defining the Grammar - Rules
● Define the Rules is the single most important
step on creating a new language
● You MUST avoid ambiguity!
● You MUST avoid infinite recursion!
● It MUST be general!
Defining the Grammar - Rules (Good News)
● Good News:
○ We don't need to create crazy things here
○ You have A LOT, really A LOT of ANTLR
Grammars :
■ https://github.com/antlr/grammars-v4
Defining the Grammar - WHEN
Remember:
WHEN { myData == "ABC" -> "A"; myData == "CB" OR myData2 > 2 -> "B"; else
-> "C"}
Examples:
myData myData2 Output
"ABC" - "A"
"CB" - "B"
(except "CB"|"ABC") 3 "B"
(except "CB"|"ABC") 2 "C"
Defining the Grammar - WHEN
Remember:
WHEN { myData == "ABC" -> "A"; myData == "CB" OR myData2 > 2 -> "B"; else
-> "C"}
Examples:
myData myData2 Output
"ABC" - "A"
"CB" - "B"
(except "CB"|"ABC") 3 "B"
(except "CB"|"ABC") 2 "C"
Defining the Grammar - WHEN
Defining the Grammar - Logical Expressions
Remember:
myData == "ABC" OR (myData2 > 2 AND myData3 < 10)
Examples:
myData myData2 myData3 Output
"ABC" - - true
(except "ABC") 2 - false
(except "ABC") 3 11 false
(except "CB"|"ABC") 3 7 true
Defining the Grammar - Logical Expressions
Remember:
myData == "ABC" OR (myData2 > 2 AND myData3 < 10)
Examples:
myData myData2 myData3 Output
"ABC" - - true
(except "ABC") 2 - false
(except "ABC") 3 11 false
(except "CB"|"ABC") 3 7 true
Defining the Grammar - Compare Expressions
50
Implementing the Grammar
Implementing the Grammar - ANTLR
● ANTLR is able to generate two kinds of APIs to implement
the grammar:
○ Listener: The default one, based on Java JDOM API
○ Visitor: Based on Visitor Pattern
Implementing the Grammar - Visitor
We will use the Visitor API.
Motivation:
● It is less stateful. One must return the value.
● You can control the visitation:
○ Short-circuit the OR/AND
○ "Lazy" When
Implementing the Grammar - Visitor
We will use the Visitor API.
Motivation:
● It is less stateful. One must return the value.
● You can control the visitation:
○ Short-circuit the OR/AND
○ "Lazy" When
Implementing the Grammar - Visitor
We will use the Visitor API.
Motivation:
● It is less stateful. One must return the value.
● You can control the visit of the nodes:
○ Short-circuit the OR/AND
○ "Lazy" When
Implementing the Grammar - Visitor
We will use the Visitor API.
Motivation:
● It is less stateful. One must return the value.
● You can control the visit of the nodes:
○ Short-circuit the OR/AND
○ "Lazy" When
Implementing the Grammar - Visitor
We will use the Visitor API.
Motivation:
● It is less stateful. One must return the value.
● You can control the visit of the nodes:
○ Short-circuit the OR/AND
○ "Lazy" When
Implementing the Grammar - Visitor
58
Possible Follow Ups
Possible Follow Ups
This grammar solved our problem!
We are now able to take complex decisions dynamically!
However there are a lot room for improvements:
● Implement all the basic arithmetic operations (+,-,/,*)
● Error Handling:
○ What should we do when the variable is NaN?
○ What should we do when the variable does now exist?
Possible Follow Ups
This grammar solved our problem!
We are now able to take complex decisions dynamically!
However there are a lot room for improvements:
● Implement all the basic arithmetic operations (+,-,/,*)
● Error Handling:
○ What should we do when the variable is NaN?
○ What should we do when the variable does now exist?
Possible Follow Ups
This grammar solved our problem!
We are now able to take complex decisions dynamically!
However there are a lot room for improvements:
● Implement all the basic arithmetic operations (+,-,/,*)
● Error Handling:
○ What should we do when the variable is NaN?
○ What should we do when the variable does now exist?
Possible Follow Ups
This grammar solved our problem!
We are now able to take complex decisions dynamically!
However there are a lot room for improvements:
● Implement all the basic arithmetic operations (+,-,/,*)
● Error Handling:
○ What should we do when the variable is NaN?
○ What should we do when the variable does not exist?
Nice to Wavy you!
Questions?
luciano.sabença@wavy.global
We Are Hiring!
Use the QR Code for more information about it ;)

Más contenido relacionado

La actualidad más candente

SSP Quality Assurance Process - Translation & Voiceover
SSP Quality Assurance Process - Translation & VoiceoverSSP Quality Assurance Process - Translation & Voiceover
SSP Quality Assurance Process - Translation & Voiceover
Protelo Studios
 

La actualidad más candente (8)

Putting the D&D in TDD
Putting the D&D in TDDPutting the D&D in TDD
Putting the D&D in TDD
 
ANTLR4 in depth
ANTLR4 in depthANTLR4 in depth
ANTLR4 in depth
 
Localization_PROCESS
Localization_PROCESSLocalization_PROCESS
Localization_PROCESS
 
Putting the D&D in TDD
Putting the D&D in TDDPutting the D&D in TDD
Putting the D&D in TDD
 
Managing Technical Debt
Managing Technical DebtManaging Technical Debt
Managing Technical Debt
 
Typed Drupal - A great combination of Drupal 8 and PHP 7
Typed Drupal - A great combination of Drupal 8 and PHP 7Typed Drupal - A great combination of Drupal 8 and PHP 7
Typed Drupal - A great combination of Drupal 8 and PHP 7
 
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
 
SSP Quality Assurance Process - Translation & Voiceover
SSP Quality Assurance Process - Translation & VoiceoverSSP Quality Assurance Process - Translation & Voiceover
SSP Quality Assurance Process - Translation & Voiceover
 

Similar a TDC 2020 - Implementing a Mini-Language

Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
Peter Kofler
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
Iván Montes
 
Top Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowTop Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To Know
Kathy Brown
 

Similar a TDC 2020 - Implementing a Mini-Language (20)

Python overview
Python overviewPython overview
Python overview
 
Go/Ruby/Java: What's next?
Go/Ruby/Java: What's next?Go/Ruby/Java: What's next?
Go/Ruby/Java: What's next?
 
Quality code 2019
Quality code 2019Quality code 2019
Quality code 2019
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
 
Dear compiler please don't be my nanny v2
Dear compiler  please don't be my nanny v2Dear compiler  please don't be my nanny v2
Dear compiler please don't be my nanny v2
 
The Ring programming language version 1.7 book - Part 89 of 196
The Ring programming language version 1.7 book - Part 89 of 196The Ring programming language version 1.7 book - Part 89 of 196
The Ring programming language version 1.7 book - Part 89 of 196
 
The Ring programming language version 1.5.3 book - Part 186 of 194
The Ring programming language version 1.5.3 book - Part 186 of 194The Ring programming language version 1.5.3 book - Part 186 of 194
The Ring programming language version 1.5.3 book - Part 186 of 194
 
Beyond the Hype: 4 Years of Go in Production
Beyond the Hype: 4 Years of Go in ProductionBeyond the Hype: 4 Years of Go in Production
Beyond the Hype: 4 Years of Go in Production
 
C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem
 
Translating Qt Applications
Translating Qt ApplicationsTranslating Qt Applications
Translating Qt Applications
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll build
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Learning to code in 2020
Learning to code in 2020Learning to code in 2020
Learning to code in 2020
 
What Is Coding And Why Should You Learn It?
What Is Coding And Why Should You Learn It?What Is Coding And Why Should You Learn It?
What Is Coding And Why Should You Learn It?
 
Cloud AI GenAI Overview.pptx
Cloud AI GenAI Overview.pptxCloud AI GenAI Overview.pptx
Cloud AI GenAI Overview.pptx
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
 
2024.04 - AI in Code Generation - April User Group Meeting
2024.04 - AI in Code Generation - April User Group Meeting2024.04 - AI in Code Generation - April User Group Meeting
2024.04 - AI in Code Generation - April User Group Meeting
 
Top Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To KnowTop Tips Every Notes Developer Needs To Know
Top Tips Every Notes Developer Needs To Know
 

Último

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Último (20)

The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 

TDC 2020 - Implementing a Mini-Language

  • 1. Implementing a Mini Programming Language: From Zero to Production Luciano Sabença Lead Software Engineer - Wavy August/2020
  • 2. ABOUT ME ● Lead Software Engineer at Wavy Global/Movile ● RedisConf Speaker (2018) / Redis Day London (2019) ● Interested in: Distributed System, Computer Theory and Machine Learning. ● BS degree with praise in Computer Science by University of Campinas ● @lucianosabenca Luciano Sabença
  • 3. ● About Wavy ● Why did we need another lang? ● Compilers 101 ● Defining the Grammar ● Implementing the Grammar ● Possible Follow ups AGENDA
  • 4. Disclaimers ● I'm not a compiler engineer ● I'm not an expert in: ○ Formal Languages ○ Lexers/Parsers ● This talk will be a little bit dense: ○ I promise I will do my best ○ Code is available at: https://github.com/luciano-sabenca/TDC-2020-Implementing-a-mini-language
  • 5.
  • 7. Why did we need another lang? ● Create good conversations are hard! ○ You really need to take dynamic decisions based on the conversation messages. Let's take an example:
  • 8. Why did we need another lang? ● Create good conversations are hard! ○ You really need to take dynamic decisions based on the conversation messages. Let's take an example:
  • 9. Why did we need another lang? 100852 From 0 to 10, what's your opinion about our last contact? 100852 From 0 to 10, what's your opinion about our last contact? 8 100852 From 0 to 10, what's your opinion about our last contact? 8 We're glad that you enjoy our contact! Please free feel to contact us anytime
  • 10. Why did we need another lang? 100852 From 0 to 10, what's your opinion about our last contact? 100852 From 0 to 10, what's your opinion about our last contact? 8 100852 From 0 to 10, what's your opinion about our last contact? 8 We're glad that you enjoy our contact! Please free feel to contact us anytime
  • 11. Why did we need another lang? 100852 From 0 to 10, what's your opinion about our last contact? 100852 From 0 to 10, what's your opinion about our last contact? 8 100852 From 0 to 10, what's your opinion about our last contact? 8 We're glad that you enjoy our contact! Please free feel to contact us anytime
  • 12. Why did we need another lang? 100852 From 0 to 10, what's your opinion about our last contact? 100852 From 0 to 10, what's your opinion about our last contact? 5 100852 From 0 to 10, what's your opinion about our last contact? 5 We're sorry for you experience. Please could you share with us your thoughts about: 100852 5 We're sorry for you experience. Please could you share with us your thoughts about: ...
  • 13. Why did we need another lang? 100852 From 0 to 10, what's your opinion about our last contact? 100852 From 0 to 10, what's your opinion about our last contact? 5 100852 From 0 to 10, what's your opinion about our last contact? 5 We're sorry for you experience. Please could you share with us your thoughts about: 100852 5 We're sorry for you experience. Please could you share with us your thoughts about: ...
  • 14. Why did we need another lang? 100852 From 0 to 10, what's your opinion about our last contact? 100852 From 0 to 10, what's your opinion about our last contact? 5 100852 From 0 to 10, what's your opinion about our last contact? 5 We're sorry for you experience. Please could you share with us your thoughts about: 100852 5 We're sorry for you experience. Please could you share with us your thoughts about: ...
  • 15. Why did we need another lang? 100852 From 0 to 10, what's your opinion about our last contact? 100852 From 0 to 10, what's your opinion about our last contact? 5 100852 From 0 to 10, what's your opinion about our last contact? 5 We're sorry for you experience. Please could you share with us your thoughts about: 100852 5 We're sorry for you experience. Please could you share with us your thoughts about: ...
  • 16. Why did we need another lang? ● It's impossible to predict ALL the possible conversations. ● We need to be prepared to take complex decisions during the conversation! We need flexibility!
  • 17. Embedding a full language? ○ It's possible, but it's too much power! It may cause: ■ Security bugs ■ Performance problems (e.g. infinite loop, expensive operations, recursions…) ■ Annoying type conversions Why did we need another lang?
  • 18. Create a mini-language? ○ Pros: ■ We can easily limit what the user can do ■ We can design the language to solve a specific problem. ○ Cons: ■ It's a little bit challenger Let's do it! Why did we need another lang?
  • 19. Create a mini-language? ○ Pros: ■ We can easily limit what the user can do ■ We can design the language to solve a specific problem. ○ Cons: ■ It's a little bit challenger Let's do it! Why did we need another lang?
  • 25. Compilers 101 - Lexical Analysis Objective: ○ Transform the input in a token sequence. ○ Categorize the tokens. Example: myData[value1] == "ABC" Type: Identifier Value: myData Type: LBRACE Value: [ Type: Identifier Value: value1 Type: RBRACE Value: ] Type: Operator Value: == Type: String Value: "ABC"
  • 26. Compilers 101 - Lexical Analysis Objective: ○ Transform the input in a token sequence. ○ Categorize the tokens. Example: myData == "ABC" Type: Identifier Value: myData Type: LBRACE Value: [ Type: Identifier Value: value1 Type: RBRACE Value: ] Type: Operator Value: == Type: String Value: "ABC"
  • 27. Compilers 101 - Lexical Analysis Objective: ○ Transform the input in a token sequence. ○ Categorize the tokens. Example: myData == "ABC" Type: Identifier Value: myData Type: Operator Value: == Type: String Value: "ABC"
  • 28. Compilers 101 - Syntax Analysis (parsing) Objective: ○ Ensure that the input is a valid grammar ○ Generate a parse tree Example (simplified): == "ABC" myData [value1]
  • 29. Compilers 101 - Syntax Analysis (parsing) Objective: ○ Ensure that the input is a valid grammar ○ Generate a parse tree Example (simplified): == "ABC" myData [value1]
  • 30. Compilers 101 - Semantic Implementation Objective: ○ Effectively implements the behaviour of this grammar. ○ We won't implement a full compiler here but we will do implement the grammar
  • 32. ● ANTLR (ANother Tool For Language Recognition) is a Lexer + Parser written in Java. ○ Used by a lot of projects: ■ Apache Cassandra ■ Twitter's Search Engine ■ Presto ■ Apex (Salesforce Programming Language) Workflow: Defining the Grammar - ANTLR Define the Grammar ANTLR Implement the grammar
  • 33. ● ANTLR (ANother Tool For Language Recognition) is a Lexer + Parser written in Java. ○ Used by a lot of projects: ■ Apache Cassandra ■ Twitter's Search Engine ■ Presto ■ Apex (Salesforce Programming Language) Workflow: Defining the Grammar - ANTLR Define the Grammar ANTLR Implement the grammar
  • 34. Defining the Grammar - ANTLR Lexical Analyser Syntax Analyser Source Code Semantic Implement ation ANTLR
  • 35. Defining the Grammar - Objectives What do we want to implement? Complex Logical Expressions: myData == "ABC" OR (myData2 > 2 AND myData3 < 10) Switch/Case Expressions: WHEN { myData == "ABC" -> "VALUE_1" ; myData2 > 2 -> "VALUE_2"; else -> "VALUE_3" }
  • 36. Defining the Grammar - Objectives What do we want to implement? Complex Logical Expressions: myData == "ABC" OR (myData2 > 2 AND myData3 < 10) Switch/Case Expressions: WHEN { myData == "ABC" -> "VALUE_1" ; myData2 > 2 -> "VALUE_2"; else -> "VALUE_3" }
  • 37. Defining the Grammar - Objectives What do we want to implement? Complex Logical Expressions: myData == "ABC" OR (myData2 > 2 AND myData3 < 10) Switch/Case Expressions: WHEN { myData == "ABC" -> "VALUE_1"; myData2 > 2 -> "VALUE_2"; else -> "VALUE_3" }
  • 38. Defining the Grammar - Tokens Defining the Tokens ("keywords"): ○ Logical Comparators: AND,OR ○ Comparators: ==, <, >, >=, <= ○ "Types": INT, String,var, true, false ○ Keywords: WHEN, ->, else ○ Others: {, }, ;
  • 40. Defining the Grammar - Rules ● Define the Rules is the single most important step on creating a new language ● You MUST avoid ambiguity! ● You MUST avoid infinite recursion! ● It MUST be general!
  • 41. Defining the Grammar - Rules ● Define the Rules is the single most important step on creating a new language ● You MUST avoid ambiguity! ● You MUST avoid infinite recursion! ● It MUST be general!
  • 42. Defining the Grammar - Rules ● Define the Rules is the single most important step on creating a new language ● You MUST avoid ambiguity! ● You MUST avoid infinite recursion! ● It MUST be general!
  • 43. Defining the Grammar - Rules (Good News) ● Good News: ○ We don't need to create crazy things here ○ You have A LOT, really A LOT of ANTLR Grammars : ■ https://github.com/antlr/grammars-v4
  • 44. Defining the Grammar - WHEN Remember: WHEN { myData == "ABC" -> "A"; myData == "CB" OR myData2 > 2 -> "B"; else -> "C"} Examples: myData myData2 Output "ABC" - "A" "CB" - "B" (except "CB"|"ABC") 3 "B" (except "CB"|"ABC") 2 "C"
  • 45. Defining the Grammar - WHEN Remember: WHEN { myData == "ABC" -> "A"; myData == "CB" OR myData2 > 2 -> "B"; else -> "C"} Examples: myData myData2 Output "ABC" - "A" "CB" - "B" (except "CB"|"ABC") 3 "B" (except "CB"|"ABC") 2 "C"
  • 47. Defining the Grammar - Logical Expressions Remember: myData == "ABC" OR (myData2 > 2 AND myData3 < 10) Examples: myData myData2 myData3 Output "ABC" - - true (except "ABC") 2 - false (except "ABC") 3 11 false (except "CB"|"ABC") 3 7 true
  • 48. Defining the Grammar - Logical Expressions Remember: myData == "ABC" OR (myData2 > 2 AND myData3 < 10) Examples: myData myData2 myData3 Output "ABC" - - true (except "ABC") 2 - false (except "ABC") 3 11 false (except "CB"|"ABC") 3 7 true
  • 49. Defining the Grammar - Compare Expressions
  • 51. Implementing the Grammar - ANTLR ● ANTLR is able to generate two kinds of APIs to implement the grammar: ○ Listener: The default one, based on Java JDOM API ○ Visitor: Based on Visitor Pattern
  • 52. Implementing the Grammar - Visitor We will use the Visitor API. Motivation: ● It is less stateful. One must return the value. ● You can control the visitation: ○ Short-circuit the OR/AND ○ "Lazy" When
  • 53. Implementing the Grammar - Visitor We will use the Visitor API. Motivation: ● It is less stateful. One must return the value. ● You can control the visitation: ○ Short-circuit the OR/AND ○ "Lazy" When
  • 54. Implementing the Grammar - Visitor We will use the Visitor API. Motivation: ● It is less stateful. One must return the value. ● You can control the visit of the nodes: ○ Short-circuit the OR/AND ○ "Lazy" When
  • 55. Implementing the Grammar - Visitor We will use the Visitor API. Motivation: ● It is less stateful. One must return the value. ● You can control the visit of the nodes: ○ Short-circuit the OR/AND ○ "Lazy" When
  • 56. Implementing the Grammar - Visitor We will use the Visitor API. Motivation: ● It is less stateful. One must return the value. ● You can control the visit of the nodes: ○ Short-circuit the OR/AND ○ "Lazy" When
  • 59. Possible Follow Ups This grammar solved our problem! We are now able to take complex decisions dynamically! However there are a lot room for improvements: ● Implement all the basic arithmetic operations (+,-,/,*) ● Error Handling: ○ What should we do when the variable is NaN? ○ What should we do when the variable does now exist?
  • 60. Possible Follow Ups This grammar solved our problem! We are now able to take complex decisions dynamically! However there are a lot room for improvements: ● Implement all the basic arithmetic operations (+,-,/,*) ● Error Handling: ○ What should we do when the variable is NaN? ○ What should we do when the variable does now exist?
  • 61. Possible Follow Ups This grammar solved our problem! We are now able to take complex decisions dynamically! However there are a lot room for improvements: ● Implement all the basic arithmetic operations (+,-,/,*) ● Error Handling: ○ What should we do when the variable is NaN? ○ What should we do when the variable does now exist?
  • 62. Possible Follow Ups This grammar solved our problem! We are now able to take complex decisions dynamically! However there are a lot room for improvements: ● Implement all the basic arithmetic operations (+,-,/,*) ● Error Handling: ○ What should we do when the variable is NaN? ○ What should we do when the variable does not exist?
  • 63. Nice to Wavy you! Questions? luciano.sabença@wavy.global We Are Hiring! Use the QR Code for more information about it ;)