# Preserving Structure, Strengthening Signals: A Grammar-Driven Approach to Optimize the Trade-off Between Quality and Detectability in Code Watermarking

arXiv: https://arxiv.org/abs/2607.10210 · HTML: https://mlsanity.com/paper/2607.10210/

## Key facts

- No citing papers in the map yet (data through 14 Oct 2026).
- Builds on 16 of its 37 references that are in the map.
- First posted on arXiv 11 Jul 2026.

## Builds on (most cited first, 16 of 16)

- [Qwen3 Technical Report](https://mlsanity.com/paper/2505.09388/) (2025)
- [Evaluating Large Language Models Trained on Code](https://mlsanity.com/paper/2107.03374/) (2021)
- [Qwen2.5-Coder Technical Report](https://mlsanity.com/paper/2409.12186/) (2024)
- [StarCoder 2 and The Stack v2: The Next Generation](https://mlsanity.com/paper/2402.19173/) (2024)
- [On the Reliability of Watermarks for Large Language Models](https://mlsanity.com/paper/2306.04634/) (2023)
- [OctoPack: Instruction Tuning Code Large Language Models](https://mlsanity.com/paper/2308.07124/) (2023)
- [An Entropy-based Text Watermarking Detection Method](https://mlsanity.com/paper/2403.13485/) (2024)
- [Towards Codable Watermarking for Injecting Multi-bits Information to LLMs](https://mlsanity.com/paper/2307.15992/) (2023)
- [Token-Specific Watermarking with Enhanced Detectability and Semantic Coherence for Large Language Models](https://mlsanity.com/paper/2402.18059/) (2024)
- [Embarrassingly Simple Text Watermarks](https://mlsanity.com/paper/2310.08920/) (2023)
- [BiMark: Unbiased Multilayer Watermarking for Large Language Models](https://mlsanity.com/paper/2506.21602/) (2025)
- [Necessary and Sufficient Watermark for Large Language Models](https://mlsanity.com/paper/2310.00833/) (2023)
- [Turning Your Strength into Watermark: Watermarking Large Language Model via Knowledge Injection](https://mlsanity.com/paper/2311.09535/) (2023)
- [Marking Code Without Breaking It: Code Watermarking for Detecting LLM-Generated Code](https://mlsanity.com/paper/2502.18851/) (2025)
- [Is The Watermarking Of LLM-Generated Code Robust?](https://mlsanity.com/paper/2403.17983/) (2024)
- [Watermarking Low-entropy Generation for Large Language Models: An Unbiased and Low-risk Method](https://mlsanity.com/paper/2405.14604/) (2024)

## Abstract (arXiv)

Text watermarking has emerged as a crucial technique for identifying AI-generated content. However, current watermarking schemes entail a trade-off between text quality and watermark detectability, which is further intensified by the inherent low-entropy nature of code. To mitigate this dilemma, we propose Grammar-Driven Watermark (GDW), a training-free watermarking approach tailored to LLM-based code generation. GDW employs a three-level masking mechanism consisting of a green-list mask, a syntactic validity mask, and a structural role mask to regulate watermark injection. These masks are integrated through hierarchical modulation, which limits the scope of watermark injection to syntactically admissible green-list tokens and adjusts watermark strength based on structural roles through a role function. Accordingly, we design a role-aware weighted detection statistic. For comprehensive evaluation, we introduce the Area Under the Trade-off Curve (AUTC), a metric that quantifies the quality-detectability trade-off. Extensive experiments demonstrate that GDW outperforms existing baselines and achieves state-of-the-art (SOTA) trade-off performance, while maintaining superior performance under seven semantics-preserving attacks.
