chomsky revisited - hh · chomsky revisited – or – everything you always wanted to know about...

64
1 × Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨ oh and Doaitse Swierstra Institute of Information and Computing Science Utrecht University e-mail: {arthurb,andres,doaitse}@cs.uu.nl May 6, 2002

Upload: others

Post on 25-Apr-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

1 JJ J I II J • ×

Chomsky Revisited

– or –

Everything you always wanted to know about

Parsing a Permutation

Arthur Baars, Andres Loh and Doaitse Swierstra

Institute of Information and Computing Science

Utrecht University

e-mail: {arthurb,andres,doaitse}@cs.uu.nl

May 6, 2002

Page 2: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

1 JJ J I II J • ×

The Chomsky Hierarchy

The Chomsky hierarchy:

I Regular

I Context-free

I Context-sensitive

I Recursively enumerable

Page 3: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

2 JJ J I II J • ×

Example 1: HTML

<img width=190 -- optional, integerheight=54 -- optional, integersrc="/icons/ics.gif" -- a stringalt="HOME" -- a string

>

Attributes: order is irrelevant, each occurs exactly once

Page 4: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

3 JJ J I II J • ×

Example 2: BibTEX

@inProceedings{ BaarsLoehSwierstra2001,

author = {Baars, Arthur and Loeh, Andresand Swierstra, S. Doaitse},

title = {Parsing Permutation Phrases},booktitle = {Preliminary proceedings of

Haskell workshop 2001,# techreport # UU-CS-2001-23},

year = 2001,pages = {171--182},editor = {Hinze, Ralf},

}

Fields: order is irrelevant, each occurs exactly once

Page 5: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

4 JJ J I II J • ×

Approach 1

Attributes ::=(Name ’=’ Value)∗

Step 1(context-free):

I Parse input as a sequence of assignments

Page 6: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

4 JJ J I II J • ×

Approach 1

Attributes ::=(Name ’=’ Value)∗

Step 1(context-free):

I Parse input as a sequence of assignments

Step 2(semantic action):

I Check for valid names

I Check whether each name occurs only once

I Check whether each value has the right type

Page 7: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

5 JJ J I II J • ×

Approach 2

Attributes ::=Field∗Field ::=Width | Height | Src | Alt

Width ::="width=" NumberHeight ::="height=" NumberSrc ::="src=" StringAlt ::="alt=" String

I Name and type checks moved to syntactic level

I Occurrence check still in second step

Page 8: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

6 JJ J I II J • ×

Approach 3

Attributes ::=Width Height Src Alt| Width Height Alt Src| Width Src Height Alt| Width Src Alt Height| Width Alt Height Src| Width Alt Src Height| Height Width Src Alt| Height Width Alt Src| Height Src Width Alt| Height Src Alt Width| Height Alt Width Src| Height Alt Src Width| Src Width Height Alt| Src Width Alt Height| Src Height Width Alt| . . .

Page 9: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

7 JJ J I II J • ×

EBNF grammars grow too large

BNF:

I alternative "|"

I composition " "

I terminals "’s’"

I empty string ε

Page 10: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

7 JJ J I II J • ×

EBNF grammars grow too large

BNF:

I alternative "|"

I composition " "

I terminals "’s’"

I empty string ε

EBNF:

I repetition "*" and "+"

I option "?"

I grouping "(" . . . ")"

Page 11: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

7 JJ J I II J • ×

EBNF grammars grow too large

BNF:

I alternative "|"

I composition " "

I terminals "’s’"

I empty string ε

EBNF:

I repetition "*" and "+"

I option "?"

I grouping "(" . . . ")"

EEBNF:

I permutations "||"

Page 12: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

7 JJ J I II J • ×

EBNF grammars grow too large

BNF:

I alternative "|"

I composition " "

I terminals "’s’"

I empty string ε

EBNF:

I repetition "*" and "+"

I option "?"

I grouping "(" . . . ")"

EEBNF:

I permutations "||"

. . .EEEEEEBNF:

Page 13: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

8 JJ J I II J • ×

What do we need?

I Formalism with basic operators

I Extension mechanism in the formalism

Page 14: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

8 JJ J I II J • ×

What do we need?

I Formalism with basic operators

I Extension mechanism in the formalism ⇐= Programming language

Page 15: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

8 JJ J I II J • ×

What do we need?

I Formalism with basic operators ⇐= Library, Package

I Extension mechanism in the formalism ⇐= Programming language

Page 16: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

9 JJ J I II J • ×

Combinator language

I Basic operators ”combine” entities from a formalism

I Extension mechanism is borrowed from a programming language

Page 17: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

9 JJ J I II J • ×

Combinator language

I Basic operators ”combine” entities from a ︸ ︷︷ ︸EBNF

formalism

I Extension mechanism is borrowed from a programming language

Page 18: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

9 JJ J I II J • ×

Combinator language

I Basic ︸ ︷︷ ︸|,∗,?,ε

operators ”combine” entities from a ︸ ︷︷ ︸EBNF

formalism

I Extension mechanism is borrowed from a programming language

Page 19: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

9 JJ J I II J • ×

Combinator language

I Basic ︸ ︷︷ ︸|,∗,?,ε

operators ”combine” ︸ ︷︷ ︸parsers

entities from a ︸ ︷︷ ︸EBNF

formalism

I Extension mechanism is borrowed from a programming language

Page 20: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

9 JJ J I II J • ×

Combinator language

I Basic ︸ ︷︷ ︸|,∗,?,ε

operators ”combine” ︸ ︷︷ ︸parsers

entities from a ︸ ︷︷ ︸EBNF

formalism

I Extension mechanism is borrowed from a ︸ ︷︷ ︸Haskell

programming language

Page 21: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | bcomposition a bterminals ’s’empty string ε

Page 22: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a | bcomposition a bterminals ’s’empty string ε

Page 23: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a <|> bcomposition a bterminals ’s’empty string ε

Page 24: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a <|> bcomposition a b a <*> bterminals ’s’empty string ε

Page 25: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a <|> bcomposition a b a <*> bterminals ’s’ symbol ’s’empty string ε

Page 26: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a <|> bcomposition a b a <*> bterminals ’s’ symbol ’s’empty string ε epsilon

Page 27: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a <|> bcomposition a b a <*> bterminals ’s’ symbol ’s’empty string ε epsilon

Other operators:

repetition a∗ many aoption a? option agrouping (. . .) (. . .)

Page 28: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a <|> bcomposition a b a <*> bterminals ’s’ symbol ’s’empty string ε epsilon

Other operators:

repetition a∗ many aoption a? option agrouping (. . .) (. . .)

Example of use:

symbol ’H’<*>symbol ’o’<*>symbol ’i’

Page 29: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a <|> bcomposition a b a <*> bterminals ’s’ symbol ’s’empty string ε epsilon

Other operators:

repetition a∗ many aoption a? option agrouping (. . .) (. . .)

Example of use:

symbol ’H’<*>option (symbol ’o’)<*>symbol ’i’

Page 30: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

10 JJ J I II J • ×

A Parser Combinator Library

Basic operators:

EBNF Haskellalternative a | b a <|> bcomposition a b a <*> bterminals ’s’ symbol ’s’empty string ε epsilon

Other operators:

repetition a∗ many aoption a? option agrouping (. . .) (. . .)

Example of use:

many (symbol ’H’<*>option (symbol ’o’)<*>symbol ’i’

)

Page 31: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

11 JJ J I II J • ×

What is a Parser

String → a

Page 32: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

11 JJ J I II J • ×

What is a Parser

String → [a ]

Page 33: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

11 JJ J I II J • ×

What is a Parser

String → [(a, String)]

Page 34: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

11 JJ J I II J • ×

What is a Parser

String → [(a, String)]

Abbreviation:

Parser a

Page 35: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

12 JJ J I II J • ×

Types of the Combinators

<|> :: Parser a → Parser a → Parser a

Page 36: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

12 JJ J I II J • ×

Types of the Combinators

<|> :: Parser a → Parser a → Parser a

<*> :: Parser a → Parser b → Parser (a, b)

Page 37: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

12 JJ J I II J • ×

Types of the Combinators

<|> :: Parser a → Parser a → Parser a

<*> :: Parser a → Parser b → Parser (a, b)

<*> :: Parser (b → c) → Parser b → Parser c

Page 38: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

12 JJ J I II J • ×

Types of the Combinators

<|> :: Parser a → Parser a → Parser a

<*> :: Parser a → Parser b → Parser (a, b)

<*> :: Parser (b → c) → Parser b → Parser c

symbol :: Char → Parser Char

Page 39: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

12 JJ J I II J • ×

Types of the Combinators

<|> :: Parser a → Parser a → Parser a

<*> :: Parser a → Parser b → Parser (a, b)

<*> :: Parser (b → c) → Parser b → Parser c

symbol :: Char → Parser Char

epsilon :: a → Parser a

Page 40: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

12 JJ J I II J • ×

Types of the Combinators

<|> :: Parser a → Parser a → Parser a

<*> :: Parser a → Parser b → Parser (a, b)

<*> :: Parser (b → c) → Parser b → Parser c

symbol :: Char → Parser Char

epsilon :: a → Parser a

<$> :: (a → b)→ Parser a → Parser b

Page 41: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

13 JJ J I II J • ×

Make your own extensions

EBNF:

many :: Parser a → Parser [a ]many x = x <*> many x

<|> epsilon

option x = . . .

Page 42: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

13 JJ J I II J • ×

Make your own extensions

EBNF:

many :: Parser a → Parser [a ]many x = (:) <$> x <*> many x

<|> epsilon

option x = . . .

Page 43: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

13 JJ J I II J • ×

Make your own extensions

EBNF:

many :: Parser a → Parser [a ]many x = (:) <$> x <*> many x

<|> epsilon [ ]

option x = . . .

Page 44: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

13 JJ J I II J • ×

Make your own extensions

EBNF:

many :: Parser a → Parser [a ]many x = (:) <$> x <*> many x

<|> epsilon [ ]

option x = . . .

EEBNF:

token :: [Char ]→ Parser [Char ]token (s : rest) = symbol s <*> token resttoken [ ] = epsilon

Page 45: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

13 JJ J I II J • ×

Make your own extensions

EBNF:

many :: Parser a → Parser [a ]many x = (:) <$> x <*> many x

<|> epsilon [ ]

option x = . . .

EEBNF:

token :: [Char ]→ Parser [Char ]token (s : rest) = (:) <$> symbol s <*> token resttoken [ ] = epsilon ""

Page 46: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

13 JJ J I II J • ×

Make your own extensions

EBNF:

many :: Parser a → Parser [a ]many x = (:) <$> x <*> many x

<|> epsilon [ ]

option x = . . .

EEBNF:

token :: [Char ]→ Parser [Char ]token (s : rest) = (:) <$> symbol s <*> token resttoken [ ] = epsilon ""

EEEBNF:

permute = . . .<||> = . . .

Page 47: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

14 JJ J I II J • ×

Example: IMG-tag attributes

Parser for attributes:

permute (field "width" integer<||>field "height" integer<||>field "src" string<||>field "alt" string)

Helper function for parsing assignments:

field name val = glue<$>token name <*> symbol ’=’ <*> valwhere glue v = v

Page 48: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

15 JJ J I II J • ×

Implementation idea

s = permute (a <||> b <||> c)

should be equivalent to:

s = a <*> b <*> c<|>a <*> c <*> b<|>b <*> a <*> c<|>b <*> c <*> a<|>c <*> a <*> b<|>c <*> b <*> a

Page 49: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

15 JJ J I II J • ×

Implementation idea

s = permute (a <||> b <||> c)

should be equivalent to:

s = a <*> b <*> c<|>a <*> c <*> b<|>b <*> a <*> c<|>b <*> c <*> a<|>c <*> a <*> b<|>c <*> b <*> a

or, with left-factorization:

s = a <*> (b <*> c <|> c <*> b)<|>b <*> (a <*> c <|> c <*> a)<|>c <*> (a <*> b <|> b <*> a)

Page 50: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

16 JJ J I II J • ×

Implementation idea

ab

b

c

c

a

ab

c

c

a

a

b

bc

I <||>: constructs an intermediate data structure(decision tree)

I permute: transforms it into a parser

Page 51: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

17 JJ J I II J • ×

Complexity

ab

b

c

c

a

ab

c

c

a

a

b

bc

Size of the intermediate structure: O(n!)

Page 52: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

17 JJ J I II J • ×

Complexity

ab

b

c

c

a

ab

c

c

a

a

b

bc

Example: parse string ”bac”

Page 53: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

17 JJ J I II J • ×

Complexity

ab

b

c

c

a

ab

c

c

a

a

b

bc

Thanks to lazy evaluation only one path is constructed

Size of the intermediate structure: O(n2)

Page 54: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

18 JJ J I II J • ×

Finishing Touch: Reordering

(a,b,c)\ c b a

(a,b,c)\ b c a

(a,b,c)\ c a b

(a,b,c)\ a c b

(a,b,c)\ b a c

(a,b,c)\ a b c

ab

b

c

c

a

ab

c

c

a

a

b

bc

Each leaf contains a special reordering function

Page 55: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

19 JJ J I II J • ×

Recap

I <||>: combinator to construct large permutation grammar

Provides notation for writing such grammars easily

Makes programmer happy

I permute: uses grammar to parse input

I Lazy evaluation:

Only small part of the grammar is computed

Makes computer happy

Page 56: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

20 JJ J I II J • ×

Intermediate data structure (1)

Structure of the tree:

data Tree = Choice [Branch ]| Leaf

data Branch = Branch (Parser?) Tree

Page 57: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

21 JJ J I II J • ×

Intermediate data structure (2)

Structure of the tree:

data Tree a = Choice [Branch a ]| Leaf a

data Branch a = ∃x .Branch (Parser x ) (Tree (x → a))

an element of type a is constructed by applying the result of the rest of the path(of type x → a) to the result of the parser in the branch (of type x )!

Page 58: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

22 JJ J I II J • ×

Transforming intermediate structure

(a,b,c)\ c b a

(a,b,c)\ b c a

(a,b,c)\ c a b

(a,b,c)\ a c b

(a,b,c)\ b a c

(a,b,c)\ a b c

ab

b

c

c

a

ab

c

c

a

a

b

bc

Page 59: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

22 JJ J I II J • ×

Transforming intermediate structure

(a,b,c)\ c b a

(a,b,c)\ b c a

(a,b,c)\ c a b

(a,b,c)\ a c b

(a,b,c)\ b a c

<|>

<|>

(a,b,c)\ a b c

ab

b

c

c

a

ab

c

c

a

a

b

bc

<|>

<|>

<|>

Page 60: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

22 JJ J I II J • ×

Transforming intermediate structure

<*>

<|><*>

<|><*>

<*>

<*>

<*>

<*>

<*>

<*>

(a,b,c)\ c b a

(a,b,c)\ b c a

(a,b,c)\ c a b

(a,b,c)\ a c b

(a,b,c)\ b a c

(a,b,c)\ a b c

ab

b

c

c

a

ab

c

c

a

a

b

bc

<|>

<*>

<*>

<*>

<*>

<*>

<*>

<|>

<|>

Page 61: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

22 JJ J I II J • ×

Transforming intermediate structure

<*>

<|><*>

<|><*>

<*>

<*>

<*>

<*>

<*>

<*>

(a,b,c)\ c b a

(a,b,c)\ b c a

(a,b,c)\ c a b

(a,b,c)\ a c b

(a,b,c)\ b a c

<*> epsilon

<*> epsilon

<*> epsilon

<*> epsilon

<*> epsilon

<*> epsilon (a,b,c)\ a b c

ab

b

c

c

a

ab

c

c

a

a

b

bc

<|>

<|>

<|>

Page 62: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

23 JJ J I II J • ×

Transforming intermediate structure

permute traverses the grammar and constructs a parser:

permute (Leaf f ) = epsilon fpermute (Choice bs) = choice [glue <$> p <*> permute rest

| Branch p rest ← bs]

where glue x f = f x

The function choice takes a list of parsers and puts <|> between them

Page 63: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

24 JJ J I II J • ×

Summary

I Permutation parsing can be done context-free

I Necessary large grammars can be generated(by <||> and permute combinators)

I Lazy evaluation reduces complexity from O(n!) to O(n2)

Page 64: Chomsky Revisited - HH · Chomsky Revisited – or – Everything you always wanted to know about Parsing a Permutation Arthur Baars, Andres L¨oh and Doaitse Swierstra Institute

25 JJ J I II J • ×

Conclusions

Combinators are cool

I Formalisms are programs

I Extendible

I Executable

Haskell is hot

I Higher order functions

I Lazy evaluation

I User defined operators

I . . .