Skip to main content
QUICK REVIEW

[Paper Review] Grammar Prompting for Domain-Specific Language Generation with Large Language Models

Bailin Wang, Zi Wang|arXiv (Cornell University)|May 30, 2023
Topic ModelingComputer Science24 citations
TL;DR

Grammar prompting lets LLMs generate DSL outputs by first predicting a minimal specialized BNF grammar for each example and then decoding constrained by that grammar, improving few-shot DSL generation across semantic parsing, planning, and molecule generation tasks.

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.

Motivation & Objective

  • Motivate data-efficient generation of structured DSL outputs when few exemplars are available.
  • Leverage a specialized, minimal BNF grammar to constrain in-context learning and guide decoding.
  • Demonstrate improvements over standard prompting across semantic parsing, planning, and molecule generation.

Proposed method

  • Augment each in-context demonstration with a minimal specialized grammar G[y] that suffices to generate the target output y.
  • Predict a specialized grammar G_hat from G and in-context examples, then generate y conditioned on G_hat.
  • Apply a constrained decoding algorithm (Earley-based) to ensure outputs lie in L(G_hat) and to correct invalid prefixes.
  • Use an Earley parser to extract valid prefixes and guide token selection when the raw LLM continuation is invalid.
  • Compare grammar prompting to standard prompting and constrained decoding baselines across multiple DSL domains.

Experimental results

Research questions

  • RQ1Can grammar prompting enable LLMs to generate DSL outputs more accurately in few-shot settings than standard prompting?
  • RQ2Does predicting and constraining with a specialized grammar improve generalization to unseen DSL constructs or functions?
  • RQ3How does constrained decoding with grammar guidance impact computational cost and output validity?
  • RQ4Is grammar prompting effective across diverse DSLs including semantic parsing, PDDL planning, and SMILES-based molecule generation?

Key findings

  • Grammar prompting improves program and execution accuracy over standard prompting in semantic parsing benchmarks (GeoQuery, SMCalFlow, Overnight-Blocks).
  • Constrained decoding with predicted grammars yields further gains over unconstrained grammar prompting in several tasks.
  • Specialized grammar prompting generalizes better to unseen functions and to compositional/generalization splits (GeoQuery), relative to raw prompting.
  • Across multiple base LLMs, grammar prompting consistently improves performance in few-shot settings, with some variation by domain and model.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.