Grammar Prompting for Domain-Specific Language Generation with Large Language Models

Large language models (LLMs) can learn to perform a wide range of natural language tasks from just a handful of in-context examples. However, for generating strings from highly structured languages (e.g., semantic parsing to complex domain-specific languages), it is challenging for the LLM to generalize from just a few exemplars. We propose \emph{grammar prompting}, a simple approach to enable LLMs to use external knowledge and domain-specific constraints, expressed through a grammar in Backus--Naur Form (BNF), during in-context learning. Grammar prompting augments each demonstration example with a specialized grammar that is minimally sufficient for generating the particular output example, where the specialized grammar is a subset of the full DSL grammar. For inference, the LLM first predicts a BNF grammar given a test input, and then generates the output according to the rules of the grammar. Experiments demonstrate that grammar prompting can enable LLMs to perform competitively on a diverse set of DSL generation tasks, including semantic parsing (SMCalFlow, Overnight, GeoQuery), PDDL planning, and SMILES-based molecule generation.

Paper

References (100)

Scroll for more · 38 remaining

Similar papers

Peer review

Reviewer 8y4i5/10 · confidence 3/52023-06-25

Summary

This paper studies generating strings from highly structured languages for large language models (LLMs). The authors propose grammar prompting to enhance LLMs to use external knowledge and domain-specific constraints, expressed through a grammar expressed in Backus–Naur Form. Serving as intermediate reasoning steps, these grammars (metalanguage) enhance the model's performance in generating highly structured languages in domains such as semantic parsing and molecule generation. The idea is simple and effective, and the experimental results well support the claim.

Strengths

+ The paper is well-written and easy to follow. + A simple and effective method was presented, leading to improved downstream performance compared with simple prompting.

Weaknesses

- The idea is not that novel, which uses metalanguage as the bridge that leads to the ultimate structured language. The method is an intuitive extension of the standard prompting (or chain-of-thought prompting) method. - Improvements over simple prompting methods are validated, but whether the method can beat more sophisticated algorithms (e.g., the recent tree-of-thought reasoning [3]) is unknown. However, as the authors claimed, the focus of this paper is not to achieve SOTA in downstream tasks. - The proposed constrained generation method is applied at the sub-sentence level, instead of the token level. Besides, the idea of constrained decoding has been explored in previous works [e.g., 1,2], which in turn challenges the novelty of this paper. [1] Hokamp, Chris, and Qun Liu. "Lexically constrained decoding for sequence generation using grid beam search." arXiv preprint arXiv:1704.07138 (2017). [2] Huang, Wenlong, et al. "Grounded decoding: Guiding text generation with grounded models for robot control." arXiv preprint arXiv:2303.00855 (2023). [3] Yao, Shunyu, et al. "Tree of thoughts: Deliberate problem solving with large language models." arXiv preprint arXiv:2305.10601 (2023).

Questions

NA

Rating

5: Borderline accept: Technically solid paper where reasons to accept outweigh reasons to reject, e.g., limited evaluation. Please use sparingly.

Confidence

3: You are fairly confident in your assessment. It is possible that you did not understand some parts of the submission or that you are unfamiliar with some pieces of related work. Math/other details were not carefully checked.

Soundness

3 good

Presentation

4 excellent

Contribution

2 fair

Limitations

NA

Reviewer pwxh8/10 · confidence 5/52023-06-28

Summary

The authors investigate the effectiveness of grammar prompting as a simple approach to help LLMs utilize external knowledge and domain-specific constraints. This approach is motivated by the goal of enabling LLMs to generate DSL outputs that differ significantly from those encountered during pretraining. The authors achieve this by using a BNF grammar during in-context learning. In their framework, the LLM first predicts a BNF grammar based on a test input and then generates output constrained by the rules of the grammar. The experiments conducted demonstrate that grammar prompting enables LLMs to perform competitively on a diverse range of DSL generation tasks, such as semantic parsing, PDDL planning, and molecule generation.

Strengths

- Novel method for prompting and constraining LLM generation using BNF grammar. - Strong experimental results. - The method is described clearly and is sound. - The method is relatively simple yet effective. The framework is refreshing and is expected to have a significant impact on the semantic parsing community.

Weaknesses

Minor: The contributions and novelty should be stated at the end of the introduction section.

Questions

Have you explored other grammar forms?

Rating

8: Strong Accept: Technically strong paper, with novel ideas, excellent impact on at least one area, or high-to-excellent impact on multiple areas, with excellent evaluation, resources, and reproducibility, and no unaddressed ethical considerations.

Confidence

5: You are absolutely certain about your assessment. You are very familiar with the related work and checked the math/other details carefully.

Soundness

4 excellent

Presentation

3 good

Contribution

4 excellent

Limitations

No negative societal impacts

Reviewer rfkJ7/10 · confidence 4/52023-07-05

Summary

This paper presents an approach for improving few-shot prompting of LLMs for tasks that produce structured outputs, where the structure can be described by a grammar in BNF form. The approach is similar to chain-of-thought prompting: for a given input x, the LLM is prompted to generate the minimal subset of grammar rules that can produce the corresponding output G[y], then conditions on x and G[y] to produce y. Decoding can be done in the standard way (e.g. greedy decoding), but the paper also presents an improved version that uses a modified Earley parsing algorithm to ensure that the outputs y conform to the predicted grammar G[y]. All variants of the approach are effective, providing substantial benefits over reasonable baselines (e.g. standard few-shot prompting and constrained decoding) across a range of tasks, including realistic semantic parsing benchmarks, SMILES molecule generation, and plan initialization for PDDL planning.

Strengths

The approach is elegant and seems simple to implement, treating the LLM as a black box and needing only knowledge of the output grammar. While the constrained generation procedure is a bit more complex (and requires multiple LLM calls), it's also well-motivated and elegant, and isn't required for strong performance. I could definitely see the overall approach being practically useful in tasks that require DSLs that were low-resource in pre-training, such as tool use. The experiments are thorough, with strong results across a range of tasks and datasets. The compositional generalization results on GeoQuery were especially interesting to me, as they indicate that the grammar examples in the prompt don't need to fully cover the grammar rules used in the output. The paper is very clearly written, especially given the number of experiments presented and space constraints (but see below for a few questions).

Weaknesses

One minor weakness is that it would benefit the paper to also show results on other LLMs to give evidence that the approach is broadly applicable, especially since of the three models evaluated, GPT-3.5 derives from Codex (I believe, if the paper is using code-davinci-002) and GPT-4's training data is unknown. StarCoder-15B could be one such model, although it may not be few-shot promptable. A few details could be made clearer, if space is available, see below.

Questions

Q1) How are the examples being retrieved in the retrieval-based ICL experiments? Do they use similarity of the generated grammar as well, or just the inputs? Q2) Which Codex model is being used in the experiments here? Minor clarification questions/points (don't need to address in author response): - Be more consistent with the bolding of numbers in results tables, e.g. in Table 5 - The limitations mentions a lack of improvement for regexes, were these in experiments not reported in this paper? - I didn't fully understand the Macro experiments on the PDDL tasks (although I did not check the appendix carefully). - spelling of "conditioned" in Fig 2 caption - while the semantic parsing experiments are well-motivated by tool use, they aren't really tool use IMO, so renaming 4.1 might be appropriate

Rating

7: Accept: Technically solid paper, with high impact on at least one sub-area, or moderate-to-high impact on more than one areas, with good-to-excellent evaluation, resources, reproducibility, and no unaddressed ethical considerations.

Confidence

4: You are confident in your assessment, but not absolutely certain. It is unlikely, but not impossible, that you did not understand some parts of the submission or that you are unfamiliar with some pieces of related work.

Soundness

4 excellent

Presentation

4 excellent

Contribution

3 good

Limitations

The paper's limitation section did a good job, I think. One additional potential limitation, though, is that, like in chain-of-thought prompting, the approach effectively introduces a pipeline: the model needs to first predict the grammar rules for an example, then condition on these rules to predict the output. This could lead to error propagation if the first step is wrong. However, the results are strong enough to indicate this probably isn't happening, and if it did it could be addressed with self-consistency / consensus decoding or another MBR-like approach.

Reviewer qhZ75/10 · confidence 3/52023-07-10

Summary

This work explores grammar prompting as a simple approach for enabling LLMs to use external knowledge and domain-specific constraints, expressed through a grammar expressed in Backus–Naur Form (BNF), during in-context learning. Grammar prompting augments each demonstration example with a specialized grammar that is minimally sufficient for generating the particular output example, where the specialized grammar is a subset of the full DSL grammar. The authors apply grammar prompting to various domain specific languages for semantic parsing (SMCalFlow, Overnight, GeoQuery), AI planning (PDDL), and molecule generation (SMILES), and find that it can meaningfully improve upon standard prompting baselines in the few-shot setting.

Strengths

This paper is interesting and well-written. This work is an efficient prompting generation algorithm for LLM. Experimental results show that the proposed method seems effective in generating sequences with grammars.

Weaknesses

In experiment part, how do you ensure the results are enough to verify the proposal? For example, the superiority in molecule generation is not well established compared with the other graph-based generation methods.

Questions

See above.

Rating

5: Borderline accept: Technically solid paper where reasons to accept outweigh reasons to reject, e.g., limited evaluation. Please use sparingly.

Confidence

3: You are fairly confident in your assessment. It is possible that you did not understand some parts of the submission or that you are unfamiliar with some pieces of related work. Math/other details were not carefully checked.

Soundness

3 good

Presentation

3 good

Contribution

3 good

Limitations

See above.

Reviewer rfkJ2023-08-12

Thanks to the authors for the response! I still feel very positively about this paper after reading it and the other reviews and responses.

Reviewer pwxh2023-08-18

Thanks for the response. I will keep the current ratings based on the responses and other reviews

Program Chairsdecision2023-09-21

Decision

Accept (poster)

© 2026 NYSGPT2525 LLC