SYNCODE is a novel framework for efficient and general syntactical decoding with large language models (LLMs), designed to ensure LLM outputs adhere to a specified context-free grammar (CFG). The framework uses an offline-constructed DFA mask store to efficiently filter valid tokens and eliminate invalid ones, ensuring syntactic correctness. SYNCODE integrates seamlessly with any language defined by a CFG and has been tested on generating JSON, Python, and Go outputs. Experiments show that SYNCODE eliminates all syntax errors in JSON generation and significantly reduces syntax errors in Python and Go code, achieving a 96.07% reduction in errors. The framework is sound and complete, ensuring all valid tokens are retained and invalid ones are rejected. SYNCODE's efficiency is achieved through a DFA mask store, which allows parallel processing on GPUs, reducing inference overhead. The framework is scalable and can be applied to various formal languages, demonstrating its effectiveness in enhancing syntactical precision in LLM generation.SYNCODE is a novel framework for efficient and general syntactical decoding with large language models (LLMs), designed to ensure LLM outputs adhere to a specified context-free grammar (CFG). The framework uses an offline-constructed DFA mask store to efficiently filter valid tokens and eliminate invalid ones, ensuring syntactic correctness. SYNCODE integrates seamlessly with any language defined by a CFG and has been tested on generating JSON, Python, and Go outputs. Experiments show that SYNCODE eliminates all syntax errors in JSON generation and significantly reduces syntax errors in Python and Go code, achieving a 96.07% reduction in errors. The framework is sound and complete, ensuring all valid tokens are retained and invalid ones are rejected. SYNCODE's efficiency is achieved through a DFA mask store, which allows parallel processing on GPUs, reducing inference overhead. The framework is scalable and can be applied to various formal languages, demonstrating its effectiveness in enhancing syntactical precision in LLM generation.