NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

LR(1) Parser Generation System: LR(1) Error Recovery, Oracles, and\n Generic Tokens

A. Sorkin, Peter Donovan · DOI

TL;DR

This paper introduces an LR(1) parser generation system that produces parsers comparable in speed and size to LALR(1) parsers, but with the added power of full LR(1) parsing. It highlights three novel features: an automatic grammar-specified error recovery algorithm, "oracles" for resolving ambiguities, and "generic tokens" for simplifying grammars.

What Problem Does This Paper Address?

Traditional parser generators like yacc often use LALR(1) parsing, which is a subset of the more powerful LR(1) parsing. While LR(1) can parse any deterministic context-free language, its exponential state explosion made it impractical. Previous work by Pager and others showed how to merge compatible states to create LR(1) parsers that are comparable in size and speed to LALR(1). However, implementing robust error recovery and handling language ambiguities efficiently remained challenges. This paper addresses these by presenting an LR(1) system that incorporates advanced error recovery, a mechanism for resolving ambiguities (oracles), and a way to simplify grammars (generic tokens).

Methods

The LR(1) Parser Generation System builds upon Pager's algorithm for constructing efficient LR(1) parsers. The paper details three key innovations:

  • LR(1) Grammar Specified Automatic Error Recovery: This algorithm leverages the preserved context in full LR(1) parse tables. When an error occurs, it performs reductions based on "error productions" (grammar rules containing an ERROR token) to find a "recoverable state." From there, it can resume parsing, potentially inserting or deleting tokens to correct the error without losing significant information.
  • Oracles: These are fragments of code executed between the scanner and parser. They act as decision-makers, answering questions like "should token X be replaced by token Y?" This is crucial for resolving ambiguities, especially when a scanner might return a token that could be interpreted in multiple ways (e.g., an identifier that could also be a type name due to typedef in C).
  • Generic Tokens: This feature allows a single "generic" token to represent a set of syntactically identical "subtokens" (reserved terminals). This simplifies grammars by reducing the number of distinct tokens and can lead to smaller parse tables and faster parsing due to a shallower parse tree.

Datasets

Not discussed in the paper. The paper focuses on the grammar analysis and parser generation system itself, using example grammars and code snippets for illustration rather than formal datasets.

Key Insights and Ideas

  • Full LR(1) Power is Achievable and Practical: The paper demonstrates that the benefits of full LR(1) parsing (recognizing all deterministic context-free languages) can be realized without the prohibitive state explosion, making it a viable alternative to LALR(1).
  • Context is King for Error Recovery: The sophisticated error recovery relies heavily on the rich context preserved by LR(1) parse tables, allowing for more intelligent and less disruptive error handling.
  • Oracles as Ambiguity Resolvers: The concept of oracles provides a powerful and flexible mechanism to delegate complex disambiguation decisions to user-defined code, bridging the gap between static grammar rules and dynamic language semantics.
  • Generic Tokens for Grammar Simplification: Representing sets of similar tokens with a single generic token offers a significant advantage in grammar design, leading to more concise and potentially more efficient parsers.

Key Results

The paper states that the generated LR(1) parsers are comparable in speed and size to those produced by LALR(1) generators like yacc. While specific quantitative benchmarks are not detailed, the system's successful use in commercial projects since 1987 is cited as evidence of its effectiveness. The paper provides detailed examples illustrating how the error recovery, oracles, and generic tokens function in practice, showcasing their ability to handle complex language features and ambiguities.

Limitations and Future Work

  • Dynamic Precedence: The paper mentions that dynamic precedence handling for generic tokens is a complex area and is discussed further in relation to maps and precedence rules. The full implementation details of dynamic precedence for generic tokens are elaborated upon.
  • Future Work: The authors indicate that an optimized Pager LR(1) implementation will be discussed in a separate paper. They also note that while context-specific oracles are possible, context-specific ones haven't proven necessary in practice. The paper focuses on the core features of the system, implying that further refinements and extensions are possible.

Summary updated

BibTeX
@inproceedings{sorkin2011lr,
  author = {A. Sorkin and Peter Donovan},
  title = {{LR(1) Parser Generation System: LR(1) Error Recovery, Oracles, and\textbackslash\{\}n Generic Tokens}},
  booktitle = {ACM SIGSOFT Software Engineering Notes},
  year = {2011},
  doi = {10.1145/1943371.1943391},
}

Loading related content...

Figure