Petitparser at the Deep into Smalltalk School 2011

40
www.tudorgirba.com based on the work of Lukas Renggli www.lukas-renggli.ch

description

I used this set of slides for a tutorial I gave at the Deep into Smalltalk School 2011 held at INRIA, Lille on March 10, 2011.

Transcript of Petitparser at the Deep into Smalltalk School 2011

Page 1: Petitparser at the Deep into Smalltalk School 2011

www.tudorgirba.com

based on the work of Lukas Renggliwww.lukas-renggli.ch

Mastering Grammarswith

Lukas Renggli

Page 2: Petitparser at the Deep into Smalltalk School 2011

Mastering Grammarswith

Lukas Rengglibuilt by Lukas Rengglideeply integrated with Smalltalkpart of the Moose Suite

Page 3: Petitparser at the Deep into Smalltalk School 2011

input outputparse

Page 4: Petitparser at the Deep into Smalltalk School 2011

input outputparse

Mastering Grammarswith

Lukas Renggli

Page 5: Petitparser at the Deep into Smalltalk School 2011

Root := Document ?Document := OPEN ElementNode * CLOSEElementNode := OPEN ELEMENTNAME AttributeNode * CLOSEAttributeNode := OPEN SIMPLENAME ValueNode * CLOSEValueNode := Primitive | ElementNodePrimitive := STRING | NUMBEROPEN := "("CLOSE := ")"ELEMENTNAME := letter ( letter | digit ) * ( "." letter ( letter | digit ) ) *SIMPLENAME := letter ( letter | digit ) *NUMBER := "-" ? digit + ( "." digit + ) ? ( ( "e" | "E" ) ( "-" | "+" ) ? digit + ) ?STRING := ( "'" [^'] * "'" ) +digit := [0-9] letter := [a-zA-Z_]comment := """ [^"] * """

target

Page 6: Petitparser at the Deep into Smalltalk School 2011
Page 7: Petitparser at the Deep into Smalltalk School 2011

IDENTIFIER ::=letter ( letter | digit ) *

a..z a..z

0..9

Page 8: Petitparser at the Deep into Smalltalk School 2011

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

a..z a..z

0..9

Page 9: Petitparser at the Deep into Smalltalk School 2011

exercise

Page 10: Petitparser at the Deep into Smalltalk School 2011

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

a..z a..z

0..9

Page 11: Petitparser at the Deep into Smalltalk School 2011

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

letter

sequence

many

choice

letter digit

Page 12: Petitparser at the Deep into Smalltalk School 2011

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

parsers are objects

letter

sequence

many

choice

letter digit

Page 13: Petitparser at the Deep into Smalltalk School 2011

$c asParser parse character “c”

‘string’ asParser parse string “string”

#any asParser parse any character

#digit asParser parse one digit

#letter asParser parse one letter

terminals

Page 14: Petitparser at the Deep into Smalltalk School 2011

terminals are defined in PPPredicateObjectParser

exercise:!browse!PPPredicateObjectParser

Page 15: Petitparser at the Deep into Smalltalk School 2011

p1 , p2 parse p1 followed by p2 (sequence)

p1 / p2 parse p1, otherwise parse p2 (ordered choice)

p star parse zero or more p

p plus parse one or more p

p optional parse p if possible

combinators

predicatesp not negation (non-consuming look-ahead)

p negate negation (consuming)

p end end of input

Page 16: Petitparser at the Deep into Smalltalk School 2011

PPParser is the root of all parsers

all operations are defined in this class

Page 17: Petitparser at the Deep into Smalltalk School 2011

exercise:!browse!PPParser!operations

Page 18: Petitparser at the Deep into Smalltalk School 2011

exercise

Page 19: Petitparser at the Deep into Smalltalk School 2011

p ==> aBlock Transforms the result of p through aBlock.

p flatten Creates a string from the result of p.

p token Creates a token from the result of p.

p trim Trims whitespaces before and after p.

actions

Page 20: Petitparser at the Deep into Smalltalk School 2011
Page 21: Petitparser at the Deep into Smalltalk School 2011

string := $' asParser , $' asParser negate star , $' asParser.

Page 22: Petitparser at the Deep into Smalltalk School 2011

string := $' asParser , $' asParser negate star flatten , $' asParser ==> [:token | token second ].

Page 23: Petitparser at the Deep into Smalltalk School 2011

stringText := $' asParser negate star flatten.string := $' asParser , stringText , $' asParser ==> [:token | token second ].

Page 24: Petitparser at the Deep into Smalltalk School 2011

exercise

Page 25: Petitparser at the Deep into Smalltalk School 2011

Root := Document ?Document := OPEN ElementNode * CLOSEElementNode := OPEN ELEMENTNAME AttributeNode * CLOSEAttributeNode := OPEN SIMPLENAME ValueNode * CLOSEValueNode := Primitive | ElementNodePrimitive := STRING | NUMBEROPEN := "("CLOSE := ")"ELEMENTNAME := letter ( letter | digit ) * ( "." letter ( letter | digit ) *)SIMPLENAME := letter ( letter | digit ) *NUMBER := "-" ? digit + ( "." digit + ) ? ( ( "e" | "E" ) ( "-" | "+" ) ? digit + ) ?STRING := ( "'" [^'] * "'" ) +digit := [0-9] letter := [a-zA-Z_]comment := """ [^"] * """

exercise

( (FAMIX.Package (name 'PackageP')) (FAMIX.Class (name 'ClassA')) (FAMIX.Method (name 'methodM')))

Page 26: Petitparser at the Deep into Smalltalk School 2011

stringText := $' asParser negate star flatten.string := $' asParser , stringText , $' asParser ==> [:token | token second ].

Page 27: Petitparser at the Deep into Smalltalk School 2011

stringText := PPUnresolvedParser new.string := $' asParser , stringText , $' asParser ==> [:token | token second ].stringText def: ($' asParser negate star flatten).

Page 28: Petitparser at the Deep into Smalltalk School 2011
Page 29: Petitparser at the Deep into Smalltalk School 2011

Root := Document ?Document := OPEN ElementNode * CLOSEElementNode := OPEN ELEMENTNAME Serial ? AttributeNode * CLOSESerial := OPEN ID INTEGER CLOSEAttributeNode := OPEN SIMPLENAME ValueNode * CLOSEValueNode := Primitive | Reference | ElementNodePrimitive := STRING | NUMBER | Boolean | UnlimitedBoolean := TRUE | FALSEUnlimited := NILReference := IntegerReference | NameReferenceIntegerReference := OPEN REF INTEGER CLOSENameReference := OPEN REF ELEMENTNAME CLOSEOPEN := "("CLOSE := ")"ID := "id:"REF := "ref:"TRUE := "true"FALSE := "false"ELEMENTNAME := letter ( letter | digit ) * ( "." letter ( letter | digit ) ) *SIMPLENAME := letter ( letter | digit ) *INTEGER := digit +NUMBER := "-" ? digit + ( "." digit + ) ? ( ( "e" | "E" ) ( "-" | "+" ) ? digit + ) ?STRING := ( "'" [^'] * "'" ) +digit := [0-9] letter := [a-zA-Z_]comment := """ [^"] * """

exercise

Page 30: Petitparser at the Deep into Smalltalk School 2011

exercise

( (FAMIX.Package (id: 1) (name 'PackageP')) (FAMIX.Class (id: 2) (name 'ClassA') (parentPackage (ref: 1))) (FAMIX.Method (id: 3) (name 'methodA') (declaredType (ref: 1)) (parentType (ref: 2))))

Page 31: Petitparser at the Deep into Smalltalk School 2011

scripting = !nice for prototyping ! but messy

Page 32: Petitparser at the Deep into Smalltalk School 2011

subclass PPCompositeParser

start = default start parser

Page 33: Petitparser at the Deep into Smalltalk School 2011

externally, parsers map on methods

internally, parsers map on instance variables

Page 34: Petitparser at the Deep into Smalltalk School 2011

to specify actions, subclass the base grammar

Page 35: Petitparser at the Deep into Smalltalk School 2011

subclass tests from PPCompositeParserTest

specify the parserClass

Page 36: Petitparser at the Deep into Smalltalk School 2011

use #parse:rule: to check the grammar

Page 37: Petitparser at the Deep into Smalltalk School 2011

subclass to check the parser result

Page 38: Petitparser at the Deep into Smalltalk School 2011

PetitParser comes with a dedicated user interface

PPBrowser open.

Page 39: Petitparser at the Deep into Smalltalk School 2011

Mastering Grammarswith

Lukas Rengglibuilt by Lukas Rengglideeply integrated with Smalltalkpart of the Moose Suite