parsing contexts for petitparser

61
Parsing Contexts for PetitParser Jan Kurš [email protected] Software Composition Group

Upload: esug

Post on 20-May-2015

287 views

Category:

Software


0 download

DESCRIPTION

Parsing Contexts for PetitParser IWST 2014 at ESUG, Cambridge

TRANSCRIPT

Page 1: Parsing Contexts for PetitParser

Parsing Contextsfor PetitParser

Jan Kurš[email protected] Composition Group

Page 2: Parsing Contexts for PetitParser

2

print “Hello!”

if (outOf(milk)):

print “We are out of milk!”

order(milk)

if (outOf(eggs)):

if keyword

block of commands

eggs identifier

Indentation Example

Page 3: Parsing Contexts for PetitParser

3

print “Hello!”

if (outOf(milk)):

print “We are out of milk!”

order(milk)

if (outOf(eggs)):

if keyword

block of commands

eggs identifier

Indentation Example

Page 4: Parsing Contexts for PetitParser

4

print “Hello!”

if (outOf(milk)):

print “We are out of milk!”

order(milk)

if (outOf(eggs)):

if keyword

block of commands

eggs identifier

Indentation Example

ifKeyword := 'if' asParser.

Page 5: Parsing Contexts for PetitParser

5

print “Hello!”

if (outOf(milk)):

print “We are out of milk!”

order(milk)

if (outOf(eggs)):

if keyword

block of commands

eggs identifier

Indentation Example

Page 6: Parsing Contexts for PetitParser

6

print “Hello!”

if (outOf(milk)):

print “We are out of milk!”

order(milk)

if (outOf(eggs)):

if keyword

block of commands

eggs identifier

Indentation Example

identifier := #letter, (#letter / #digit) star.

Page 7: Parsing Contexts for PetitParser

7

print “Hello!”

if (outOf(milk)):

print “We are out of milk!”

order(milk)

if (outOf(eggs)):

if keyword

block of commands

eggs identifier

Indentation Example

Page 8: Parsing Contexts for PetitParser

8

print “Hello!”

if (outOf(milk)):

print “We are out of milk!”

order(milk)

if (outOf(eggs)):

if keyword

block of commands

eggs identifier

Indentation Example

block := indent, command plus, dedent.

Page 9: Parsing Contexts for PetitParser

9

print “Hello!”

if (outOf(milk))

print “We are out of milk!”

order(milk)

if (outOf(eggs))

if keyword

block of commands

eggs identifier

Indentation Example

block := indent, command plus, dedent.

Page 10: Parsing Contexts for PetitParser

10

print “Hello!”

if (outOf(milk))

print “We are out of milk!”

order(milk)

if (outOf(eggs))

if keyword

block of commands

eggs identifier

Indentation Example

block := indent, command plus, dedent.

indent := ???

dedent := ???

Page 11: Parsing Contexts for PetitParser

11Simple Complex

Re

gu

lar

Lan

gu

age

sC

on

text

-Fre

eLa

ng

uag

es

State of the ArtC

on

text

-Se

nsi

tive

Lan

gu

age

s

Page 12: Parsing Contexts for PetitParser

12

RegularExpression

Simple Complex

Re

gu

lar

Lan

gu

age

sC

on

text

-Fre

eLa

ng

uag

es

State of the ArtC

on

text

-Se

nsi

tive

Lan

gu

age

s

Page 13: Parsing Contexts for PetitParser

13

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Lan

gu

age

sC

on

text

-Fre

eLa

ng

uag

es

State of the ArtC

on

text

-Se

nsi

tive

Lan

gu

age

s

Page 14: Parsing Contexts for PetitParser

14

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Lan

gu

age

sC

on

text

-Fre

eLa

ng

uag

es

State of the ArtC

on

text

-Se

nsi

tive

Lan

gu

age

s

Page 15: Parsing Contexts for PetitParser

15

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Lan

gu

age

sC

on

text

-Fre

eLa

ng

uag

es

State of the ArtC

on

text

-Se

nsi

tive

Lan

gu

age

s

Page 16: Parsing Contexts for PetitParser

16

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Lan

gu

age

sC

on

text

-Fre

eLa

ng

uag

es

State of the ArtC

on

text

-Se

nsi

tive

Lan

gu

age

s

Page 17: Parsing Contexts for PetitParser

17

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Lan

gu

age

sC

on

text

-Fre

eLa

ng

uag

es

State of the ArtC

on

text

-Se

nsi

tive

Lan

gu

age

s

Page 18: Parsing Contexts for PetitParser

18

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Co

nte

xt-F

ree

Co

nte

xt-S

en

siti

ve

Goal

Page 19: Parsing Contexts for PetitParser

19

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Co

nte

xt-F

ree

Co

nte

xt-S

en

siti

ve

ParsingParsingContextsContexts

Goal

Page 20: Parsing Contexts for PetitParser

20

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Co

nte

xt-F

ree

Co

nte

xt-S

en

siti

ve

ParsingParsingContextsContexts

Goal

Page 21: Parsing Contexts for PetitParser

21

PetitParser uses Parser Combinators

Page 22: Parsing Contexts for PetitParser

22

#letter, (#letter / #digit) star

PetitParser uses Parser Combinators

Page 23: Parsing Contexts for PetitParser

23

#letter, (#letter / #digit) star

PetitParser uses Parser Combinators

Page 24: Parsing Contexts for PetitParser

24

#letter, (#letter / #digit) star

,,

#letter

#digit #letter

//

PetitParser uses Parser Combinators

star

Page 25: Parsing Contexts for PetitParser

25

String

String

Page 26: Parsing Contexts for PetitParser

26

String

String

Page 27: Parsing Contexts for PetitParser

27

String

String

“Hello”

“ello”

#letter #letter

Page 28: Parsing Contexts for PetitParser

28

print “Hello!”

if (outOf(milk))

print “We are out of milk!”

order(milk)

if (outOf(eggs))

if keyword

block of commands

eggs identifier

Indentation Example

block := indent, command plus, dedent.

Page 29: Parsing Contexts for PetitParser

29

print “Hello!”

if (outOf(milk))

print “We are out of milk!”

order(milk)

if (outOf(eggs))

if keyword

block of commands

eggs identifier

Indentation Example

block := indent, command plus, dedent.

indent := ???

dedent := ???

Page 30: Parsing Contexts for PetitParser

30

print “Hello!”

if (outOf(milk))

print “We are out of milk!”

order(milk)

if (outOf(eggs))

if keyword

block of commands

eggs identifier

Indentation Example

block := indent, command plus, dedent.

Page 31: Parsing Contexts for PetitParser

31

print “Hello!”

if (outOf(milk))

print “We are out of milk!”

order(milk)

if (outOf(eggs))

if keyword

block of commands

eggs identifier

Indentation Example

block := indent, command plus, dedent.

indent := ???

dedent := ???

Page 32: Parsing Contexts for PetitParser

32

String

String

Page 33: Parsing Contexts for PetitParser

33

String

String

Page 34: Parsing Contexts for PetitParser

34

String

String

String&

Indentation Stack

String&

Indentation Stack

Page 35: Parsing Contexts for PetitParser

35

String

String

ParsingParsingContextsContexts

ParsingParsingContextsContexts

Page 36: Parsing Contexts for PetitParser

36

String

String

ParsingParsingContextsContexts

ParsingParsingContextsContexts

String

Other Data

Page 37: Parsing Contexts for PetitParser

37

print “Hello!”

if (outOf(milk))

print “We are out of milk!”

order(milk)

if (outOf(eggs))

if keyword

block of commands

eggs identifier

Indentation Example

block := , command plus,

[:context | | column indentation stack |

(context isBeginOfLine) ifFalse: [ ^ Failure ]. context consumeLeadingWhitespace. " Save the current column " column := context stream column. stack := (context propertyAt: #indent). indentation := stack top.

(column > indentation) ifTrue: [ stack push: column. ^ #indent ]. ^ Failure]

[:context | | column referenceIndentation stack |

(context isBeginOfLine) ifFalse: [ ^ Failure ]. context consumeLeadingWhitespace. column := context stream column. " Restore previous column from the " " stack and compare with current " stack := (context propertyAt: #indent). stack pop. referenceIndentation := stack top.

(column == referenceIndentation) ifTrue: [ ^ #dedent ]. ^ Failure]

Page 38: Parsing Contexts for PetitParser

38

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Co

nte

xt-F

ree

Co

nte

xt-S

en

siti

ve

ParsingParsingContextsContexts

Page 39: Parsing Contexts for PetitParser

39

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Co

nte

xt-F

ree

Co

nte

xt-S

en

siti

ve

ParsingParsingContextsContexts

Page 40: Parsing Contexts for PetitParser

40

GrammarImplementors

Page 41: Parsing Contexts for PetitParser

41

FrameworkImplementors

GrammarImplementors

Page 42: Parsing Contexts for PetitParser

42

FrameworkImplementors

GrammarImplementors

N (N ← ∪ T)*

Page 43: Parsing Contexts for PetitParser

43

FrameworkImplementors

GrammarImplementors

N (N ← ∪ T)*

indent, command plus, dedent

Page 44: Parsing Contexts for PetitParser

44

FrameworkImplementors

GrammarImplementors

N (N ← ∪ T)*

indent, command plus, dedent

, command plus,

[:context | | column indentation stack |

(context isBeginOfLine) ifFalse: [ ^ Failure ]. context consumeLeadingWhitespace. " Save the current column " column := context stream column. stack := (context propertyAt: #indent). indentation := stack top.

(column > indentation) ifTrue: [ stack push: column. ^ #indent ]. ^ Failure]

[:context | | column referenceIndentation stack |

(context isBeginOfLine) ifFalse: [ ^ Failure ]. context consumeLeadingWhitespace. column := context stream column. " Restore previous column from the " " stack and compare with current " stack := (context propertyAt: #indent). stack pop. referenceIndentation := stack top.

(column == referenceIndentation) ifTrue: [ ^ #dedent ]. ^ Failure]

Page 45: Parsing Contexts for PetitParser

45

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Co

nte

xt-F

ree

Co

nte

xt-S

en

siti

ve

ParsingParsingContextsContexts

ParsingParsingContextsContexts

Page 46: Parsing Contexts for PetitParser

46

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Co

nte

xt-F

ree

Co

nte

xt-S

en

siti

ve

ParsingParsingContextsContexts

Page 47: Parsing Contexts for PetitParser

47

TuringMachine

RegularExpression

Simple Complex

Re

gu

lar

Co

nte

xt-F

ree

Co

nte

xt-S

en

siti

ve

ParsingParsingContextsContexts

Page 48: Parsing Contexts for PetitParser

48

indentation

Page 49: Parsing Contexts for PetitParser

49

indentation

Page 50: Parsing Contexts for PetitParser

50

indentation

tolerant parsing

Page 51: Parsing Contexts for PetitParser

51

indentation

tolerant parsing

Page 52: Parsing Contexts for PetitParser

52

indentation

typedef

tolerant parsing

Page 53: Parsing Contexts for PetitParser

53

indentation

typedef

tolerant parsing

Page 54: Parsing Contexts for PetitParser

54

indentation

typedef

recursion

tolerant parsing

Page 55: Parsing Contexts for PetitParser

55

indentation

typedef

recursion

tolerant parsing

Page 56: Parsing Contexts for PetitParser

56

indentation

typedef

recursion

ParsingParsingContextsContexts

tolerant parsing

Page 57: Parsing Contexts for PetitParser

57

http://smalltalkhub.com/#!/~JanKurs/PetitParser

Page 58: Parsing Contexts for PetitParser

58

TuringMachine

RegularExpression

ParsingParsingContextsContexts

Page 59: Parsing Contexts for PetitParser

59

String

String

ParsingContext

Parsing Context

TuringMachine

RegularExpression

ParsingParsingContextsContexts

Page 60: Parsing Contexts for PetitParser

60

String

String

ParsingContext

Parsing Context

Framework Implementors

Grammar Implementor

N (N T)*← ∪

TuringMachine

RegularExpression

ParsingParsingContextsContexts

Page 61: Parsing Contexts for PetitParser

61

String

String

ParsingContext

Parsing Context

indentation

typedef

tolerant parsing

recursion

ParsingParsingContextsContexts

Framework Implementors

Grammar Implementor

N (N T)*← ∪

TuringMachine

RegularExpression

ParsingParsingContextsContexts