Title: Grammar Prompting for Domain-Specific Language Generation with  Large Language Models
Venue: NeurIPS 2023
Accepted: yes
Keywords: semantic parsing

large language models

PDDL

AI planning

molecule generation

data efficiency

grammar-based learning
Abstract: 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.

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

Abstract

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 domainspecific languages), it is challenging for the LLM to generalize from just a few exemplars. We propose 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.

1 Introduction

Prompting large language models (LLMs) with demonstrations optionally combined with natural language instructions has been shown to be an effective approach for surfacing their myriad capabilities acquired through pretraining [10]. This approach is however inadequate for applications where the task specifications cannot be fully delineated through just a handful of exemplars, for example in semantic parsing where an LLM must translate a natural language utterance to an executable program in a domain-specific language (DSL). DSLs often incorporate domain-specific abstractions and semantics that are difficult to characterize via just a few demonstrations. And unlike generalpurpose programming languages, DSLs are by definition specialized and thus unlikely to have been encountered often enough (or at all) during pretraining for the LLM to acquire its full syntax.

How can we draw on the few-shot learning capabilities of LLMs to generate structured strings that are substantially different from those seen during pretraining? This work explores grammar prompting as a simple approach for data-efficient generation of structured languages where an output string in the language can be derived through a series of symbolic manipulations. We exploit the fact that constraints over a structured output space can often be succinctly described by a context-free grammar in Backus-Naur Form (BNF), which is commonly used to define the syntax of a language. Grammar prompting augments each in-context example ( x , y ) with a specialized BNF grammar G [ y ] that is minimally sufficient for generating y . Given a new input, the LLM first predicts the specialized BNF grammar and then generates the answer conditioned on the grammar.

Grammar prompting follows the recent line of work which enhances the few-shot reasoning capabilities of LLMs by interleaving intermediate 'reasoning' steps between each in-context input and

Code and data available at: https://github.com/berlino/grammar-prompting .

output [51, 24, 86, 80, 73]. The key difference in our approach is that the intermediate variable is in the form of a formal grammar rather than in natural language, which focuses on eliciting the symbolic manipulation capabilities of LLMs. The use of a formal grammar moreover makes it possible to impose constraints during incremental decoding such that syntactic validity is guaranteed. Finally, unlike chain-of-thought-style prompts [86] which typically require manual verbalization of the intermediate reasoning steps, in our approach the specialized grammar G [ y ] can be derived automatically by parsing the output y with the full (unspecialized) DSL grammar.

To summarize,

We propose grammar prompting as a simple approach for enabling LLMs to generate highly structured languages from just a few exemplars.

We design a constrained LLM decoding algorithm tailored to grammar prompting, which guarantees syntactic validity while minimizing the number of LLM API calls.

We 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.

2 Background

In this section, we define our problem and review the few-shot learning method that we build on.

2.1 Problem Formulation: Domain-Specific Language Generation

Let Σ ∗ be the set of all finite strings over an alphabet Σ , and further let D ⊆ Σ ∗ be a domain-specific language (DSL) for an application of interest. Given an input x (e.g., a natural language command) we are interested in generating y ∈ D (e.g., a program in a DSL fulfilling the command), as shown by the following calendar assistant example from SMCalFlow [6]:

x : Add meeting with Jean's manager on Wednesday at 3PM.

y : CreateEvent (& ( start _ ? WednesdayNumberPM ( 3 ))( attendee _ ? FindManager ( Jean )))

DSLs are crafted by experts who use their domain-specific knowledge to incorporate higher-level abstractions than are typically found in general-purpose programming languages. We assume access to an expertdefined grammar G that fully specifies the DSL's syntax. As is the case with many DSLs, we further assume that G is a context-free grammar in Backus-Naur Form (BNF). See Figure 1 for a simple example adapted from SMCalFlow [6]. Letting L ( G ) be the language generated by G , we have D ⊆ L ( G ) ⊆ Σ ∗ (not all syntactically valid programs are semantically valid).

event

::=

"CreateEvent(" constraint ")"

|

"QueryEvent(" constraint ")"

constraint

::=

"(&" constraint constraint ")"

|

"(start_?" date time?

")"

|

"(attendee_?" attendee* ")"

date

::=

"Wednesday"

|

"Monday"

number

::=

("0".."9")+

time

::=

"NumberAM(" number ")"

|

"NumberPM(" number ")"

attendee

::=

|

"Bob"

|

"Carol"

|

|

"FindManager"

2.2 Few-shot Learning with Large Language Models

In-context learning with large language models (LLMs) has been shown to be an effective approach for few-shot learning [10]. Under this approach, a pretrained LLM is conditioned on N demonstration examples ( x ( i ) , y ( i ) ) N i =1 followed by a test example x , and the output is given by decoding from the prompted LLM, i.e., P LLM ( y | x , ( x ( i ) , y ( i ) ) N i =1 ) . The demonstration examples can be optionally preceded by natural language instructions to further improve performance or even enable zeroshot learning [85, 62]. Recent work has additionally shown that interleaving natural language verbalizations of intermediate reasoning steps between each x ( i ) and y ( i ) can greatly improve few-shot performance on complex reasoning tasks [51, 86, 80, 73, 16].

The effectiveness of few-shot in-context learning depends both on how useful the implicit knowledge acquired through pretraining is for the task, and on how effectively the task specifications can be conveyed through the demonstrations. For DSL, the structured nature of combinatorial output space (i.e., the DSL grammar G ) cannot be adequately captured through just a handful of demonstrations. Thus, few-shot generation of strings of a DSL remains challenging for LLMs.

LLMPrompt You are an expert programmer, and you need to write a program for the given natural language query. First, you should write a grammar that contains all the necessary BNF rules. Then, you should write programs that conform to your predicted rules. (optional) G : [BEGIN RULES] . . . [END RULES] x (1) : find the meeting on Wednesday with Bob and Carol G [ y (1) ] : event ::= "QueryEvent(" constraint ")" constraint ::= "(&" constraint constraint ")" | "(start_?" date ")" | "(attendee_?" attendee attendee ")" date ::= "Wednesday" attendee ::= "Bob" | "Carol" y (1) : QueryEvent (& ( start _ ? Wednesday )( attendee _ ? Bob Carol )) . . . x : Add meeting with Jean's manager on Wednesday at 3PM

3 Grammar Prompting

Grammar prompting exploits the fact that while the actual strings of a DSL may not have been encountered frequently enough (or at all) during pretraining for the LLM to implicitly acquire its syntax, the LLM will likely have encountered many instances of metalanguages (languages used to describe other languages). BNF grammars are a standard metalanguage for specifying a language's syntax, and are expected to occur in the LLM training corpus with some frequency (e.g., in computer science textbooks). We thus focus on using BNF grammars for few-shot DSL generation.

Here <symbol> is a nonterminal symbol and each <expr 1 > is a sequence of nonterminal and terminal symbols. 1 A straightforward approach for incorporating a BNF grammar during in-context learning is to simply prepend the string representation of the full grammar G to the demonstration examples, along with an instruction to use the grammar. However in preliminary experiments, we found that this did not yield any improvements. 2

3.1 Specialized Grammars

We propose to use specialized grammars to enable better use of domain-specific knowledge and constraints. A specialized grammar G ′ ⊆ G is a grammar obtained from taking a subset of the rules of the full grammar G . We further define G [ y ] , a minimal specialized grammar of y , to be a BNF

grammar with the following properties: (1) y ∈ L ( G [ y ]) , and (2) ∀ r ∈ G [ y ] , y ̸∈ L ( G [ y ] \ { r } ) . 3 We can readily obtain a minimal specialized grammar by using G to parse y and then taking the union of rules that were used in the derivation of y .

Grammar prompting feeds a sequence of ( x ( i ) , G [ y ( i ) ] , y ( i ) ) N i =1 along with x as a prompt to an LLM. For inference we first obtain the specialized grammar with an (approximate) arg max decoding

We then obtain the program conditioned on ̂ G ,

We discuss how to perform constrained decoding with ̂ G ⊆ G and ̂ y ∈ L ( ̂ G ) in the next section. Grammar prompting views DSL program generation as a grammar specialization process where given a natural language specification x , a set of production rules, ̂ G , is selected from G , and then a program ̂ y is deduced according to the selected rules. Grammar prompting can also be viewed as an instance of chain-of-thought prompting [51, 86] where the intermediate thought is in the form of a formal grammar. However, unlike typical chain-of-thought prompting where the answer is (usually) deterministic given the intermediate reasoning steps, in our case there is still some uncertainty with respect to ̂ y given ̂ G (e.g., L ( ̂ G ) could still be infinite).

3.2 Constrained Decoding

The use of a formal grammar as an intermediate variable makes it possible to enforce grammatical constraints during autoregressive LLM decoding. We first discuss how we enforce the constraint y ∈ L ( ̂ G ) . One approach to constrained decoding is to use ̂ G to obtain a left-to-right Earley parser [18] and only decode from valid continuations at each decoding step. However this simple strategy poses several practical challenges when working with API-only LLMs. For one, a valid terminal continuation in ̂ G may consist of multiple BPE tokens. Moreover, while we can sample a valid continuation at each time step by disallowing invalid tokens, 4 since the set of valid continuations changes at each time step, this strategy would require calling the LLM API at each time step with the full

Algorithm 1 Earley-based Constrained Generation

prompt and prefix along with the disallowed continuations, which is prohitively expensive. 5

While there are many methods for grammar-constrained LM decoding [68, 64, 26], we present a simple strategy which speculatively decodes from the LLM to look ahead for multiple tokens. The pseudocode is shown in Algorithm 1. At each prediction step, we ask the LLM to speculatively decode the full program conditioned on the current prefix (lines 4-5). If the resulting continuation leads to a valid program, we return it (lines 6-7). Otherwise, we consult an Earley parser to extract the longest valid prefix from the current prediction ( y prefix), along with a set of valid terminals that can

Earley-based Constrained Decoding

ˆ

y

(

t

)

:

CreateEvent

(& (

start

_

?

Wednesday NumberPM

(

3

))(

attendee

_

?

Manager

))

Extract the longest valid prefix, and possible fixes (i.e., next terminals) based on Earley parsing

:

y

prefix

:

QueryEvent

(& (

start

_

?

Wednesday

)(

attendee

_

?

Σ[

y

prefix

]

:

{

,

FindManager(

}

Find the best candidate and concatenate it with the prefix

:

ˆ

y

(

t

)

QueryEvent

(& (

start

_

?

Wednesday

)(

attendee

_

?

FindManager

(

follow the prefix ( Σ[ y prefix ] ). Finally, we rely on the LLM's probabilities to decide which terminal to use, with which a new partial program can be constructed (lines 10-11). 6 Figure 3 illustrates one prediction step where the predicted program is corrected into a new valid partial program. Note that w can consist of multiple BPE tokens, e.g., "FindManager(" in Figure 3. By scoring over multi-token terminals, the search procedure is implicitly augmented by looking ahead for a few tokens.

We use a similar procedure to operationalize the constraint G ′ ⊆ G , except that EarleyParse (used at Algorithm 1, line 9) is constructed with a metagrammar (i.e., the grammar of G ) for grammar prediction. See appendix A.1 for more details. In our ablation study we find that while these constraints are helpful insofar as they guarantee syntactic validity, grammar prompting still meaningfully improves upon standard prompting with even with simple unconstrained decoding.

4 Experiments

Weapply grammar prompting to diverse domains: DSLs for semantic parsing (SMCalFlow, Overnight, GeoQuery), an action DSL (PDDL planning), and a molecule generation DSL (SMILES). These experiments are not necessarily intended to improve upon the state-of-the-art on these benchmarks but rather intended to assess whether LLMs can improve upon standard prompting for few-shot DSL generation by learning to predict and use grammars during in-context learning.

4.1 Semantic Parsing for Tool Usage

Software tools are typically accompanied by a collection of human-interpretable APIs which provide a platform for developers to interact programmatically with the tools. These APIs constitute a DSL, where each production rule of the grammar specifies the input and output types for a specific API call (see Figure 1 for an example). These tools demonstrate a broad spectrum in terms of DSL complexity, ranging from single-function tools such as Google(user_query) , Translate(sentence, language) to more complex tools such as the entirety of Wolfram language. 7 Enabling LLMs to use external tools via APIs is an important step towards enhancing their capabilities [63, 56, 53, 72, 47].

We test our approach on standard semantic parsing benchmarks involving complex DSLs: SMCalFlow [6], which features human-generated utterances about calendar management (see Figure 2); GeoQuery [99] which features queries against a US Geography database; and Overnight-Blocks [81], which features queries about blocks in a synthetic block world. See appendix B for examples of input-output pairs along with the specialized grammars. The original benchmarks target the training of conventional semantic parsers and thus contain hundreds/thousands of training examples. Even prompting-based approaches on these benchmark rely on retrieval-based in-context learning which first retrieves m exemplars from a large training set of n examples ( n ≫ m ) based on some similarity measure (e.g., BM-25), and then performs in-context learning with the retrieved exemplars [57, 95, 68, 46]. In contrast, we target the true few-shot setting where we only assume access to 16-32 demonstration examples.

Our baselines here include: (1) standard prompting, (2) standard prompting with constrained decoding based on the full DSL grammar G [68, 64], and (3) a derivation tree-based prompting baseline which imbues more structural information to the exemplars by feeding the linearized derivation

tree instead of the surface form program. 8 We use Codex-davinci-002 [13] as the base LLM for these main experiments. Language models trained on code (such as Codex) have shown to be particularly effective on semantic parsing benchmarks [67]. We evaluate according to program accuracy (matching the predicted and reference programs) as well as execution accuracy (same execution in both programs) if possible.

Few-shot results. The main results are shown in Table 1. We find that grammar prompting can meaningfully improve upon the standard prompting baseline even without constrained decoding. Interestingly, grammar prompting outperforms derivation tree prompting which actually provides more information than the minimal specialized grammar G [ y ] (since the derivation tree explicitly shows how the rules are actually applied to obtain the program). This potentially indicates that having the LLM 'plan out' the program by forcing it to predict the specialized grammar ̂ G first is an effective strategy. We also analyze the effect of constrained decoding on the number of LLM API calls in Table 7 of appendix A.1, where we observe that constrained decoding requires roughly three times more API calls than unconstrained decoding. However, despite the promising performance of grammar prompting, there is a large gap between using the predicted grammar versus using the oracle grammar (i.e., setting ̂ G = G [ y ] ), indicating opportunities for further work in this area.

Retrieval-based in-context learning. While our core target application is few-shot semantic parsing, we also apply grammar prompting for retrieval-based in-context learning to test whether it can still improve performance in the data-rich regime and also to compare against prior work on these benchmarks. Results in Table 2 (left) show that grammar prompting can improve results even in this setting, although the improvements are less pronounced than in the few-shot setting.

Out-of-distribution generalization. We experiment to see whether grammar prompting can improve compositional generalization on GeoQuery. Specifically, we test grammar prompting on the compositional splits of GeoQuery split from Shaw et al. [66]. These splits feature structural divergence between training and test examples, e.g., programs have different templates or length. Results in Table 2 (right) shows that grammar prompting can improve upon standard prompting, across all splits (Template, TMCD, Length).

We next assess whether grammar prompting can enable LLMs to make zero-shot use of unseen functions (NewFunc) that are not even part of the retrieval set. We set aside 8 functions ( smallest, shortest, most, highest, sum, population_1, count, major ) and remove them from the retrieval set, simulating a scenario where new functions are supported in the backend yet no NL-program paired data is available for adapting a semantic parser. Note that for GeoQuery (and Overnight-Blk), we always prepend the full DSL grammar G -which includes the held-out functions-before the in-context exemplars. Table 2 (right-most column) shows that grammar-prompted LLMs achieve significantly better performance than standard prompting. Our results suggest that the explicit prediction of specialized grammars elicits understanding and reasoning at the grammar level, thereby enabling generalization to unseen functions. We also found that without constrained generation, LLMs were often able to guess functions that did not exist but were nonetheless sensible. An interesting direction is to explore whether LLMs can tackle DSL-open benchmarks such as LARC [1].

Different base LLMs. We finally experiment with grammar prompting across different base LLMs. Since GPT3.5's 4K token limit is smaller than Codex's (8K) and GPT-4's (8K) limits, we use fewer exemplars in these experiments than before (24/8/16 exemplars for GeoQuery/SMCalFlow/Overnight-B respectively). Due to API cost, we limit our experiments to a smaller subset of 100 test examples instead of the full test set.

Table 3 shows that grammar prompting improves upon standard prompting in the majority of the settings. The exceptions are

SMCalFlow with GPT-3.5 where both methods performed poorly, and GeoQuery with PaLM 2-L[7], where standard prompting already performed well.

4.2 Class-Specific Molecule Generation

We next demonstrate an application of grammar prompting beyond language parsing problems with a molecule generation task. Existing methods for molecule generation typically focus on training specialized neural models using large training sets [45, 37, 15, 2, 79, 61, 19]. We instead follow Guo et al. [29] and explore a few-shot setting where the task is to generate class-specific molecules given a small number of exemplars of that class. Formally, given a small set of molecules { y ( i ) c } N i =1 belonging to a particular molecule class c ∈ { Acrylates, Chain Extenders, Isocyanates } , our goal is to generate novel molecules y c of the same class that can be synthesized using existing molecules. Since the in-context examples in this case will only consist of molecules of the same class, the 'input' x ( i ) c is the empty string in this case. The data contains 32 Acrylates, 11 Chain Extenders, and 11 Isocyanates (see appendix G of Guo et al. [29]).

While molecules can be more faithfully represented with 3D graph structure, the SMILES string representation [87] remains popular due to its ease of use. 9 The specialized grammars G [ y c ] (which are specialized from the SMILES grammar) encode various structural properties of the molecule that are specific to the molecule class. Figure 4 shows an example of a specialized grammar and the corresponding molecule in SMILES format. In this example, ring_closure ::= "1" specifies the number of rings, and branch ::= "(" smiles ")" specifies whether there is a branch.

We test our approach by generating 100 molecules for each class and assessing the quality of the generated molecules. In addition to the standard prompting baseline, we also run the graph grammar baseline from Guo et al. [29] which learns a hypergraph grammar [38] from the given molecules. We use four metrics: Validity (V) , the percentage of chemically valid molecules; Diversity (D) , average pairwise Tanimoto distance over Morgan fingerprints [60]; Retrosynthesis score (R) , the percentage

G

[

y

]

:

smiles

::=

atom chain branch chain chain

|

atom chain

atom

::=

organic_symbol

organic_symbol

::=

"C"

|

"N"

|

"O"

chain

::=

atom ring_closure bond atom

|

bond atom

|

bond atom ring_closure

|

atom

|

atom bond atom bond atom

|

bond atom bond atom

ring_closure

::=

"1"

bond

::=

"="

branch

::=

"(" smiles ")"

O

O

O

y

:

CC

(=

C

)

C

(=

O

)

OCCOC1

=

CC

=

CC

=

C1

of molecules that are synthesizable from existing molecules, which is computed approximately via the Retro* model [12]; Membership (M) , the percentage of molecules that belong to the desired monomer class. We use GPT-3.5 as the base LLM and sample from the LLM without constrained decoding, as constrained decoding was found to decrease the diversity of samples. See appendix A.2 for the full experimental setup.

Results. Table 4 shows that, compared with standard prompting, grammar prompting significantly improves the synthesis of Acrylates and Chain Extenders across all metrics, while yielding mixed results for Isocyanates. Notably, both prompting-based methods outperform the graph grammar baseline in terms of Retro score, possibly due to that LLMs may have been pre-trained on existing datasets of molecules, enabling them to effectively generate synthesizable molecules. In contrast, the baseline method cannot incorporate any external knowledge beyond the 11 or 32 molecules provided. Our preliminary results indicate that LLMs can serve as a useful tool for generating string representations of chemical structures (and potentially other biological/chemical structures).

4.3 Action Subset Selection for Efficient PDDL Planning

Our final experiments show how grammar prompting can improve the efficiency of classical AI planners. Classical planning is the problem of finding a sequence of actions (i.e., a plan) that goes from an initial state s 0 to a goal state s g . An action is represented by a ground operator (e.g., unstack ( block1 , block2 ) which consists of an operator unstack along with two object arguments). We additionally consider macro-operators which can potentially speed up planning [9]. 10 Planning tasks, along with actions, are represented in Planning Domain Definition Language (PDDL) [27]. We explore how grammar prompted LLMs can help guide classical planning algorithms.

We design specialized grammars to provide guidance to the classical greedy best-first search (GBFS) algorithm [5] by selecting a set of relevant actions. Figure 5 illustrates an example of such a specialized grammar, which captures all the necessary actions for the final plan y that solves the given task. The process of the guided planning consists of the following steps: (1) given a task, predict a specialized grammar G [ y ] ; (2) use the specialized grammar G [ y ] to subsequently generate a plan within the restricted action space derived from G [ y ] ; (3) initialize GBFS's priority queue with the LLM-generated plan, and (4) search for the final plan in the restricted action space. Our setup builds upon the idea of using an LLM-generated plan to initialize GBFS from Silver et al. [69], which has a simpler two-step process: (1) given a task, predict a plan via standard prompting, and (2) utilize this plan to guide GBFS. We use their method as our baseline.

Following Silver et al. [69], we create a similar few-shot setting for LLM planning, using 5 tasks as in-context examples and 10 tasks for evaluation from Pyperplan [5]. We test our approach on 3 classic

Specialized Action Grammar for PDDL Planning

s

0

:

A

B

C

D

s

g

:

A

C

B

D

G

[

y

]

:

plan

::=

ground_operator+

ground_operator

::=

"pick_up-and-stack(" block block ")"

|

"unstack-and-put_down(" block block ")"

block

::=

"A"

|

"B"

|

"C"

y

:

unstack-and-put_down(B, A)

;

pick_up-and-stack(C, A)

;

pick_up-and-stack(B, C)

domains in PDDL planning, including Blocks, Depot and Satellite. For the action space, we use either a set of primitive actions (Prim) or an augmented set with macro actions (Macro). In addition to standard prompting, we add two more baselines: (1) No LLM: planning with the entire set of actions; (2) Min Macro: where we construct a minimal set of macro actions for each domain by selecting actions from existing plans for the training tasks. The Min Macro baseline is a domain-specific method to reduce the action space. By comparing to Min Macro, we can verify the effectiveness of instance-specific v.s. domain-specific action selection. See appendix A.3 for more details.

Results. We evaluate the efficiency of planning in terms of the number of search nodes created/expanded, as well as the success rate. Table 5 shows the promising performance of LLM-guided planning via grammar prompting. In Blocks, grammar prompting significantly improves efficiency while maintaining 100% success rate. In Depot, grammar prompting with macro actions improves the success rate by 20% over the best competing baseline. In Satellite, using primitive actions yields the best performance with 100% success rate and a reduction of 57% expanded nodes comparing to the No LLM baseline. While our experiments are not intended to complete with the state-of-the-art algorithms for fast planning [20-22, 32, 25, 84], they indicate the promise of LLMs for improving existing planning algorithms.

5 Discussion and Limitations

We discuss several limitations of our approach including some negative results. Grammar prompting did not yield any improvements for DSLs that were likely to have been frequently encountered during pretraining (e.g., regular expressions, SQL). Moreover, constrained generation based on specialized grammars led to increased API calls, and was not always beneficial for tasks beyond semantic parsing. For instance, in molecule generation we discovered that enforcing constraints can sometimes result in lower diversity. Additionally, in PDDL planning we observed that the constraints applied to prune objects can sometimes negatively impact performance, suggesting that relevant object selection is still very challenging for LLMs. It may be interesting to explore whether finetuning of moderately-sized LLMs using specialized grammars can lead to better grammar-based models for DSL generation.

On the positive front, our work demonstrates that LLMs have the capacity to understand and generate metalanguages. Working in this 'metalanguage space' can be combined with chain-of-thought-style [86] prompts by, for example, manually providing natural language comments to the rules of the specialized grammars. We found this to improve results slightly on semantic parsing (see Figure 6

of appendix A.1). Moreover, many scientific problems can be formally approached by representing hypotheses as DSL programs [71], and DSLs can enable easier encoding of human prior knowledge and scientific principles, providing a foundation for scientific discovery. Recent work shows that state-of-the-art LLMs can follow previously unseen formal systems [75]. Techniques like grammar prompting can widen the scope of scientific problems for which LLMs could be effectively applied by more explicitly accounting for external knowledge and constraints.

6 Related Work

Chain-of-thought prompting. Grammar prompting extends a recent line of work on improving reasoning capabilities by requesting explicit reasoning steps as part of the prompt [51, 24, 86, 80, 14, 94]. Our approach is closely related to concurrent work on employing symbolic variables as part of the prompt [30, 50, 33, 97, 52], though we are not aware of any existing work that uses formal grammars as the intermediate reasoning step.

LLMs for program generation and semantic parsing. Generating programs from natural language specifications, a task often referred to as semantic parsing, is a sub-problem of program synthesis; for surveys, see Kamath and Das [39] and Gulwani et al. [28]. Recent works [8, 89] have explored using LLMs for generating code in general-purpose programming languages (e.g., Python). Our work further extends this line by examining whether LLMs can generate DSL programs, which are intrinsically scarce. There has also been work on using LLMs for tool usage via further training [63] or prompting [56, 77], investigating how model scales [57] and retrievers [96, 46] affect in-context learning for semantic parsing, and constrained decoding [64, 68, 55] for program generation.

Neural grammars. Grammar prompting can also been seen as a 'fully LLM' instantiation of a line of work on neural parameterizations of symbolic grammars [35, 17, 43, 42, 36, 100, 92, 91, 93]. Indeed, our approach to semantic parsing essentially uses prompt-based learning to define a quasisynchronous grammar [70, 78] whose rules dynamically depend on the source sentence. Concretely, in contrast to recent works which embed learnable neural components within synchronous grammars [41, 23, 76], grammar prompting relies on the implicit in-context learning capabilities of LLMs for the learning component. (However unlike these works, our conditional grammar does not explicitly align its rules to the subparts of the source sentence).

Grammar-based molecule generation. Grammar-based methods have gained significant interest in the realm of molecule generation, offering advantages in interpretability, data-efficiency, and controllability. One line of research involves integrating generic SMILES grammars with neural networks to generate syntactically correct molecules [45, 15]. Another approach centers on datadriven induction of grammars for generation [29, 38]. Our work aligns with the former, viewing grammar prompting as a straightforward method for integrating grammar into an LLM without the need for additional training.

LLMs for planning. Recently, LLMs have been increasingly studied in the context of planning for autonomous agents. When given goals expressed in natural language in household environments, earlier works [3, 65, 34, 48] directly prompted LLMs to predict executable actions. However, in PDDL domains, recent works [69, 74] showed that LLMs underperform classical planners if the desired action sequences are very long. Grammar prompting represents a promising strategy for augmenting classical planners with LLMs to get the best of both worlds. Other related efforts include translating between problems and PDDL models [49] and corrective re-prompting [58]. Besides using LLMs, integrating learning and planning has been extensively studied in the past literature, e.g., learning actions [4, 82], skills [84], macro-actions [9], rules [88] and guidance strategies [90, 83, 40] for more efficient planning.

7 Conclusion

We propose grammar prompting as a simple approach for improving few-shot DSL generation with large language models. Experiments across a range of structured languages including DSLs for semantic parsing (SMCalFlow, GeoQuery, Overnight), PDDL planning (action DSL), and molecule generation (SMILES), show that grammar prompting can improve upon standard prompting baselines. The encouraging results in semantic parsing indicate its potential to assist LLMs with tool usage, and the promising results in other domains indicate that grammar prompting can enable application of LLMs in domains that intrinsically depend on DSLs.

Acknowledgments

We thank Jacob Andreas, Gabriel Grand, Linlu Qiu, Tom Silver, and Hunter Lang for helpful discussion and feedback. This study was supported by funds from the Google-MIT research collaborations program and the GIST-MIT joint research program.

References

Sam Acquaviva, Yewen Pu, Marta Kryven, Theodoros Sechopoulos, Catherine Wong, Gabrielle Ecanow, Maxwell Nye, Michael Tessler, and Josh Tenenbaum. Communicating natural programs to humans and machines. Proceedings of NeurIPS , 35:3731-3743, 2022.

Walid Ahmad, Elana Simon, Seyone Chithrananda, Gabriel Grand, and Bharath Ramsundar. Chemberta-2: Towards chemical foundation models. arXiv preprint arXiv:2209.01712 , 2022.

Michael Ahn, Anthony Brohan, Noah Brown, Yevgen Chebotar, Omar Cortes, Byron David, Chelsea Finn, Keerthana Gopalakrishnan, Karol Hausman, Alex Herzog, et al. Do as I can, not as I say: Grounding language in robotic affordances. arXiv preprint arXiv:2204.01691 , 2022.

Diego Aineto, Sergio Jiménez, and Eva Onaindia. Learning STRIPS action models with classical planning. In Proceedings of the International Conference on Automated Planning and Scheduling , volume 28, pages 399-407, 2018.

Yusra Alkhazraji, Matthias Frorath, Markus Grützner, Malte Helmert, Thomas Liebetraut, Robert Mattmüller, Manuela Ortlieb, Jendrik Seipp, Tobias Springenberg, Philip Stahl, and Jan Wülfing. Pyperplan (v1.3), 2020. URL https://doi.org/10.5281/zenodo.3701399 .

Jacob Andreas, John Bufe, David Burkett, Charles Chen, Josh Clausman, Jean Crawford, Kate Crim, Jordan DeLoach, Leah Dorner, Jason Eisner, Hao Fang, Alan Guo, David Hall, Kristin Hayes, Kellie Hill, Diana Ho, Wendy Iwaszuk, Smriti Jha, Dan Klein, Jayant Krishnamurthy, Theo Lanman, Percy Liang, Christopher H. Lin, Ilya Lintsbakh, Andy McGovern, Aleksandr Nisnevich, Adam Pauls, Dmitrij Petters, Brent Read, Dan Roth, Subhro Roy, Jesse Rusak, Beth Short, Div Slomin, Ben Snyder, Stephon Striplin, Yu Su, Zachary Tellman, Sam Thomson, Andrei Vorobev, Izabela Witoszko, Jason Wolfe, Abby Wray, Yuchen Zhang, and Alexander Zotov. Task-oriented dialogue as dataflow synthesis. Transactions of the Association for Computational Linguistics , 8:556-571, 2020. doi: 10.1162/tacl_a_00333. URL https: //aclanthology.org/2020.tacl-1.36 .

Rohan Anil, Andrew M Dai, Orhan Firat, Melvin Johnson, Dmitry Lepikhin, Alexandre Passos, Siamak Shakeri, Emanuel Taropa, Paige Bailey, Zhifeng Chen, et al. PaLM 2 technical report. arXiv preprint arXiv:2305.10403 , 2023.

Jacob Austin, Augustus Odena, Maxwell Nye, Maarten Bosma, Henryk Michalewski, David Dohan, Ellen Jiang, Carrie Cai, Michael Terry, Quoc Le, et al. Program synthesis with large language models. arXiv preprint arXiv:2108.07732 , 2021.

Adi Botea, Markus Enzenberger, Martin Müller, and Jonathan Schaeffer. Macro-ff: Improving AI planning with automatically learned macro-operators. Journal of Artificial Intelligence Research , 24:581-621, 2005.

Tom B Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah, Jared Kaplan, Prafulla Dhariwal, Arvind Neelakantan, Pranav Shyam, Girish Sastry, Amanda Askell, et al. Language models are few-shot learners. In Proceedings of NeurIPS , 2020.

Ruisheng Cao, Su Zhu, Chen Liu, Jieyu Li, and Kai Yu. Semantic parsing with dual learning. In Proceedings of ACL , pages 51-64, Florence, Italy, July 2019. Association for Computational Linguistics. doi: 10.18653/v1/P19-1007. URL https://aclanthology.org/P19-1007 .

Binghong Chen, Chengtao Li, Hanjun Dai, and Le Song. Retro*: Learning retrosynthetic planning with neural guided A* search. In Proceedings of ICML , pages 1608-1616. PMLR, 2020.

Mark Chen, Jerry Tworek, Heewoo Jun, Qiming Yuan, Henrique Ponde de Oliveira Pinto, Jared Kaplan, Harri Edwards, Yuri Burda, Nicholas Joseph, Greg Brockman, Alex Ray, Raul Puri, Gretchen Krueger, Michael Petrov, Heidy Khlaaf, Girish Sastry, Pamela Mishkin, Brooke Chan, Scott Gray, Nick Ryder, Mikhail Pavlov, Alethea Power, Lukasz Kaiser, Mohammad Bavarian, Clemens Winter, Philippe Tillet, Felipe Petroski Such, Dave Cummings, Matthias Plappert, Fotios Chantzis, Elizabeth Barnes, Ariel Herbert-Voss, William Hebgen Guss, Alex Nichol, Alex Paino, Nikolas Tezak, Jie Tang, Igor Babuschkin, Suchir Balaji, Shantanu Jain, William Saunders, Christopher Hesse, Andrew N. Carr, Jan Leike, Josh Achiam, Vedant Misra, Evan Morikawa, Alec Radford, Matthew Knight, Miles Brundage, Mira Murati, Katie Mayer, Peter Welinder, Bob McGrew, Dario Amodei, Sam McCandlish, Ilya Sutskever, and Wojciech Zaremba. Evaluating large language models trained on code. 2021.

Wenhu Chen, Xueguang Ma, Xinyi Wang, and William W Cohen. Program of thoughts prompting: Disentangling computation from reasoning for numerical reasoning tasks. arXiv preprint arXiv:2211.12588 , 2022.

Hanjun Dai, Yingtao Tian, Bo Dai, Steven Skiena, and Le Song. Syntax-directed variational autoencoder for structured data. arXiv preprint arXiv:1802.08786 , 2018.

David Dohan, Winnie Xu, Aitor Lewkowycz, Jacob Austin, David Bieber, Raphael Gontijo Lopes, Yuhuai Wu, Henryk Michalewski, Rif A. Saurous, Jascha Sohl-dickstein, Kevin Murphy, and Charles Sutton. Language Model Cascades. arXiv:2207.10342 , 2022.

Chris Dyer, Adhiguna Kuncoro, Miguel Ballesteros, and Noah A. Smith. Recurrent neural network grammars. In Proceedings of NAACL , 2016.

Jay Earley. An efficient context-free parsing algorithm. Communications of the ACM , 13(2): 94-102, 1970.

Carl Edwards, Tuan Lai, Kevin Ros, Garrett Honke, and Heng Ji. Translation between molecules and natural language. arXiv preprint arXiv:2204.11817 , 2022.

Richard E Fikes and Nils J Nilsson. STRIPS: A new approach to the application of theorem proving to problem solving. Artificial intelligence , 2(3-4):189-208, 1971.

Maria Fox and Derek Long. PDDL2. 1: An extension to PDDL for expressing temporal planning domains. Journal of Artificial Intelligence Research , 20:61-124, 2003.

Maria Fox and Derek Long. Modelling mixed discrete-continuous domains for planning. Journal of Artificial Intelligence Research , 27:235-297, 2006.

Dan Friedman, Alexander Wettig, and Danqi Chen. Finding Dataset Shortcuts with Grammar Induction. In Proceedings of EMNLP , 2022.

Luyu Gao, Aman Madaan, Shuyan Zhou, Uri Alon, Pengfei Liu, Yiming Yang, Jamie Callan, and Graham Neubig. PAL: Program-aided Language Models. arXiv:2211.10435 , 2022.

Caelan R. Garrett, Tomas Lozano-Perez, and Leslie P. Kaelbling. PDDLStream: Integrating symbolic planners and blackbox samplers. In International Conference on Automated Planning and Scheduling (ICAPS) , 2020. URL https://arxiv.org/abs/1802.08705 .

Saibo Geng, Martin Josifosky, Maxime Peyrard, and Robert West. Flexible Grammar-Based Constrained Decoding for Language Models. arXiv:2305.13971 , 2023.

Malik Ghallab, Adele Howe, Craig Knoblock, Drew McDermott, Ashwin Ram, Manuela Veloso, Daniel Weld, David Wilkins SRI, Anthony Barrett, Dave Christianson, et al. PDDL the planning domain definition language. Technical Report CVC TR98003/DCS TR1165. New Haven, CT: Yale Center for Computational Vision and Control. , 1998.

Sumit Gulwani, Oleksandr Polozov, Rishabh Singh, et al. Program synthesis. Foundations and Trends® in Programming Languages , 4(1-2):1-119, 2017.

Minghao Guo, Veronika Thost, Beichen Li, Payel Das, Jie Chen, and Wojciech Matusik. Data-efficient graph grammar learning for molecular generation. In Proceedings of ICLR , 2022.

Joy He-Yueya, Gabriel Poesia, Rose E Wang, and Noah D Goodman. Solving math word problems by combining language models with symbolic solvers. arXiv preprint arXiv:2304.09102 , 2023.

Jonathan Herzig and Jonathan Berant. Span-based semantic parsing for compositional generalization. In Proceedings of ACL , pages 908-921, Online, August 2021. Association for Computational Linguistics. doi: 10.18653/v1/2021.acl-long.74. URL https: //aclanthology.org/2021.acl-long.74 .

Jörg Hoffmann. The metric-ff planning system: Translating'ignoring delete lists'to numeric state variables. Journal of Artificial Intelligence Research , 20:291-341, 2003.

Hanxu Hu, Hongyuan Lu, Huajian Zhang, Wai Lam, and Yue Zhang. Chain-of-symbol prompting elicits planning in large langauge models. arXiv preprint arXiv:2305.10276 , 2023.

Wenlong Huang, Pieter Abbeel, Deepak Pathak, and Igor Mordatch. Language models as zero-shot planners: Extracting actionable knowledge for embodied agents. In Proceedings of ICML , pages 9118-9147. PMLR, 2022.

Yong Jiang, Wenjuan Han, Kewei Tu, et al. Unsupervised neural dependency parsing. Association for Computational Linguistics (ACL), 2016.

Lifeng Jin, Finale Doshi-Velez, Timothy Miller, Lane Schwartz, and William Schuler. Unsupervised learning of PCFGs with normalizing flow. In Proceedings of ACL , Florence, Italy, July 2019. Association for Computational Linguistics.

Wengong Jin, Regina Barzilay, and Tommi Jaakkola. Junction tree variational autoencoder for molecular graph generation. In Proceedings of ICML , pages 2323-2332. PMLR, 2018.

Hiroshi Kajino. Molecular hypergraph grammar with its application to molecular optimization. In Proceedings of ICML , pages 3183-3191. PMLR, 2019.

Aishwarya Kamath and Rajarshi Das. A survey on semantic parsing. arXiv preprint arXiv:1812.00978 , 2018.

Beomjoon Kim, Zi Wang, Leslie Pack Kaelbling, and Tomás Lozano-Pérez. Learning to guide task and motion planning using score-space representation. The International Journal of Robotics Research , 38(7):793-812, 2019.

Yoon Kim. Sequence-to-sequence learning with latent neural grammars. In Proceedings of NeurIPS , pages 26302-26317, 2021.

Yoon Kim, Chris Dyer, and Alexander Rush. Compound probabilistic context-free grammars for grammar induction. In Proceedings of ACL , Florence, Italy, July 2019. Association for Computational Linguistics.

Yoon Kim, Alexander Rush, Lei Yu, Adhiguna Kuncoro, Chris Dyer, and Gábor Melis. Unsupervised recurrent neural network grammars. In Proceedings of NAACL , Minneapolis, Minnesota, June 2019. Association for Computational Linguistics.

Mario Krenn, Florian Häse, AkshatKumar Nigam, Pascal Friederich, and Alan Aspuru-Guzik. Self-referencing embedded strings (selfies): A 100% robust molecular string representation. Machine Learning: Science and Technology , 1(4):045024, oct 2020.

Matt J Kusner, Brooks Paige, and José Miguel Hernández-Lobato. Grammar variational autoencoder. In Proceedings of ICML , pages 1945-1954. PMLR, 2017.

Itay Levy, Ben Bogin, and Jonathan Berant. Diverse demonstrations improve in-context compositional generalization. arXiv preprint arXiv:2212.06800 , 2022.

Yaobo Liang, Chenfei Wu, Ting Song, Wenshan Wu, Yan Xia, Yu Liu, Yang Ou, Shuai Lu, Lei Ji, Shaoguang Mao, Yun Wang, Linjun Shou, Ming Gong, and Nan Duan. TaskMatrix.AI: Completing Tasks by Connecting Foundation Models with Millions of APIs. arXiv:2303.16434 , 2023.

Bill Yuchen Lin, Chengsong Huang, Qian Liu, Wenda Gu, Sam Sommerer, and Xiang Ren. On grounded planning for embodied tasks with language models. arXiv preprint arXiv:2209.00465 , 2022.

Bo Liu, Yuqian Jiang, Xiaohan Zhang, Qiang Liu, Shiqi Zhang, Joydeep Biswas, and Peter Stone. Llm+ p: Empowering large language models with optimal planning proficiency. arXiv preprint arXiv:2304.11477 , 2023.

Qing Lyu, Shreya Havaldar, Adam Stein, Li Zhang, Delip Rao, Eric Wong, Marianna Apidianaki, and Chris Callison-Burch. Faithful chain-of-thought reasoning. arXiv preprint arXiv:2301.13379 , 2023.

Maxwell Nye, Anders Andreassen, Guy Gur-Ari, Henryk Witold Michalewski, Jacob Austin, David Bieber, David Martin Dohan, Aitor Lewkowycz, Maarten Paul Bosma, David Luan, Charles Sutton, and Augustus Odena. Show your work: Scratchpads for intermediate computation with language models. arXiv:2112.00114 , 2021.

Liangming Pan, Alon Albalak, Xinyi Wang, and William Yang Wang. Logic-LM: Empowering Large Language Models with Symbolic Solvers for Faithful Logical Reasoning. arXiv:2305.12295 , 2023.

Bhargavi Paranjape, Scott Lundberg anbd Sameer Singh, Hannaneh Hajishirzi, Luke Zettlemoyer, and Marco Tulio Ribeiro. ART: Automatic multi-step reasoning and tool-use for large language models. arXiv:2303.09014 , 2023.

Emmanouil Antonios Platanios, Adam Pauls, Subhro Roy, Yuchen Zhang, Alexander Kyte, Alan Guo, Sam Thomson, Jayant Krishnamurthy, Jason Wolfe, Jacob Andreas, and Dan Klein. Value-agnostic conversational semantic parsing. In Proceedings of ACL , August 2021.

Gabriel Poesia, Alex Polozov, Vu Le, Ashish Tiwari, Gustavo Soares, Christopher Meek, and Sumit Gulwani. Synchromesh: Reliable Code Generation from Pre-trained Language Models. Proceedings of ICLR, 2022.

Yujia Qin, Shengding Hu, Yankai Lin, Weize Chen, Ning Ding, Ganqu Cui, Zheni Zeng, Yufei Huang, Chaojun Xiao, Chi Han, et al. Tool learning with foundation models. arXiv preprint arXiv:2304.08354 , 2023.

Linlu Qiu, Peter Shaw, Panupong Pasupat, Tianze Shi, Jonathan Herzig, Emily Pitler, Fei Sha, and Kristina Toutanova. Evaluating the impact of model scale for compositional generalization in semantic parsing. In Proceedings of EMNLP , December 2022.

Shreyas Sundara Raman, Vanya Cohen, Eric Rosen, Ifrah Idrees, David Paulius, and Stefanie Tellex. Planning with large language models via corrective re-prompting. arXiv preprint arXiv:2211.09935 , 2022.

Nils Reimers and Iryna Gurevych. Sentence-bert: Sentence embeddings using siamese bertnetworks. arXiv preprint arXiv:1908.10084 , 2019.

David Rogers and Mathew Hahn. Extended-connectivity fingerprints. Journal of chemical information and modeling , 50(5):742-754, 2010.

Yu Rong, Yatao Bian, Tingyang Xu, Weiyang Xie, Ying Wei, Wenbing Huang, and Junzhou Huang. Self-supervised graph transformer on large-scale molecular data. Proceedings of NeurIPS , 33:12559-12571, 2020.

Victor Sanh, Albert Webson, Colin Raffel, Stephen H. Bach, et al. Multitask prompted training enables zero-shot task generalization. In Proceedings of ICLR , 2022.

Timo Schick, Jane Dwivedi-Yu, Roberto Dessi, Roberta Raileanu, Maria Lomeli, Luke Zettlemoyer, Nicola Cancedda, and Thomas Scialom. Toolformer: Language Models Can Teach Themselves to Use Tools. arXiv:2302.04761 , 2023.

Torsten Scholak, Nathan Schucher, and Dzmitry Bahdanau. PICARD: Parsing incrementally for constrained auto-regressive decoding from language models. In Proceedings of EMNLP , Online and Punta Cana, Dominican Republic, November 2021. Association for Computational Linguistics.

Pratyusha Sharma, Antonio Torralba, and Jacob Andreas. Skill induction and planning with latent language. arXiv preprint arXiv:2110.01517 , 2021.

Peter Shaw, Ming-Wei Chang, Panupong Pasupat, and Kristina Toutanova. Compositional generalization and natural language variation: Can a semantic parsing approach handle both? In Proceedings of ACL , August 2021.

Richard Shin and Benjamin Van Durme. Few-shot semantic parsing with language models trained on code. In Proceedings of NAACL , pages 5417-5425, Seattle, United States, July 2022. Association for Computational Linguistics. doi: 10.18653/v1/2022.naacl-main.396. URL https://aclanthology.org/2022.naacl-main.396 .

Richard Shin, Christopher Lin, Sam Thomson, Charles Chen, Subhro Roy, Emmanouil Antonios Platanios, Adam Pauls, Dan Klein, Jason Eisner, and Benjamin Van Durme. Constrained language models yield few-shot semantic parsers. In Proceedings of EMNLP . Association for Computational Linguistics, November 2021.

Tom Silver, Varun Hariprasad, Reece S Shuttleworth, Nishanth Kumar, Tomás Lozano-Pérez, and Leslie Pack Kaelbling. PDDL planning with pretrained large language models. In NeurIPS 2022 Foundation Models for Decision Making Workshop , 2022.

David Smith and Jason Eisner. Quasi-Synchronous Grammars: Alignment by Soft Projection of Syntactic Dependencies. In Proceedings on the Workshop on Statistical Machine Translation , 2006.

Jennifer J Sun, Megan Tjandrasuwita, Atharva Sehgal, Armando Solar-Lezama, Swarat Chaudhuri, Yisong Yue, and Omar Costilla-Reyes. Neurosymbolic programming for science. arXiv preprint arXiv:2210.05050 , 2022.

Didac Suris, Sachit Menon, and Carl Vondrick. ViperGPT: Visual Inference via Python Execution for Reasoning. arXiv:2303.08128 , 2023.

Mirac Suzgun, Nathan Scales, Nathanael Scharli, Sebastian Gehrmann, Yi Tay, Hyung Won Chung, Aakanksha Chowdhery, Quoc V. Le, Ed H. Chi, Denny Zhou, and Jason Wei. Challenging big-bench tasks and whether chain-of-thought can solve them. arXiv:2210.09261 , 2022.

Karthik Valmeekam, Alberto Olmo, Sarath Sreedharan, and Subbarao Kambhampati. Large language models still can't plan (a benchmark for llms on planning and reasoning about change). arXiv preprint arXiv:2206.10498 , 2022.

Gregor vom Scheidt. Experimental results from applying GPT-4 to an unpublished formal language. arXiv:2305.12196 , 2023.

Bailin Wang, Ivan Titov, Jacob Andreas, and Yoon Kim. Hierarchical Phrase-based Sequenceto-Sequence Learning. In Proceedings of EMNLP , 2022.

Guanzhi Wang, Yuqi Xie, Yunfan Jiang, Ajay Mandlekar, Chaowei Xiao, Yuke Zhu, Linxi Fan, and Anima Anandkumar. Voyager: An open-ended embodied agent with large language models. arXiv preprint arXiv:2305.16291 , 2023.

Mengqiu Wang, Noah A. Smith, and Teruko Mitamura. What is the Jeopardy Model? A Quasi-Synchronous Grammar for QA. In Proceedings of EMNLP , 2007.

Sheng Wang, Yuzhi Guo, Yuhong Wang, Hongmao Sun, and Junzhou Huang. Smiles-bert: large scale unsupervised pre-training for molecular property prediction. In Proceedings of the 10th ACM international conference on bioinformatics, computational biology and health informatics , pages 429-436, 2019.

Xuezhi Wang, Jason Wei, Dale Schuurmans, Quoc Le, Ed Chi, Sharan Narang, Aakanksha Chowdhery, and Denny Zhou. Self-consistency improves chain of thought reasoning in language models. In Proceedings of ICLR , 2023.

Yushi Wang, Jonathan Berant, and Percy Liang. Building a semantic parser overnight. In Proceedings of ACL , Beijing, China, July 2015.

Zi Wang, Stefanie Jegelka, Leslie Pack Kaelbling, and Tomás Lozano-Pérez. Focused modellearning and planning for non-Gaussian continuous state-action systems. In 2017 IEEE International conference on robotics and automation (ICRA) , pages 3754-3761. IEEE, 2017.

Zi Wang, Caelan Reed Garrett, Leslie Pack Kaelbling, and Tomás Lozano-Pérez. Active model learning and diverse action sampling for task and motion planning. In 2018 IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS) , pages 4107-4114. IEEE, 2018.

Zi Wang, Caelan Reed Garrett, Leslie Pack Kaelbling, and Tomás Lozano-Pérez. Learning compositional models of robot skills for task and motion planning. The International Journal of Robotics Research , 40(6-7):866-894, 2021.

Jason Wei, Maarten Paul Bosma, Vincent Zhao, Kelvin Guu, Adams Wei Yu, Brian Lester, Nan Du, Andrew Mingbo Dai, and Quoc V. Le. Finetuned language models are zero-shot learners. In Proceedings of ICLR , 2022.

Jason Wei, Xuezhi Wang, Dale Schuurmans, Maarten Bosma, Brian Ichter, Fei Xia, Ed Chi, Quoc Le, and Denny Zhou. Chain of thought prompting elicits reasoning in large language models. In Proceedings of NeurIPS , 2022.

David Weininger. Smiles, a chemical language and information system. 1. introduction to methodology and encoding rules. Journal of chemical information and computer sciences , 28 (1):31-36, 1988.

Victoria Xia, Zi Wang, and Leslie Pack Kaelbling. Learning sparse relational transition models. In International Conference on Learning Representations , 2019.

Frank F Xu, Uri Alon, Graham Neubig, and Vincent Josua Hellendoorn. A systematic evaluation of large language models of code. In Proceedings of the 6th ACM SIGPLAN International Symposium on Machine Programming , pages 1-10, 2022.

Ryan Yang, Tom Silver, Aidan Curtis, Tomas Lozano-Perez, and Leslie Pack Kaelbling. Pg3: Policy-guided planning for generalized policy generation. arXiv preprint arXiv:2204.10420 , 2022.

Songlin Yang, Yanpeng Zhao, and Kewei Tu. Neural bi-lexicalized PCFG induction. In Proceedings of ACL , Online, August 2021. Association for Computational Linguistics.

Songlin Yang, Yanpeng Zhao, and Kewei Tu. PCFGs can do better: Inducing probabilistic context-free grammars with many symbols. In Proceedings of NAACL , 2021.

Songlin Yang, Roger P Levy, and Yoon Kim. Unsupervised discontinuous constituency parsing with mildly context-sensitive grammars. arXiv preprint arXiv:2212.09140 , 2022.

Shunyu Yao, Dian Yu, Jeffrey Zhao, Izhak Shafran, Thomas L Griffiths, Yuan Cao, and Karthik Narasimhan. Tree of thoughts: Deliberate problem solving with large language models. arXiv preprint arXiv:2305.10601 , 2023.

Jiacheng Ye, Zhiyong Wu, Jiangtao Feng, Tao Yu, and Lingpeng Kong. Compositional exemplars for in-context learning. arXiv preprint arXiv:2302.05698 , 2023.

Xi Ye, Qiaochu Chen, Isil Dillig, and Greg Durrett. Benchmarking multimodal regex synthesis with complex structures. In Proceedings of ACL . Association for Computational Linguistics, July 2020.

Xi Ye, Qiaochu Chen, Isil Dillig, and Greg Durrett. Satisfiability-aided language models using declarative prompting. arXiv preprint arXiv:2305.09656 , 2023.

Pengcheng Yin, Hao Fang, Graham Neubig, Adam Pauls, Emmanouil Antonios Platanios, Yu Su, Sam Thomson, and Jacob Andreas. Compositional generalization for neural semantic parsing via span-level supervised attention. Proceedings of ACL, 2021.

John M Zelle and Raymond J Mooney. Learning to parse database queries using inductive logic programming. In Proceedings of the national conference on artificial intelligence , pages 1050-1055, 1996.

Hao Zhu, Yonatan Bisk, and Graham Neubig. The return of lexical dependencies: Neural lexicalized PCFGs. Transactions of the Association for Computational Linguistics , 8, 2020.

A Experiment Details

A.1 Semantic Parsing

Statistics and Splits. We show the statistics for the splits used for the experiments in Table 6.

For GeoQuery, we utilize the standard split from Zelle and Mooney [99] in the retrieval-based setting and the Template, TMCD, and Length splits from Shaw et al. [66]. We randomly sample 32 examples from the training set of the standard split to create the few-shot split. To generate the NewFunc split, we designate examples utilizing the following eight functions as test examples: smallest, shortest, most, highest, sum, population_1, count, major ; the remaining examples are incorporated into the training set.

For SMCalFlow, we adopt the 16-shot and 128-shot cross-domain settings from Yin et al. [98] as our few-shot and retrieval-based settings, respectively. It is noteworthy that the training set of the original splits contains approximately 25k in-domain training examples. Previous work [96, 57] utilized these examples as their retrieval set. However, in our experiments, we found that incorporating in-domain examples did not enhance performance. Consequently, we use 16/128 cross-domain examples as our training set in the few-shot and retrieval settings, respectively. For all experiments on SMCalFlow, we used the preprocessed version from Qiu et al. [57], which employs a more concise LISPRESS format [54] than the original version [98]. This format aligns with Ye et al. [96] for a fair comparison.

For Overnight-Blocks, we employ the standard split from Wang et al. [81] in the retrieval setting. We randomly sample 32 examples from the training set of the standard split to create the few-shot split.

Scoring Functions for Constrained Generation. For each candidate continuation w ∈ Σ[ y prefix ] for correction, we first form a partial program via concatenation y prefix · w and then feed it into Codex to obtain the score for the candidate via

In the case where w consists of multiple BPE tokens, e.g., FindManger( is tokenized into Find , Manager , and ( , we average the token-level log-likelihood to obtain a candidate-level score. However, when the size of Σ[ y prefix ] exceeds 16, invoking Codex for each candidate becomes too expensive. To address this issue, we employ SentenceBERT to select 16 most plausible candidates first via a dot product,

where SentenceBERT yields the embeddings for the incorrect prediction ˆ y t and a candidate of corrected partial program y prefix · w .

The functionality of obtaining the log-likelihood for a candidate continuation given a prefix is applicable via Codex APIs 11 via setting logprobs=True and echo=True . Unfortunately, subsequent models (e.g., GPT-3.5 and GPT-4) disable such functionality. As a workaround, we simply use the scoring function based on SentenceBERT to directly select the best candidate in our PDDL planning experiments.

Cost Efficiency. We assess various decoding strategies for their cost efficiency, focusing on the number of API calls. The number of Codex calls resulting from the few-shot semantic parsing experiments is presented in Figure 7, alongside the corresponding accuracy metrics. The results indicate that standard prompting under constrained decoding leads to a significantly higher number of Codex calls. Similarly, grammar-prompting with constraints also results in an increased number of Codex calls. However, when employing both grammar and program constraints, the number of calls decreases meaningfully in comparison to standard prompting under constrained decoding. Future work might consider exploring strategies for more cost-efficient constrained decoding.

Grammar Prompting with Annotated Rules. We have additionally experimented with enhancing BNF rules by appending natural language comments. As illustrated in Figure 6, we pair each BNF rule with its corresponding natural language phrases extracted from the given query x . These manually annotated comments yield an explicit correspondence between natural language phrases and their corresponding BNF rules, thereby better facilitating interpretation and application of the grammars for generating programs. When employing the augmented grammar prompting, we noticed marginal improvements on SMCalFlow (+1.0%) and Overnight-Blocks (0.5%), with no observed enhancement on GeoQuery. While the gains might not appear significant, this predicted alignment could potentially contribute to improved interpretability and further constraints on generation. For instance, the phrase 'someone's manager' should consistently trigger the function FindManager( . We leave the exploration of utilizing the augmented rules for future work.

A.2 Molecule Generation

Sampling Procedure Different from semantic parsing and PDDL planning, where the most probable program y needs to be found via arg max inference, molecule generation has empty specification x and requires sampling from a prompting-based distribution. The sampling procedure for grammar prompting consists of three stages: (1) we randomly sample a permutation of given molecules, denoted as π , (2) based on a prompt formed by the permutation, we sample a specialized grammar ̂ G via

iii) we finally obtain the molecule conditioned ̂ G ,

We list the hyperparameters used for the sampling procedure in for (2) in Table 8 (top) and for (3) in Table 8 (bottom).

In comparison, the sampling procedure for standard prompting only consists of two stages: (1) we randomly sample a permutation of given molecules, denoted as π , (2) based on a prompt formed by the permutation, we directly sample a molecule via

The hyperparameters used for Step (2) is the same as in grammar prompting and shown in Table 8 (bottom).

While we observed that Earley-based constrained generation enhances grammar prompting in terms of improving validity, other metrics, such as the retrosynthesis score, decreased significantly. This discrepancy could be attributed to the fact that existing LLMs, due to their limited exposure to molecules represented in SMILES format, struggle with comprehending and applying the BNF grammar rules of SMILES. Overall, our current findings serve as preliminary evidence that grammar prompting can tap into the capacity of LLMs to understand and apply BNF rules. However such capacity still remains limited in text-focused LLMs.

A.3 PDDL Planning

Restricted Action Space The specialized grammar defined for PDDL planning essentially delineates a constrained action space that includes necessary actions and their associated objects. Our empirical results found that limiting the classical GBFS planner to the objects selected by a specialized grammar proved too restrictive, yielding beneficial results only within the Blocks domain. Therefore, we decided to remove this limitation, thus expanding the action space of GBFS to contain the actions predicted from the grammar with an unrestricted range of objects.

B Prompts

Figures 7, 8, and 9 demonstrate the prompts with grammars, based on actual examples in the SMCalFlow, GeoQuery, and Overnight datasets respectively. Because the general grammar of SMCalFlow is long (around 4k tokens), we do not include it within the prompt. For GeoQuery and Overnight, the general grammar is integrated as part of the instruction within the prompt. In the context of molecule generation, the general grammar for SMILES 12 is also included. Figures 10 and 11 demonstrate the prompts with action DSLs for PDDL planning.

LLMPrompt

You are an expert programmer, and you need to write a program for the given natural language query. First, you should write a grammar that contains all the necessary BNF rules. Then, you should write programs that conform to your predicted rules.

query:

I need a meeting with Elli and her team on Wednesday afternoon .

BNF grammar rules:

call

::=

"(Yield" event ")"

event

::=

"(CreatePreflightEventWrapper" event_constraint ")"

event_constraint

::=

"(&" event_constraint event_constraint ")"

datetime_constraint

time

date

day

::=

"(NextDOW" day ")"

|

DAY

DAY

attendee_constraint

::=

"(&" attendee_constraint attendee_constraint ")"

|

"(AttendeeListHasPeople" person ")"

person

::=

recipient

|

"(FindTeamOf" person ")"

recipient

::=

"(Execute (refer (extensionConstraint" recipient ")))"

|

"(RecipientWithNameLike (PersonName.apply" string "))"

string

::=

"'Elli'"

program based on the BNF grammar rules:

(

Yield

(

CreatePreflightEventWrapper

(&(

Event

.

start

_

?(

DateTimeConstraint

(

Afternoon

)

(

NextDOW

(

Wednesday

))))(

Event

.

attendees

_

?(&(

AttendeeListHasRecipient

(

Execute

(

refer

(

extensionConstraint

(

RecipientWithNameLike

(

PersonName

.

apply

'

Elli

'))))))

(

AttendeeListHasPeople

(

FindTeamOf

(

Execute

(

refer

(

extensionConstraint

(

RecipientWithNameLike

(

PersonName

.

apply

'

Elli

'))))))))))))

. . .

query:

Create a meeting tomorrow with the staff .

LLMPrompt

You are an expert programmer, and you need to write a program for the given natural language query. First, you should write grammar rules by choosing from the following BNF rules. Then, you should write programs that conform to your predicted rules.

[BEGIN RULES]

. . .

[END RULES]

query:

what states border hawaii ?

BNF grammar rules:

query

::=

"answer(" answer_type ")"

answer_type

::=

state

state

::=

"state(" state ")"

|

"next_to_2(" state ")"

|

"stateid('" STATENAME "')"

STATENAME

::=

"hawaii"

program based on the BNF grammar rules:

answer

(

state

(

next

_

to

_

2

(

stateid

('

hawaii

′

))))

. . .

query:

how many major cities are in arizona ?

LLMOutput

LLMPrompt

You are an expert programmer, and you need to write a program for the given natural language query.

First, you should write grammar rules by choosing from the following BNF rules.

Then, you should write

programs that conform to your predicted rules.

[BEGIN RULES]

list_value

::=

"(listValue" list_value ")"

|

"(filter" list_value PROPERTY ")"

|

"(filter" list_value PROPERTY OP list_value ")"

|

"(superlative" list_value AGGREGATE "

|

(ensureNumericProperty" PROPERTY "))"

|

"(countSuperlative" list_value AGGREGATE PROPERTY ")"

|

"(_size" list_value ")"

|

"(aggregate" AGGREGATE list_value ")"

|

"(getProperty" list_value PROPERTY ")"

|

"(getProperty (singleton" SINGLETON_VALUE ") !type)"

|

"(concat" ENTITY_VALUE ENTITY_VALUE ")"

|

"(concat" NUMBER_VALUE NUMBER_VALUE ")"

|

ENTITY_VALUE

|

NUMBER_VALUE

PROPERTY

::=

"shape"

|

"color"

|

"length"

|

"is_special"

|

"width"

|

"height"

|

"left"

|

"right"

|

"above"

|

"below"

|

"(reverse left)"

|

"(reverse right)"

|

"(reverse above)"

|

"(reverse below)"

SINGLETON_VALUE

::=

"en.block"

|

"en.shape"

|

"en.color"

ENTITY_VALUE

::=

"en.block.block1"

|

"en.block.block2"

|

"en.shape.pyramid"

|

"en.shape.cube"

|

"en.color.red"

|

"en.color.green"

NUMBER_VALUE

::=

"3 en.inch"

|

"6

en.inch"

|

"2"

OP

::=

"="

|

">"

|

"<"

|

">="

|

"<="

|

"!="

AGGREGATE

::=

"sum"

|

|

"min"

|

"avg"

[END RULES]

query:

which brick is taller than brick 1

BNF grammar rules:

list_value

::=

"(listValue" list_value ")"

|

"(aggregate" AGGREGATE list_value ")"

|

"(getProperty" list_value PROPERTY ")"

|

"(getProperty (singleton" SINGLETON_VALUE ") !type)"

AGGREGATE

::=

"avg"

SINGLETON_VALUE

::=

"en.block"

PROPERTY

::=

"width"

program based on the BNF grammar rules:

(

listValue

(

aggregate avg

(

getProperty

(

getProperty

(

singleton en

.

block

) !

type

)

width

)))

. . .

query:

look right of block 1 and get the special block

LLMOutput

BNF grammar rules:

list_value

::=

"(listValue" list_value ")"

|

"(filter" list_value PROPERTY OP list_value ")"

|

"(filter" list_value PROPERTY ")"

|

"(getProperty (singleton" SINGLETON_VALUE ") !type)"

|

ENTITY_VALUE

SINGLETON_VALUE

::=

"en.block"

PROPERTY

::=

"is_special"

|

"(reverse left)"

OP

::=

"="

ENTITY_VALUE

::=

"en.block.block1"

program based on the BNF grammar rules:

(

listValue

(

filter

(

filter

(

getProperty

(

singleton en

.

block

) !

type

)

is

_

special

)

(

reverse left

) =

en

.

block

.

block1

))

LLMPrompt

LLMPrompt

Q:

(:objects crate0 crate1 crate2 crate3 crate4 crate5 depot0 distributor0 distributor1 hoist0 hoist1 hoist2 pallet0 pallet1

pallet2 pallet3 pallet4 pallet5 truck0 truck1 - object)

(:init (pallet pallet0) (surface pallet0) (at pallet0 depot0) (clear crate5) (pallet pallet1) (surface pallet1) (at pallet1

distributor0) (clear pallet1) (pallet pallet2) (surface pallet2) (at pallet2 distributor1) (clear crate3) (pallet pallet3)

(surface pallet3) (at pallet3 distributor0) (clear pallet3) (pallet pallet4) (surface pallet4) (at pallet4 distributor0)

(clear crate4) (pallet pallet5) (surface pallet5) (at pallet5 distributor1) (clear crate1) (truck truck0) (at truck0 dis-

tributor1) (truck truck1) (at truck1 depot0) (hoist hoist0) (at hoist0 depot0) (available hoist0) (hoist hoist1) (at

hoist1 distributor0) (available hoist1) (hoist hoist2) (at hoist2 distributor1) (available hoist2) (crate crate0) (surface

crate0) (at crate0 distributor0) (on crate0 pallet4) (crate crate1) (surface crate1) (at crate1 distributor1) (on crate1

pallet5) (crate crate2) (surface crate2) (at crate2 distributor1) (on crate2 pallet2) (crate crate3) (surface crate3) (at

crate3 distributor1) (on crate3 crate2) (crate crate4) (surface crate4) (at crate4 distributor0) (on crate4 crate0) (crate

crate5) (surface crate5) (at crate5 depot0) (on crate5 pallet0) (place depot0) (place distributor0) (place distribu-

tor1))

(:goal (on crate0 pallet3) (on crate1 crate4) (on crate3 pallet1) (on crate4 pallet5) (on crate5 crate1))

DSL:

plan

::=

action+

action

::=

"(lift-and-drive" object object object object object object ")"

|

"(drive-and-load" object object object object ")"

|

"(drive-and-lift" object object object object object ")"

|

"(lift" object object object object ")"

|

"(drive-and-unload" object object object object object ")"

|

"(drop" object object object object ")"

object

::=

"truck1"

|

"hoist0"

|

"crate5"

|

"pallet0"

|

"depot0"

|

"truck0"

|

"hoist2"

|

"crate4"

|

"crate0"

|

"distributor1"

|

"hoist1"

|

"pallet4"

|

"distributor0"

|

"crate1"

|

"pallet5"

|

"crate3"

|

"crate2"

|

"pallet1"

|

"pallet3"

A:

(lift-and-drive truck1 hoist0 crate5 pallet0 depot0 depot0) (drive-and-load truck1 hoist0 crate5 depot0) (drive-and-

lift truck0 hoist2 crate4 crate0 distributor1) (lift hoist1 crate0 pallet4 distributor0) (drive-and-lift truck1 hoist0

crate1 pallet5 depot0) (drive-and-load truck1 hoist0 crate1 depot0) (drive-and-lift truck1 hoist0 crate3 crate2 de-

pot0) (drive-and-load truck1 hoist0 crate3 depot0) (drive-and-unload truck1 hoist0 crate3 pallet1 depot0) (drop

hoist2 crate4 pallet5 distributor1) (drive-and-unload truck1 hoist0 crate1 crate2 depot0) (drive-and-lift truck0 hoist2

crate1 crate2 distributor1) (drop hoist2 crate1 crate4 distributor1) (drive-and-unload truck1 hoist0 crate5 crate1

. . .

Q:

(:objects crate0 crate1 crate2 crate3 depot0 distributor0 distributor1 hoist0 hoist1 hoist2 pallet0 pallet1 pallet2

truck0 truck1 - object) (:init (pallet pallet0) (surface pallet0) (at pallet0 depot0) (clear crate0) (pallet pallet1)

(surface pallet1) (at pallet1 distributor0) (clear crate3) (pallet pallet2) (surface pallet2) (at pallet2 distributor1)

(clear crate2) (truck truck0) (at truck0 depot0) (truck truck1) (at truck1 depot0) (hoist hoist0) (at hoist0 depot0)

(available hoist0) (hoist hoist1) (at hoist1 distributor0) (available hoist1) (hoist hoist2) (at hoist2 distributor1)

(available hoist2) (crate crate0) (surface crate0) (at crate0 depot0) (on crate0 pallet0) (crate crate1) (surface crate1)

(at crate1 distributor1) (on crate1 pallet2) (crate crate2) (surface crate2) (at crate2 distributor1) (on crate2 crate1)

(crate crate3) (surface crate3) (at crate3 distributor0) (on crate3 pallet1) (place depot0) (place distributor0) (place

distributor1)) (:goal (on crate0 pallet2) (on crate1 crate3) (on crate2 pallet0) (on crate3 pallet1))

DSL:

LLMOutput

plan

::=

action+

action

::=

"(lift-and-drive" object object object object object object ")"

|

"(lift" object object object object ")"

|

"(drive" object object object ")"

|

"(drive-and-lift" object object object object object ")"

|

"(drop" object object object object ")"

|

"(drive-and-load" object object object object ")"

|

"(drive-and-unload" object object object object object ")"

object

::=

"truck0"

|

"hoist0"

|

"crate0"

|

"pallet0"

|

"depot0"

|

"hoist2"

|

"crate2"

|

"crate1"

|

"distributor1"

|

"distributor0"

|

"hoist1"

|

"pallet2"

|

"crate3"

|

"truck1"

A:

(lift-and-drive truck0 hoist0 crate0 pallet0 depot0 depot0) (lift hoist2 crate2 crate1 distributor1) (drive truck0

depot0 distributor0) (drive-and-lift truck0 hoist1 crate1 pallet2 distributor0) (drop hoist1 crate1 crate3 distributor0)

(drive-and-load truck1 hoist0 crate0 depot0) (drive-and-unload truck1 hoist0 crate0 pallet2 depot0) (drive truck1

depot0 distributor1) (drive-and-load truck1 hoist2 crate2 distributor1) (drive-and-unload truck1 hoist2 crate2 pallet0

distributor1)

Peer reviews:
{"IS_META_REVIEW": false, "comments": "SUMMARY:\nThis 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.\n\nSTRENGTHS:\n+ The paper is well-written and easy to follow.\n\n+ A simple and effective method was presented, leading to improved downstream performance compared with simple prompting.\n\nWEAKNESSES:\n- 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.\n\n- 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.\n\n- 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.\n\n\n[1] Hokamp, Chris, and Qun Liu. \"Lexically constrained decoding for sequence generation using grid beam search.\" arXiv preprint arXiv:1704.07138 (2017).\n[2] Huang, Wenlong, et al. \"Grounded decoding: Guiding text generation with grounded models for robot control.\" arXiv preprint arXiv:2303.00855 (2023).\n[3] Yao, Shunyu, et al. \"Tree of thoughts: Deliberate problem solving with large language models.\" arXiv preprint arXiv:2305.10601 (2023).\n\nQUESTIONS:\nNA\n\nLIMITATIONS:\nNA", "TITLE": "Official Review", "RECOMMENDATION": 5, "REVIEWER_CONFIDENCE": 3, "SOUNDNESS_CORRECTNESS": 3, "DATE": "2023-06-25"}

comments: 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?

LIMITATIONS:
No negative societal impacts
TITLE: Official Review
SOUNDNESS_CORRECTNESS: 4
DATE: 2023-06-28

{"IS_META_REVIEW": false, "comments": "SUMMARY:\nThis 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.\n\nSTRENGTHS:\nThe 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.\n\nThe 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.\n\nThe paper is very clearly written, especially given the number of experiments presented and space constraints (but see below for a few questions).\n\nWEAKNESSES:\nOne 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. \n\nA few details could be made clearer, if space is available, see below.\n\nQUESTIONS:\nQ1) 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?\n\nQ2) Which Codex model is being used in the experiments here?\n\nMinor clarification questions/points (don't need to address in author response):\n- Be more consistent with the bolding of numbers in results tables, e.g. in Table 5\n- The limitations mentions a lack of improvement for regexes, were these in experiments not reported in this paper?\n- I didn't fully understand the Macro experiments on the PDDL tasks (although I did not check the appendix carefully).\n- spelling of \"conditioned\" in Fig 2 caption\n- 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\n\nLIMITATIONS:\nThe paper's limitation section did a good job, I think.\n\nOne 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.", "TITLE": "Official Review", "RECOMMENDATION": 7, "REVIEWER_CONFIDENCE": 4, "SOUNDNESS_CORRECTNESS": 4, "DATE": "2023-07-05"}

comments: 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.

LIMITATIONS:
See above.
TITLE: Official Review
SOUNDNESS_CORRECTNESS: 3
DATE: 2023-07-10