Petitparser at the Deep into Smalltalk School 2011

6,637 views
6,843 views

Published on

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.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,637
On SlideShare
0
From Embeds
0
Number of Embeds
4,153
Actions
Shares
0
Downloads
42
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Petitparser at the Deep into Smalltalk School 2011

  1. 1. with Lukas Renggliwww.tudorgirba.combased on the work of Lukas Renggliwww.lukas-renggli.ch
  2. 2. with Lukas Rengglibuilt by Lukas Rengglideeply integrated with Smalltalkpart of the Moose Suite
  3. 3. input parse output
  4. 4. Mastering Grammars with parse Lukas Renggli
  5. 5. 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
  6. 6. IDENTIFIER ::= letter ( letter | digit ) * a..z a..z 0..9
  7. 7. identifier := #letter asParser , ( #letter asParser / #digit asParser ) star. a..z a..z 0..9
  8. 8. exercise
  9. 9. identifier := #letter asParser , ( #letter asParser / #digit asParser ) star. a..z a..z 0..9
  10. 10. identifier := #letter asParser , ( #letter asParser / #digit asParser ) star. sequence letter many choice letter digit
  11. 11. identifier := #letter asParser , ( #letter asParser / #digit asParser ) star. sequence letter manyparsers are objects choice letter digit
  12. 12. terminals$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
  13. 13. terminals are defined in PPPredicateObjectParserexercise:!browse!PPPredicateObjectParser
  14. 14. combinatorsp1 , p2 parse p1 followed by p2 (sequence)p1 / p2 parse p1, otherwise parse p2 (ordered choice)p star parse zero or more pp plus parse one or more pp optional parse p if possiblepredicatesp not negation (non-consuming look-ahead)p negate negation (consuming)p end end of input
  15. 15. PPParser is the root of all parsersall operations are defined in this class
  16. 16. exercise:!browse!PPParser!operations
  17. 17. exercise
  18. 18. actionsp ==> 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.
  19. 19. string := $ asParser , $ asParser negate star , $ asParser.
  20. 20. string := $ asParser , $ asParser negate star flatten , $ asParser ==> [:token | token second ].
  21. 21. stringText := $ asParser negate star flatten.string := $ asParser , stringText , $ asParser ==> [:token | token second ].
  22. 22. exercise
  23. 23. Root := Document ?Document := OPEN ElementNode * CLOSE exerciseElementNode := 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 := """ [^"] * """( (FAMIX.Package (name PackageP)) (FAMIX.Class (name ClassA)) (FAMIX.Method (name methodM)))
  24. 24. stringText := $ asParser negate star flatten.string := $ asParser , stringText , $ asParser ==> [:token | token second ].
  25. 25. stringText := PPUnresolvedParser new.string := $ asParser , stringText , $ asParser ==> [:token | token second ].stringText def: ($ asParser negate star flatten).
  26. 26. Root := Document ?Document := OPEN ElementNode * CLOSE exerciseElementNode := 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 := """ [^"] * """
  27. 27. 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))))
  28. 28. scripting = ! nice for prototyping ! but messy
  29. 29. subclass PPCompositeParserstart = default start parser
  30. 30. externally, parsers map on methodsinternally, parsers map on instance variables
  31. 31. to specify actions, subclass the base grammar
  32. 32. subclass tests from PPCompositeParserTestspecify the parserClass
  33. 33. use #parse:rule: to check the grammar
  34. 34. subclass to check the parser result
  35. 35. PetitParser comes with a dedicated user interfacePPBrowser open.
  36. 36. with Lukas Rengglibuilt by Lukas Rengglideeply integrated with Smalltalkpart of the Moose Suite
  37. 37. Tudor Gîrba www.tudorgirba.comcreativecommons.org/licenses/by/3.0/

×