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 order(milk) if (outOf(eggs)): eggs identifjer … 2
Indentation using Petit Parser print “Hello!” if keyword if (outOf(milk)): print “We are out of milk!” block of commands order(milk) if (outOf(eggs)): eggs identifjer … 3
Indentation using Petit Parser print “Hello!” if keyword if (outOf(milk)): print “We are out of milk!” block of commands order(milk) if (outOf(eggs)): eggs identifjer … ifKeyword := 'if' asParser. 4
Indentation using Petit Parser print “Hello!” if keyword if (outOf(milk)): print “We are out of milk!” block of commands order(milk) if (outOf(eggs)): eggs identifjer … 5
Indentation using Petit Parser print “Hello!” if keyword if (outOf(milk)): print “We are out of milk!” block of commands order(milk) if (outOf(eggs)): eggs identifjer … identifier := #letter, (#letter / #digit) star. 6
Indentation using Petit Parser print “Hello!” if keyword if (outOf(milk)): print “We are out of milk!” block of commands order(milk) if (outOf(eggs)): eggs identifjer … 7
Indentation using Petit Parser print “Hello!” if keyword if (outOf(milk)): print “We are out of milk!” block of commands order(milk) if (outOf(eggs)): eggs identifjer … block := indent, command plus, dedent. 8
Indentation using Petit Parser print “Hello!” if keyword if (outOf(milk)) print “We are out of milk!” block of commands order(milk) if (outOf(eggs)) eggs identifjer … block := indent, command plus, dedent. 9
Indentation using Petit Parser print “Hello!” if keyword if (outOf(milk)) print “We are out of milk!” block of commands order(milk) if (outOf(eggs)) eggs identifjer … block := indent, command plus, dedent. indent := ??? dedent := ??? 10
Regular Context-Free Context-Sensitive Languages Languages Languages Simple State of the Art Complex 11
Regular Context-Free Context-Sensitive Languages Languages Languages Expression Regular Simple State of the Art Complex 12
Regular Context-Free Context-Sensitive Languages Languages Languages Expression Regular Simple State of the Art Machine Turing Complex 13
Regular Context-Free Context-Sensitive Languages Languages Languages Expression Regular Simple State of the Art Machine Turing Complex 14
Regular Context-Free Context-Sensitive Languages Languages Languages Expression Regular Simple State of the Art Machine Turing Complex 15
Regular Context-Free Context-Sensitive Languages Languages Languages Expression Regular Simple State of the Art Machine Turing Complex 16
Regular Context-Free Context-Sensitive Languages Languages Languages Expression Regular Simple State of the Art Machine Turing Complex 17
Goal Context-Sensitive Turing Machine Context-Free Regular Regular Expression Complex 18 Simple
Goal Context-Sensitive Turing Machine Parsing Parsing Contexts Contexts Context-Free Regular Regular Expression Complex 19 Simple
Goal Context-Sensitive Turing Machine Parsing Parsing Contexts Contexts Context-Free Regular Regular Expression Complex 20 Simple
PetitParser uses Parser Combinators 21
PetitParser uses Parser Combinators #letter, (#letter / #digit) star 22
PetitParser uses Parser Combinators #letter, (#letter / #digit) star 23
PetitParser uses Parser Combinators , , # letter star #letter, (#letter / #digit) star / / # letter # digit 24
String String 25
String String 26
String “Hello” #letter #letter String “ello” 27
Indentation Example print “Hello!” if keyword if (outOf(milk)) print “We are out of milk!” block of commands order(milk) if (outOf(eggs)) eggs identifjer … block := indent, command plus, dedent. 28
Indentation Example print “Hello!” if keyword if (outOf(milk)) print “We are out of milk!” block of commands order(milk) if (outOf(eggs)) eggs identifjer … block := indent, command plus, dedent. indent := ??? dedent := ??? 29
Indentation Example print “Hello!” ▼ if keyword if (outOf(milk)) print “We are out of milk!” block of commands order(milk) if (outOf(eggs)) eggs identifjer … block := indent, command plus, dedent. 30
Indentation Example print “Hello!” ▼ if keyword if (outOf(milk)) print “We are out of milk!” block of commands order(milk) if (outOf(eggs)) eggs identifjer … block := indent, command plus, dedent. indent := ??? dedent := ??? 31
String String 32
String String 33
String & String Indentation Stack String String & Indentation Stack 34
Parsing Parsing Contexts Contexts String String Parsing Parsing Contexts Contexts 35
String Parsing Parsing Contexts Contexts String Other Data String Parsing Parsing Contexts Contexts 36
Indentation Example print “Hello!” ▼ if keyword if (outOf(milk)) print “We are out of milk!” block of commands order(milk) if (outOf(eggs)) eggs identifjer … [ :context | [ :context | | column referenceIndentation stack | | column indentation stack | (context isBeginOfLine) ifFalse: [ (context isBeginOfLine) ifFalse: [ ^ Failure ^ Failure ]. ]. context consumeLeadingWhitespace. block := context consumeLeadingWhitespace. , command plus, column := context stream column. " Save the current column " " Restore previous column from the " column := context stream column. " stack and compare with current " stack := (context propertyAt: #indent). stack := (context propertyAt: #indent). indentation := stack top. stack pop. referenceIndentation := stack top. (column > indentation) ifTrue: [ stack push: column. (column == referenceIndentation) ifTrue: [ ^ #indent ^ #dedent ]. ]. ^ Failure ^ Failure ] 37 ]
Context-Sensitive Turing Machine Parsing Parsing Contexts Contexts Context-Free Regular Regular Expression Complex 38 Simple
Context-Sensitive Turing Parsing Parsing Machine Contexts Contexts Context-Free Regular Regular Expression Complex 39 Simple
Context-Sensitive Turing Parsing Parsing Machine Contexts Contexts Context-Free Regular Regular Expression Complex 40 Simple
Grammar Implementors 41
Grammar Implementors Framework Implementors 42
← N (N ∪ T)* Grammar Implementors Framework Implementors 43
← N (N ∪ T)* indent, command plus, dedent Grammar Implementors Framework Implementors 44
← N (N ∪ T)* indent, command plus, dedent Grammar Implementors [ :context | [ :contex t | | column referenceIndentation stack | | column indentation stack | (context isBeginOfLine) ifFalse: [ (context isBeginOfLine) ifFalse: [ ^ Failure ^ Failure ]. ]. context consumeLeadingWhitespace. context consumeLeadingWhitespace. , command plus, column := context stream column. " Save the current column " " Restore previous column from the " column := context stream column. " stack and compare with current " stack := (context propertyAt: #indent). stack := (context propertyAt: #indent). indentation := stack top. stack pop. referenceIndentation := stack top. (column > indentation) ifTrue: [ stack push: column. (column == referenceIndentation) ifTrue: [ ^ #indent ^ #dedent ]. ]. ^ Failure ^ Failure ] ] Framework Implementors 45
Context-Sensitive Turing Machine Parsing Parsing Contexts Contexts Parsing Parsing Contexts Contexts Context-Free Regular Regular Expression Complex 46 Simple
Context-Sensitive Turing Machine Parsing Parsing Contexts Contexts Context-Free Regular Regular Expression Complex 47 Simple
Context-Sensitive Turing Machine Parsing Parsing Contexts Contexts Context-Free Regular Regular Expression Complex 48 Simple
Parsing Parsing Contexts Contexts 49
Parsing Parsing Contexts Contexts indentation 50
Parsing Parsing Contexts Contexts indentation tolerant parsing 51
Parsing Parsing Contexts Contexts indentation tolerant parsing debugging 52
http://smalltalkhub.com/#!/~JanKurs/PetitParser 53
Turing Machine Parsing Parsing Contexts Contexts Regular Expression 54
Parsing String Turing Context Machine Parsing Parsing Contexts Contexts Regular Expression Parsing String Context 55
Parsing String Turing Context Machine Parsing Parsing Contexts Contexts Regular Expression Parsing String Context ← ∪ N (N T)* Grammar Framework Implementor Implementors 56
Parsing String Turing Context Machine Parsing Parsing Contexts Contexts Regular Expression Parsing String Context ← ∪ N (N T)* Parsing Parsing Contexts Contexts indentation tolerant parsing Grammar Framework debugging Implementor Implementors 57
Recommend
More recommend