🤖 AI Summary
Current approaches to controlling the outputs of large language models rely on manually defined context-sensitive constraints, which are difficult to scale and require significant expertise. This work proposes a novel two-stage framework that integrates context-sensitive grammar learning with large language model generation for the first time: it first automatically collects diverse model outputs through syntactic exploration to learn constraints, then enforces the learned rules during text generation. The method guarantees output validity without any human intervention and achieves a 100% constraint compliance rate using only a 1B-parameter model—outperforming both larger-scale models and state-of-the-art reasoning systems in constrained generation tasks.
📝 Abstract
Controlling the output of Large Language Models (LLMs) through context-sensitive constraints has emerged as a promising approach to overcome the limitations of Context-Free Grammars (CFGs) in guaranteeing generation validity. However, such constraints typically require manual specification -- a significant barrier demanding specialized expertise. We introduce a framework that automatically learns context-sensitive constraints from LLM interactions through a two-phase process: syntactic exploration to gather diverse outputs for constraint learning, followed by constraint exploitation to enforce these learned rules during generation. Experiments demonstrate that our method enables even small LLMs (1B parameters) to learn and generate with perfect constraint adherence, outperforming larger counterparts and state-of-the-art reasoning models. This work represents the first integration of context-sensitive grammar learning with LLM generation, eliminating manual specification while maintaining generation validity.