Parser Combinator Compiler (Pc-Compiler)

Parser Combinator Compiler (Pc-Compiler)

Optimizing Parser Combinators Jan Kurˇs†, Jan Vran´y‡, Mohammad Ghafari†, Mircea Lungu¶ and Oscar Nierstrasz† †Software Composition Group, ‡Software Engineering Group, ¶Software Engineering and University of Bern, Czech Technical University, Architecture Group, Switzerland Czech Republic Universtiy of Groningen, scg.unibe.ch swing.fit.cvut.cz Netherlands http://www.cs.rug.nl/search Abstract also powerful in their expressiveness as they can parse Parser combinators are a popular approach to pars- beyond context-free languages (e.g., layout-sensitive ing. Parser combinators follow the structure of an un- languages (Hutton and Meijer 1996; Adams and A˘gacan derlying grammar, are modular, well-structured, easy 2014)). to maintain, and can recognize a large variety of lan- Nevertheless, parser combinators yet remain a tech- guages including context-sensitive ones. However, their nology for prototyping and not for the actual deploy- universality and flexibility introduces a noticeable per- ment. Firstly, naive implementations do not handle left- formance overhead. Time-wise, parser combinators can- recursive grammars, though there are already solutions not compete with parsers generated by well-performing to this issue (Frost et al. 2007; Warth et al. 2008). Sec- parser generators or optimized hand-written code. ondly, the expressive power of parser combinators comes Techniques exist to achieve a linear asymptotic per- at a price of less efficiency. A parser combinator uses formance of parser combinators, yet there is still a sig- the full power of a Turing-equivalent formalism to rec- nificant constant multiplier. This can be further lowered ognize even simple languages that could be recognized using meta-programming techniques. by finite state machines or pushdown automata. That In this work we present a more traditional approach is, parser combinators cannot reach the peak perfor- to optimization — a compiler — applied to the do- mance of parser generators (Levine 2009), hand-written main of parser combinators. A parser combinator com- parsers or optimized code (see section 5 or (B´eguetand piler (pc-compiler) analyzes a parser combinator, ap- Jonnalagedda 2014)§4). plies parser combinator-specific optimizations and, gen- Meta-programming approaches such as macros (Bur- erates an equivalent high-performance top-down parser. mako 2013) and staging (Rompf and Odersky 2010) Such a compiler preserves the advantages of parser com- have been applied to Scala parser combinators (Moors binators while complementing them with better perfor- et al. 2008) with significant performance improve- mance. ments (B´eguetand Jonnalagedda 2014; Jonnalagedda et al. 2014). In general, these approaches remove compo- 1. Introduction sition overhead and intermediate representations. Many other parser combinator implementations battle perfor- Parser combinators (Wadler 1995; Moors et al. 2008) mance problems by using efficient structures, macros represent a popular approach to parsing. They are etc. (see Parboiled 2,1 attoparsec2 or FParsec3). straightforward to construct, readable, modular, well- Nevertheless, none of the proposed optimizations yet structured and easy to maintain. Parser combinators are reach the performance of hand-written code. In our work we focus on the performance of PetitParser (Reng- gli et al. 2010; Kurˇset al. 2013) — a parser combi- nator framework utilizing packrat parsing (Ford 2002). We present a parser combinator compiler (pc-compiler) 1 https://github.com/sirthias/parboiled2 2 http://www.serpentine.com/blog/2014/05/31/attoparsec/ [Copyright notice will appear here once ’preprint’ option is removed.] 3 http://www.quanttec.com/fparsec/ 1 2016/7/8 that utilizes standard compiler techniques and applies Operator Description optimizations tailored to parser combinators. Based on 0 0 a compile-time analysis of a grammar, we avoid compo- Literal string sition overhead, reduce object allocations, and optimize [ ] Character class choices to minimize the backtracking overhead when- [ ] negate Complement of a character class ever possible. The result of “compilation” is an opti- #letter Characters [a-zA-Z] mized top-down parser that provides performance as #digit Characters [0-9] fast as hand-written code. In particular, based on our #space Characters [\t\n ] benchmarks covering five different grammars for Petit- e? Optional Parser,4 a pc-compiler offers a performance improve- e∗ Zero or more ment of a factor ranging from two to ten, depending on e+ One or more the grammar. Based on our Smalltalk case study, our &e And-predicate approach is 20% slower compared to a highly-optimized, !e Not-predicate hand-written parser. e1 e2 Sequence The paper makes the following contributions: i) ap- e1/e2 Prioritized Choice plication of compiler-technology approaches to the do- e map: action Semantic Action main of parser combinators; ii) an identification of per- e token Build a token formance bottlenecks for PetitParser; iii) a description of optimization techniques addressing these bottlenecks; Table 1. PetitParser operators and iv) an analysis of their effectiveness. The paper is organized as follows: In section 2 we rule is a "class " keyword followed by a space. Identi- introduce PetitParser, and in section 3 we describe its fiers start with a letter followed by any number of letters performance bottlenecks. In section 4 we introduce a or digits. Class keyword and identifiers are transformed pc-compiler — a framework to deal with problems de- into instances of Token , which keep information about scribed in section 3. In section 5 we provide a detailed start and end positions and the string value of a token. performance analysis of a pc-compiler and its optimiza- There is a semantic action associated to a class rule tions. In section 6 we describe other approaches to im- that creates an instance of ClassNode filled with an prove parser combinators performance, and we briefly identifier value and a class body. discuss similarities and differences between them and A class body is indentation-sensitive, i.e., indent our approach. Finally, section 7 concludes this paper. and dedent determine the scope of a class (instead of 2. Petit Parser commonly used brackets e.g., f and g ). The indent In this section we introduce PetitParser and inspect in and dedent rules determine whether a line is indented, detail its implementation. In the following section we i.e., on a column strictly greater than the previous line describe performance bottlenecks of this implementa- or dedented, i.e., column strictly smaller than the pre- tion. vious line. The indent and dednet rules are repre- PetitParser (Renggli et al. 2010; Kurˇset al. 2013) sented by specialized action parsers that manipulate an indentation stack by pushing and popping the current is a parser combinator framework (Hutton and Meijer 9 1996) that utilizes packrat parsing (Ford 2002), scanner- indentation level, similarly to the scanner of Python. less parsing (Visser 1997) and parsing expression gram- The class body contains a sequence of classes and meth- mars (PEGs) (Ford 2004). PetitParser is implemented ods. 5 6 7 8 in Pharo, Smalltalk/X, Java and Dart. 2.1 Deep into PetitParser PetitParser uses an internal DSL similar to a stan- dard PEG syntax as briefly described in Table 1. A Figure 1 shows a composition of parser combinators grammar fragment describing a simple programming that are created after evaluating the code in Listing 1. language is shown in Listing 1. This composition is created “ahead-of-time” before a A program in this grammar consists of a non-empty parse attempt takes place and can be reused for multiple sequence of classes. A class starts with classToken , parse attempts. followed by an idToken and body . The classToken The root program rule is translated into a Plus parser referencing the class combinator. The class 4 arithmetic expressions, Java, Smalltalk and Python rule is an Action parser — a parser that evaluates 5 http://smalltalkhub.com/#!/˜Moose/PetitParser a block — specified by a map: parameter, the argu- 6 https://bitbucket.org/janvrany/stx-goodies-petitparser 7 https://github.com/petitparser/java-petitparser 9 https://docs.python.org/3/reference/lexical_naalysis. 8 https://github.com/petitparser/dart-petitparser html#indentation 2 2016/7/8 letterOrDigit← #letter / #digit identifier ← (#letter letterOrDigit*) Plus idToken ← identifier token program classToken ← (’class’ &#space) token class ← classToken idToken body Action class ClassNode new map: [:classToken :idToken :body | id: idToken value ClassNode new map: body: body name: idToken value; Sequence body: body] body ← indent Token Token Sequence (class / method)* classToken idToken body dedent program ← class+ … Sequence Indent Star Dedent Listing 1. Simple grammar in PetitParser DSL. identifier indent dedent CharClass Star Choice ments being collected from the result of an underlying #letter Sequence parser. The idToken and classToken rules are Token Choice Action letterOrDigit method parsers, which consume whitespace and create token in- stances. The ’class’ rule is a Literal parser, which is a leaf combinator and does not refer to any other com- CharClass … #digit binators. The classToken rule contains a sequence of Literal and AndPredicate combinators (omitted in the Figure 1). The identifier rule is a sequence of Figure 1. The structure of parser combinators created CharClass and Star parsers. The Choice parser after evaluating the code of Listing 1. letterOrDigit shares the CharClass parser with its identifier grand-parent.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    15 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us