HackerPapers

arXiv:2209.08383cs.FL

Practical LR Parser Generation

Joe Zimmerman

Early discovery. Linked by one identifiable commenter. This paper is outside the main ranking; a mention is not an endorsement.

Abstract

Parsing is a fundamental building block in modern compilers, and for industrial programming languages, it is a surprisingly involved task. There are known approaches to generate parsers automatically, but the prevailing consensus is that automatic parser generation is not practical for real programming languages: LR/LALR parsers are considered to be far too restrictive in the grammars they support, and LR parsers are often considered too inefficient in practice. As a result, virtually all modern languages use recursive-descent parsers written by hand, a lengthy and error-prone process that dramatically increases the barrier to new programming language development. In this work we demonstrate that, contrary to the prevailing consensus, we can have the best of both worlds: for a very general, practical class of grammars -- a strict superset of Knuth's canonical LR -- we can generate parsers automatically, and the resulting parser code, as well as the generation procedure itself, is highly efficient. This advance relies on several new ideas, including novel automata optimization procedures; a new grammar transformation ("CPS"); per-symbol attributes; recursive-descent actions; and an extension of canonical LR parsing, which we refer to as XLR, which endows shift/reduce parsers with the power of bounded nondeterministic choice. With these ingredients, we can automatically generate efficient parsers for virtually all programming languages that are intuitively easy to parse -- a claim we support experimentally, by implementing the new algorithms in a new software tool called langcc, and running them on syntax specifications for Golang 1.17.8 and Python 3.9.12. The tool handles both languages automatically, and the generated code, when run on standard codebases, is 1.2x faster than the corresponding hand-written parser for Golang, and 4.3x faster than the CPython parser, respectively.

arXiv comments
Journal reference
DOI
Subjects
cs.FLFormal Languages and Automata Theory cs.PLProgramming Languages

On Hacker News

Linked by 1 identifiable commenter in 3 comments across 1 thread, from Sep 2022 to Sep 2022.

Comments linking this paper, per year.

Discussed in comments1 thread

    • > Whereas langcc boasts automatically-generated parsers for the easy-to-parse Python and Go, my old employer has automatically-generated parsers for Ruby, C++, COBOL, and about 100 others. Python may be (comparatively) easy to parse, but Golang is not - it is _barely_ LR(1) with…

      jzimmerman64View on HNParent context

    • > back when it was considered impossibly impractical (the "dragon book" said so!) Yes, with some optimizations (e.g., Pager's algorithm), LR(1) can be practical; in langcc we even implement LR(k), and this is also practical for many real-world grammars. However, a key…

      jzimmerman64View on HNParent context

    • Readers who are interested in parsing theory may also enjoy the companion technical report: https://arxiv.org/pdf/2209.08383.pdf as well as a brief users' manual, available here: https://arxiv.org/pdf/2209.08385.pdf

      jzimmerman64View on HNParent context

Submitted to Hacker News

Connected through shared HN threads, not inferred similarity or a recommendation.