Learning and Enforcing Context-Sensitive Control for LLMs

Mohammad Albinhassan, Pranava Madhyastha, Mark Law, Alessandra Russo

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.

Introduction

Large Language Models (LLMs) are increasingly deployed for structured generation tasks, such as producing code or data in specific formats. While existing tools can enforce Context-Free Grammars (CFGs)—ensuring, for example, that a JSON file has balanced brackets—they struggle with context-sensitive constraints that involve long-range dependencies. A classic example is the language $a^n b^n c^n$, where the count of each character must be identical. While a CFG can ensure the correct order of characters, it cannot enforce the equality of their counts. Manually specifying these rules requires significant expertise in formal logic, creating a barrier for developers. This research introduces a framework to bridge this "expressivity gap" by automatically learning and enforcing context-sensitive constraints.

Methodology

The proposed framework operates in two distinct phases: Syntactic Exploration and Constraint Exploitation.

Syntactic Exploration (Learning)

In this phase, the LLM is prompted to generate diverse outputs under a basic CFG that handles general structure. By using temperature-based sampling, the system intentionally generates a mix of valid and invalid strings. A task-specific oracle labels these outputs as positive or negative examples. These labeled examples are then processed by a logic-based learner, Inductive Learning of Answer Set Programs (ILASP), which induces the underlying context-sensitive rules.

Constraint Exploitation (Enforcement)

The learned rules are converted into an Answer Set Grammar (ASG). During the generation phase, this ASG acts as a dynamic mask on the LLM’s logit distribution. It "redacts" any token that would lead to a violation of the learned rules, ensuring that the final output is 100% valid according to the discovered constraints.

The two-phase framework: Syntactic Exploration (left) for learning constraints through labeled interactions, and Constraint Exploitation (right) for enforcing learned rules during decoding.

Experimental Results

The framework was evaluated on two synthetic grammar tasks: $L_1 = \{a^n b^n c^n\}$ and $L_2 = \{a^n b^n c^m\}$ (where $a$ and $b$ counts must match). The researchers compared the performance of the Llama 3.1/3.2 family (1B to 70B parameters) against state-of-the-art reasoning models including GPT-o1, GPT-o4-mini, and DeepSeek-R1.

The results demonstrate a significant performance leap. While a standard Llama 1B model achieves only ~20% accuracy on $L_1$, the addition of the learned ASG constraints brings its accuracy to a perfect 100%. Remarkably, this neuro-symbolic approach allows the 1B model to outperform much larger models and specialized reasoning models like DeepSeek-R1 (80%) and GPT-o1 (86.7%), which fail to maintain perfect consistency through pure prompting.

Accuracy comparison across models. The integration of learned ASG constraints enables even the smallest models to achieve 100% accuracy, surpassing state-of-the-art reasoning models.

Key Insights and Conclusion

This research highlights the power of neuro-symbolic synergy: the LLM provides creative token proposals, while the symbolic ASG layer provides a guarantee of correctness. This offloads the burden of logical reasoning and counting from the model's parameters to an external enforcement layer. Furthermore, the study underscores the importance of negative examples in the learning phase; by observing what constitutes a "wrong" answer, the logic learner can more precisely define the boundaries of valid generation.

While currently focused on hard binary constraints and requiring an oracle for labeling, future work aims to extend this framework to "soft" stylistic preferences and complex real-world applications like Text-to-SQL and autonomous agent planning. By eliminating the need for manual grammar specification, this framework makes context-sensitive control accessible for a wider range of LLM applications.

Summary updated

BibTeX
@inproceedings{albinhassan2025learning,
  author = {Mohammad Albinhassan and Pranava Madhyastha and Mark Law and Alessandra Russo},
  title = {{Learning and Enforcing Context-Sensitive Control for LLMs}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
  url = {https://openreview.net/forum?id=4Qrvaaz94H},
}
Figure