2018-11-29 12:15
Page 1

Parsing

"(1+2)*3"

Page 2
Parsing
Page 3

Application

Imagine a simple calculator

Page 4

Show and Read

Page 5

Show and Read 2

Page 6

Grammars

Page 7

The use of grammars

Page 8

The purpose of a parser

A parser usually does two things

Page 9

Parser correctness

Page 10

The parsing problem

Page 11

Writing parsers in Haskell

Page 12

A first example

Page 13

Parsing libraries in Haskell

A parsing library proves

Page 14

Running a parser

Page 15

Exercise

Page 16

Building parsers

Page 17

Parsing sequences

Page 18

Rewriting our first parser (live demo)

Page 19

Rewriting our first parser

Page 20

A grammar for expressions

Page 21

A grammar for expressions, version 2

Page 22
A grammar for expressions, version 2

Problems

Page 23

A grammar for expressions, final version

Page 24

An expression parser

Page 25

Two useful parsing combinator

Factoring out the a pattern

Page 26

A few more combinator from the library

Page 27

A more elegant expression parser

Page 28

Looking inside the Parsing module

Page 29
Looking inside the Parsing module
Page 30
Looking inside the Parsing module
Page 31
Looking inside the Parsing module
Page 32
Looking inside the Parsing module
Page 33

What about supporting the do notation?

Page 34

Making a parsing monad

Page 35

Our parsing monad

instance Monad Parser where
  --return :: a -> Parser a
  return x = P (\ s -> Just (x,s))
  
  -- (>>=)  :: Parser a -> (a -> Parser b) -> Parser b
  P p >>= f = P (\ s -> case p s of
                          Nothing    -> Nothing
                          Just (a,r) -> parse (f a) r)
Page 36

About monads

Page 37

Summary