Slide 1

Slide 1 text

Combinator Parsing By Swanand Pagnis

Slide 2

Slide 2 text

Higher-Order Functions for Parsing By Graham Hutton

Slide 3

Slide 3 text

• Abstract & Introduction • Build a parser, one fn at a time • Moving beyond toy parsers

Slide 4

Slide 4 text

Abstract

Slide 5

Slide 5 text

In combinator parsing, the text of parsers resembles BNF notation. We present the basic method, and a number of extensions. We address the special problems presented by whitespace, and parsers with separate lexical and syntactic phases. In particular, a combining form for handling the “offside rule” is given. Other extensions to the basic method include an “into” combining form with many useful applications, and a simple means by which combinator parsers can produce more informative error messages.

Slide 6

Slide 6 text

• Combinators that resemble BNF notation • Whitespace handling through "Offside Rule" • "Into" combining form for advanced parsing • Strategy for better error messages

Slide 7

Slide 7 text

Introduction

Slide 8

Slide 8 text

Primitive Parsers • Take input • Process one character • Return results and unused input

Slide 9

Slide 9 text

Combinators • Combine primitives • Define building blocks • Return results and unused input

Slide 10

Slide 10 text

Lexical analysis and syntax • Combine the combinators • Define lexical elements • Return results and unused input

Slide 11

Slide 11 text

input: "from:swiggy to:me" output: [("f", "rom:swiggy to:me")]

Slide 12

Slide 12 text

input: "42 !=> ans" output: [("4", "2 !=> ans")]

Slide 13

Slide 13 text

rule: 'a' followed by 'b' input: "abcdef" output: [(('a','b'),"cdef")]

Slide 14

Slide 14 text

rule: 'a' followed by 'b' input: "abcdef" output: [(('a','b'),"cdef")] Combinator

Slide 15

Slide 15 text

Language choice

Slide 16

Slide 16 text

Suggested: Lazy Functional Languages

Slide 17

Slide 17 text

Miranda: Author's choice

Slide 18

Slide 18 text

Haskell: An obvious choice.

Slide 19

Slide 19 text

Racket: Another obvious choice.

Slide 20

Slide 20 text

Ruby: to so $ for learning

Slide 21

Slide 21 text

OCaml: Functional, but not lazy.

Slide 22

Slide 22 text

Haskell %

Slide 23

Slide 23 text

Simple when stick to fundamental FP • Higher order functions • Immutability • Recursive problem solving • Algebraic types

Slide 24

Slide 24 text

Let's build a parser, one fn at a time

Slide 25

Slide 25 text

type Parser a b = [a] !-> [(b, [a])]

Slide 26

Slide 26 text

Types help with abstraction • We'll be dealing with parsers and combinators • Parsers are functions, they accept input and return results • Combinators accept parsers and return parsers

Slide 27

Slide 27 text

A parser is a function that accepts an input and returns parsed results and the unused input for each result

Slide 28

Slide 28 text

Parser is a function type that accepts a list of type a and returns all possible results as a list of tuples of type (b, [a])

Slide 29

Slide 29 text

(Parser Char Number) input: "42 it is!" !-- a is a [Char] output: [(42, " it is!")] !-- b is a Number

Slide 30

Slide 30 text

type Parser a b = [a] !-> [(b, [a])]

Slide 31

Slide 31 text

Primitive Parsers

Slide 32

Slide 32 text

succeed !:: b !-> Parser a b succeed v inp = [(v, inp)]

Slide 33

Slide 33 text

Always succeeds Returns "v" for all inputs

Slide 34

Slide 34 text

failure !:: Parser a b failure inp = []

Slide 35

Slide 35 text

Always fails Returns "[]" for all inputs

Slide 36

Slide 36 text

satisfy !:: (a !-> Bool) !-> Parser a a satisfy p [] = failure [] satisfy p (x:xs) | p x = succeed x xs !-- if p(x) is true | otherwise = failure []

Slide 37

Slide 37 text

satisfy !:: (a !-> Bool) !-> Parser a a satisfy p [] = failure [] satisfy p (x:xs) | p x = succeed x xs !-- if p(x) is true | otherwise = failure [] Guard Clauses, if you want to Google

Slide 38

Slide 38 text

literal !:: Eq a !=> a !-> Parser a a literal x = satisfy (!== x)

Slide 39

Slide 39 text

match_3 = (literal '3') match_3 "345" !-- !=> [('3',"45")] match_3 "456" !-- !=> []

Slide 40

Slide 40 text

succeed failure satisfy literal

Slide 41

Slide 41 text

Combinators

Slide 42

Slide 42 text

match_3_or_4 = match_3 `alt` match_4 match_3_or_4 "345" !-- !=> [('3',"45")] match_3_or_4 "456" !-- !=> [('4',"56")]

Slide 43

Slide 43 text

alt !:: Parser a b !-> Parser a b !-> Parser a b (p1 `alt` p2) inp = p1 inp !++ p2 inp

Slide 44

Slide 44 text

(p1 `alt` p2) inp = p1 inp !++ p2 inp List concatenation

Slide 45

Slide 45 text

(match_3 `and_then` match_4) "345" # !=> [(('3','4'),"5")]

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

and_then !:: Parser a b !-> Parser a c !-> Parser a (b, c) (p1 `and_then` p2) inp = [ ((v1, v2), out2) | (v1, out1) !<- p1 inp, (v2, out2) !<- p2 out1 ]

Slide 48

Slide 48 text

and_then !:: Parser a b !-> Parser a c !-> Parser a (b, c) (p1 `and_then` p2) inp = [ ((v1, v2), out2) | (v1, out1) !<- p1 inp, (v2, out2) !<- p2 out1 ] List comprehensions

Slide 49

Slide 49 text

(v11, out11) (v12, out12) (v13, out13) … (v21, out21) (v22, out22) … (v31, out31) (v32, out32) … p1 p2

Slide 50

Slide 50 text

((v11, v21), out21) ((v11, v22), out22) …

Slide 51

Slide 51 text

(match_3 `and_then` match_4) "345" # !=> [(('3','4'),"5")]

Slide 52

Slide 52 text

Manipulating values

Slide 53

Slide 53 text

match_3 = (literal '3') match_3 "345" !-- !=> [('3',"45")] match_3 "456" !-- !=> []

Slide 54

Slide 54 text

(number "42") "42 !=> answer" # !=> [(42, " answer")]

Slide 55

Slide 55 text

(keyword "for") "for i in 1!..42" # !=> [(:for, " i in 1!..42")]

Slide 56

Slide 56 text

using !:: Parser a b !-> (b !-> c) !-> Parser a c (p `using` f) inp = [(f v, out) | (v, out) !<- p inp ]

Slide 57

Slide 57 text

((string "3") `using` float) "3" # !=> [(3.0, "")]

Slide 58

Slide 58 text

Levelling up

Slide 59

Slide 59 text

many !:: Parser a b !-> Parser a [b] many p = ((p `and_then` many p) `using` cons) `alt` (succeed [])

Slide 60

Slide 60 text

0 or many

Slide 61

Slide 61 text

(many (literal 'a')) "aab" !=> [("aa","b"),("a","ab"),("","aab")]

Slide 62

Slide 62 text

(many (literal 'a')) "xyz" !=> [("","xyz")]

Slide 63

Slide 63 text

some !:: Parser a b !-> Parser a [b] some p = ((p `and_then` many p) `using` cons)

Slide 64

Slide 64 text

1 or many

Slide 65

Slide 65 text

(some (literal 'a')) "aab" !=> [("aa","b"),("a","ab")]

Slide 66

Slide 66 text

(some (literal 'a')) "xyz" !=> []

Slide 67

Slide 67 text

positive_integer = some (satisfy Data.Char.isDigit) negative_integer = ((literal '-') `and_then` positive_integer) `using` cons positive_decimal = (positive_integer `and_then` (((literal '.') `and_then` positive_integer) `using` cons)) `using` join negative_decimal = ((literal '-') `and_then` positive_decimal) `using` cons

Slide 68

Slide 68 text

number !:: Parser Char [Char] number = negative_decimal `alt` positive_decimal `alt` negative_integer `alt` positive_integer

Slide 69

Slide 69 text

word !:: Parser Char [Char] word = some (satisfy isLetter)

Slide 70

Slide 70 text

string !:: (Eq a) !=> [a] !-> Parser a [a] string [] = succeed [] string (x:xs) = (literal x `and_then` string xs) `using` cons

Slide 71

Slide 71 text

(string "begin") "begin end" # !=> [("begin"," end")]

Slide 72

Slide 72 text

xthen !:: Parser a b !-> Parser a c !-> Parser a c p1 `xthen` p2 = (p1 `and_then` p2) `using` snd

Slide 73

Slide 73 text

thenx !:: Parser a b !-> Parser a c !-> Parser a b p1 `thenx` p2 = (p1 `and_then` p2) `using` fst

Slide 74

Slide 74 text

ret !:: Parser a b !-> c !-> Parser a c p `ret` v = p `using` (const v)

Slide 75

Slide 75 text

succeed, failure, satisfy, literal, alt, and_then, using, string, many, some, string, word, number, xthen, thenx, ret

Slide 76

Slide 76 text

Expression Parser & Evaluator

Slide 77

Slide 77 text

data Expr = Const Double | Expr `Add` Expr | Expr `Sub` Expr | Expr `Mul` Expr | Expr `Div` Expr

Slide 78

Slide 78 text

(Const 3) `Mul` ((Const 6) `Add` (Const 1))) # !=> "3*(6+1)"

Slide 79

Slide 79 text

parse "3*(6+1)" # !=> (Const 3) `Mul` ((Const 6) `Add` (Const 1)))

Slide 80

Slide 80 text

(Const 3) Mul ((Const 6) `Add` (Const 1))) # !=> 21

Slide 81

Slide 81 text

BNF Notation expn !::= expn + expn | expn − expn | expn ∗ expn | expn / expn | digit+ | (expn)

Slide 82

Slide 82 text

Improving a little: expn !::= term + term | term − term | term term !::= factor ∗ factor | factor / factor | factor factor !::= digit+ | (expn)

Slide 83

Slide 83 text

Parsers that resemble BNF

Slide 84

Slide 84 text

addition = ((term `and_then` ((literal '+') `xthen` term)) `using` plus)

Slide 85

Slide 85 text

subtraction = ((term `and_then` ((literal '-') `xthen` term)) `using` minus)

Slide 86

Slide 86 text

multiplication = ((factor `and_then` ((literal '*') `xthen` factor)) `using` times)

Slide 87

Slide 87 text

division = ((factor `and_then` ((literal '/') `xthen` factor)) `using` divide)

Slide 88

Slide 88 text

parenthesised_expression = ((nibble (literal '(')) `xthen` ((nibble expn) `thenx`(nibble (literal ')'))))

Slide 89

Slide 89 text

value xs = Const (numval xs) plus (x,y) = x `Add` y minus (x,y) = x `Sub` y times (x,y) = x `Mul` y divide (x,y) = x `Div` y

Slide 90

Slide 90 text

expn = addition `alt` subtraction `alt` term

Slide 91

Slide 91 text

term = multiplication `alt` division `alt` factor

Slide 92

Slide 92 text

factor = (number `using` value) `alt` parenthesised_expn

Slide 93

Slide 93 text

expn "12*(5+(7-2))" # !=> [ (Const 12.0 `Mul` (Const 5.0 `Add` (Const 7.0 `Sub` Const 2.0)),""), … ]

Slide 94

Slide 94 text

value xs = Const (numval xs) plus (x,y) = x `Add` y minus (x,y) = x `Sub` y times (x,y) = x `Mul` y divide (x,y) = x `Div` y

Slide 95

Slide 95 text

value = numval plus (x,y) = x + y minus (x,y) = x - y times (x,y) = x * y divide (x,y) = x / y

Slide 96

Slide 96 text

expn "12*(5+(7-2))" # !=> [(120.0,""), (12.0,"*(5+(7-2))"), (1.0,"2*(5+(7-2))")]

Slide 97

Slide 97 text

expn "(12+1)*(5+(7-2))" # !=> [(130.0,""), (13.0,"*(5+(7-2))")]

Slide 98

Slide 98 text

Moving beyond toy parsers

Slide 99

Slide 99 text

Whitespace? (

Slide 100

Slide 100 text

white = (literal " ") `alt` (literal "\t") `alt` (literal "\n")

Slide 101

Slide 101 text

white = many (any literal " \t\n")

Slide 102

Slide 102 text

/\s!*/

Slide 103

Slide 103 text

any p = foldr (alt.p) fail

Slide 104

Slide 104 text

any p [x1,x2,!!...,xn] = (p x1) `alt` (p x2) `alt` !!... `alt` (p xn)

Slide 105

Slide 105 text

white = many (any literal " \t\n")

Slide 106

Slide 106 text

nibble p = white `xthen` (p `thenx` white)

Slide 107

Slide 107 text

The parser (nibble p) has the same behaviour as parser p, except that it eats up any white- space in the input string before or afterwards

Slide 108

Slide 108 text

(nibble (literal 'a')) " a " # !=> [('a',""),('a'," "),('a'," ")]

Slide 109

Slide 109 text

symbol = nibble.string

Slide 110

Slide 110 text

symbol "$fold" " $fold " # !=> [("$fold", ""), ("$fold", " ")]

Slide 111

Slide 111 text

The Offside Rule

Slide 112

Slide 112 text

w = x + y where x = 10 y = 15 - 5 z = w * 2

Slide 113

Slide 113 text

w = x + y where x = 10 y = 15 - 5 z = w * 2

Slide 114

Slide 114 text

When obeying the offside rule, every token must lie either directly below, or to the right of its first token

Slide 115

Slide 115 text

i.e. A weak indentation policy

Slide 116

Slide 116 text

The Offside Combinator

Slide 117

Slide 117 text

type Pos a = (a, (Integer, Integer))

Slide 118

Slide 118 text

prelex "3 + \n 2 * (4 + 5)" # !=> [('3',(0,0)), ('+',(0,2)), ('2',(1,2)), ('*',(1,4)), … ]

Slide 119

Slide 119 text

satisfy !:: (a !-> Bool) !-> Parser a a satisfy p [] = failure [] satisfy p (x:xs) | p x = succeed x xs !-- if p(x) is true | otherwise = failure []

Slide 120

Slide 120 text

satisfy !:: (a !-> Bool) !-> Parser (Pos a) a satisfy p [] = failure [] satisfy p (x:xs) | p a = succeed a xs !-- if p(a) is true | otherwise = failure [] where (a, (r, c)) = x

Slide 121

Slide 121 text

satisfy !:: (a !-> Bool) !-> Parser (Pos a) a satisfy p [] = failure [] satisfy p (x:xs) | p a = succeed a xs !-- if p(a) is true | otherwise = failure [] where (a, (r, c)) = x

Slide 122

Slide 122 text

offside !:: Parser (Pos a) b !-> Parser (Pos a) b offside p inp = [(v, inpOFF) | (v, []) !<- (p inpON)] where inpON = takeWhile (onside (head inp)) inp inpOFF = drop (length inpON) inp onside (a, (r, c)) (b, (r', c')) = r' !>= r !&& c' !>= c

Slide 123

Slide 123 text

offside !:: Parser (Pos a) b !-> Parser (Pos a) b

Slide 124

Slide 124 text

offside !:: Parser (Pos a) b !-> Parser (Pos a) b offside p inp = [(v, inpOFF) | (v, []) !<- (p inpON)]

Slide 125

Slide 125 text

(nibble (literal 'a')) " a " # !=> [('a',""),('a'," "),('a'," ")]

Slide 126

Slide 126 text

offside !:: Parser (Pos a) b !-> Parser (Pos a) b offside p inp = [(v, inpOFF) | (v, []) !<- (p inpON)]

Slide 127

Slide 127 text

offside !:: Parser (Pos a) b !-> Parser (Pos a) b offside p inp = [(v, inpOFF) | (v, []) !<- (p inpON)] where inpON = takeWhile (onside (head inp)) inp

Slide 128

Slide 128 text

offside !:: Parser (Pos a) b !-> Parser (Pos a) b offside p inp = [(v, inpOFF) | (v, []) !<- (p inpON)] where inpON = takeWhile (onside (head inp)) inp inpOFF = drop (length inpON) inp

Slide 129

Slide 129 text

offside !:: Parser (Pos a) b !-> Parser (Pos a) b offside p inp = [(v, inpOFF) | (v, []) !<- (p inpON)] where inpON = takeWhile (onside (head inp)) inp inpOFF = drop (length inpON) inp onside (a, (r, c)) (b, (r', c')) = r' !>= r !&& c' !>= c

Slide 130

Slide 130 text

(3 + 2 * (4 + 5)) + (8 * 10) (3 + 2 * (4 + 5)) + (8 * 10)

Slide 131

Slide 131 text

(offside expn) (prelex inp_1) # !=> [(21.0,[('+',(2,0)),('(',(2,2)),('8',(2,3)),('*', (2,5)),('1',(2,7)),('0',(2,8)),(')',(2,9))])] (offside expn) (prelex inp_2) # !=> [(101.0,[])]

Slide 132

Slide 132 text

Quick recap before we

Slide 133

Slide 133 text

∅ !|> succeed, fail !|> satisfy, literal !|> alt, and_then, using !|> many, some !|> string, thenx, xthen, return !|> expression parser & evaluator !|> any, nibble, symbol !|> prelex, offside

Slide 134

Slide 134 text

Practical parsers

Slide 135

Slide 135 text

Syntactical analysis Lexical analysis Parse trees

Slide 136

Slide 136 text

type Parser a b = [a] !-> [(b, [a])] type Pos a = (a, (Integer, Integer))

Slide 137

Slide 137 text

data Tag = Ident | Number | Symbol | Junk deriving (Show, Eq) type Token = (Tag, [Char])

Slide 138

Slide 138 text

(Symbol, "if") (Number, "123")

Slide 139

Slide 139 text

Parse the string with parser p, & apply token t to the result

Slide 140

Slide 140 text

(p `tok` t) inp = [ (((t, xs), (r, c)), out) | (xs, out) !<- p inp] where (x, (r,c)) = head inp

Slide 141

Slide 141 text

(p `tok` t) inp = [ ((,),) | (xs, out) !<- p inp] where (x, (r,c)) = head inp

Slide 142

Slide 142 text

(p `tok` t) inp = [ (((t, xs), (r, c)), out) | (xs, out) !<- p inp] where (x, (r,c)) = head inp

Slide 143

Slide 143 text

((string "where") `tok` Symbol) inp # !=> ((Symbol,"where"), (r, c))

Slide 144

Slide 144 text

many ((p1 `tok` t1) `alt` (p2 `tok` t2) `alt` !!... `alt` (pn `tok` tn))

Slide 145

Slide 145 text

[(p1, t1), (p2, t2), …, (pn, tn)]

Slide 146

Slide 146 text

lex = many.(foldr op failure) where (p, t) `op` xs = (p `tok` t) `alt` xs

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

lex = many.(foldr op failure) where (p, t) `op` xs = (p `tok` t) `alt` xs

Slide 149

Slide 149 text

# Rightmost computation cn = (pn `tok` tn) `alt` failure

Slide 150

Slide 150 text

# Followed by (pn-1 `tok` tn-1) `alt` cn

Slide 151

Slide 151 text

many ((p1 `tok` t1) `alt` (p2 `tok` t2) `alt` !!... `alt` (pn `tok` tn))

Slide 152

Slide 152 text

lexer = lex [ ((some (any_of literal " \n\t")), Junk), ((string "where"), Symbol), (word, Ident), (number, Number), ((any_of string ["(", ")", "="]), Symbol)]

Slide 153

Slide 153 text

lexer = lex [ ((some (any_of literal " \n\t")), Junk), ((string "where"), Symbol), (word, Ident), (number, Number), ((any_of string ["(", ")", "="]), Symbol)]

Slide 154

Slide 154 text

lexer = lex [ ((some (any_of literal " \n\t")), Junk), ((string "where"), Symbol), (word, Ident), (number, Number), ((any_of string ["(", ")", "="]), Symbol)]

Slide 155

Slide 155 text

head (lexer (prelex "where x = 10")) # !=> ([((Symbol,"where"),(0,0)), ((Ident,"x"),(0,6)), ((Symbol,"="),(0,8)), ((Number,"10"),(0,10)) ],[])

Slide 156

Slide 156 text

(head.lexer.prelex) "where x = 10" # !=> ([((Symbol,"where"),(0,0)), ((Ident,"x"),(0,6)), ((Symbol,"="),(0,8)), ((Number,"10"),(0,10)) ],[])

Slide 157

Slide 157 text

(head.lexer.prelex) "where x = 10" # !=> ([((Symbol,"where"),(0,0)), ((Ident,"x"),(0,6)), ((Symbol,"="),(0,8)), ((Number,"10"),(0,10)) ],[]) Function composition

Slide 158

Slide 158 text

length ((lexer.prelex) "where x = 10") # !=> 198

Slide 159

Slide 159 text

Conflicts? Ambiguity?

Slide 160

Slide 160 text

In this case, "where" is a source of conflict. It can be a symbol, or identifier.

Slide 161

Slide 161 text

lexer = lex [ {- 1 -} ((some (any_of literal " \n\t")), Junk), {- 2 -} ((string "where"), Symbol), {- 3 -} (word, Ident), {- 4 -} (number, Number), {- 5 -} ((any_of string ["(",")","="]), Symbol)]

Slide 162

Slide 162 text

Higher priority, higher precedence

Slide 163

Slide 163 text

Removing Junk

Slide 164

Slide 164 text

strip !:: [(Pos Token)] !-> [(Pos Token)] strip = filter ((!!= Junk).fst.fst)

Slide 165

Slide 165 text

((!!= Junk).fst.fst) ((Symbol,"where"),(0,0)) # !=> True ((!!= Junk).fst.fst) ((Junk,"where"),(0,0)) # !=> False

Slide 166

Slide 166 text

(fst.head.lexer.prelex) "where x = 10" # !=> [((Symbol,"where"),(0,0)), ((Junk," "),(0,5)), ((Ident,"x"),(0,6)), ((Junk," "),(0,7)), ((Symbol,"="),(0,8)), ((Junk," "),(0,9)), ((Number,"10"),(0,10))]

Slide 167

Slide 167 text

(strip.fst.head.lexer.prelex) "where x = 10" # !=> [((Symbol,"where"),(0,0)), ((Ident,"x"),(0,6)), ((Symbol,"="),(0,8)), ((Number,"10"),(0,10))]

Slide 168

Slide 168 text

Syntax Analysis

Slide 169

Slide 169 text

characters !|> lexical analysis !|> tokens

Slide 170

Slide 170 text

tokens !|> syntax analysis !|> parse trees

Slide 171

Slide 171 text

f x y = add a b where a = 25 b = sub x y answer = mult (f 3 7) 5

Slide 172

Slide 172 text

f x y = add a b where a = 25 b = sub x y answer = mult (f 3 7) 5 Script

Slide 173

Slide 173 text

f x y = add a b where a = 25 b = sub x y answer = mult (f 3 7) 5 Definition

Slide 174

Slide 174 text

f x y = add a b where a = 25 b = sub x y answer = mult (f 3 7) 5 Body

Slide 175

Slide 175 text

f x y = add a b where a = 25 b = sub x y answer = mult (f 3 7) 5 Expression

Slide 176

Slide 176 text

f x y = add a b where a = 25 b = sub x y answer = mult (f 3 7) 5 Definition

Slide 177

Slide 177 text

f x y = add a b where a = 25 b = sub x y answer = mult (f 3 7) 5 Primitives

Slide 178

Slide 178 text

data Script = Script [Def] data Def = Def Var [Var] Expn data Expn = Var Var | Num Double | Expn `Apply` Expn | Expn `Where` [Def] type Var = [Char]

Slide 179

Slide 179 text

prog = (many defn) `using` Script

Slide 180

Slide 180 text

defn = ( (some (kind Ident)) `and_then` ((lit "=") `xthen` (offside body))) `using` defnFN

Slide 181

Slide 181 text

body = ( expr `and_then` (((lit "where") `xthen` (some defn)) `opt` [])) `using` bodyFN

Slide 182

Slide 182 text

expr = (some prim) `using` (foldl1 Apply)

Slide 183

Slide 183 text

prim = ((kind Ident) `using` Var) `alt` ((kind Number) `using` numFN) `alt` ((lit "(") `xthen` (expr `thenx` (lit ")")))

Slide 184

Slide 184 text

!-- only allow a kind of tag kind !:: Tag !-> Parser (Pos Token) [Char] kind t = (satisfy ((!== t).fst)) `using` snd — only allow a given symbol lit !:: [Char] !-> Parser (Pos Token) [Char] lit xs = (literal (Symbol, xs)) `using` snd

Slide 185

Slide 185 text

prog = (many defn) `using` Script

Slide 186

Slide 186 text

defn = ( (some (kind Ident)) `and_then` ((lit "=") `xthen` (offside body))) `using` defnFN

Slide 187

Slide 187 text

body = ( expr `and_then` (((lit "where") `xthen` (some defn)) `opt` [])) `using` bodyFN

Slide 188

Slide 188 text

expr = (some prim) `using` (foldl1 Apply)

Slide 189

Slide 189 text

prim = ((kind Ident) `using` Var) `alt` ((kind Number) `using` numFN) `alt` ((lit "(") `xthen` (expr `thenx` (lit ")")))

Slide 190

Slide 190 text

data Script = Script [Def] data Def = Def Var [Var] Expn data Expn = Var Var | Num Double | Expn `Apply` Expn | Expn `Where` [Def] type Var = [Char]

Slide 191

Slide 191 text

Orange functions are for transforming values.

Slide 192

Slide 192 text

Use data constructors to generate parse trees

Slide 193

Slide 193 text

Use evaluation functions to evaluate and generate a value

Slide 194

Slide 194 text

f x y = add a b where a = 25 b = sub x y answer = mult (f 3 7) 5

Slide 195

Slide 195 text

Script [ Def "f" ["x","y"] ( ((Var "add" `Apply` Var "a") `Apply` Var "b") `Where` [ Def "a" [] (Num 25.0), Def "b" [] ((Var "sub" `Apply` Var "x") `Apply` Var "y")]), Def "answer" [] ( (Var "mult" `Apply` ( (Var "f" `Apply` Num 3.0) `Apply` Num 7.0)) `Apply` Num 5.0)]

Slide 196

Slide 196 text

Strategy for writing parsers

Slide 197

Slide 197 text

1. Identify components i.e. Lexical elements

Slide 198

Slide 198 text

lexer = lex [ ((some (any_of literal " \n\t")), Junk), ((string "where"), Symbol), (word, Ident), (number, Number), ((any_of string ["(", ")", "="]), Symbol)]

Slide 199

Slide 199 text

2. Structure these elements a.k.a. syntax

Slide 200

Slide 200 text

defn = ((some (kind Ident)) `and_then` ((lit "=") `xthen` (offside body))) `using` defnFN body = (expr `and_then` (((lit "where") `xthen` (some defn)) `opt` [])) `using` bodyFN expr = (some prim) `using` (foldl1 Apply) prim = ((kind Ident) `using` Var) `alt` ((kind Number) `using` numFN) `alt` ((lit "(") `xthen` (expr `thenx` (lit ")")))

Slide 201

Slide 201 text

3. BNF notation is very helpful

Slide 202

Slide 202 text

4. TDD in the absence of types

Slide 203

Slide 203 text

Where to, next?

Slide 204

Slide 204 text

Monadic Parsers Graham Hutton, Eric Meijer

Slide 205

Slide 205 text

Introduction to FP Philip Wadler

Slide 206

Slide 206 text

The Dragon Book If your interest is in compilers

Slide 207

Slide 207 text

Libraries?

Slide 208

Slide 208 text

Haskell: Parsec, MegaParsec. ✨ OCaml: Angstrom. ✨ Ruby: rparsec, or roll you own Elixir: Combine, ExParsec Python: Parsec. ✨

Slide 209

Slide 209 text

Thank you!

Slide 210

Slide 210 text

Twitter: @_swanand GitHub: @swanandp