PetitParser is a dynamic parser framework combining ideas from scannerless parsing, parser combinators, parsing expression grammars and packrat parsers. In this hands-on session we learn how to build simple parsers and how to model, test, compose and reuse complex grammars. Additionally, we will look at some tools and the reflective facilities provided by the PetitParser framework. Basic knowledge of the Smalltalk programming language is a requirement. Bring your laptop to reproduce the examples and solve some simple tasks.
11. Parser Terminals
$a
asParser
letter ‘a’
'abc'
asParser
string ‘abc’
These factory methods are defined in
PPPredicateObjectParser
class
#any
asParser any character
#digit
asParser the digits 0..9
#letter
asParser the letters a..z and A..Z
nil
asParser the empty parser
12. Parser Operators
p1
,
p2
sequence
p1
/
p2
ordered choice
p
star
zero-or-more (0..*)
p
plus
one-or-more (1..*)
p
optional
zero-or-one (0..1)
see the operations
p1
separatedBy:
p2
protocols in PPParser
p1
delimitedBy:
p2 for more operators
13. Parser Predicates
p
and
conjunction
(non-consuming look-ahead)
p
not
negation
(non-consuming look-ahead)
p
end
end of input
14. Parser Actions
p
==>
[
:arg
|
] transformation
p
flatten
create string
p
token
create token
p
trim trim whitespaces
see the operations-mapping
protocol in PPParser
for more actions
15. term
::=
prod
"+"
term
|
prod
;
prod
::=
prim
"*"
prod
|
prim
;
prim
::=
"("
term
")"
|
number
;
number
::=
"0"
..
"9"
;
16. number
:=
#digit
asParser
plus
flatten
trim
==>
[
:string
|
string
asNumber
].
term
:=
PPUnresolvedParser
new.
prod
:=
PPUnresolvedParser
new.
prim
:=
PPUnresolvedParser
new.
term
def:
(prod
,
$+
asParser
trim
,
term
==>
[
:nodes
|
nodes
first
+
nodes
last
])
/
prod.
prod
def:
(prim
,
$*
asParser
trim
,
prod
==>
[
:nodes
|
nodes
first
*
nodes
last
])
/
prim.
prim
def:
($(
asParser
trim
,
term
,
$)
asParser
trim
==>
[
:nodes
|
nodes
second
])
/
number.
start
:=
term
end.
36. Transformations
p
replace:
p1
with:
p2.
p
transform:
[
:parser
|
].
Like #collect: on Collection, but
transforms the whole grammar graph.
37. Pattern Searching
A placeholder
matching any parser
found
:=
PPSearcher
new
matches:
PPPattern
any
star
plus
do:
[
:parser
:answer
|
parser
];
execute:
p
initialAnswer:
nil
Grammar to
be searched
38. Pattern Rewriting
Same pattern used in
search and replace.
pattern
:=
PPPattern
any.
rewritten
:=
PPRewriter
new
replace:
pattern
star
plus
with:
pattern
star;
execute:
p
Grammar to
be rewritten
39. Grammar Optimization
fast
:=
slow
optimize
Many behavior preserving
rewrite rules applied for you.
40. 3000000
2000000
chars/sec
1000000
0
LALR PetitParser Hand-Written
Old VM Cog VM