Parsing Contexts Context Sensitive Grammars for Everyone Jan Kur - - PowerPoint PPT Presentation
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
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
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
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.
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
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.
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
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.
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.
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 := ???
11
Simple Complex Regular Languages Context-Free Languages
State of the Art
Context-Sensitive Languages
12
Regular Expression Simple Complex Regular Languages Context-Free Languages
State of the Art
Context-Sensitive Languages
13
Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages
State of the Art
Context-Sensitive Languages
14
Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages
State of the Art
Context-Sensitive Languages
15
Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages
State of the Art
Context-Sensitive Languages
16
Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages
State of the Art
Context-Sensitive Languages
17
Turing Machine Regular Expression Simple Complex Regular Languages Context-Free Languages
State of the Art
Context-Sensitive Languages
18
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive
Goal
19
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive Parsing Parsing Contexts Contexts
Goal
20
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive
Parsing Parsing Contexts Contexts
Goal
21
PetitParser uses Parser Combinators
22
#letter, (#letter / #digit) star
PetitParser uses Parser Combinators
23
#letter, (#letter / #digit) star
PetitParser uses Parser Combinators
24
#letter, (#letter / #digit) star
, , #letter #digit #letter / /
PetitParser uses Parser Combinators
star
25
String String
26
String String
27
String String “Hello” “ello”
#letter #letter
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.
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 := ???
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.
▼
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 := ???
▼
32
String String
33
String String
34
String String
String & Indentation Stack String & Indentation Stack
35
String String
Parsing Parsing Contexts Contexts Parsing Parsing Contexts Contexts
36
String String
Parsing Parsing Contexts Contexts Parsing Parsing Contexts Contexts
String Other Data
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 ]
38
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive
Parsing Parsing Contexts Contexts
39
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive
Parsing Parsing Contexts Contexts
40
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive
Parsing Parsing Contexts Contexts
41
Grammar Implementors
42
Framework Implementors Grammar Implementors
43
Framework Implementors Grammar Implementors
N (N ←
∪ T)*
44
Framework Implementors Grammar Implementors
N (N ←
∪ T)* indent, command plus, dedent
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 ]
46
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive
Parsing Parsing Contexts Contexts
Parsing Parsing Contexts Contexts
47
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive
Parsing Parsing Contexts Contexts
48
Turing Machine Regular Expression Simple Complex Regular Context-Free Context-Sensitive
Parsing Parsing Contexts Contexts
49 Parsing Parsing Contexts Contexts
50
indentation
Parsing Parsing Contexts Contexts
51
indentation
Parsing Parsing Contexts Contexts
tolerant parsing
52
indentation debugging
Parsing Parsing Contexts Contexts
tolerant parsing
53
http://smalltalkhub.com/#!/~JanKurs/PetitParser
54
Turing Machine Regular Expression Parsing Parsing Contexts Contexts
55
String String Parsing Context Parsing Context
Turing Machine Regular Expression Parsing Parsing Contexts Contexts
56
String String Parsing Context Parsing Context Framework Implementors Grammar Implementor
N (N T)* ← ∪
Turing Machine Regular Expression Parsing Parsing Contexts Contexts
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