Parsing Contexts Context Sensitive Grammars for Everyone Jan Kur - - PowerPoint PPT Presentation

parsing contexts
SMART_READER_LITE
LIVE PREVIEW

Parsing Contexts Context Sensitive Grammars for Everyone Jan Kur - - PowerPoint PPT Presentation

Parsing Contexts Context Sensitive Grammars for Everyone Jan Kur kurs@iam.unibe.ch Software Composition Group Indentation using Petit Parser print Hello! if keyword if (outOf(milk)): print We are out of milk! block of commands


slide-1
SLIDE 1

Parsing Contexts

Context Sensitive Grammars for Everyone

Jan Kurš kurs@iam.unibe.ch Software Composition Group

slide-2
SLIDE 2

2

print “Hello!” if (outOf(milk)): print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)): … if keyword block of commands eggs identifjer

Indentation using Petit Parser

slide-3
SLIDE 3

3

print “Hello!” if (outOf(milk)): print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)): … if keyword block of commands eggs identifjer

Indentation using Petit Parser

slide-4
SLIDE 4

4

print “Hello!” if (outOf(milk)): print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)): … if keyword block of commands eggs identifjer

Indentation using Petit Parser

ifKeyword := 'if' asParser.

slide-5
SLIDE 5

5

print “Hello!” if (outOf(milk)): print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)): … if keyword block of commands eggs identifjer

Indentation using Petit Parser

slide-6
SLIDE 6

6

print “Hello!” if (outOf(milk)): print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)): … if keyword block of commands eggs identifjer

Indentation using Petit Parser

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

slide-7
SLIDE 7

7

print “Hello!” if (outOf(milk)): print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)): … if keyword block of commands eggs identifjer

Indentation using Petit Parser

slide-8
SLIDE 8

8

print “Hello!” if (outOf(milk)): print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)): … if keyword block of commands eggs identifjer

Indentation using Petit Parser

block := indent, command plus, dedent.

slide-9
SLIDE 9

9

print “Hello!” if (outOf(milk)) print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)) … if keyword block of commands eggs identifjer

Indentation using Petit Parser

block := indent, command plus, dedent.

slide-10
SLIDE 10

10

print “Hello!” if (outOf(milk)) print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)) … if keyword block of commands eggs identifjer

Indentation using Petit Parser

block := indent, command plus, dedent. indent := ??? dedent := ???

slide-11
SLIDE 11

11

Simple Complex Regular Languages Context-Free Languages

State of the Art

Context-Sensitive Languages

slide-12
SLIDE 12

12

Regular Expression Simple Complex Regular Languages Context-Free Languages

State of the Art

Context-Sensitive Languages

slide-13
SLIDE 13

13

Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages

State of the Art

Context-Sensitive Languages

slide-14
SLIDE 14

14

Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages

State of the Art

Context-Sensitive Languages

slide-15
SLIDE 15

15

Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages

State of the Art

Context-Sensitive Languages

slide-16
SLIDE 16

16

Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages

State of the Art

Context-Sensitive Languages

slide-17
SLIDE 17

17

Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages

State of the Art

Context-Sensitive Languages

slide-18
SLIDE 18

18

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive

Goal

slide-19
SLIDE 19

19

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive Parsing Parsing Contexts Contexts

Goal

slide-20
SLIDE 20

20

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive

Parsing Parsing Contexts Contexts

Goal

slide-21
SLIDE 21

21

PetitParser uses Parser Combinators

slide-22
SLIDE 22

22

#letter, (#letter / #digit) star

PetitParser uses Parser Combinators

slide-23
SLIDE 23

23

#letter, (#letter / #digit) star

PetitParser uses Parser Combinators

slide-24
SLIDE 24

24

#letter, (#letter / #digit) star

, , #letter #digit #letter / /

PetitParser uses Parser Combinators

star

slide-25
SLIDE 25

25

String String

slide-26
SLIDE 26

26

String String

slide-27
SLIDE 27

27

String String “Hello” “ello”

#letter #letter

slide-28
SLIDE 28

28

print “Hello!” if (outOf(milk)) print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)) … if keyword block of commands eggs identifjer

Indentation Example

block := indent, command plus, dedent.

slide-29
SLIDE 29

29

print “Hello!” if (outOf(milk)) print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)) … if keyword block of commands eggs identifjer

Indentation Example

block := indent, command plus, dedent. indent := ??? dedent := ???

slide-30
SLIDE 30

30

print “Hello!” if (outOf(milk)) print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)) … if keyword block of commands eggs identifjer

Indentation Example

block := indent, command plus, dedent.

slide-31
SLIDE 31

31

print “Hello!” if (outOf(milk)) print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)) … if keyword block of commands eggs identifjer

Indentation Example

block := indent, command plus, dedent. indent := ??? dedent := ???

slide-32
SLIDE 32

32

String String

slide-33
SLIDE 33

33

String String

slide-34
SLIDE 34

34

String String

String & Indentation Stack String & Indentation Stack

slide-35
SLIDE 35

35

String String

Parsing Parsing Contexts Contexts Parsing Parsing Contexts Contexts

slide-36
SLIDE 36

36

String String

Parsing Parsing Contexts Contexts Parsing Parsing Contexts Contexts

String Other Data

slide-37
SLIDE 37

37

print “Hello!” if (outOf(milk)) print “We are out of milk!”

  • rder(milk)

if (outOf(eggs)) … if keyword block of commands eggs identifjer

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 ]

slide-38
SLIDE 38

38

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive

Parsing Parsing Contexts Contexts

slide-39
SLIDE 39

39

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive

Parsing Parsing Contexts Contexts

slide-40
SLIDE 40

40

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive

Parsing Parsing Contexts Contexts

slide-41
SLIDE 41

41

Grammar Implementors

slide-42
SLIDE 42

42

Framework Implementors Grammar Implementors

slide-43
SLIDE 43

43

Framework Implementors Grammar Implementors

N (N ←

∪ T)*

slide-44
SLIDE 44

44

Framework Implementors Grammar Implementors

N (N ←

∪ T)* indent, command plus, dedent

slide-45
SLIDE 45

45

Framework Implementors Grammar Implementors

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 ]

slide-46
SLIDE 46

46

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive

Parsing Parsing Contexts Contexts

Parsing Parsing Contexts Contexts

slide-47
SLIDE 47

47

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive

Parsing Parsing Contexts Contexts

slide-48
SLIDE 48

48

Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive

Parsing Parsing Contexts Contexts

slide-49
SLIDE 49

49 Parsing Parsing Contexts Contexts

slide-50
SLIDE 50

50

indentation

Parsing Parsing Contexts Contexts

slide-51
SLIDE 51

51

indentation

Parsing Parsing Contexts Contexts

tolerant parsing

slide-52
SLIDE 52

52

indentation debugging

Parsing Parsing Contexts Contexts

tolerant parsing

slide-53
SLIDE 53

53

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

slide-54
SLIDE 54

54

Turing Machine Regular Expression Parsing Parsing Contexts Contexts

slide-55
SLIDE 55

55

String String Parsing Context Parsing Context

Turing Machine Regular Expression Parsing Parsing Contexts Contexts

slide-56
SLIDE 56

56

String String Parsing Context Parsing Context Framework Implementors Grammar Implementor

N (N T)* ← ∪

Turing Machine Regular Expression Parsing Parsing Contexts Contexts

slide-57
SLIDE 57

57

String String Parsing Context Parsing Context indentation tolerant parsing

Parsing Parsing Contexts Contexts

debugging Framework Implementors Grammar Implementor

N (N T)* ← ∪

Turing Machine Regular Expression Parsing Parsing Contexts Contexts