SlideShare a Scribd company logo
1 of 23
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

Relations and functions remediation notes
Relations and functions remediation notesRelations and functions remediation notes
Relations and functions remediation notes
carolinevest77
 
Relations & functions.pps
Relations  &  functions.ppsRelations  &  functions.pps
Relations & functions.pps
indu psthakur
 
Storyboard math
Storyboard mathStoryboard math
Storyboard math
shandex
 
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

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
 
Distributive property ppt
Distributive property pptDistributive property ppt
Distributive property ppt
nglaze10
 
Apendice calculadora fx 115 es
Apendice calculadora fx 115 esApendice calculadora fx 115 es
Apendice calculadora fx 115 es
TORNARSOL
 

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

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
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
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
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
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
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-...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 

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!