SlideShare a Scribd company logo
31.08 – 01.09.2018.
OSIJEK
Big thanks to:
ORGANIZERS PARTNERS
COMMUNITY SUPPORTERS MEDIA PARTNERS
Designing and implementing DSLs
in F#
Grammar vocabulary
Associativity
1+2+3 = 1+(2+3) = (1+2)+3 Addition is both left or right associative
1*2*3 = 1*(2*3) = (1*2)*3 Multiplication too
1-2-3 ≠ 1–(2-3) = (1-2)-3 Subtraction is left associative
1/2/3 ≠ 1/(2/3) = (1/2)/3 Division is left associative
Grammar vocabulary
Precedence
1+2*3/4-5 = (1+((2*3)/4))-5
Grammar vocabulary
Backus-Naur form (BNF)
Named after John Backus and Peter Naur
::= : definition
| : choice operator
<abc>: non-terminal symbol
ABC: terminal symbol
Related and useful:
Kleene star – e* - zero or many
Grammar vocabulary
Shift/reduce conflicts
Reduce/reduce conflicts
"Dangling else" problem
Exists when "else" is optional
if e1 then if e2 then e3 else e4 ->
if e1 then (if e2 then e3) else e4 or
if e1 then (if e2 then e3 else e4)
Grammar vocabulary
Internal vs External
Internal
via Workflows/Computation expressions
via custom operators/combinators
via augmentations
Excellent resource: dungpa's DSL cheatsheet
https://github.com/dungpa/dsls-in-action-
fsharp/blob/master/DSLCheatsheet.md
Grammar
<term> ::=
| <factor> * <term>
| <factor> / <term>
| <factor>
<e> ::=
| <term> + <expr>
| <term> – <expr>
| <term>
<factor> ::=
| i // 12
| f() // foo()
| f(<e>(, <e>)*) // foo(arg1, …, argn)
| v // x
| (<e>) // (1+2)
foo(x, (1+2)*3/4)
Abstract syntax trees
type var = string
type Expr =
| Num of float
| Add of Expr * Expr
| Sub of Expr * Expr
| Mul of Expr * Expr
| Div of Expr * Expr
| Var of var
| Call of var * Expr list
Parsing
• Active patterns
• FsYacc
Parsing with active patterns
let matchToken patt s =
Regex.Match(s, sprintf "A(%s)((?s).*)" patt, RegexOptions.Multiline)
|> fun m ->
if m.Success then
Some (m.Groups.[1].Value, m.Groups.[2].Value)
else
None
// val matchToken: string -> string -> (string * string) option
Parsing with active patterns
let (|WS|_|) = matchToken "[ |t|n|rn]+"
let (|COMMENT|_|) = matchToken "#.*[n|rn]"
let (|WHITESPACE|_|) s =
match s with
| WS rest -> Some rest
| COMMENT rest -> Some rest
| _ -> None
Parsing with active patterns
let rec (|Star|_|) f acc s =
match f s with
| Some (res, rest) ->
(|Star|_|) f (res :: acc) rest
| None ->
Some (List.rev acc, s)
Parsing with active patterns
… Demo …
Parsing with active patterns - limitations
• Right associative by default
• Left associativity is tricky
• Need to refactor grammar rules -> gets pretty complicated
• A ::= A B | A C | D | E
• A ::= (D | E) (B | C)*
• Otherwise end up with 1/2/3/4/5 -> 1/(2/(3/(4/5)))
FsLex+FsYacc
Explicit precedences
Explicit associativity
A more powerful class of grammars (LALR(k) vs LL(k))
FsYacc - lists
expr_list_rev:
| expr {
[$1]
}
| expr_list_rev COMMA expr {
$3 :: $1
}
FsYacc – optional values
expr_list_opt:
| {
[]
}
| expr_list {
$1
}
FsYacc - lists
expr_list:
| expr_list_rev {
List.rev $1
}
FsLex + FsYacc
… Demo …
Thanks for listening! Questions?
Get in touch
Adam Granicz
@granicz
Where are the instructions?
https://aka.ms/ms-docs
 Docs.microsoft.com
https://aka.ms/intldocs
Do you like Open Source?
https://aka.ms/msossloc
in your language,
today!
VS Code
SQL on Linux
SQL Ops Studio
Team Explorer Everywhere
… and more!

More Related Content

What's hot

8.1 Relations And Functions
8.1 Relations And Functions8.1 Relations And Functions
8.1 Relations And Functions
Jessca Lundin
 
Function and relation
Function and relationFunction and relation
Function and relation
EfgelBolibol
 
Lect17
Lect17Lect17
Lect17
DrASSayyad
 
Unification grammar
Unification grammarUnification grammar
Unification grammar
Hywel Evans
 
Relations and functions remediation notes
Relations and functions remediation notesRelations and functions remediation notes
Relations and functions remediation notes
carolinevest77
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
Thabani Masoka
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
Vishwakarma Nutan Prakash
 
To determine if a relation is a function
To determine if a relation is a functionTo determine if a relation is a function
To determine if a relation is a function
Anthony Abidakun
 
Alg2 lesson 2.1
Alg2 lesson 2.1Alg2 lesson 2.1
Alg2 lesson 2.1
Carol Defreese
 
Set concepts
Set conceptsSet concepts
Relations & functions.pps
Relations  &  functions.ppsRelations  &  functions.pps
Relations & functions.pps
indu psthakur
 
Module 4 topic 3 2nd
Module 4 topic 3   2ndModule 4 topic 3   2nd
Module 4 topic 3 2nd
Annie cox
 
GENERAL MATHEMATICS Module 1: Review on Functions
GENERAL MATHEMATICS Module 1: Review on FunctionsGENERAL MATHEMATICS Module 1: Review on Functions
GENERAL MATHEMATICS Module 1: Review on Functions
Galina Panela
 
Relations and functions
Relations and functions Relations and functions
Relations and functions
Seyid Kadher
 
Relations, functions, and their graphs
Relations, functions, and their graphsRelations, functions, and their graphs
Relations, functions, and their graphs
Elkin Guillen
 
Storyboard math
Storyboard mathStoryboard math
Storyboard math
shandex
 
Alg2 lesson 2.1
Alg2 lesson 2.1Alg2 lesson 2.1
Alg2 lesson 2.1
Carol Defreese
 
Chapter i
Chapter iChapter i
PPt on Functions
PPt on FunctionsPPt on Functions
PPt on Functions
coolhanddav
 
Day 3 examples b u1f13
Day 3 examples b u1f13Day 3 examples b u1f13
Day 3 examples b u1f13
jchartiersjsd
 

What's hot (20)

8.1 Relations And Functions
8.1 Relations And Functions8.1 Relations And Functions
8.1 Relations And Functions
 
Function and relation
Function and relationFunction and relation
Function and relation
 
Lect17
Lect17Lect17
Lect17
 
Unification grammar
Unification grammarUnification grammar
Unification grammar
 
Relations and functions remediation notes
Relations and functions remediation notesRelations and functions remediation notes
Relations and functions remediation notes
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
 
To determine if a relation is a function
To determine if a relation is a functionTo determine if a relation is a function
To determine if a relation is a function
 
Alg2 lesson 2.1
Alg2 lesson 2.1Alg2 lesson 2.1
Alg2 lesson 2.1
 
Set concepts
Set conceptsSet concepts
Set concepts
 
Relations & functions.pps
Relations  &  functions.ppsRelations  &  functions.pps
Relations & functions.pps
 
Module 4 topic 3 2nd
Module 4 topic 3   2ndModule 4 topic 3   2nd
Module 4 topic 3 2nd
 
GENERAL MATHEMATICS Module 1: Review on Functions
GENERAL MATHEMATICS Module 1: Review on FunctionsGENERAL MATHEMATICS Module 1: Review on Functions
GENERAL MATHEMATICS Module 1: Review on Functions
 
Relations and functions
Relations and functions Relations and functions
Relations and functions
 
Relations, functions, and their graphs
Relations, functions, and their graphsRelations, functions, and their graphs
Relations, functions, and their graphs
 
Storyboard math
Storyboard mathStoryboard math
Storyboard math
 
Alg2 lesson 2.1
Alg2 lesson 2.1Alg2 lesson 2.1
Alg2 lesson 2.1
 
Chapter i
Chapter iChapter i
Chapter i
 
PPt on Functions
PPt on FunctionsPPt on Functions
PPt on Functions
 
Day 3 examples b u1f13
Day 3 examples b u1f13Day 3 examples b u1f13
Day 3 examples b u1f13
 

Similar to Designing and implementing DSLs in F# - Kulendayz 2018

Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
 
Mathematical Modeling With Maple
Mathematical Modeling With MapleMathematical Modeling With Maple
Mathematical Modeling With Maple
Vaitheeswaran Gnanaraj
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
Chu An
 
Functions
FunctionsFunctions
Functions
Shaun Wilson
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
Luca Molteni
 
Order of operations
Order of operationsOrder of operations
Order of operations
formatic20
 
Computer algebra-system-maple
Computer algebra-system-mapleComputer algebra-system-maple
Computer algebra-system-maple
Азат Ажибеков
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
Yogendra Chaubey
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
Eelco Visser
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
Software Guru
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
Dmitri Nesteruk
 
Applied numerical methods lec2
Applied numerical methods lec2Applied numerical methods lec2
Applied numerical methods lec2
Yasser Ahmed
 
evaluating algebraic expression
evaluating algebraic expressionevaluating algebraic expression
evaluating algebraic expression
shahanieduardo
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
aboma2hawi
 
Distributive property ppt
Distributive property pptDistributive property ppt
Distributive property pptnglaze10
 
Tool mathematics l4
Tool mathematics l4Tool mathematics l4
Tool mathematics l4
MavenPolymath
 
R lecture oga
R lecture ogaR lecture oga
R lecture oga
Osamu Ogasawara
 
Apendice calculadora fx 115 es
Apendice calculadora fx 115 esApendice calculadora fx 115 es
Apendice calculadora fx 115 es
TORNARSOL
 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
 
Puppet Language 4.0 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014Puppet Language 4.0 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014
Puppet
 

Similar to Designing and implementing DSLs in F# - Kulendayz 2018 (20)

Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
Mathematical Modeling With Maple
Mathematical Modeling With MapleMathematical Modeling With Maple
Mathematical Modeling With Maple
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Functions
FunctionsFunctions
Functions
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
Order of operations
Order of operationsOrder of operations
Order of operations
 
Computer algebra-system-maple
Computer algebra-system-mapleComputer algebra-system-maple
Computer algebra-system-maple
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
 
Applied numerical methods lec2
Applied numerical methods lec2Applied numerical methods lec2
Applied numerical methods lec2
 
evaluating algebraic expression
evaluating algebraic expressionevaluating algebraic expression
evaluating algebraic expression
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Distributive property ppt
Distributive property pptDistributive property ppt
Distributive property ppt
 
Tool mathematics l4
Tool mathematics l4Tool mathematics l4
Tool mathematics l4
 
R lecture oga
R lecture ogaR lecture oga
R lecture oga
 
Apendice calculadora fx 115 es
Apendice calculadora fx 115 esApendice calculadora fx 115 es
Apendice calculadora fx 115 es
 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
 
Puppet Language 4.0 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014Puppet Language 4.0 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014
 

Recently uploaded

Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 

Recently uploaded (20)

Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 

Designing and implementing DSLs in F# - Kulendayz 2018

  • 2. Big thanks to: ORGANIZERS PARTNERS COMMUNITY SUPPORTERS MEDIA PARTNERS
  • 4. Grammar vocabulary Associativity 1+2+3 = 1+(2+3) = (1+2)+3 Addition is both left or right associative 1*2*3 = 1*(2*3) = (1*2)*3 Multiplication too 1-2-3 ≠ 1–(2-3) = (1-2)-3 Subtraction is left associative 1/2/3 ≠ 1/(2/3) = (1/2)/3 Division is left associative
  • 6. Grammar vocabulary Backus-Naur form (BNF) Named after John Backus and Peter Naur ::= : definition | : choice operator <abc>: non-terminal symbol ABC: terminal symbol Related and useful: Kleene star – e* - zero or many
  • 7. Grammar vocabulary Shift/reduce conflicts Reduce/reduce conflicts "Dangling else" problem Exists when "else" is optional if e1 then if e2 then e3 else e4 -> if e1 then (if e2 then e3) else e4 or if e1 then (if e2 then e3 else e4)
  • 8. Grammar vocabulary Internal vs External Internal via Workflows/Computation expressions via custom operators/combinators via augmentations Excellent resource: dungpa's DSL cheatsheet https://github.com/dungpa/dsls-in-action- fsharp/blob/master/DSLCheatsheet.md
  • 9. Grammar <term> ::= | <factor> * <term> | <factor> / <term> | <factor> <e> ::= | <term> + <expr> | <term> – <expr> | <term> <factor> ::= | i // 12 | f() // foo() | f(<e>(, <e>)*) // foo(arg1, …, argn) | v // x | (<e>) // (1+2) foo(x, (1+2)*3/4)
  • 10. Abstract syntax trees type var = string type Expr = | Num of float | Add of Expr * Expr | Sub of Expr * Expr | Mul of Expr * Expr | Div of Expr * Expr | Var of var | Call of var * Expr list
  • 12. Parsing with active patterns let matchToken patt s = Regex.Match(s, sprintf "A(%s)((?s).*)" patt, RegexOptions.Multiline) |> fun m -> if m.Success then Some (m.Groups.[1].Value, m.Groups.[2].Value) else None // val matchToken: string -> string -> (string * string) option
  • 13. Parsing with active patterns let (|WS|_|) = matchToken "[ |t|n|rn]+" let (|COMMENT|_|) = matchToken "#.*[n|rn]" let (|WHITESPACE|_|) s = match s with | WS rest -> Some rest | COMMENT rest -> Some rest | _ -> None
  • 14. Parsing with active patterns let rec (|Star|_|) f acc s = match f s with | Some (res, rest) -> (|Star|_|) f (res :: acc) rest | None -> Some (List.rev acc, s)
  • 15. Parsing with active patterns … Demo …
  • 16. Parsing with active patterns - limitations • Right associative by default • Left associativity is tricky • Need to refactor grammar rules -> gets pretty complicated • A ::= A B | A C | D | E • A ::= (D | E) (B | C)* • Otherwise end up with 1/2/3/4/5 -> 1/(2/(3/(4/5)))
  • 17. FsLex+FsYacc Explicit precedences Explicit associativity A more powerful class of grammars (LALR(k) vs LL(k))
  • 18. FsYacc - lists expr_list_rev: | expr { [$1] } | expr_list_rev COMMA expr { $3 :: $1 }
  • 19. FsYacc – optional values expr_list_opt: | { [] } | expr_list { $1 }
  • 20. FsYacc - lists expr_list: | expr_list_rev { List.rev $1 }
  • 21. FsLex + FsYacc … Demo …
  • 22. Thanks for listening! Questions? Get in touch Adam Granicz @granicz
  • 23. Where are the instructions? https://aka.ms/ms-docs  Docs.microsoft.com https://aka.ms/intldocs Do you like Open Source? https://aka.ms/msossloc in your language, today! VS Code SQL on Linux SQL Ops Studio Team Explorer Everywhere … and more!