Using Grammar Masking to Ensure Syntactic Validity in LLM-based Modeling Tasks
Not on the map. Why?
Abstract (arXiv)
We present and evaluate a method called grammar masking, which is used to guide large language models (LLMs) toward producing syntactically correct models for a given context-free grammar. Prompt engineering methods such as few-shot learning or priming can be used to improve the chances of an LLM producing correct syntax, but the more complex the grammar, the more time-consuming and less promising these methods become. Previous work is focused primarily on the usage of either language model training or prompt engineering. In this work, a method is presented that restricts the output to a given grammar using constrained decoding to ensure the output adheres to a valid syntax. We use several DSLs built with MontiCore and task multiple LLMs to produce models with and without constrained decoding. A corresponding parser is used to confirm the syntactic correctness of each model. We show that grammar masking can dramatically improve the modeling capabilities of several LLMs, reducing the need for well-refined prompting while increasing the chance of producing correct models.
Key facts. Cited by 6 papers in the map (data through 7 Oct 2026). Builds on 9 of its 44 references that are in the map. First posted on arXiv 8 Jul 2024.
Builds on
9 of 44 references.
References · 9
- 2017Attention Is All You Needcited by 59,932
- 2020Language Models are Few-Shot Learnerscited by 32,199
- 2022Self-Consistency Improves Chain of Thought Reasoning in Language Modelscited by 5,601
- 2021Finetuned Language Models Are Zero-Shot Learnerscited by 3,747
- 2021Program Synthesis with Large Language Modelscited by 3,509
- 2023Lost in the Middle: How Language Models Use Long Contextscited by 3,383
- 2022Language Model Cascadescited by 104
- 2022Experiences from Using Code Explanations Generated by Large Language Models in a Web Software Development E-Bookcited by 84
- 2023A Brief History of Prompt: Leveraging Language Models. (Through Advanced Prompting)cited by 4
Built on by
Citing papers per year: 2025: 4; 2026: 2.
6 papers.
- 2026Lookahead-then-Verify: Reliable Constrained Decoding for Diffusion LLMs under Context-Free Grammarscited by 6
- 2025The Future of Generative AI in Software Engineering: A Vision from Industry and Academia in the European GENIUS Projectcited by 4
- 2025DiCoRe: Enhancing Zero-shot Event Detection via Divergent-Convergent LLM Reasoningcited by 2
- 2025Accurate and Consistent Graph Model Generation from Text with Large Language Modelscited by 2
- 2025Static Sandboxes Are Inadequate: Modeling Societal Complexity Requires Open-Ended Co-Evolution in LLM-Based Multi-Agent Simulationscited by 2
- 2026Grammar-Constrained Decoding Can Jailbreak LLMs into Generating Malicious Codecited by 0
- 2026Grammar-Constrained Decoding Can Jailbreak LLMs into Generating Malicious Code10 Jun 2026
- 2026Lookahead-then-Verify: Reliable Constrained Decoding for Diffusion LLMs under Context-Free Grammars31 Jan 2026
- 2025The Future of Generative AI in Software Engineering: A Vision from Industry and Academia in the European GENIUS Project3 Nov 2025
- 2025Static Sandboxes Are Inadequate: Modeling Societal Complexity Requires Open-Ended Co-Evolution in LLM-Based Multi-Agent Simulations15 Oct 2025
- 2025Accurate and Consistent Graph Model Generation from Text with Large Language Models1 Aug 2025
- 2025DiCoRe: Enhancing Zero-shot Event Detection via Divergent-Convergent LLM Reasoning5 Jun 2025