Papers
Topics
Authors
Recent
Search
2000 character limit reached

Prefix Parsing is Just Parsing

Published 23 Apr 2026 in cs.CL | (2604.21191v1)

Abstract: Prefix parsing asks whether an input prefix can be extended to a complete string generated by a given grammar. In the weighted setting, it also provides prefix probabilities, which are central to context-free language modeling, psycholinguistic analysis, and syntactically constrained generation from LLMs. We introduce the prefix grammar transformation, an efficient reduction of prefix parsing to ordinary parsing. Given a grammar, our method constructs another grammar that generates exactly the prefixes of its original strings. Prefix parsing is then solved by applying any ordinary parsing algorithm on the transformed grammar without modification. The reduction is both elegant and practical: the transformed grammar is only a small factor larger than the input, and any optimized implementation can be used directly, eliminating the need for bespoke prefix-parsing algorithms. We also present a strategy-based on algorithmic differentiation-for computing the next-token weight vector, i.e., the prefix weights of all one-token extensions, enabling efficient prediction of the next token. Together, these contributions yield a simple, general, and efficient framework for prefix parsing.

Summary

  • The paper demonstrates that prefix parsing is reduced to ordinary parsing through a compact grammar transformation that increases grammar size by only a constant factor.
  • It introduces an efficient next-token weight algorithm using lattice parsing and reverse-mode differentiation, achieving near the performance of standard parsing.
  • Empirical evaluations confirm that the transformed grammar maintains comparable runtime scaling, proving its applicability in real-world NLP systems.

Authoritative Summary of "Prefix Parsing is Just Parsing" (2604.21191)

Motivation and Context

Prefix parsing, which determines whether an input prefix can be extended to a complete string generated by some grammar (and, in weighted/probabilistic cases, computes the prefix probability), is a foundational operation for context-free language modeling, incremental psycholinguistic analysis (e.g., surprisal), and syntactically constrained token generation in LLMs. Traditionally, prefix parsing has required elaborate modifications to standard parsing algorithms, leading to algorithmic complexity and implementation duplication. This paper rigorously demonstrates that prefix parsing can be reduced to ordinary parsing through a transformation of the input grammar, preserving algorithmic efficiency and enabling broad compatibility with optimized parsing engines.

Prefix Grammar Transformation and Reduction

The central theoretical contribution is the "prefix grammar transformation," an efficient reduction from prefix parsing to ordinary parsing. Given any weighted context-free grammar (WCFG), the transformation constructs a new grammar generating exactly the (weighted) set of prefixes of original strings. The transformed grammar is only a small constant factor larger than the input (bounded by 83\frac{8}{3} times the original grammar size plus 3), as formalized in Proposition 2. This enables any off-the-shelf parser—including those with subcubic asymptotic complexity or optimized for GPU/CPU—to function as a prefix parser with negligible additional overhead. Runtime complexity and grammar blow-up are analyzed for the two canonical algorithms, CKY and Earley; the overhead is absorbed almost entirely in grammar structure, leaving per-string runtime scaling unchanged.

Correctness and Formal Guarantees

The reduction is proven correct via a strengthened induction argument: the prefix grammar exactly computes the prefix language as defined by summing the probabilities/weights of all completions of a given prefix. The construction is also formally tied to WFST (weighted finite-state transducer) composition, showing that the prefix language is a composition of the original CFG with a two-state prefix transducer. This perspective generalizes beyond CFGs, offering potential for extending the reduction to other grammar frameworks (e.g. tree-adjoining grammars).

Efficient Next-Token Weight Vector Algorithms

A critical practical application is the computation of next-token weight vectors: for a given string $\str$, efficiently compute the prefix weights of all possible one-token extensions, yielding the conditional distribution over the next token. Naïvely, this would require ∣Σ∣|\Sigma| invocations of a prefix parser, which is computationally prohibitive. The authors develop an algorithm based on lattice parsing and reverse-mode algorithmic differentiation: by parsing a compact automaton representing all single-token extensions, and differentiating the aggregate sum over token parameters, one obtains the next-token vector at the same asymptotic runtime as prefix parsing. The key meta-theorem guarantees symbolic gradients of lattice parser evaluations can be computed within a constant-factor (at most 4×4\times) of the original operation count. Empirical results confirm that the next-token algorithm is only ≈\approx1.2×\times slower than prefix parsing, both being within constant-factor overheads compared to ordinary parsing.

Empirical Evaluation

Experiments on large grammars (e.g., WSJ 5000: >35,000 rules, 448 nonterminals) confirm theoretical predictions. Runtime for parsing, prefix parsing, and next-token vector algorithms all scale similarly with string length (≈N2\approx N^2), with prefix parsing ≈2.9×\approx 2.9\times slower than parsing, and next-token vector ≈1.2×\approx 1.2\times slower than prefix parsing—matching the theoretical grammar-size blowup and constant-factor bounds. The approach performs comparably to specialized prefix parsing implementations (e.g., EarleyX) but achieves superior efficiency constants due to optimizations. Figure 1

Figure 1: Comparative runtime scaling of parsing, prefix parsing, and next-token weight vector algorithms on WSJ 5000, demonstrating preserved input-length dependence and constant-factor overheads.

Figure 2

Figure 2

Figure 2: Prefix parser versus EarleyX performance on WSJ 5000 and Social Discourse grammars, showing near-identical complexity exponents but improved fitted coefficients.

Technical Implications

This work establishes:

  • Prefix parsing for weighted CFGs (generalized to any commutative semiring) requires only a small grammar transformation, obviating the need for specialized prefix parser development.
  • Prefix probabilities and next-token distributions central to language modeling and incremental processing become efficiently accessible through standard parsing engines, even at scale.
  • The method is directly compatible with high-performance, hardware-optimized parsers and subcubic algorithms, broadening applicability in production NLP systems.

Strong numerical results demonstrate:

  • Prefix parsing incurs only a multiplicative, grammar-size overhead compared to standard parsing.
  • The next-token weight vector algorithm computes all token extensions in the same asymptotic time as a single prefix parsing call.

Contradictory or Non-Trivial Claims

The paper asserts—contradicting the common belief in the need for specialized algorithms—that prefix parsing is "just parsing," and that any correct parser can be converted into a prefix parser via grammar transformation. It further claims that next-token weights, typically seen as requiring expensive enumeration, can be obtained at essentially no asymptotic extra cost through algorithmic differentiation.

Limitations and Open Problems

The runtime guarantees depend on the worst-case grammar-size blowup after preprocessing, especially in CNF conversion and unary rule removal—though empirical results show these are often modest. Experiments are limited to CKY and Earley; extension to more exotic parsing algorithms or highly ambiguous grammars (e.g., context-sensitive) remains speculative.

Future Directions

  • Generalizing the prefix grammar transformation to context-sensitive, mildly context-sensitive, or probabilistic tree-adjoining grammars using FST composition.
  • Leveraging prefix parsing in constrained LLM decoding, incremental suprisal computation, and real-time syntactic control for LLMs.
  • Integrating the next-token weight computation into structured prediction frameworks and token-level control applications.

Conclusion

The paper establishes a unified, theoretically rigorous, and empirically validated framework for prefix parsing and next-token weight computation in weighted context-free grammars: any ordinary parser suffices, following a compact grammar transformation. This eliminates the need for bespoke prefix parsers and unlocks practical, efficient, incremental processing for language modeling and syntactic control. The reduction has broad implications for both theoretical linguistics, computational psycholinguistics, and practical NLP systems.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We haven't generated a list of open problems mentioned in this paper yet.

Collections

Sign up for free to add this paper to one or more collections.