🤖 AI Summary
This work addresses the problem of efficiently determining whether a given input prefix can be extended into a complete sentence by a context-free grammar and computing its prefix probability. To this end, the authors propose a prefix grammar transformation that reduces prefix parsing entirely to a standard parsing task: by constructing a new grammar only slightly larger than the original, any existing parser can be directly reused without requiring custom algorithms. Furthermore, by integrating algorithmic differentiation, the method efficiently computes the prefix weight vector for all single-token extensions. This approach establishes, for the first time, a general, concise, and efficient framework for prefix parsing that maintains full compatibility with off-the-shelf parsers while significantly simplifying and accelerating syntactically constrained language modeling and generation.
📝 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 large language models. 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.