parsing contexts for petitparser
DESCRIPTION
Parsing Contexts for PetitParser IWST 2014 at ESUG, CambridgeTRANSCRIPT
Parsing Contextsfor PetitParser
Jan Kurš[email protected] Composition Group
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
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
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.
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
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.
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
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.
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.
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 := ???
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
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
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
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
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
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
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
18
TuringMachine
RegularExpression
Simple Complex
Re
gu
lar
Co
nte
xt-F
ree
Co
nte
xt-S
en
siti
ve
Goal
19
TuringMachine
RegularExpression
Simple Complex
Re
gu
lar
Co
nte
xt-F
ree
Co
nte
xt-S
en
siti
ve
ParsingParsingContextsContexts
Goal
20
TuringMachine
RegularExpression
Simple Complex
Re
gu
lar
Co
nte
xt-F
ree
Co
nte
xt-S
en
siti
ve
ParsingParsingContextsContexts
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!”
order(milk)
if (outOf(eggs))
…
if keyword
block of commands
eggs identifier
Indentation Example
block := indent, command plus, dedent.
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 := ???
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.
▼
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 := ???
▼
32
String
String
33
String
String
34
String
String
String&
Indentation Stack
String&
Indentation Stack
35
String
String
ParsingParsingContextsContexts
ParsingParsingContextsContexts
36
String
String
ParsingParsingContextsContexts
ParsingParsingContextsContexts
String
Other Data
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]
38
TuringMachine
RegularExpression
Simple Complex
Re
gu
lar
Co
nte
xt-F
ree
Co
nte
xt-S
en
siti
ve
ParsingParsingContextsContexts
39
TuringMachine
RegularExpression
Simple Complex
Re
gu
lar
Co
nte
xt-F
ree
Co
nte
xt-S
en
siti
ve
ParsingParsingContextsContexts
40
GrammarImplementors
41
FrameworkImplementors
GrammarImplementors
42
FrameworkImplementors
GrammarImplementors
N (N ← ∪ T)*
43
FrameworkImplementors
GrammarImplementors
N (N ← ∪ T)*
indent, command plus, dedent
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]
45
TuringMachine
RegularExpression
Simple Complex
Re
gu
lar
Co
nte
xt-F
ree
Co
nte
xt-S
en
siti
ve
ParsingParsingContextsContexts
ParsingParsingContextsContexts
46
TuringMachine
RegularExpression
Simple Complex
Re
gu
lar
Co
nte
xt-F
ree
Co
nte
xt-S
en
siti
ve
ParsingParsingContextsContexts
47
TuringMachine
RegularExpression
Simple Complex
Re
gu
lar
Co
nte
xt-F
ree
Co
nte
xt-S
en
siti
ve
ParsingParsingContextsContexts
48
indentation
49
indentation
50
indentation
tolerant parsing
51
indentation
tolerant parsing
52
indentation
typedef
tolerant parsing
53
indentation
typedef
tolerant parsing
54
indentation
typedef
recursion
tolerant parsing
55
indentation
typedef
recursion
tolerant parsing
56
indentation
typedef
recursion
ParsingParsingContextsContexts
tolerant parsing
57
http://smalltalkhub.com/#!/~JanKurs/PetitParser
58
TuringMachine
RegularExpression
ParsingParsingContextsContexts
59
String
String
ParsingContext
Parsing Context
TuringMachine
RegularExpression
ParsingParsingContextsContexts
60
String
String
ParsingContext
Parsing Context
Framework Implementors
Grammar Implementor
N (N T)*← ∪
TuringMachine
RegularExpression
ParsingParsingContextsContexts
61
String
String
ParsingContext
Parsing Context
indentation
typedef
tolerant parsing
recursion
ParsingParsingContextsContexts
Framework Implementors
Grammar Implementor
N (N T)*← ∪
TuringMachine
RegularExpression
ParsingParsingContextsContexts