SlideShare a Scribd company logo
1 of 40
Download to read offline
with




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




            Lukas Renggli
built by Lukas Renggli
deeply integrated with Smalltalk
part of the Moose Suite
input   parse   output
Mastering Grammars
          with



      parse
      Lukas Renggli
Root := Document ?
Document := OPEN ElementNode * CLOSE
ElementNode := OPEN ELEMENTNAME AttributeNode * CLOSE
AttributeNode := OPEN SIMPLENAME ValueNode * CLOSE
ValueNode := Primitive | ElementNode
Primitive := STRING | NUMBER
OPEN := "("
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
IDENTIFIER ::=	letter
               ( letter |
                 digit ) *




         a..z          a..z


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




         a..z          a..z


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




         a..z          a..z


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


             sequence



    letter               many



                        choice



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


               sequence



      letter               many



parsers are objects       choice



                letter             digit
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
terminals are defined in PPPredicateObjectParser




exercise:!browse!PPPredicateObjectParser
combinators
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



predicates
p not         negation (non-consuming look-ahead)

p negate      negation (consuming)

p end         end of input
PPParser is the root of all parsers




all operations are defined in this class
exercise:!browse!PPParser!operations
exercise
actions
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.
string   := $' asParser ,
            $' asParser negate star ,
            $' asParser.
string   := $' asParser ,
            $' asParser negate star flatten ,
            $' asParser
            ==> [:token | token second ].
stringText := $' asParser negate star flatten.
string     := $' asParser ,
              stringText ,
              $' asParser
              ==> [:token | token second ].
exercise
Root := Document ?
Document := OPEN ElementNode * CLOSE

                                                                      exercise
ElementNode := OPEN ELEMENTNAME AttributeNode * CLOSE
AttributeNode := OPEN SIMPLENAME ValueNode * CLOSE
ValueNode := Primitive | ElementNode
Primitive := STRING | NUMBER
OPEN := "("
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'))
)
stringText   := $' asParser negate star flatten.
string       := $' asParser ,
                stringText ,
                $' asParser
                ==> [:token | token second ].
stringText   := PPUnresolvedParser new.
string       := $' asParser ,
                stringText ,
                $' asParser
                ==> [:token | token second ].
stringText def: ($' asParser negate star flatten).
Root := Document ?
Document := OPEN ElementNode * CLOSE
                                                                     exercise
ElementNode := OPEN ELEMENTNAME Serial ? AttributeNode * CLOSE
Serial := OPEN ID INTEGER CLOSE
AttributeNode := OPEN SIMPLENAME ValueNode * CLOSE
ValueNode := Primitive | Reference | ElementNode
Primitive := STRING | NUMBER | Boolean | Unlimited
Boolean := TRUE | FALSE
Unlimited := NIL
Reference := IntegerReference | NameReference
IntegerReference := OPEN REF INTEGER CLOSE
NameReference := OPEN REF ELEMENTNAME CLOSE
OPEN := "("
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


(
    (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)))
)
scripting = ! nice for prototyping
            ! but messy
subclass PPCompositeParser




start = default start parser
externally, parsers map on methods




internally, parsers map on instance variables
to specify actions, subclass the base grammar
subclass tests from PPCompositeParserTest




specify the parserClass
use #parse:rule: to check the grammar
subclass to check the parser result
PetitParser comes with a dedicated user interface




PPBrowser open.
with




            Lukas Renggli
built by Lukas Renggli
deeply integrated with Smalltalk
part of the Moose Suite
Tudor Gîrba
        www.tudorgirba.com




creativecommons.org/licenses/by/3.0/

More Related Content

What's hot

Decorators in Python
Decorators in PythonDecorators in Python
Decorators in Python
Ben James
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
mussawir20
 
Advanced perl finer points ,pack&unpack,eval,files
Advanced perl   finer points ,pack&unpack,eval,filesAdvanced perl   finer points ,pack&unpack,eval,files
Advanced perl finer points ,pack&unpack,eval,files
Shankar D
 
Python decorators
Python decoratorsPython decorators
Python decorators
Alex Su
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
ujihisa
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
Yandex
 

What's hot (20)

UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Awk programming
Awk programming Awk programming
Awk programming
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
 
Decorators in Python
Decorators in PythonDecorators in Python
Decorators in Python
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
 
Write Your Own Compiler in 24 Hours
Write Your Own Compiler in 24 HoursWrite Your Own Compiler in 24 Hours
Write Your Own Compiler in 24 Hours
 
Cleancode
CleancodeCleancode
Cleancode
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented Perl
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Park
 
Advanced perl finer points ,pack&unpack,eval,files
Advanced perl   finer points ,pack&unpack,eval,filesAdvanced perl   finer points ,pack&unpack,eval,files
Advanced perl finer points ,pack&unpack,eval,files
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
FParsec Hands On - F#unctional Londoners 2014
FParsec Hands On -  F#unctional Londoners 2014FParsec Hands On -  F#unctional Londoners 2014
FParsec Hands On - F#unctional Londoners 2014
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 

Similar to Petitparser at the Deep into Smalltalk School 2011

Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
adrianoalmeida7
 
TI1220 Lecture 9: Parsing & interpretation
TI1220 Lecture 9: Parsing & interpretationTI1220 Lecture 9: Parsing & interpretation
TI1220 Lecture 9: Parsing & interpretation
Eelco Visser
 
Perl Xpath Lightning Talk
Perl Xpath Lightning TalkPerl Xpath Lightning Talk
Perl Xpath Lightning Talk
ddn123456
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
ujihisa
 

Similar to Petitparser at the Deep into Smalltalk School 2011 (20)

Hidden treasures of Ruby
Hidden treasures of RubyHidden treasures of Ruby
Hidden treasures of Ruby
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Ch2
Ch2Ch2
Ch2
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In Erlang
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
 
Slaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in RubySlaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in Ruby
 
TI1220 Lecture 9: Parsing & interpretation
TI1220 Lecture 9: Parsing & interpretationTI1220 Lecture 9: Parsing & interpretation
TI1220 Lecture 9: Parsing & interpretation
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
SWP - A Generic Language Parser
SWP - A Generic Language ParserSWP - A Generic Language Parser
SWP - A Generic Language Parser
 
Perl Xpath Lightning Talk
Perl Xpath Lightning TalkPerl Xpath Lightning Talk
Perl Xpath Lightning Talk
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
Tork03 LT
Tork03 LT Tork03 LT
Tork03 LT
 
あたかも自然言語を書くようにコーディングしてみる
あたかも自然言語を書くようにコーディングしてみるあたかも自然言語を書くようにコーディングしてみる
あたかも自然言語を書くようにコーディングしてみる
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
 
Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility Modules
 

More from Tudor Girba

More from Tudor Girba (20)

Beyond software evolution: Software environmentalism
Beyond software evolution: Software environmentalismBeyond software evolution: Software environmentalism
Beyond software evolution: Software environmentalism
 
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
 
GT Spotter
GT SpotterGT Spotter
GT Spotter
 
Don't demo facts. Demo stories! (handouts)
Don't demo facts. Demo stories! (handouts)Don't demo facts. Demo stories! (handouts)
Don't demo facts. Demo stories! (handouts)
 
Don't demo facts. Demo stories!
Don't demo facts. Demo stories!Don't demo facts. Demo stories!
Don't demo facts. Demo stories!
 
Humane assessment on cards
Humane assessment on cardsHumane assessment on cards
Humane assessment on cards
 
Underneath Scrum: Reflective Thinking
Underneath Scrum: Reflective ThinkingUnderneath Scrum: Reflective Thinking
Underneath Scrum: Reflective Thinking
 
1800+ TED talks later
1800+ TED talks later1800+ TED talks later
1800+ TED talks later
 
Software assessment by example (lecture at the University of Bern)
Software assessment by example (lecture at the University of Bern)Software assessment by example (lecture at the University of Bern)
Software assessment by example (lecture at the University of Bern)
 
Humane assessment: Taming the elephant from the development room
Humane assessment: Taming the elephant from the development roomHumane assessment: Taming the elephant from the development room
Humane assessment: Taming the elephant from the development room
 
Moose: how to solve real problems without reading code
Moose: how to solve real problems without reading codeMoose: how to solve real problems without reading code
Moose: how to solve real problems without reading code
 
Software Environmentalism (ECOOP 2014 Keynote)
Software Environmentalism (ECOOP 2014 Keynote)Software Environmentalism (ECOOP 2014 Keynote)
Software Environmentalism (ECOOP 2014 Keynote)
 
The emergent nature of software systems
The emergent nature of software systemsThe emergent nature of software systems
The emergent nature of software systems
 
Presenting is storytelling at Uni Zurich - slides (2014-03-05)
Presenting is storytelling at Uni Zurich - slides (2014-03-05)Presenting is storytelling at Uni Zurich - slides (2014-03-05)
Presenting is storytelling at Uni Zurich - slides (2014-03-05)
 
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
 
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
 
Demo-driven innovation teaser
Demo-driven innovation teaserDemo-driven innovation teaser
Demo-driven innovation teaser
 
Software assessment essentials (lecture at the University of Bern 2013)
Software assessment essentials (lecture at the University of Bern 2013)Software assessment essentials (lecture at the University of Bern 2013)
Software assessment essentials (lecture at the University of Bern 2013)
 
Demo-driven innovation (University of Zurich, June 2013)
Demo-driven innovation (University of Zurich, June 2013)Demo-driven innovation (University of Zurich, June 2013)
Demo-driven innovation (University of Zurich, June 2013)
 
Humane assessment with Moose at GOTO Aarhus 2011
Humane assessment with Moose at GOTO Aarhus 2011Humane assessment with Moose at GOTO Aarhus 2011
Humane assessment with Moose at GOTO Aarhus 2011
 

Recently uploaded

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Petitparser at the Deep into Smalltalk School 2011

  • 1. with Lukas Renggli www.tudorgirba.com based on the work of Lukas Renggli www.lukas-renggli.ch
  • 2. with Lukas Renggli built by Lukas Renggli deeply integrated with Smalltalk part of the Moose Suite
  • 3. input parse output
  • 4. Mastering Grammars with parse Lukas Renggli
  • 5. Root := Document ? Document := OPEN ElementNode * CLOSE ElementNode := OPEN ELEMENTNAME AttributeNode * CLOSE AttributeNode := OPEN SIMPLENAME ValueNode * CLOSE ValueNode := Primitive | ElementNode Primitive := STRING | NUMBER OPEN := "(" 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.
  • 7. IDENTIFIER ::= letter ( letter | digit ) * a..z a..z 0..9
  • 8. identifier := #letter asParser , ( #letter asParser / #digit asParser ) star. a..z a..z 0..9
  • 10. identifier := #letter asParser , ( #letter asParser / #digit asParser ) star. a..z a..z 0..9
  • 11. identifier := #letter asParser , ( #letter asParser / #digit asParser ) star. sequence letter many choice letter digit
  • 12. identifier := #letter asParser , ( #letter asParser / #digit asParser ) star. sequence letter many parsers are objects choice letter digit
  • 13. 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
  • 14. terminals are defined in PPPredicateObjectParser exercise:!browse!PPPredicateObjectParser
  • 15. combinators 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 predicates p not negation (non-consuming look-ahead) p negate negation (consuming) p end end of input
  • 16. PPParser is the root of all parsers all operations are defined in this class
  • 19. actions 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.
  • 20.
  • 21. string := $' asParser , $' asParser negate star , $' asParser.
  • 22. string := $' asParser , $' asParser negate star flatten , $' asParser ==> [:token | token second ].
  • 23. stringText := $' asParser negate star flatten. string := $' asParser , stringText , $' asParser ==> [:token | token second ].
  • 25. Root := Document ? Document := OPEN ElementNode * CLOSE exercise ElementNode := OPEN ELEMENTNAME AttributeNode * CLOSE AttributeNode := OPEN SIMPLENAME ValueNode * CLOSE ValueNode := Primitive | ElementNode Primitive := STRING | NUMBER OPEN := "(" 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')) )
  • 26. stringText := $' asParser negate star flatten. string := $' asParser , stringText , $' asParser ==> [:token | token second ].
  • 27. stringText := PPUnresolvedParser new. string := $' asParser , stringText , $' asParser ==> [:token | token second ]. stringText def: ($' asParser negate star flatten).
  • 28.
  • 29. Root := Document ? Document := OPEN ElementNode * CLOSE exercise ElementNode := OPEN ELEMENTNAME Serial ? AttributeNode * CLOSE Serial := OPEN ID INTEGER CLOSE AttributeNode := OPEN SIMPLENAME ValueNode * CLOSE ValueNode := Primitive | Reference | ElementNode Primitive := STRING | NUMBER | Boolean | Unlimited Boolean := TRUE | FALSE Unlimited := NIL Reference := IntegerReference | NameReference IntegerReference := OPEN REF INTEGER CLOSE NameReference := OPEN REF ELEMENTNAME CLOSE OPEN := "(" 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 := """ [^"] * """
  • 30. 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))) )
  • 31. scripting = ! nice for prototyping ! but messy
  • 32. subclass PPCompositeParser start = default start parser
  • 33. externally, parsers map on methods internally, parsers map on instance variables
  • 34. to specify actions, subclass the base grammar
  • 35. subclass tests from PPCompositeParserTest specify the parserClass
  • 36. use #parse:rule: to check the grammar
  • 37. subclass to check the parser result
  • 38. PetitParser comes with a dedicated user interface PPBrowser open.
  • 39. with Lukas Renggli built by Lukas Renggli deeply integrated with Smalltalk part of the Moose Suite
  • 40. Tudor Gîrba www.tudorgirba.com creativecommons.org/licenses/by/3.0/