Incremental Packrat Parsing

Incremental Packrat Parsing

Incremental Packrat Parsing Patrick Dubroy Alessandro Warth Y Combinator Research, USA Y Combinator Research, USA [email protected] [email protected] Abstract techniques for reducing the size of the parser’s memo ta- Packrat parsing is a popular technique for implementing ble [1, 8, 11, 16]. top-down, unlimited-lookahead parsers that operate in guar- In this paper, we approach the packrat parser’s memo anteed linear time. In this paper, we describe a method for table not as a problem, but as an opportunity. Namely, we turning a standard packrat parser into an incremental parser present a straightforward modification to the memoization through a simple modification to its memoization strategy. mechanism used by packrat parsers that enables them to By łincremental”, we mean that the parser can perform syn- support incremental parsing [6, 7]. This is particularly useful tax analysis without completely reparsing the input after in an interactive setting such as a code editor or an Integrated each edit operation. This makes packrat parsing suitable for Development Environment (IDE) where, regardless of the interactive use in code editors and IDEs — even with large input size, near-instantaneous parse times are required in inputs. Our experiments show that with our technique, an order to provide syntax highlighting, type checking, etc. as incremental packrat parser for JavaScript can outperform part of a responsive user experience. even a hand-optimized, non-incremental parser. The main contributions of this paper are: CCS Concepts · Software and its engineering → Parsers; Keywords packrat parsing, incremental parsing (a) An algorithm for interactive packrat parsing, which is (to our knowledge) the first such algorithm. It requires ACM Reference Format: Patrick Dubroy and Alessandro Warth. 2017. Incremental Packrat no changes to the grammars to support incrementality. Parsing. In Proceedings of 2017 ACM SIGPLAN International Confer- (b) Two modifications to the core data structure of packrat ence on Software Language Engineering (SLE’17). ACM, New York, parsing, the memo table, that allow our algorithm to NY, USA, 12 pages. https://doi.org/10.1145/3136014.3136022 perform efficiently on large inputs with real-world grammars. 1 Introduction (c) The JavaScript source code for a simple packrat parser, and for an incremental one based on our algorithm. To- Packrat parsers [3, 4] are backtracking, recursive-descent gether, they show precisely what changes are required parsers that support unlimited lookahead while guaranteeing to make a standard packrat parser incremental. linear parse times. They do this łby saving all intermediate parsing results as they are computed and ensuring that no result is evaluated more than once.” [4] A well-known disadvantage of this technique is its large Experiments with our prototype implementation in an memory footprint: because a packrat parser łliterally squir- interactive setting (see Section 4) show that the proposed rels away everything it has ever computed about the input modification introduces only a small memory overhead (ap- text” [4], its memory consumption also grows linearly with prox. 12%) and results in a huge speedup (two orders of the size of the input. While this usually isn’t a problem for magnitude) compared to a standard packrat parser. When moderately-sized inputs, to make packrat parsing practical used interactively, our prototype is also faster than Acorn [9], for larger inputs, researchers have introduced a number of a best-of-breed, non-incremental JavaScript parser. The rest of this paper is structured as follows: Section 2 Permission to make digital or hard copies of all or part of this work for provides a brief overview of packrat parsing. Section 3 de- personal or classroom use is granted without fee provided that copies scribes our modification to the memoization mechanism, and are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights two optimizations that make the algorithm more efficient. for components of this work owned by others than the author(s) must Section 4 discusses the effects of our strategy on parse times, be honored. Abstracting with credit is permitted. To copy otherwise, or when used in batch mode as well as incrementally, and com- republish, to post on servers or to redistribute to lists, requires prior specific pares the performance of our prototype to that of Acorn, permission and/or a fee. Request permissions from [email protected]. a popular non-incremental JavaScript parser. Section 5 dis- SLE’17, October 23–24, 2017, Vancouver, Canada cusses related work, and Section 6 concludes. The appendix © 2017 Copyright held by the owner/author(s). Publication rights licensed to Association for Computing Machinery. presents the full JavaScript source code for an incremental ACM ISBN 978-1-4503-5525-4/17/10...$15.00 parser based on our algorithm. https://doi.org/10.1145/3136014.3136022 14 SLE’17, October 23–24, 2017, Vancouver, Canada Patrick Dubroy and Alessandro Warth 2 An Overview of Packrat Parsing 8 9 6 – 7 8 9 6 – 7 The key idea of packrat parsing is that by memoizing all intermediate parse results as they are computed, it’s possi- 0 1 2 3 4 5 0 1 2 3 4 ble to guarantee linear parse times even in the presence of digit … backtracking and unlimited lookahead.1 To understand how digit digit this works, consider the following grammar fragment from ✗digit nextPos 2 2 digit a simple language of arithmetic expressions : num tree … digit ✗digit nextPos 1 expr = num ”+” num digit — num ”-” num num tree … expr nextPos 3 num num = digit+ tree … nextPos 5 expr digit = ”0”..”9” tree … When a recursive-descent parser attempts to match the input ł869–7” with the expr rule shown above, it begins with Figure 1. Contents of a packrat parser’s memo table after the first alternative, successfully parsing ł896-7”. Left: a compact representation that is used throughout this paper, showing the consumed num ”+” num interval for successful applications (failed applications in The first term, num, matches a sequence of one or more digits. italics). Right: a detail view showing the contents of the first Here, it succeeds after consuming the first three characters two columns. from the input stream (ł869”). Next, the parser attempts to match a ł+” character, which fails because the next character in the input stream is ł-”. This causes the parser to backtrack • nextPos, which is an offset into the input string indi- to position 0, which is where the current alternative started. cating where the remaining input begins, and Then, the parser tries the second alternative: • tree, which contains a parse tree if the application Fail num ”-” num succeeded, or the special value . Each time a rule r is applied at position p, the parser checks At this point, a conventional recursive-descent parser to see if there is a memo table entry for r at that position. If would apply the num rule again, duplicating work that was an entry exists, the parser’s current position is updated to done for the first alternative — i.e., matching and consuming nextPos and the value stored in tree is returned. If not, the the digits at position 0, 1, and 2. In a packrat parser, however, parser evaluates r and records the results in a new memo the result of applying num at position 0 is memoized on the table entry. In this way, a packrat parser ensures that no rule first attempt, so almost no work is required this time around. is ever evaluated more than once at a given position. Because the parser already łknows” that num succeeds at position 0 after consuming three characters, it can simply update the position to 3 and attempt to match the next part of An important property of packrat parsing and its mem- the pattern (ł-”), which succeeds. Finally, the parser applies oization mechanism is that łthe parsing function for num at position 4, which consumes the final character (ł7”) each nonterminal depends only on the input string, and and causes the entire parse of expr to succeed. not on any other information accumulated during the parsing process.” [3] 2.1 Memoization in Packrat Parsers In other words, individual memo table entries When an intermediate parsing result is memoized (e.g., the neither capture nor depend upon the specific state result of matching num at position 0), the result is stored in of the parser – łthere is ‘only one way’ to parse a the parser’s memo table. The memo table can be modeled as given nonterminal at any given input position.” [3] This an m × n matrix, with a column for each of the n characters property is central to our incremental packrat parsing in the input, and one row for each rule in the grammar. We algorithm, which is described in the next section. refer to each matrix element as a memo table entry. Figure 1 shows the contents of the memo table (using a sparse matrix representation) after matching expr as de- 3 Incremental Packrat Parsing scribed above. Each memo table entry has two fields: The goal of incremental parsing is to efficiently reparsea modified input string by reusing as much as possible fromthe 1 Packrat parsers are said to support łunlimited lookahead” [4] because in previous result(s). Conveniently, a regular (non-incremental) packrat parsing there is little practical difference between backtracking and conventional lookahead (i.e., examining but not consuming tokens). packrat parser records all of its intermediate results in a 2The concrete syntax is based on a variant of parsing expression gram- memo table — but that memo table is discarded when parsing mars [5], a grammar formalism closely associated with packrat parsing.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 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