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

The parsing problem

Page 10

Writing parsers in Haskell

Page 11

A first example

Page 12

Parsing libraries in Haskell

There is some similarity between parsing and generating random test data

Page 13

Running a parser

Page 14

Exercise

Page 15

Building parsers

Page 16

Parsing sequences

Page 17

Rewriting our first parser (live demo)

Page 18

Rewriting our first parser

Page 19

A grammar for expressions

Page 20

A grammar for expressions, version 2

Page 21
A grammar for expressions, version 2

Problems

Page 22

A grammar for expressions, final version

Page 23

An expression parser

Page 24

Two useful parsing combinator

Factoring out the a pattern

Page 25

A few more combinator from the library

Page 26

A more elegant expression parser

Page 27

Looking inside the Parsing module

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

What about supporting the do notation?

Page 33

Making a parsing monad

Page 34

Our parsing monad

instance Monad Parser where
  return x = P (\ s -> Just (x,s))

  P p >>= f = P (\ s -> case p s of
                          Nothing    -> Nothing
                          Just (a,r) -> parse (f a) r)
Page 35

About monads

Page 36

Summary