skip to main content
article

Packrat parsing:: simple, powerful, lazy, linear time, functional pearl

Published: 17 September 2002 Publication History

Abstract

Packrat parsing is a novel technique for implementing parsers in a lazy functional programming language. A packrat parser provides the power and flexibility of top-down parsing with backtracking and unlimited lookahead, but nevertheless guarantees linear parse time. Any language defined by an LL(k) or LR(k) grammar can be recognized by a packrat parser, in addition to many languages that conventional linear-time algorithms do not support. This additional power simplifies the handling of common syntactic idioms such as the widespread but troublesome longest-match rule, enables the use of sophisticated disambiguation strategies such as syntactic and semantic predicates, provides better grammar composition properties, and allows lexical analysis to be integrated seamlessly into parsing. Yet despite its power, packrat parsing shares the same simplicity and elegance as recursive descent parsing; in fact converting a backtracking recursive descent parser into a linear-time packrat parser often involves only a fairly straightforward structural change. This paper describes packrat parsing informally with emphasis on its use in practical applications, and explores its advantages and disadvantages with respect to the more conventional alternatives.

References

[1]
Stephen Robert Adams. Modular Grammars for Programming Language Prototyping. PhD thesis, University of Southampton, 1991.
[2]
Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, and Tools. Addison-Wesley, 1986.
[3]
Alfred V. Aho and Jeffrey D. Ullman. The Theory of Parsing, Translation and Compiling-Vol. I: Parsing. Prentice Hall, Englewood Cliffs, N.J., 1972.
[4]
Alexander Birman and Jeffrey D. Ullman. Parsing algorithms with backtrack. Information and Control, 23(1):1--34, Aug 1973.
[5]
Dmitri Bronnikov. Free Yacc-able Java(tm) grammar, 1998. http://home.inreach.com/bronikov/grammars/java.html.
[6]
Carlos Camar�o and Luc�lia Figueiredo. A monadic combinator compiler compiler. In 5th Brazilian Symposium on Programming Languages, Curitiba - PR - Brazil, May 2001. Universidade Federal do Paran�.
[7]
Luca Cardelli, Florian Matthes, and Mart�n Abadi. Extensible syntax with lexical scoping. Technical Report 121, Digital Systems Research Center, 1994.
[8]
Jeroen Fokker. Functional parsers. In Advanced Functional Programming, pages 1--23, 1995.
[9]
Bryan Ford. Packrat parsing: a practical linear-time algorithm with backtracking. Master's thesis, Massachusetts Institute of Technology, Sep 2002.
[10]
Andy Gill and Simon Marlow. Happy: The parser generator for Haskell. http://www.haskell.org/happy.
[11]
Graham Hutton and Erik Meijer. Monadic parsing in Haskell. Journal of Functional Programming, 8(4):437--444, Jul 1998.
[12]
Lillian Lee. Fast context-free grammar parsing requires fast boolean matrix multiplication. Journal of the ACM, 2002. To appear.
[13]
Daan Leijen. Parsec, a fast combinator parser. http://www.cs.uu.nl/~daan.
[14]
Terence J. Parr and Russell W. Quong. Adding semantic and syntactic predicates to LL(k): pred-LL(k). In Computational Complexity, pages 263--277, 1994.
[15]
Terence J. Parr and Russell W. Quong. ANTLR: A predicatedLL(k) parser generator. Software Practice and Experience, 25(7):789--810, 1995.
[16]
Terence John Parr. Obtaining practical variants of LL(k) and LR(k) for kρ 1 by splitting the atomic k-tuple. PhD thesis, Purdue University, Apr 1993.
[17]
Peter Pepper. LR parsing = grammar transformation + LL parsing: Making LR parsing more understandable and more efficient. Technical Report 99-5, TU Berlin, Apr 1999.
[18]
Daniel J. Salomon and Gordon V. Cormack. Scannerless NSLR(1) parsing of programming languages. In Proceedings of the ACM SIGPLAN'89 Conference on Programming Language Design and Implementation (PLDI), pages 170--178, Jul 1989.
[19]
Kuo-Chung Tai. Noncanonical SLR(1) grammars. ACM Transactions on Programming Languages and Systems, 1(2): 295--320, Oct 1979.
[20]
Masaru Tomita. Efficient parsing for natural language. Kluwer Academic Publishers, 1985.
[21]
M. G. J. van den Brand, J. Scheerder, J. J. Vinju, and E. Visser. Disambiguation filters for scannerless generalized LR parsers. In Compiler Construction, 2002.
[22]
Eelco Visser. Scannerless generalized-LR parsing. Technical Report P9707, Programming Research Group, University of Amsterdam, 1997.
[23]
Philip Wadler. How to replace failure by a list of successes: A method for exception handling, backtracking, and pattern matching in lazy functional languages. In Functional Programming Languages and Computer Architecture, pages 113--128, 1985.

Cited By

View all

Recommendations

Comments

Information & Contributors

Information

Published In

cover image ACM SIGPLAN Notices
ACM SIGPLAN Notices  Volume 37, Issue 9
September 2002
283 pages
ISSN:0362-1340
EISSN:1558-1160
DOI:10.1145/583852
Issue’s Table of Contents
  • cover image ACM Conferences
    ICFP '02: Proceedings of the seventh ACM SIGPLAN international conference on Functional programming
    October 2002
    294 pages
    ISBN:1581134878
    DOI:10.1145/581478
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies 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 for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 17 September 2002
Published in�SIGPLAN�Volume 37, Issue 9

Check for updates

Author Tags

  1. Haskell
  2. backtracking
  3. lexical analysis
  4. memoization
  5. parser combinators
  6. scannerless parsing
  7. top-down parsing

Qualifiers

  • Article

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)67
  • Downloads (Last 6 weeks)11
Reflects downloads up to 17 Oct 2024

Other Metrics

Citations

Cited By

View all
  • (2023)Reverse Template Processing Using Abstract InterpretationStatic Analysis10.1007/978-3-031-44245-2_18(403-433)Online publication date: 24-Oct-2023
  • (2023)Left Recursion by Recursive AscentConcurrency, Specification and Programming10.1007/978-3-031-26651-5_2(29-46)Online publication date: 5-May-2023
  • (2021)Generation of Custom Textual Model EditorsModelling10.3390/modelling20400322:4(609-625)Online publication date: 6-Nov-2021
  • (2021)Fast incremental PEG parsingProceedings of the 14th ACM SIGPLAN International Conference on Software Language Engineering10.1145/3486608.3486900(99-112)Online publication date: 17-Oct-2021
  • (2020)The computational power of parsing expression grammarsJournal of Computer and System Sciences10.1016/j.jcss.2020.01.001111(1-21)Online publication date: Aug-2020
  • (2020)Top-Down Parsing Error Correction Applied to Part of Speech TaggingComputational Science and Its Applications – ICCSA 202010.1007/978-3-030-58799-4_47(650-662)Online publication date: 1-Oct-2020
  • (2020)Computing the Shortest String and the Edit-Distance for Parsing Expression LanguagesDevelopments in Language Theory10.1007/978-3-030-48516-0_4(43-54)Online publication date: 26-May-2020
  • (2020)Simplified Parsing Expression DerivativesLanguage and Automata Theory and Applications10.1007/978-3-030-40608-0_30(425-436)Online publication date: 25-Feb-2020
  • (2019)PARSELF: A Practical PCRE Parser2019 4th International Conference on Computer Science and Engineering (UBMK)10.1109/UBMK.2019.8907059(167-170)Online publication date: Sep-2019
  • (2018)Towards automatic error recovery in parsing expression grammarsProceedings of the XXII Brazilian Symposium on Programming Languages10.1145/3264637.3264638(3-10)Online publication date: 20-Sep-2018
  • Show More Cited By

View Options

Get Access

Login options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media