Practical Packrat Parsing

Practical Packrat Parsing

Practical Packrat Parsing Robert Grimm [email protected] Technical Report TR2004-854 Dept. of Computer Science New York University ABSTRACT 1. INTRODUCTION A considerable number of research projects are exploring A considerable number of research projects have been ex- how to extend object-oriented programming languages such ploring language extensions that improve the expressivity of as Java with, for example, support for generics, multiple dis- object-oriented programming languages, with many projects patch, or pattern matching. To keep up with these changes, focusing on Java as a base language. Examples include language implementors need appropriate tools. In this con- generics [9, 10, 19, 27, 35], aspect-oriented programming [20, text, easily extensible parser generators are especially im- 21], multiple dispatch [11, 25], pattern matching [22, 24], portant because parsing program sources is a necessary first and support for controlling information flow [26]. Other step for any language processor, be it a compiler, syntax- projects have been exploring extensibility mechanisms for highlighting editor, or API documentation generator. Un- object-oriented languages, focusing either on macro systems fortunately, context-free grammars and the corresponding [2, 3] or on the compiler itself [28]. LR or LL parsers, while well understood and widely used, Taken together, these efforts illustrate that programming are also unnecessarily hard to extend. To address this lack languages are not static entities, but rather in constant flux. of appropriate tools, we introduce Rats!, a parser genera- In fact, Sun has incorporated generics, among several other tor for Java that supports easily modifiable grammars and new features, into the upcoming 1.5 release of Java [8, 17]. avoids the complexities associated with altering LR or LL An important challenge, then, is how to gracefully evolve grammars. Our work builds on recent research on pack- programming languages [32] and, more specifically, how to rat parsers, which are recursive descent parsers that per- provide language implementors with the appropriate tools form backtracking but also memoize all intermediate results for tracking ever changing languages. While our larger re- (hence their name), thus ensuring linear-time performance. search agenda aims to explore how to express, compose, Our work makes this parsing technique, which has been de- and efficiently implement language extensions for C-like lan- veloped in the context of functional programming languages, guages, for the purposes of this paper we focus on the ex- practical for object-oriented languages. Furthermore, our tensibility of programming language grammars and their parser generator supports simpler grammar specifications parsers. After all, parsing program sources is a necessary and more convenient error reporting, while also producing first step for any language processor, be it a compiler, inter- better performing parsers through aggressive optimizations. preter, syntax-highlighting editor, API documentation gen- In this paper, we motivate the need for more easily extensi- erator, or source measurement tool. ble parsers, describe our parser generator and its optimiza- Unfortunately, context-free grammars (CFGs) and the cor- tions in detail, and present the results of our experimental responding LR or LL parsers [1], while well understood and evaluation. widely used, offer only limited extensibility [7, 15] and thus represent an unsuitable foundation for managing the evolu- tion of programming languages and their implementations. Categories and Subject Descriptors On the other hand, parsing expression grammars [4, 5, 15] D.3.4 [Programming Languages]: Processors—compil- (PEGs) and packrat parsers [13, 14] provide an attractive ers, optimization, parsing, translator writing systems and alternative. While PEGs share many constructs with the fa- compiler generators miliar EBNF notation [18, 36], a key difference is that PEGs rely on ordered choices instead of the unordered choices used General Terms in CFGs. As a result, PEGs can avoid unnecessary ambigu- ities and are more easily modifiable. Additional flexibility Design, languages, performance is offered through syntactic predicates, which match expres- sions but do not consume the input, thus providing unlim- Keywords ited lookahead, and through the integration of lexing with parsing, which greatly simplifies the addition of new tokens Parser generators, parsing expression grammars, packrat to a grammar. parsers, extensible programming languages Parsing expression grammars can be implemented by so- called packrat parsers. They are recursive descent parsers, disambiguation by factoring such prefixes by hand, but this which perform backtracking but also memoize all interme- requires extra effort and obfuscates the language specifica- diate results (hence the name), thus ensuring linear-time tion. JavaCC [12] supports explicit lookahead expressions, performance. So far, Ford [13, 14] has implemented sev- but they still tend to obfuscate the language specification. eral handwritten packrat parsers as well as a packrat parser In contrast, ANTLR [31] supports a global lookahead flag, generator, called Pappy, for and in Haskell. As a lazy, func- but, in practice, still requires local options to fine-tune the tional programming language, Haskell certainly provides a lookahead. In either case, the need for explicit lookahead convenient platform for implementing this memoizing pars- specifications complicates the grammar and makes it more ing technique. However, the choice of target language (ar- difficult to modify. guably) also limits the accessibility of packrat parsers. An additional problem common to both LR and LL parser To make packrat parsers more widely accessible, this pa- generators is the separation of lexing and parsing, which can per introduces Rats!,1 a packrat parser generator for Java. make it unnecessarily hard to add new tokens to a gram- By leveraging Java’s object-oriented features, parsers gen- mar. As an example, consider adding support for character erated by Rats! have a simple and elegant implementation classes, such as “[0-9a-fA-F]” for hexadecimal digits, to of memoization in a strict, imperative programming lan- the grammar of a parser generator. At the grammar-level, guage. Compared to Pappy, Ford’s packrat parser generator the corresponding parsing expression should look as follows for Haskell, Rats! features more concise grammar specifica- (with ‘/’ denoting the ordered choice operator): tions, has better support for debugging grammars and for reporting parser errors, and, through aggressive optimiza- ’[’ ( Char ’-’ Char / Char )+ ’]’ tions, generates better performing code. Our parser gen- However, because the definition of the Char token overlaps erator has been implemented within our own framework for almost all other tokens, its addition results in a substan- building extensible source-to-source language processors and tial number of ambiguity errors for the lexical specification. includes optional support for using the framework in gener- Common workarounds are the use of separate lexer states, ated parsers, thus simplifying the implementation of other as supported by Lex [23], or the composition of different language processors. Rats! has been released as open source lexers, as supported by ANTLR, both of which, again, ob- and is available at http://www.cs.nyu.edu/rgrimm/xtc/. fuscate the language specification. The rest of this paper is organized as follows. In Section 2, Packrat parsers can avoid these problems while still ex- we motivate our work and review other approaches to pars- hibiting linear-time performance. In particular, because they ing programming languages. We follow with an overview are recursive descent parsers, they avoid the brittleness of of our parser generator in Section 3. We then discuss our LR parsers. Next, because they backtrack while also mem- object-oriented implementation of packrat parsers in Sec- oizing all intermediate results, they do not require explicit tion 4 and describe the optimizations performed by Rats! management of lookahead. Unlimited lookahead is still avail- in Section 5. In Section 6, we present the results of our able through syntactic predicates, though not to ensure lin- experimental evaluation. In Section 7, we follow with a dis- ear-time performance but rather to increase expressiveness. cussion of our framework for building extensible source-to- For example, by using syntactic predicates, packrat parsers source language processors. Finally, we outline future work can recognize {anbncn | n > 0}, which cannot be expressed in Section 8 and conclude in Section 9. by CFGs. Finally, because packrat parsers effectively treat every character in the input as a token, they do not require 2. MOTIVATION AND RELATED WORK separate lexical specifications and make the full power of From the perspective of language extensibility, using a parser parsing expression grammars available for recognizing lexi- generator to create a parser has an important advantage over cal syntax. a handwritten parser: the grammar provides a concise spec- However, this last property of packrat parsers also repre- ification of the corresponding language. As a result, we gen- sents the biggest challenge to an efficient implementation: erally expect it to be easier to modify the machine-generated The data structure for memoizing intermediate results is parser than the handwritten one. However, LALR(1) gram- a possibly very large table, with the characters

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