{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/flexible-grammar-based-constrained-decoding","title":"Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning","arxiv_id":"2305.13971","date":"2023-05-23","proceeding":null,"authors":["Saibo Geng","Martin Josifoski","Maxime Peyrard","Robert West"],"abstract":"Despite their impressive performance, large language models (LMs) still struggle with reliably generating complex output structures when not finetuned to follow the required output format exactly. To address this issue, grammar-constrained decoding (GCD) can be used to control the generation of LMs, guaranteeing that the output follows a given structure. Most existing GCD methods are, however, limited to specific tasks, such as parsing or code generation. In this work, we demonstrate that formal grammars can describe the output space for a much wider range of tasks and argue that GCD can serve as a unified framework for structured NLP tasks in general. For increased flexibility, we introduce input-dependent grammars, which allow the grammar to depend on the input and thus enable the generation of different output structures for different inputs. We then empirically demonstrate the power and flexibility of GCD-enhanced LMs on (1) information extraction, (2) entity disambiguation, and (3) constituency parsing. Our results indicate that grammar-constrained LMs substantially outperform unconstrained LMs or even beat task-specific finetuned models. Grammar constraints thus hold great promise for harnessing off-the-shelf LMs for a wide range of structured NLP tasks, especially where training data is scarce or finetuning is expensive. Code and data: https://github.com/epfl-dlab/GCD.","url_abs":"https://arxiv.org/abs/2305.13971v6","url_pdf":"https://arxiv.org/pdf/2305.13971v6.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"flexible-grammar-based-constrained-decoding","repo_url":"https://github.com/epfl-dlab/gcd","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}},{"paper_slug":"flexible-grammar-based-constrained-decoding","repo_url":"https://github.com/uiuc-focal-lab/syncode","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"none","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"code-generation","task_name":"Code Generation"},{"task_slug":"constituency-parsing","task_name":"Constituency Parsing"},{"task_slug":"entity-disambiguation","task_name":"Entity Disambiguation"},{"task_slug":null,"task_name":"valid"}],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2305.13971","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2305.13971"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/uiuc-focal-lab/syncode","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/epfl-dlab/gcd","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"deterministic:regex_extraction","url":"https://github.com/epfl-dlab/GCD","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran_draft_wrong":1,"ran_violates":1,"unverified":12},"by_repo_kind":{"official":{"samples":14,"ran":2,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"ed8d711ce0734068","entry":"_concat_input_ids_to_str","repo":"epfl-dlab/GCD","repo_kind":"official","path":"src/constrained_generation/gf_constrained.py","file_url":"https://github.com/epfl-dlab/GCD/blob/HEAD/src/constrained_generation/gf_constrained.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"ed8d711ce0734068"}},{"code_sha256_prefix":"3003b6f719460c61","entry":"_is_port_available","repo":"epfl-dlab/GCD","repo_kind":"official","path":"src/constrained_generation/gf_constrained.py","file_url":"https://github.com/epfl-dlab/GCD/blob/HEAD/src/constrained_generation/gf_constrained.py","link_basis":"first_harvest_node","language":"python","status":"ran_violates","verification_level":1,"contract_check":"VIOLATES","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"3003b6f719460c61"}},{"code_sha256_prefix":"7024d35647596984","entry":"GFRuntime","repo":"epfl-dlab/GCD","repo_kind":"official","path":"src/constrained_generation/gf_constrained.py","file_url":"https://github.com/epfl-dlab/GCD/blob/HEAD/src/constrained_generation/gf_constrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"7024d35647596984"}},{"code_sha256_prefix":"93584044e1dadbac","entry":"GFServerPoolRuntime","repo":"epfl-dlab/GCD","repo_kind":"official","path":"src/constrained_generation/gf_constrained.py","file_url":"https://github.com/epfl-dlab/GCD/blob/HEAD/src/constrained_generation/gf_constrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"93584044e1dadbac"}},{"code_sha256_prefix":"5d09c13b2fbf4c59","entry":"GFServerRuntimeForLM","repo":"epfl-dlab/GCD","repo_kind":"official","path":"src/constrained_generation/gf_constrained.py","file_url":"https://github.com/epfl-dlab/GCD/blob/HEAD/src/constrained_generation/gf_constrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"5d09c13b2fbf4c59"}},{"code_sha256_prefix":"e3ddcf615dc49c27","entry":"GF_Constrained","repo":"epfl-dlab/GCD","repo_kind":"official","path":"src/constrained_generation/gf_constrained.py","file_url":"https://github.com/epfl-dlab/GCD/blob/HEAD/src/constrained_generation/gf_constrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e3ddcf615dc49c27"}},{"code_sha256_prefix":"50b8ba603819282e","entry":"HFConstrained","repo":"epfl-dlab/GCD","repo_kind":"official","path":"src/constrained_generation/gf_constrained.py","file_url":"https://github.com/epfl-dlab/GCD/blob/HEAD/src/constrained_generation/gf_constrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"50b8ba603819282e"}},{"code_sha256_prefix":"8ecfc9ae31f9f793","entry":"get_linearization_class","repo":"epfl-dlab/gcd","repo_kind":"official","path":"src/ie/linearization.py","file_url":"https://github.com/epfl-dlab/gcd/blob/HEAD/src/ie/linearization.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"8ecfc9ae31f9f793"}},{"code_sha256_prefix":"6bb4fbb17069f81d","entry":"get_prefix_allowed_tokens_from_gf","repo":"epfl-dlab/GCD","repo_kind":"official","path":"src/constrained_generation/gf_constrained.py","file_url":"https://github.com/epfl-dlab/GCD/blob/HEAD/src/constrained_generation/gf_constrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"6bb4fbb17069f81d"}},{"code_sha256_prefix":"9c4e1d92f6b083ca","entry":"get_score_from_metric","repo":"epfl-dlab/gcd","repo_kind":"official","path":"run_process_predictions.py","file_url":"https://github.com/epfl-dlab/gcd/blob/HEAD/run_process_predictions.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"9c4e1d92f6b083ca"}},{"code_sha256_prefix":"a38dbf7df79f2ae5","entry":"get_start_index_parts","repo":"epfl-dlab/gcd","repo_kind":"official","path":"src/ie/ordering.py","file_url":"https://github.com/epfl-dlab/gcd/blob/HEAD/src/ie/ordering.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"a38dbf7df79f2ae5"}},{"code_sha256_prefix":"b9465eab8ef5a841","entry":"get_trie_from_strings","repo":"epfl-dlab/gcd","repo_kind":"official","path":"src/constrained_generation/trie_constrained_generation_utils.py","file_url":"https://github.com/epfl-dlab/gcd/blob/HEAD/src/constrained_generation/trie_constrained_generation_utils.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"b9465eab8ef5a841"}},{"code_sha256_prefix":"b05538e9a0658b50","entry":"read_constrained_world","repo":"epfl-dlab/gcd","repo_kind":"official","path":"src/constrained_generation/trie_constrained_generation_utils.py","file_url":"https://github.com/epfl-dlab/gcd/blob/HEAD/src/constrained_generation/trie_constrained_generation_utils.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"b05538e9a0658b50"}},{"code_sha256_prefix":"6c5967a6c10f8a99","entry":"remove_accents_from_str","repo":"epfl-dlab/gcd","repo_kind":"official","path":"src/ie/linearization.py","file_url":"https://github.com/epfl-dlab/gcd/blob/HEAD/src/ie/linearization.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"6c5967a6c10f8a99"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}