cs.CLApr 12, 2026

Learning and Enforcing Context-Sensitive Control for LLMs

Authors: Mohammad Albinhassan, Pranava Madhyastha, Mark Law, Alessandra Russo

Organizations: Imperial College London · City University of London · The Alan Turing Institute · ILASP Limited, UK

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.

Figures & tables

Appendix figures & tables3 assets

Supplementary material from the paper’s appendix.

Appendix

Explore similar work

CardsList
  1. Suffix-Constrained Greedy Search Algorithms for Causal Language Models

    Mar 1, 2026Ayoub Hammal, Pierre Zweigenbaum, Caio CorroConstrained Generative ModelingConstrained Decoding

  2. GrammarRL: Effective Grammar-Constrained Decoding via Reinforcement Learning

    Sep 30, 2026Gabriele Tuccio, Antonino Furnari, Aldo Gangemi +1RL for Language ModelsConstrained Decoding