generalised parsing and combinator parsing
play

Generalised Parsing and Combinator Parsing A Happy Marriage? L. - PowerPoint PPT Presentation

Conventional Parser Combinators Aspects of Generalised Parsing Combinators for Generalised Parsing Generalised Parsing and Combinator Parsing A Happy Marriage? L. Thomas van Binsbergen ltvanbinsbergen@acm.org Royal Holloway, University of


  1. Conventional Parser Combinators Aspects of Generalised Parsing Combinators for Generalised Parsing Generalised Parsing and Combinator Parsing A Happy Marriage? L. Thomas van Binsbergen ltvanbinsbergen@acm.org Royal Holloway, University of London October 30, 2016

  2. Conventional Parser Combinators Aspects of Generalised Parsing Combinators for Generalised Parsing Conventional Parser Combinators 1 Recursive Descent Monadic Parser Combinators Benefits of Conventional Parser Combinators Aspects of Generalised Parsing 2 Observable Sharing Derivation Representation Combinators for Generalised Parsing 3 Design Space Exploration Challenges

  3. Conventional Parser Combinators Recursive Descent Aspects of Generalised Parsing Monadic Parser Combinators Combinators for Generalised Parsing Benefits of Conventional Parser Combinators Section 1 Conventional Parser Combinators

  4. Conventional Parser Combinators Recursive Descent Aspects of Generalised Parsing Monadic Parser Combinators Combinators for Generalised Parsing Benefits of Conventional Parser Combinators Recursive Descent Parsing • Every symbol is implemented by a parse function • A parse function: Receives the input string, and a pivot Returns a new pivot, and a bit of parse tree / semantic value • The parse function for a nonterminal: Chooses one of its productions ( somehow ) Executes the symbols of the production in sequence Combinator Approach • Forget all about symbols and production • Focus on parse functions and their composition!

  5. Conventional Parser Combinators Recursive Descent Aspects of Generalised Parsing Monadic Parser Combinators Combinators for Generalised Parsing Benefits of Conventional Parser Combinators type Parser t a = [ t ] → Int → [( Int , a )] • Full power of host language available to construct parsers myparser :: ... → Parser t a Elementary matchers - create parsers from non-parser values term :: t → Parser t t pred :: ( t → Bool ) → Parser t t prefix :: [ t ] → Parser t [ t ]

  6. Conventional Parser Combinators Recursive Descent Aspects of Generalised Parsing Monadic Parser Combinators Combinators for Generalised Parsing Benefits of Conventional Parser Combinators type Parser t a = [ t ] → Int → [( Int , a )] • Full power of host language available to construct parsers myparser :: ... → Parser t a Elementary combinators - are defined by library internals • e.g. seq for placing parsers in sequence • e.g. alt for choosing between parsers alt :: Parser t a → Parser t a → Parser t a alt p q str k = p str k + + q str k

  7. Conventional Parser Combinators Recursive Descent Aspects of Generalised Parsing Monadic Parser Combinators Combinators for Generalised Parsing Benefits of Conventional Parser Combinators • Define return and > = > • Prove monadic laws Parsers are monadic! return :: a → Parser t a return a str l = ... ( > =) :: Parser t a → ( a → Parser t b ) → Parser t b > ( p > = a2q ) str l = ... >

  8. Conventional Parser Combinators Recursive Descent Aspects of Generalised Parsing Monadic Parser Combinators Combinators for Generalised Parsing Benefits of Conventional Parser Combinators • Define return and > = > • Prove monadic laws Parsers are monadic! return :: a → Parser t a return a str l = [( l , a )] ( > =) :: Parser t a → ( a → Parser t b ) → Parser t b > ( p > = a2q ) str l = [ ( r , b ) > | ( k , a ) ← p str l , ( r , b ) ← ( a2q a ) str k ]

  9. Conventional Parser Combinators Recursive Descent Aspects of Generalised Parsing Monadic Parser Combinators Combinators for Generalised Parsing Benefits of Conventional Parser Combinators What do combinators offer beyond parser generators? Easy to define alternative elementary matchers/combinators Easy to define derived combinators Some form of context-sensitivity • Advantages follow from the simplicity of the underlying algorithm • There may be other advantages specific to your application

  10. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing Section 2 Aspects of Generalised Parsing

  11. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing • Conventional P.C. do not provide any grammar information • Generalised Parsing requires explicit grammar information , for: GSS construction, memoisation, curtailing left-recursive calls ... • At the least, G.P. requires unique identifiers for symbols • At the very least, G.P. requires identifiers for recursive positions • All we need is...

  12. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing • Conventional P.C. do not provide any grammar information • Generalised Parsing requires explicit grammar information , for: GSS construction, memoisation, curtailing left-recursive calls ... • At the least, G.P. requires unique identifiers for symbols • At the very least, G.P. requires identifiers for recursive positions • All we need is... observable sharing Observable sharing Detecting that two expressions arose from the same binding Simple pure ‘solution’: Ask the programmer! The way we obtain observable sharing influences how derived combinators are defined

  13. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing X Y Z ⊕ ⊗ ⊗ a c Y Z b Z X ::= Y | Z Y ::= ‘ a ‘ ‘ b ‘ Z ::= ‘ c ‘ Z

  14. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing X Y Z ⊕ ⊗ ⊗ a c Y Z b Z X X ::= Y | Z Y ::= ‘ a ‘ ‘ b ‘ Z ::= ‘ c ‘ Z term = 1 ⊕ = (+) ⊗ = (+)

  15. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing X Y Z ⊕ ⊗ ⊗ a c Y Z b Z ⊕ X ::= Y | Z Y Z Y ::= ‘ a ‘ ‘ b ‘ Z ::= ‘ c ‘ Z term = 1 ⊕ = (+) ⊗ = (+)

  16. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing X Y Z ⊕ ⊗ ⊗ a c Y Z b Z ⊕ X ::= Y | Z ⊗ Z Y ::= ‘ a ‘ ‘ b ‘ a Z ::= ‘ c ‘ Z b term = 1 ⊕ = (+) ⊗ = (+)

  17. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing X Y Z ⊕ ⊗ ⊗ a c Y Z b Z ⊕ X ::= Y | Z ⊗ ⊗ Y ::= ‘ a ‘ ‘ b ‘ a c Z ::= ‘ c ‘ Z b Z term = 1 ⊕ = (+) ⊗ = (+)

  18. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing X Y Z ⊕ ⊗ ⊗ a c Y Z b Z ⊕ X ::= Y | Z ⊗ ⊗ Y ::= ‘ a ‘ ‘ b ‘ a c ⊗ Z ::= ‘ c ‘ Z b c term = 1 Z ⊕ = (+) ⊗ = (+)

  19. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing X Y Z “X” “Y” “Z” ⊕ ⊗ ⊗ a c Y Z b Z ⊕ X ::= Y | Z ⊗ ⊗ Y ::= ‘ a ‘ ‘ b ‘ a c ⊗ Z ::= ‘ c ‘ Z b c term = 1 Z ⊕ = (+) ⊗ = (+)

  20. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing “X” ⊕ “Y” “Z” ⊗ ⊗ a c b “Z” ⊗ c Z

  21. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing • A Generalised Parser produces a representation of all derivations • Potentially exponentially many derivations may be embedded • Downstream enumeration would result in exponential runtimes...

  22. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing Input string (Ljungl¨ of 2002 / Ridge 2014) Parser Expression SPPF Semantic values Evaluator • A Generalised Parser produces a representation of all derivations • Potentially exponentially many derivations may be embedded • Downstream enumeration would result in exponential runtimes...

  23. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing Input string (Ljungl¨ of 2002 / Ridge 2014) Parser (1) Expression SPPF Semantic values (2) (3) Evaluator How may disambiguation strategies manifest themselves? 1 A more deterministic parsers being constructed 2 Pruning the representation of all derivations (top-down) 3 Choosing between derivations by evaluation (bottom-up)

  24. Conventional Parser Combinators Observable Sharing Aspects of Generalised Parsing Derivation Representation Combinators for Generalised Parsing Input string (Ljungl¨ of 2002 / Ridge 2014) Parser (1) Expression SPPF Semantic values (2) (3) Evaluator How may disambiguation strategies manifest themselves? 1 A more deterministic parsers being constructed 2 Pruning the representation of all derivations (top-down) 3 Choosing between derivations by evaluation (bottom-up) • How to provide expressive , but opaque , strategies?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend